<commit_before># Base Ruboto stuff and dependencies
require 'ruboto'

# Command-specific dependencies
require 'ruboto/util/asset_copier'
require 'ruboto/util/log_action'
require 'ruboto/util/xml_element'
require 'ruboto/util/code_formatting'
require 'ruboto/util/build'
require 'ruboto/util/update'
require 'ruboto/util/verify'
require 'ruboto/util/scan_in_api'

require 'ruboto/core_ext/array'
require 'ruboto/core_ext/object'

module Ruboto
  module Commands
    module Base
      def self.main
        Main {
          mode "gen" do
            mode "app" do
              include Ruboto::Util::LogAction
              include Ruboto::Util::Build
              include Ruboto::Util::Update
              include Ruboto::Util::Verify

              option("package"){
                required
                argument :required
                description "Name of package. Must be unique for every app. A common pattern is yourtld.yourdomain.appname (Ex. org.ruboto.irb)"
              }
              option("name"){
                argument :required
                description "Name of your app.  Defaults to the last part of the package name capitalized."
              }
              option("activity"){
                argument :required
                description "Name of your primary Activity.  Defaults to the name of the application with Activity appended."
              }
              option("path"){
                argument :required
                description "Path to where you want your app.  Defaults to the last part of the package name."
              }
              option("min_sdk") {
                argument :required
                defaults 'android-7'
                description "Minimum android version supported. must begin with 'android-'."
              }
              option("target") {
                argument :required
                description "Android version to target. must begin with 'android-' (e.g., 'android-8' for froyo)"
              }

              def run
                package = params['package'].value
                name = params['name'].value || package.split('.').last.split('_').map{|s| s.capitalize}.join
                activity = params['activity'].value || "#{name}Activity"
                path = params['path'].value || package.split('.').last
                min_sdk = params['min_sdk'].value
                target = params['target'].value || min_sdk

                abort "path must be to a directory that does not yet exist. It will be created." if File.exists?(path)

                root = File.expand_path(path)
                print "\nGenerating Android app #{name} in #{root}..."
                system "android create project -n #{name} -t #{target} -p #{path} -k #{package} -a #{activity}"
                exit $? unless $? == 0
                Dir.chdir path do
                  verify_strings.root.elements['string'].text = name
                  File.open("res/values/strings.xml", 'w') {|f| verify_strings.document.write(f, 4)}
                end
                puts "Done"

                print "\nGenerating Android test project #{name} in #{root}..."
                system "android create test-project -m .. -n #{name}Test -p #{path}/test"
                FileUtils.rm_rf File.join(root, 'test', 'src', package.split('.'))
                puts "Done"

                puts "\nCopying files:"
                copier = Ruboto::Util::AssetCopier.new Ruboto::ASSETS, root

                %w{Rakefile .gitignore assets res test}.each do |f|
                  log_action(f) {copier.copy f}
                end

                log_action("Ruboto java classes"){copier.copy "src/org/ruboto/*.java", "src/org/ruboto"}
                log_action("Ruboto java test classes"){copier.copy "src/org/ruboto/test/*.java", "test/src/org/ruboto/test"}

                Dir.chdir root do
                  update_jruby true

                  log_action("\nAdding activities (RubotoActivity and RubotoDialog) and SDK versions to the manifest") do
                    verify_manifest.elements['application'].attributes['android:hardwareAccelerated'] = 'true'
                    verify_manifest.elements['application'].add_element 'activity', {"android:name" => "org.ruboto.RubotoActivity"}
                    verify_manifest.elements['application'].add_element 'activity', {"android:name" => "org.ruboto.RubotoDialog",
                      "android:theme" => "@android:style/Theme.Dialog"}
                      verify_manifest.add_element 'uses-sdk', {"android:minSdkVersion" => min_sdk[/\d+/], "android:targetSdkVersion" => target[/\d+/]}
                      File.open("AndroidManifest.xml", 'w') {|f| verify_manifest.document.write(f, 4)}
                    end

                    update_ruboto true

                    generate_core_classes(:class => "all", :method_base => "on", :method_include => "", :method_exclude => "", :force => true, :implements => "")
                  end

                  log_action("Generating the default Activity and script") do
                    generate_inheriting_file "Activity", activity, package, "#{underscore(activity)}.rb", path
                  end

                  Dir.chdir File.join(root, 'test') do
                    test_manifest = REXML::Document.new(File.read('AndroidManifest.xml')).root
                    test_manifest.elements['instrumentation'].attributes['android:name'] = 'org.ruboto.test.InstrumentationTestRunner'
                    File.open("AndroidManifest.xml", 'w') {|f| test_manifest.document.write(f, 4)}
                    File.open('build.properties', 'a'){|f| f.puts 'test.runner=org.ruboto.test.InstrumentationTestRunner'}
                    ant_setup_line = /^(\s*<setup\s*\/>\n)/
                    run_tests_override = <<-EOF
                    <macrodef name="run-tests-helper">
                    <attribute name="emma.enabled" default="false"/>
                    <element name="extra-instrument-args" optional="yes"/><commit_msg>* Only add hardware acceleration if min_sdk >= 11<commit_after># Base Ruboto stuff and dependencies
require 'ruboto'

# Command-specific dependencies
require 'ruboto/util/asset_copier'
require 'ruboto/util/log_action'
require 'ruboto/util/xml_element'
require 'ruboto/util/code_formatting'
require 'ruboto/util/build'
require 'ruboto/util/update'
require 'ruboto/util/verify'
require 'ruboto/util/scan_in_api'

require 'ruboto/core_ext/array'
require 'ruboto/core_ext/object'

module Ruboto
  module Commands
    module Base
      def self.main
        Main {
          mode "gen" do
            mode "app" do
              include Ruboto::Util::LogAction
              include Ruboto::Util::Build
              include Ruboto::Util::Update
              include Ruboto::Util::Verify

              option("package"){
                required
                argument :required
                description "Name of package. Must be unique for every app. A common pattern is yourtld.yourdomain.appname (Ex. org.ruboto.irb)"
              }
              option("name"){
                argument :required
                description "Name of your app.  Defaults to the last part of the package name capitalized."
              }
              option("activity"){
                argument :required
                description "Name of your primary Activity.  Defaults to the name of the application with Activity appended."
              }
              option("path"){
                argument :required
                description "Path to where you want your app.  Defaults to the last part of the package name."
              }
              option("min_sdk") {
                argument :required
                defaults 'android-7'
                description "Minimum android version supported. must begin with 'android-'."
              }
              option("target") {
                argument :required
                description "Android version to target. must begin with 'android-' (e.g., 'android-8' for froyo)"
              }

              def run
                package = params['package'].value
                name = params['name'].value || package.split('.').last.split('_').map{|s| s.capitalize}.join
                activity = params['activity'].value || "#{name}Activity"
                path = params['path'].value || package.split('.').last
                min_sdk = params['min_sdk'].value
                target = params['target'].value || min_sdk

                abort "path must be to a directory that does not yet exist. It will be created." if File.exists?(path)

                root = File.expand_path(path)
                print "\nGenerating Android app #{name} in #{root}..."
                system "android create project -n #{name} -t #{target} -p #{path} -k #{package} -a #{activity}"
                exit $? unless $? == 0
                Dir.chdir path do
                  verify_strings.root.elements['string'].text = name
                  File.open("res/values/strings.xml", 'w') {|f| verify_strings.document.write(f, 4)}
                end
                puts "Done"

                print "\nGenerating Android test project #{name} in #{root}..."
                system "android create test-project -m .. -n #{name}Test -p #{path}/test"
                FileUtils.rm_rf File.join(root, 'test', 'src', package.split('.'))
                puts "Done"

                puts "\nCopying files:"
                copier = Ruboto::Util::AssetCopier.new Ruboto::ASSETS, root

                %w{Rakefile .gitignore assets res test}.each do |f|
                  log_action(f) {copier.copy f}
                end

                log_action("Ruboto java classes"){copier.copy "src/org/ruboto/*.java", "src/org/ruboto"}
                log_action("Ruboto java test classes"){copier.copy "src/org/ruboto/test/*.java", "test/src/org/ruboto/test"}

                Dir.chdir root do
                  update_jruby true

                  log_action("\nAdding activities (RubotoActivity and RubotoDialog) and SDK versions to the manifest") do
                    if min_sdk.to_i >= 11
                      verify_manifest.elements['application'].attributes['android:hardwareAccelerated'] = 'true'
                    end
                    verify_manifest.elements['application'].add_element 'activity', {"android:name" => "org.ruboto.RubotoActivity"}
                    verify_manifest.elements['application'].add_element 'activity', {"android:name" => "org.ruboto.RubotoDialog",
                      "android:theme" => "@android:style/Theme.Dialog"}
                      verify_manifest.add_element 'uses-sdk', {"android:minSdkVersion" => min_sdk[/\d+/], "android:targetSdkVersion" => target[/\d+/]}
                      File.open("AndroidManifest.xml", 'w') {|f| verify_manifest.document.write(f, 4)}
                    end

                    update_ruboto true

                    generate_core_classes(:class => "all", :method_base => "on", :method_include => "", :method_exclude => "", :force => true, :implements => "")
                  end

                  log_action("Generating the default Activity and script") do
                    generate_inheriting_file "Activity", activity, package, "#{underscore(activity)}.rb", path
                  end

                  Dir.chdir File.join(root, 'test') do
                    test_manifest = REXML::Document.new(File.read('AndroidManifest.xml')).root
                    test_manifest.elements['instrumentation'].attributes['android:name'] = 'org.ruboto.test.InstrumentationTestRunner'
                    File.open("AndroidManifest.xml", 'w') {|f| test_manifest.document.write(f, 4)}
                    File.open('build.properties', 'a'){|f| f.puts 'test.runner=org.ruboto.test.InstrumentationTestRunner'}
                    ant_setup_line = /^(\s*<setup\s*\/>\n)/
                    run_tests_override = <<-EOF
                    <macrodef name="run-tests-helper">
                    <attribute name="emma.enabled" default="false"/>
                    <element name="extra-instrument-args" optional="yes"/>
{'fraction_non_alphanumeric': 0.12024378191401747, 'fraction_numerical': 0.0013177400757700543, 'mean_word_length': 1.8700070905223352, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '5187740', 'n_tokens_mistral': 3281, 'n_tokens_neox': 3049, 'n_words': 782}