Class: Dryrun::MainApp

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

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ MainApp

Returns a new instance of MainApp.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dryrun.rb', line 17

def initialize(arguments)
  outdated_verification

  @url = %w(-h --help -v --version -w --wipe).include?(arguments.first) ? nil : arguments.shift

  # defaults
  @app_path = nil
  @custom_module = nil
  @flavour = ''
  @tag = nil
  @branch = 'master'
  @devices = []
  @cleanup = false
  @command = InstallApplicationCommand.new

  # Parse Options
  create_options_parser(arguments)
end

Instance Method Details

#android_sdk_root_is_definedObject



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

def android_sdk_root_is_defined
  @sdk = if !Gem.win_platform?
           `echo $ANDROID_SDK_ROOT`.delete('\n')
         else
           `echo %ANDROID_SDK_ROOT%`.delete('\n')
         end
  !@sdk.empty?
end

#callObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/dryrun.rb', line 162

def call
  unless android_sdk_root_is_defined
    puts "\nWARNING: your #{'$ANDROID_SDK_ROOT'.yellow} is not defined\n"
    puts "\nhint: in your #{'~/.bashrc'.yellow} or #{'~/.bash_profile'.yellow}  add:\n  #{"export ANDROID_SDK_ROOT='/Users/cesarferreira/Library/Android/sdk/'".yellow}"
    puts "\nNow type #{'source ~/.bashrc'.yellow}\n\n"
    exit 1
  end

  if @url.nil?
    puts 'You need to insert a valid GIT URL/folder'
    exit 1
  end

  pick_device

  if DryrunUtils::is_folder? (@url)
    repository_path = File.expand_path @url
  else

    @url = @url.split('?').first
    @url.chop! if @url.end_with? '/'

    github = Github.new(@url)

    unless github.valid?
      puts "#{@url.red} is not a valid git @url"
      exit 1
    end

    # clone the repository
    repository_path = github.clone(@branch, @tag, @cleanup)

  end

  android_project = AndroidProject.new(repository_path, @app_path, @custom_module, @flavour, @device)

  # is a valid android project?
  unless android_project.valid?
    puts "#{@url.red} is not a valid android project"
    exit 1
  end

  puts "Using custom app folder: #{@app_path.green}" if @app_path
  puts "Using custom module: #{@custom_module.green}" if @custom_module

  # clean and install the apk
  android_project.execute_command(@command)

  puts "\n> If you want to remove the app you just installed, execute:\n#{android_project.uninstall_command.yellow}\n\n"
end

#create_options_parser(args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dryrun.rb', line 36

def create_options_parser(args)
  args.options do |opts|
    opts.banner = 'Usage: dryrun GIT_URL [OPTIONS]'
    opts.separator ''
    opts.separator 'Options'

    opts.on('-m MODULE_NAME', '--module MODULE_NAME', 'Custom module to run') do |custom_module|
      @custom_module = custom_module
    end

    opts.on('-b BRANCH_NAME', '--branch BRANCH_NAME', 'Checkout custom branch to run') do |branch|
      @branch = branch
    end

    opts.on('-f', '--flavour FLAVOUR', 'Custom flavour (e.g. dev, qa, prod)') do |flavour|
      @flavour = flavour.capitalize
    end

    opts.on('-p PATH', '--path PATH', 'Custom path to android project') do |app_path|
      @app_path = app_path
    end

    opts.on('-t TAG', '--tag TAG', 'Checkout tag/commit hash to clone (e.g. "v0.4.5", "6f7dd4b")') do |tag|
      @tag = tag
    end

    opts.on('-c', '--cleanup', 'Clean the temporary folder before cloning the project') do
      @cleanup = true
    end

    opts.on('-w', '--wipe', 'Wipe the temporary dryrun folder') do
      wipe_temporary_folder
    end

    opts.on('-h', '--help', 'Displays help') do
      puts opts.help
      exit
    end

    opts.on('-v', '--version', 'Displays the version') do
      puts Dryrun::VERSION
      exit
    end

    opts.on('-a', '--android-test', 'Execute android tests') do
      @command = TestApplicationCommand.new
    end

    opts.parse!
  end
end

#outdated_verificationObject



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

def outdated_verification
  return if DryrunUtils.up_to_date

  input = nil

  begin
    input = ask "\n#{'Your Dryrun version is outdated, want to update?'.yellow} #{'[Y/n]:'.white}"
  end until %w(y n s).include?(input.downcase)

  DryrunUtils.execute('gem update dryrun') if input.casecmp('y') == 0
end

#pick_deviceObject



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dryrun.rb', line 100

def pick_device
  @device = nil

  if !Gem.win_platform?
    @sdk = `echo $ANDROID_SDK_ROOT`.delete("\n")
  else
    @sdk = `echo %ANDROID_SDK_ROOT%`.delete("\n")
  end

  @sdk = 'adb' if @sdk.empty?

  $sdk = @sdk

  puts 'Searching for devices...'.yellow

  @devices = DryrunUtils.run_adb('devices')

  # if @devices.nil? || @devices.empty?
  #   puts 'Killing adb, there might be an issue with it...'
  #   DryrunUtils.run_adb('kill-server')
  #   @devices = DryrunUtils.run_adb('devices')
  # end

  puts 'No devices attached, but I\'ll run anyway' if @devices.empty?

  if @devices.size >= 2
    puts 'Pick your device (1,2,3...):'

    @devices.each_with_index.map {|key, index| puts "#{index.to_s.green} -  #{key.name} \n"}

    input = gets.chomp

    @device = if input.match(/^\d+$/) && input.to_i <= (@devices.length - 1) && input.to_i >= 0
                @devices[input.to_i]
              else
                @devices.first
              end
  else
    @device = @devices.first
  end

  $device = @device
  puts "Picked #{@device.name.to_s.green}" unless @device.nil?
end

#wipe_temporary_folderObject



154
155
156
157
158
159
160
# File 'lib/dryrun.rb', line 154

def wipe_temporary_folder
  tmpdir = Dir.tmpdir + '/dryrun/'
  puts 'Wiping ' + tmpdir.red
  FileUtils.rm_rf tmpdir
  puts 'Folder totally removed!'.green
  exit 1
end