Class: DryRun::AndroidProject

Inherits:
Object
  • Object
show all
Defined in:
lib/dryrun/android_project.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, custom_app_path, custom_module, flavour, device) ⇒ AndroidProject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dryrun/android_project.rb', line 10

def initialize(path, custom_app_path, custom_module, flavour, device)

  @custom_app_path = custom_app_path
  @custom_module = custom_module
  @base_path = @custom_app_path? File.join(path, @custom_app_path) : path
  @flavour = flavour
  @device = device

  @settings_gradle_path = settings_gradle_file
  @main_gradle_file = main_gradle_file

  check_custom_app_path

  @modules = find_modules
end

Instance Method Details

#check_custom_app_pathObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dryrun/android_project.rb', line 26

def check_custom_app_path
  return unless @custom_app_path

  full_custom_path = @base_path
  settings_path = settings_gradle_file(full_custom_path)
  main_gradle_path = main_gradle_file(full_custom_path)
  return unless is_valid(main_gradle_path)

  @settings_gradle_path = settings_path
  @main_gradle_file = main_gradle_file

  @base_path = full_custom_path
end

#clear_app_dataObject



157
158
159
# File 'lib/dryrun/android_project.rb', line 157

def clear_app_data
   @device.shell("pm clear #{@package}")
end

#find_modulesObject



79
80
81
82
83
84
85
# File 'lib/dryrun/android_project.rb', line 79

def find_modules
  return [] unless is_valid

  content = File.open(@settings_gradle_path, "rb").read
  modules = content.scan(/'([^']*)'/)
  modules.each {|replacement| replacement.first.gsub!(':', '/')}
end

#get_execution_line_command(path_to_sample) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dryrun/android_project.rb', line 165

def get_execution_line_command(path_to_sample)
  manifest_file = get_manifest(path_to_sample)

  if manifest_file.nil?
    return false
  end

  doc = Oga.parse_xml(manifest_file)

  @package = get_package(doc)
  @launcher_activity = get_launcher_activity(doc)

  if !@launcher_activity
    return false
  end

  manifest_file.close

  return "am start -n \"#{get_launchable_activity}\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"
end

#get_launchable_activityObject



197
198
199
200
# File 'lib/dryrun/android_project.rb', line 197

def get_launchable_activity
  full_path_to_launcher = "#{@package}#{@launcher_activity.gsub(@package,'')}"
  "#{@package}/#{full_path_to_launcher}"
end

#get_launcher_activity(doc) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/dryrun/android_project.rb', line 206

def get_launcher_activity(doc)
  activities = doc.css('activity')
  activities.each do |child|
    intent_filter = child.css('intent-filter')

    if intent_filter != nil and intent_filter.length != 0
      return child.attr("android:name").value
    end
  end
  false
end

#get_manifest(path_to_sample) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/dryrun/android_project.rb', line 186

def get_manifest(path_to_sample)
  default_path = File.join(path_to_sample, 'src/main/AndroidManifest.xml')
  if File.exist?(default_path)
    return File.open(default_path)
  else
    Find.find(path_to_sample) do |path|
      return File.open(path) if path =~ /.*AndroidManifest.xml$/
    end
  end
end

#get_package(doc) ⇒ Object



202
203
204
# File 'lib/dryrun/android_project.rb', line 202

def get_package(doc)
  doc.xpath("//manifest").attr('package').first.value
end

#get_uninstall_commandObject



153
154
155
# File 'lib/dryrun/android_project.rb', line 153

def get_uninstall_command
   "adb uninstall \"#{@package}\""
end

#installObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dryrun/android_project.rb', line 87

def install
  Dir.chdir @base_path

  path, execute_line = sample_project

  if path == false and execute_line == false
    puts "Couldn't open the sample project, sorry!".red
    exit 1
  end

  builder = "gradle"

  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') and !is_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")
    puts "#{builder} install#{@flavour}Debug"
    DryrunUtils.execute("#{builder} install#{@flavour}Debug")
  end

  clear_app_data

  puts "Installing #{@package.green}...\n"
  puts "executing: #{execute_line.green}\n"

  @device.shell("#{execute_line}")
end

#is_gradle_wrappedObject



131
132
133
134
135
# File 'lib/dryrun/android_project.rb', line 131

def is_gradle_wrapped
  return false if !File.directory?('gradle/')

  File.exist?('gradle/wrapper/gradle-wrapper.properties') and File.exist?('gradle/wrapper/gradle-wrapper.jar')
end

#is_valid(main_gradle_file = @main_gradle_file) ⇒ Object



75
76
77
# File 'lib/dryrun/android_project.rb', line 75

def is_valid(main_gradle_file = @main_gradle_file)
  File.exist?(main_gradle_file)
end

#main_gradle_file(path = @base_path) ⇒ Object



71
72
73
# File 'lib/dryrun/android_project.rb', line 71

def main_gradle_file(path = @base_path)
  File.join(path, 'build.gradle')
end

#remove_application_idObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dryrun/android_project.rb', line 50

def remove_application_id
  # Open temporary file
  tmp = Tempfile.new("extract")

  file = "#{@path_to_sample}/build.gradle"

  # Write good lines to temporary file
  File.open(file, 'r') do |file|
    file.each do |l| tmp << l unless l.include? 'applicationId'
    end
  end
  tmp.close

  # Move temp file to origin
  FileUtils.mv(tmp.path, file)
end

#remove_local_propertiesObject



40
41
42
43
44
45
46
47
48
# File 'lib/dryrun/android_project.rb', line 40

def remove_local_properties
  Dir.chdir @base_path
  file_name = 'local.properties'

  File.delete(file_name) if File.exist?(file_name)
  if !Gem.win_platform?
    DryrunUtils.execute("touch #{file_name}")
  end
end

#sample_projectObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dryrun/android_project.rb', line 137

def sample_project
  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)
  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
    end
  end
  [false, false]
end

#settings_gradle_file(path = @base_path) ⇒ Object



67
68
69
# File 'lib/dryrun/android_project.rb', line 67

def settings_gradle_file(path = @base_path)
  File.join(path, 'settings.gradle')
end

#uninstall_applicationObject



161
162
163
# File 'lib/dryrun/android_project.rb', line 161

def uninstall_application
  @device.shell("pm uninstall #{@package}")
end