<commit_before>require 'rbconfig'
require 'rubygems'
require 'time'
require 'rake/clean'
require 'rexml/document'

ON_WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i)

ANT_CMD = ON_WINDOWS ? 'ant.bat' : 'ant'

if `#{ANT_CMD} -version` !~ /version (\d+)\.(\d+)\.(\d+)/ || $1.to_i < 1 || ($1.to_i == 1 && $2.to_i < 8)
  puts "ANT version 1.8.0 or later required.  Version found: #{$1}.#{$2}.#{$3}"
  exit 1
end

adb_version_str = `adb version`
(puts 'Android SDK platform tools not in PATH (adb command not found).';exit 1) unless $? == 0
(puts "Unrecognized adb version: #$1";exit 1) unless adb_version_str =~ /Android Debug Bridge version (\d+\.\d+\.\d+)/
(puts "adb version 1.0.31 or later required.  Version found: #$1";exit 1) unless Gem::Version.new($1) >= Gem::Version.new('1.0.31')
unless ENV['ANDROID_HOME']
  unless ON_WINDOWS
    begin
      adb_path = `which adb`
      ENV['ANDROID_HOME'] = File.dirname(File.dirname(adb_path)) if $? == 0
    rescue Errno::ENOENT
      puts "Unable to detect adb location: #$!"
    end
  end
end
(puts 'You need to set the ANDROID_HOME environment variable.';exit 1) unless ENV['ANDROID_HOME']

# FIXME(uwe):  On windows the file is called dx.bat
dx_filename = File.join(ENV['ANDROID_HOME'], 'platform-tools', ON_WINDOWS ? 'dx.bat' : 'dx')
unless File.exists? dx_filename
  puts 'You need to install the Android SDK Platform-tools!'
  exit 1
end
new_dx_content = File.read(dx_filename).dup
# set defaultXmx=-Xmx1024M
xmx_pattern = /^defaultMx="-Xmx(\d+)(M|m|G|g|T|t)"/
if new_dx_content =~ xmx_pattern &&
    ($1.to_i * 1024 ** {'M' => 2, 'G' => 3, 'T' => 4}[$2.upcase]) < 3*1024**3
  puts "Increasing max heap space from #$1#$2 to 3G in #{dx_filename}"
  new_dx_content.sub!(xmx_pattern, 'defaultMx="-Xmx3G"')
  File.open(dx_filename, 'w') { |f| f << new_dx_content } rescue puts "\n!!! Unable to increase dx heap size !!!\n\n"
end

def manifest; @manifest ||= REXML::Document.new(File.read(MANIFEST_FILE)) end
def package; manifest.root.attribute('package') end
def build_project_name; @build_project_name ||= REXML::Document.new(File.read('build.xml')).elements['project'].attribute(:name).value end
def scripts_path; @sdcard_path ||= "/mnt/sdcard/Android/data/#{package}/files/scripts" end
def app_files_path; @app_files_path ||= "/data/data/#{package}/files" end

PROJECT_DIR        = File.expand_path('..', File.dirname(__FILE__))
UPDATE_MARKER_FILE = File.join(PROJECT_DIR, 'bin', 'LAST_UPDATE')
BUNDLE_JAR         = File.expand_path 'libs/bundle.jar'
BUNDLE_PATH        = File.expand_path 'bin/bundle'
MANIFEST_FILE      = File.expand_path 'AndroidManifest.xml'
PROJECT_PROPS_FILE = File.expand_path 'project.properties'
RUBOTO_CONFIG_FILE = File.expand_path 'ruboto.yml'
GEM_FILE           = File.expand_path 'Gemfile.apk'
GEM_LOCK_FILE      = File.expand_path 'Gemfile.apk.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']
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
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

CLEAN.include('bin', 'gen', 'test/bin', 'test/gen')
<commit_msg>* Try building with travis-ci - slightly lower dex heap size<commit_after>require 'rbconfig'
require 'rubygems'
require 'time'
require 'rake/clean'
require 'rexml/document'

ON_WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i)

ANT_CMD = ON_WINDOWS ? 'ant.bat' : 'ant'

if `#{ANT_CMD} -version` !~ /version (\d+)\.(\d+)\.(\d+)/ || $1.to_i < 1 || ($1.to_i == 1 && $2.to_i < 8)
  puts "ANT version 1.8.0 or later required.  Version found: #{$1}.#{$2}.#{$3}"
  exit 1
end

adb_version_str = `adb version`
(puts 'Android SDK platform tools not in PATH (adb command not found).';exit 1) unless $? == 0
(puts "Unrecognized adb version: #$1";exit 1) unless adb_version_str =~ /Android Debug Bridge version (\d+\.\d+\.\d+)/
(puts "adb version 1.0.31 or later required.  Version found: #$1";exit 1) unless Gem::Version.new($1) >= Gem::Version.new('1.0.31')
unless ENV['ANDROID_HOME']
  unless ON_WINDOWS
    begin
      adb_path = `which adb`
      ENV['ANDROID_HOME'] = File.dirname(File.dirname(adb_path)) if $? == 0
    rescue Errno::ENOENT
      puts "Unable to detect adb location: #$!"
    end
  end
end
(puts 'You need to set the ANDROID_HOME environment variable.';exit 1) unless ENV['ANDROID_HOME']

# FIXME(uwe):  On windows the file is called dx.bat
dx_filename = File.join(ENV['ANDROID_HOME'], 'platform-tools', ON_WINDOWS ? 'dx.bat' : 'dx')
unless File.exists? dx_filename
  puts 'You need to install the Android SDK Platform-tools!'
  exit 1
end
new_dx_content = File.read(dx_filename).dup
# set defaultXmx=-Xmx1024M
xmx_pattern = /^defaultMx="-Xmx(\d+)(M|m|G|g|T|t)"/
if new_dx_content =~ xmx_pattern &&
    ($1.to_i * 1024 ** {'M' => 2, 'G' => 3, 'T' => 4}[$2.upcase]) < 2560*1024**2
  puts "Increasing max heap space from #$1#$2 to 2560M in #{dx_filename}"
  new_dx_content.sub!(xmx_pattern, 'defaultMx="-Xmx2560M"')
  File.open(dx_filename, 'w') { |f| f << new_dx_content } rescue puts "\n!!! Unable to increase dx heap size !!!\n\n"
end

def manifest; @manifest ||= REXML::Document.new(File.read(MANIFEST_FILE)) end
def package; manifest.root.attribute('package') end
def build_project_name; @build_project_name ||= REXML::Document.new(File.read('build.xml')).elements['project'].attribute(:name).value end
def scripts_path; @sdcard_path ||= "/mnt/sdcard/Android/data/#{package}/files/scripts" end
def app_files_path; @app_files_path ||= "/data/data/#{package}/files" end

PROJECT_DIR        = File.expand_path('..', File.dirname(__FILE__))
UPDATE_MARKER_FILE = File.join(PROJECT_DIR, 'bin', 'LAST_UPDATE')
BUNDLE_JAR         = File.expand_path 'libs/bundle.jar'
BUNDLE_PATH        = File.expand_path 'bin/bundle'
MANIFEST_FILE      = File.expand_path 'AndroidManifest.xml'
PROJECT_PROPS_FILE = File.expand_path 'project.properties'
RUBOTO_CONFIG_FILE = File.expand_path 'ruboto.yml'
GEM_FILE           = File.expand_path 'Gemfile.apk'
GEM_LOCK_FILE      = File.expand_path 'Gemfile.apk.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']
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
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

CLEAN.include('bin', 'gen', 'test/bin', 'test/gen')

{'fraction_non_alphanumeric': 0.17126436781609194, 'fraction_numerical': 0.014942528735632184, 'mean_word_length': 4.703568827385288, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4792296', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2954, 'n_words': 595}