lib/dryrun/android_project.rb in dryrun-1.0.0 vs lib/dryrun/android_project.rb in dryrun-1.1.0

- old
+ new

@@ -1,10 +1,12 @@ require 'oga' require 'fileutils' require 'tempfile' require 'find' require_relative 'dryrun_utils' +require_relative 'manifest_parser' +require_relative 'gradle_adapter' module Dryrun class AndroidProject def initialize(path, custom_app_path, custom_module, flavour, device) @custom_app_path = custom_app_path @@ -50,11 +52,12 @@ file = "#{@path_to_sample}/build.gradle" # Write good lines to temporary file File.open(file, 'r') do |f| - f.each do |l| tmp << l unless l.include? 'applicationId' + f.each do |l| + tmp << l unless l.include? 'applicationId' end end tmp.close # Move temp file to origin @@ -69,123 +72,81 @@ File.join(path, 'build.gradle') end def valid?(main_gradle_file = @main_gradle_file) File.exist?(main_gradle_file) && - File.exist?(@settings_gradle_path) + File.exist?(@settings_gradle_path) end def find_modules return [] unless valid? content = File.open(@settings_gradle_path, 'rb').read modules = content.scan(/'([^']*)'/) - modules.each { |replacement| replacement.first.tr!(':', '/') } + modules.each {|replacement| replacement.first.tr!(':', '/')} end - def install + def execute_command(command) Dir.chdir @base_path - path, execute_line = sample_project + path = sample_project + manifest_parsed = parse_manifest(path) - if path == false && execute_line == false + if path == false && manifest_parsed == false puts "Couldn't open the sample project, sorry!".red exit 1 end - builder = 'gradle' + builder = create_builder - if File.exist?('gradlew') - if !Gem.win_platform? - DryrunUtils.execute('chmod +x gradlew') - else - DryrunUtils.execute('icacls gradlew /T') - end - builder = './gradlew' - end - - # Generate the gradle/ folder - DryrunUtils.execute('gradle wrap') if File.exist?('gradlew') && !gradle_wrapped? - remove_application_id remove_local_properties - if @custom_module - DryrunUtils.execute("#{builder} clean") - DryrunUtils.execute("#{builder} :#{@custom_module}:install#{@flavour}Debug") - else - DryrunUtils.execute("#{builder} clean") - - if @device.nil? - puts 'No devices picked/available, proceeding with assemble instead'.green - puts "#{builder} assemble#{@flavour}Debug" - DryrunUtils.execute("#{builder} assemble#{@flavour}Debug") - else - puts "#{builder} install#{@flavour}Debug" - DryrunUtils.execute("#{builder} install#{@flavour}Debug") - end - end - - unless @device.nil? - clear_app_data - puts "Installing #{@package.green}...\n" - puts "executing: #{execute_line.green}\n" - - DryrunUtils.run_adb("shell #{execute_line}") - end + command.run(builder, @package, @launcher_activity, @custom_module, @flavour, @device) end def gradle_wrapped? return false unless File.directory?('gradle/') File.exist?('gradle/wrapper/gradle-wrapper.properties') && - File.exist?('gradle/wrapper/gradle-wrapper.jar') + File.exist?('gradle/wrapper/gradle-wrapper.jar') end def sample_project - if @custom_module && @modules.any? { |m| m.first == "/#{@custom_module}" } + if @custom_module && @modules.any? {|m| m.first == "/#{@custom_module}"} @path_to_sample = File.join(@base_path, "/#{@custom_module}") - return @path_to_sample, get_execution_line_command(@path_to_sample) + return @path_to_sample else @modules.each do |child| full_path = File.join(@base_path, child.first) @path_to_sample = full_path - - execution_line_command = get_execution_line_command(full_path) - return full_path, execution_line_command if execution_line_command + return full_path end end - [false, false] + false end def uninstall_command "adb uninstall \"#{@package}\"" end - def clear_app_data - DryrunUtils.run_adb("shell pm clear #{@package}") - end - def uninstall_application DryrunUtils.run_adb("shell pm uninstall #{@package}") end - def get_execution_line_command(path_to_sample) + def parse_manifest(path_to_sample) manifest_file = get_manifest(path_to_sample) return false if manifest_file.nil? - doc = Oga.parse_xml(manifest_file) + manifest_parser = ManifestParser.new(manifest_file) + @package = manifest_parser.package + @launcher_activity = manifest_parser.launcher_activity - @package = get_package(doc) - @launcher_activity = get_launcher_activity(doc) - return false unless @launcher_activity manifest_file.close - - "am start -n \"#{launcheable_activity}\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER" end def get_manifest(path_to_sample) default_path = File.join(path_to_sample, 'src/main/AndroidManifest.xml') @@ -197,27 +158,23 @@ return File.open(path) if path =~ /.*AndroidManifest.xml$/ end end end - def launcheable_activity - full_path_to_launcher = "#{@package}#{@launcher_activity.gsub(@package, '')}" - "#{@package}/#{full_path_to_launcher}" - end + def create_builder + builder = 'gradle' - def get_package(doc) - doc.xpath('//manifest').attr('package').first.value - end - - def get_launcher_activity(doc) - activities = doc.css('activity') - activities.each do |child| - intent_filter = child.css('intent-filter') - - if !intent_filter.nil? && !intent_filter.empty? - return child.attr('android:name').value + if File.exist?('gradlew') + if !Gem.win_platform? + DryrunUtils.execute('chmod +x gradlew') + else + DryrunUtils.execute('icacls gradlew /T') end + builder = './gradlew' end - false + + # Generate the gradle/ folder + DryrunUtils.execute('gradle wrap') if File.exist?('gradlew') && !gradle_wrapped? + GradleAdapter.new(builder) end end end