1#!/usr/bin/env ruby
2#
3# This script can be used to regenerate sequential resource ids when the
4# R.java file is modified in the src/test/resources/res folder.
5#
6# Note: this script does *NOT* generate resource ids that are consistent
7# with the Android aapt tool. This script will likely be removed at some
8# near point in the future and replaced by something that invokes aapt
9# directly.
10
11GIT_ROOT = `git rev-parse --show-toplevel`.chomp
12START = 0x7f000000
13INCR = 0x10000
14
15path_to_r = File.join(GIT_ROOT, "robolectric/src/test/java/org/robolectric/R.java")
16if path_to_r =~ /^\/path\/to/
17  raise "please change the path to this file!"
18else
19  original_contents = File.read(path_to_r)
20  num_classes = 0
21  x = START
22  new_contents = original_contents.gsub(/class|0x[0-9a-fA-F]+;/) do |match|
23    if match == "class"
24      x = START + INCR * num_classes
25      num_classes += 1
26      "class"
27    else
28      val = "0x#{"%x"%x};"
29      x += 1
30      val
31    end
32  end
33  File.open(path_to_r, "w") { |f| f << new_contents }
34end
35