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

Returns a new instance of AndroidProject.



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

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
  @gradle_file_extension = gradle_file_extension
  @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



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dryrun/android_project.rb', line 34

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 valid?(main_gradle_path)

  @settings_gradle_path = settings_path
  @main_gradle_file = main_gradle_file

  @base_path = full_custom_path
end

#create_builderObject



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

def create_builder
  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') && !gradle_wrapped?
  GradleAdapter.new(builder)
end

#execute_command(command) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dryrun/android_project.rb', line 99

def execute_command(command)
  Dir.chdir @base_path

  path = sample_project
  if path == false or !@launcher_activity
    puts "Couldn't open or there isnt any sample project, sorry!".red
    exit 1
  end

  builder = create_builder

  remove_application_id
  remove_local_properties

  command.run(builder, @package, @launcher_activity, @custom_module, @flavour, @device)
end

#find_modulesObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/dryrun/android_project.rb', line 88

def find_modules
  return [] unless valid?

  content = File.open(@settings_gradle_path, 'rb').read
  
  content = content.split(/\n/).delete_if { |x| !x.start_with?("include")}.join("\n")
  modules = content.scan(/'([^']*)'/) + content.scan(/\"([^"]*)\"/)
  
  modules.each {|replacement| replacement.first.tr!(':', '')}
end

#get_manifest(path_to_sample) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dryrun/android_project.rb', line 157

def get_manifest(path_to_sample)
  default_path = File.join(path_to_sample, 'src/main/AndroidManifest.xml')

  if File.exist?(default_path)
    File.open(default_path)
  else
    puts path_to_sample
    Find.find(path_to_sample) do |path|
      return File.open(path) if path =~ /.*AndroidManifest.xml$/
    end
  end
end

#gradle_file_extensionObject



26
27
28
29
30
31
32
# File 'lib/dryrun/android_project.rb', line 26

def gradle_file_extension
  gradle_file = File.join(@base_path, 'settings.gradle.kts')
  if (File.exist?(gradle_file))
    return ".gradle.kts"
  end
  ".gradle"
end

#gradle_wrapped?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
# File 'lib/dryrun/android_project.rb', line 116

def gradle_wrapped?
  return false unless File.directory?('gradle/')

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

#main_gradle_file(path = @base_path) ⇒ Object



79
80
81
# File 'lib/dryrun/android_project.rb', line 79

def main_gradle_file(path = @base_path)
  File.join(path, "build#{@gradle_file_extension}")
end

#parse_manifest(path_to_sample) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dryrun/android_project.rb', line 145

def parse_manifest(path_to_sample)
  manifest_file = get_manifest(path_to_sample)

  return false if manifest_file.nil?

  manifest_parser = ManifestParser.new(manifest_file)
  @package = manifest_parser.package
  @launcher_activity = manifest_parser.launcher_activity
  manifest_file.close
  @launcher_activity && @package
end

#remove_application_idObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dryrun/android_project.rb', line 57

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

  file = "#{@path_to_sample}/build#{@gradle_file_extension}"

  # Write good lines to temporary file
  File.open(file, 'r') do |f|
    f.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



48
49
50
51
52
53
54
55
# File 'lib/dryrun/android_project.rb', line 48

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

  File.delete(file_name) if File.exist?(file_name)

  DryrunUtils.execute("touch #{file_name}") unless Gem.win_platform?
end

#sample_projectObject



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dryrun/android_project.rb', line 123

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 if parse_manifest(@path_to_sample)
  else
    @modules.each do |child|
      full_path = File.join(@base_path, child.first)
      @path_to_sample = full_path
      return full_path if parse_manifest(full_path)
    end
  end
  false
end

#settings_gradle_file(path = @base_path) ⇒ Object



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

def settings_gradle_file(path = @base_path)
  File.join(path, "settings#{@gradle_file_extension}")
end

#uninstall_applicationObject



141
142
143
# File 'lib/dryrun/android_project.rb', line 141

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

#uninstall_commandObject



137
138
139
# File 'lib/dryrun/android_project.rb', line 137

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

#valid?(main_gradle_file = @main_gradle_file) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/dryrun/android_project.rb', line 83

def valid?(main_gradle_file = @main_gradle_file)
  File.exist?(main_gradle_file) &&
      File.exist?(@settings_gradle_path)
end