assets/rakelib/ruboto.rake in ruboto-0.13.0.rc.0 vs assets/rakelib/ruboto.rake in ruboto-0.13.0

- old
+ new

@@ -2,11 +2,10 @@ require 'rubygems' require 'time' require 'rake/clean' require 'rexml/document' require 'timeout' -require 'net/telnet' ON_WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i) ANT_CMD = ON_WINDOWS ? 'ant.bat' : 'ant' @@ -48,11 +47,11 @@ exit 1 end new_dx_content = File.read(dx_filename).dup xmx_pattern = ON_WINDOWS ? /^set defaultXmx=-Xmx(\d+)(M|m|G|g|T|t)/ : /^defaultMx="-Xmx(\d+)(M|m|G|g|T|t)"/ -MINIMUM_DX_HEAP_SIZE = 1536 +MINIMUM_DX_HEAP_SIZE = 1600 if new_dx_content =~ xmx_pattern && ($1.to_i * 1024 ** {'M' => 2, 'G' => 3, 'T' => 4}[$2.upcase]) < MINIMUM_DX_HEAP_SIZE*1024**2 puts "Increasing max heap space from #$1#$2 to #{MINIMUM_DX_HEAP_SIZE}M in #{dx_filename}" new_xmx_value = ON_WINDOWS ? %Q{set defaultXmx=-Xmx#{MINIMUM_DX_HEAP_SIZE}M} : %Q{defaultMx="-Xmx#{MINIMUM_DX_HEAP_SIZE}M"} new_dx_content.sub!(xmx_pattern, new_xmx_value) @@ -84,26 +83,38 @@ GEM_FILE = File.expand_path 'Gemfile.apk' GEM_LOCK_FILE = "#{GEM_FILE}.lock" RELEASE_APK_FILE = File.expand_path "bin/#{build_project_name}-release.apk" APK_FILE = File.expand_path "bin/#{build_project_name}-debug.apk" TEST_APK_FILE = File.expand_path "test/bin/#{build_project_name}Test-debug.apk" -JRUBY_JARS = Dir[File.expand_path 'libs/jruby-*.jar'] +JRUBY_JARS = Dir[File.expand_path 'libs/{jruby-*,dx}.jar'] +JARS = Dir[File.expand_path 'libs/*.jar'] - JRUBY_JARS RESOURCE_FILES = Dir[File.expand_path 'res/**/*'] JAVA_SOURCE_FILES = Dir[File.expand_path 'src/**/*.java'] RUBY_SOURCE_FILES = Dir[File.expand_path 'src/**/*.rb'] -APK_DEPENDENCIES = [MANIFEST_FILE, RUBOTO_CONFIG_FILE, BUNDLE_JAR] + JRUBY_JARS + JAVA_SOURCE_FILES + RESOURCE_FILES + RUBY_SOURCE_FILES +CLASSES_CACHE = "#{PROJECT_DIR}/bin/#{build_project_name}-debug-unaligned.apk.d" +APK_DEPENDENCIES = [MANIFEST_FILE, RUBOTO_CONFIG_FILE, BUNDLE_JAR, CLASSES_CACHE] + JRUBY_JARS + JARS + JAVA_SOURCE_FILES + RESOURCE_FILES + RUBY_SOURCE_FILES KEYSTORE_FILE = (key_store = File.readlines('ant.properties').grep(/^key.store=/).first) ? File.expand_path(key_store.chomp.sub(/^key.store=/, '').sub('${user.home}', '~')) : "#{build_project_name}.keystore" KEYSTORE_ALIAS = (key_alias = File.readlines('ant.properties').grep(/^key.alias=/).first) ? key_alias.chomp.sub(/^key.alias=/, '') : build_project_name APK_FILE_REGEXP = /^-rw-r--r--\s+(?:system|\d+\s+\d+)\s+(?:system|\d+)\s+(\d+)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}|\w{3} \d{2}\s+(?:\d{4}|\d{2}:\d{2}))\s+(.*)$/ -# -rw-r--r-- system system 7487556 2013-04-21 14:01 org.ruboto.example.gps-1.apk -# -rw-r--r-- 1 1000 1000 59252 Aug 15 2010 /data/app/org.update_test-1.apk -# -rw-r--r-- 1 1000 1000 59265 Aug 15 01:11 /data/app/org.update2_test-1.apk +JRUBY_ADAPTER_FILE = "#{PROJECT_DIR}/src/org/ruboto/JRubyAdapter.java" CLEAN.include('bin', 'gen', 'test/bin', 'test/gen') task :default => :debug +if File.exists?(CLASSES_CACHE) + expected_jars = File.readlines(CLASSES_CACHE).grep(%r{#{PROJECT_DIR}/libs/(.*\.jar) \\}).map { |l| l =~ %r{#{PROJECT_DIR}/libs/(.*\.jar) \\}; $1 } + actual_jars = Dir['libs/*.jar'].map { |f| f =~ /libs\/(.*\.jar)/; $1 } + changed_jars = ((expected_jars | actual_jars) - (expected_jars & actual_jars)) + unless changed_jars.empty? + puts "Jars have changed: #{changed_jars.join(', ')}" + FileUtils.touch(CLASSES_CACHE) + end +end + +file CLASSES_CACHE + file JRUBY_JARS => RUBOTO_CONFIG_FILE do next unless File.exists? RUBOTO_CONFIG_FILE jruby_jars_mtime = JRUBY_JARS.map { |f| File.mtime(f) }.min ruboto_yml_mtime = File.mtime(RUBOTO_CONFIG_FILE) next if jruby_jars_mtime > ruboto_yml_mtime @@ -122,11 +133,11 @@ desc 'build debug package' task :debug => APK_FILE namespace :debug do desc 'build debug package if compiled files have changed' - task :quick => [MANIFEST_FILE, RUBOTO_CONFIG_FILE, BUNDLE_JAR] + JRUBY_JARS + JAVA_SOURCE_FILES + RESOURCE_FILES do |t| + task :quick => APK_DEPENDENCIES - RUBY_SOURCE_FILES do |t| build_apk(t, false) end end desc 'build package and install it on the emulator or device' @@ -182,14 +193,17 @@ raise "\nWorkspace not clean!\n#{output}" unless output.empty? sh "git tag #{version}" sh 'git push origin master --tags' end -desc 'Start the emulator' +# FIXME(uwe): Remove December 2013 +desc 'Deprecated: Use "ruboto emulator" instead.' task :emulator do - start_emulator(sdk_level) + puts '"rake emulator" is deprecated. Use "ruboto emulator" instead.' + sh 'ruboto emulator' end +# EMXIF desc 'Start the application on the device/emulator.' task :start do start_app end @@ -210,15 +224,42 @@ file MANIFEST_FILE => PROJECT_PROPS_FILE do old_manifest = File.read(MANIFEST_FILE) manifest = old_manifest.dup manifest.sub!(/(android:minSdkVersion=').*?(')/) { "#$1#{sdk_level}#$2" } manifest.sub!(/(android:targetSdkVersion=').*?(')/) { "#$1#{sdk_level}#$2" } - File.open(MANIFEST_FILE, 'w') { |f| f << manifest } if manifest != old_manifest + if manifest != old_manifest + puts "\nUpdating #{File.basename MANIFEST_FILE} with target from #{File.basename PROJECT_PROPS_FILE}\n\n" + File.open(MANIFEST_FILE, 'w') { |f| f << manifest } + end end file RUBOTO_CONFIG_FILE +file JRUBY_ADAPTER_FILE => RUBOTO_CONFIG_FILE do + require 'yaml' + if (heap_alloc = YAML.load(File.read(RUBOTO_CONFIG_FILE))['heap_alloc']) + config = <<EOF + // BEGIN Ruboto HeapAlloc + @SuppressWarnings("unused") + byte[] arrayForHeapAllocation = new byte[#{heap_alloc} * 1024 * 1024]; + arrayForHeapAllocation = null; + // END Ruboto HeapAlloc +EOF + else + config = <<EOF + // BEGIN Ruboto HeapAlloc + // @SuppressWarnings("unused") + // byte[] arrayForHeapAllocation = new byte[13 * 1024 * 1024]; + // arrayForHeapAllocation = null; + // END Ruboto HeapAlloc +EOF + end + source = File.read(JRUBY_ADAPTER_FILE) + heap_alloc_pattern = %r{^\s*// BEGIN Ruboto HeapAlloc\n.*^\s*// END Ruboto HeapAlloc\n}m + File.open(JRUBY_ADAPTER_FILE, 'w'){|f| f << source.sub(heap_alloc_pattern, config)} +end + file APK_FILE => APK_DEPENDENCIES do |t| build_apk(t, false) end desc 'Copy scripts to emulator or device' @@ -453,19 +494,10 @@ FileUtils.rm_rf BUNDLE_PATH end # Methods -API_LEVEL_TO_VERSION = { - 7 => '2.1', 8 => '2.2', 10 => '2.3.3', 11 => '3.0', 12 => '3.1', - 13 => '3.2', 14 => '4.0', 15 => '4.0.3', 16 => '4.1.2', 17 => '4.2.2', -} - -def sdk_level_name - API_LEVEL_TO_VERSION[sdk_level] -end - def sdk_level File.read(PROJECT_PROPS_FILE).scan(/(?:target=android-)(\d+)/)[0][0].to_i end def mark_update(time = Time.now) @@ -543,11 +575,11 @@ def build_apk(t, release) apk_file = release ? RELEASE_APK_FILE : APK_FILE if File.exist?(apk_file) changed_prereqs = t.prerequisites.select do |p| - File.file?(p) && !Dir[p].empty? && Dir[p].map { |f| File.mtime(f) }.max > File.mtime(APK_FILE) + File.file?(p) && !Dir[p].empty? && Dir[p].map { |f| File.mtime(f) }.max > File.mtime(apk_file) end return false if changed_prereqs.empty? changed_prereqs.each { |f| puts "#{f} changed." } puts "Forcing rebuild of #{apk_file}." end @@ -659,170 +691,6 @@ end def stop_app output = `adb shell ps | grep #{package} | awk '{print $2}' | xargs adb shell kill` output !~ /Operation not permitted/ -end - -def start_emulator(sdk_level) - STDOUT.sync = true - if RbConfig::CONFIG['host_cpu'] == 'x86_64' - emulator_cmd = 'emulator64-arm' - else - emulator_cmd = 'emulator-arm' - end - - emulator_opts = '-partition-size 256' - if !ON_WINDOWS && ENV['DISPLAY'].nil? - emulator_opts << ' -no-window -no-audio' - end - - avd_name = "Android_#{sdk_level_name}" - new_snapshot = false - - if `adb devices` =~ /emulator-5554/ - t = Net::Telnet.new('Host' => 'localhost', 'Port' => 5554, 'Prompt' => /^OK\n/) - t.waitfor(/^OK\n/) - output = '' - t.cmd('avd name') { |c| output << c } - t.close - if output =~ /(.*)\nOK\n/ - running_avd_name = $1 - if running_avd_name == avd_name - puts "Emulator #{avd_name} is already running." - return - else - puts "Emulator #{running_avd_name} is running." - end - else - raise "Unexpected response from emulator: #{output.inspect}" - end - else - puts 'No emulator is running.' - end - - loop do - `killall -0 #{emulator_cmd} 2> /dev/null` - if $? == 0 - `killall #{emulator_cmd}` - 10.times do |i| - `killall -0 #{emulator_cmd} 2> /dev/null` - if $? != 0 - break - end - if i == 3 - print 'Waiting for emulator to die: ...' - elsif i > 3 - print '.' - end - sleep 1 - end - puts - `killall -0 #{emulator_cmd} 2> /dev/null` - if $? == 0 - puts 'Emulator still running.' - `killall -9 #{emulator_cmd}` - sleep 1 - end - end - - if [17, 16, 15, 13, 11].include? sdk_level - abi_opt = '--abi armeabi-v7a' - elsif sdk_level == 10 - abi_opt = '--abi armeabi' - end - - unless File.exists? "#{ENV['HOME']}/.android/avd/#{avd_name}.avd" - puts "Creating AVD #{avd_name}" - puts `echo n | android create avd -a -n #{avd_name} -t android-#{sdk_level} #{abi_opt} -c 64M -s HVGA` - if $? != 0 - puts 'Failed to create AVD.' - exit 3 - end - avd_config_file_name = "#{ENV['HOME']}/.android/avd/#{avd_name}.avd/config.ini" - old_avd_config = File.read(avd_config_file_name) - heap_size = (File.read('AndroidManifest.xml') =~ /largeHeap/) ? 256 : 48 - new_avd_config = old_avd_config.gsub(/vm.heapSize=([0-9]*)/){|m| p m ; m.to_i < heap_size ? "vm.heapSize=#{heap_size}" : m} - File.write(avd_config_file_name, new_avd_config) if new_avd_config != old_avd_config - new_snapshot = true - end - - puts 'Start emulator' - system "emulator -avd #{avd_name} #{emulator_opts} #{'&' unless ON_WINDOWS}" - return if ON_WINDOWS - - 3.times do |i| - sleep 1 - `killall -0 #{emulator_cmd} 2> /dev/null` - if $? == 0 - break - end - if i == 3 - print 'Waiting for emulator: ...' - elsif i > 3 - print '.' - end - end - puts - `killall -0 #{emulator_cmd} 2> /dev/null` - if $? != 0 - puts 'Unable to start the emulator. Retrying without loading snapshot.' - system "emulator -no-snapshot-load -avd #{avd_name} #{emulator_opts} #{'&' unless ON_WINDOWS}" - 10.times do |i| - `killall -0 #{emulator_cmd} 2> /dev/null` - if $? == 0 - new_snapshot = true - break - end - if i == 3 - print 'Waiting for emulator: ...' - elsif i > 3 - print '.' - end - sleep 1 - end - end - - `killall -0 #{emulator_cmd} 2> /dev/null` - if $? == 0 - print 'Emulator started: ' - 50.times do - if `adb get-state`.chomp == 'device' - break - end - print '.' - sleep 1 - end - puts - if `adb get-state`.chomp == 'device' - break - end - end - puts 'Unable to start the emulator.' - end - - if new_snapshot - puts 'Allow the emulator to calm down a bit.' - sleep 15 - end - - system '( - set +e - for i in 1 2 3 4 5 6 7 8 9 10 ; do - sleep 6 - adb shell input keyevent 82 >/dev/null 2>&1 - if [ "$?" == "0" ] ; then - set -e - adb shell input keyevent 82 >/dev/null 2>&1 - adb shell input keyevent 4 >/dev/null 2>&1 - exit 0 - fi - done - echo "Failed to unlock screen" - set -e - exit 1 - ) &' - - system 'adb logcat > adb_logcat.log &' - - puts "Emulator #{avd_name} started OK." end