Class: Lasertag::MainApp

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

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ MainApp

Returns a new instance of MainApp.



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

def initialize(arguments)

  # defaults
  @app_path = Dir.pwd
  @app_module = nil
  @app_flavor = nil
  @app_remote = 'origin'
  @allow_push = true

  @require_analyses = true

  # Parse Options
  arguments.push "-h" if arguments.length == 0
  create_options_parser(arguments)

  manage_opts

end

Instance Method Details

#android_home_is_definedObject

IS THE ANDROID HOME DEFINED?



170
171
172
173
# File 'lib/lasertag.rb', line 170

def android_home_is_defined
  sdk = `echo $ANDROID_HOME`.gsub("\n",'')
  !sdk.empty?
end

#assemble_commandObject

Assemble command



256
257
258
# File 'lib/lasertag.rb', line 256

def assemble_command
  "gradle :#{@app_module}:assemble#{(@app_flavor or "").capitalize}Release"
end

#callObject



175
176
177
178
179
180
181
182
# File 'lib/lasertag.rb', line 175

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

#convert_values_to_hash(str) ⇒ Object

Convert the app info to an hash



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/lasertag.rb', line 186

def convert_values_to_hash (str)
  hash = Hash.new

  str.split(/\r?\n|\r/).each { |line|
    next unless line.include? ':'
    indexOf = line.index(':')
    next unless indexOf > 0

    key = line[0..indexOf-1].strip
    value = line[indexOf+1..line.length].strip

    hash[key] = value
  }

  hash
end

#create_options_parser(args) ⇒ Object

Options parser



29
30
31
32
33
34
35
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
# File 'lib/lasertag.rb', line 29

def create_options_parser(args)

  args.options do |opts|
    opts.banner = "Usage: lasertag [OPTIONS]"
    opts.separator  ''
    opts.separator  "Options"

    opts.on('-m', '--module MODULE', 'Specifies the app module') do |app_module|
      @app_module = app_module
    end

    opts.on('-f', '--flavor FLAVOR', 'Specifies the flavor (e.g. dev, qa, prod)') do |app_flavor|
      @app_flavor = app_flavor
    end

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

    opts.on('-r REMOTE', '--remote REMOTE', 'Custom remote to your project (default: "origin")') do |app_remote|
      @app_remote = app_remote
    end

    opts.on('-d', '--dont-push', 'Only tags the code, doesn\'t push it upstream') do |dont|
      @allow_push = false
    end

    opts.on('-h', '--help', 'Displays help') do
      @require_analyses = false
      puts opts.help
      exit
    end
    opts.on('-v', '--version', 'Displays version') do
      @require_analyses = false
      puts Lasertag::VERSION
      exit
    end
    opts.parse!

  end
end

#delete_tag(tag) ⇒ Object

DELETE TAG



147
148
149
150
# File 'lib/lasertag.rb', line 147

def delete_tag(tag)
  g = Git.open(Dir.pwd)
  g.delete_tag(tag)
end

#get_app_infoObject

Get the app info



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/lasertag.rb', line 214

def get_app_info
  path = get_path_to_merged_manifest
  handle = File.open(path)

  parser = Oga.parse_xml(handle)

  package     = parser.xpath("//manifest").attr('package').last.value
  versionCode = parser.xpath("//manifest").attr('android:versionCode').last.value
  versionName = parser.xpath("//manifest").attr('android:versionName').last.value

  {
    package: package,
    versionCode: versionCode,
    versionName: versionName,
  }
end

#get_path_to_merged_manifestObject

Path to the merged manifest



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/lasertag.rb', line 232

def get_path_to_merged_manifest
  build_dir = @hash['buildDir']

  flavor = @app_flavor ? "/#{@app_flavor}/" : "/"

  path = "#{build_dir}/intermediates/manifests/full"
  path = "#{path}#{flavor}release/"

  full_path = File.join(path, 'AndroidManifest.xml')

  exists = File.exist?(full_path)

  unless exists
    puts "The merged manifest could not be found\n"
    puts "   Try specifying a Flavor\n".green
    system("lasertag -h")
    exit 1
  end


  full_path
end

#has_uncommited_codeObject

HAS UNCOMMITED CODE?



129
130
131
132
# File 'lib/lasertag.rb', line 129

def has_uncommited_code
  g = Git.open(Dir.pwd)
  g.status.changed.size > 0
end

#manage_optsObject

Manage options



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
# File 'lib/lasertag.rb', line 72

def manage_opts

  unless @app_module
    puts "Please give me an app module name".yellow
    exit 1
  end

  Dir.chdir @app_path

  if has_uncommited_code
    puts "You have uncommited code, please commit it first".red
    exit 1
  end

  ### Assemble
  is_success = system assemble_command

  unless is_success
    puts "\n\nSomething went wrong so I stopped all the madness!!\n".red
    exit 1
  end

  ### Get project properties
  @hash = project_properties @app_module

  ### Find app info
  app_info = get_app_info

  puts "\nFor package #{app_info[:package].yellow}\n"

  ### git tag -a "versionNumber" -m "versionName"

  package = app_info[:package]
  name = @app_module # not quite correct
  version = app_info[:versionName]

  builded = "#{@app_module}/build/outputs/apk/#{@app_module}#{"-#{@app_flavor}" if @app_flavor}-release-unsigned.apk"

  puts "#{name} #{version} built to #{builded}.".green

  tag_code "v#{version}"
  puts "Tagged v#{version}.".green

  unless @allow_push
    exit 1
  end

  #$ git push origin v#{version}
  push_tag "v#{version}"

  puts "Pushed git commits and tags.".green
  puts "Pushed #{name} #{version} to remote.".green

end

#project_properties(project = nil) ⇒ Object

Get the project properties



204
205
206
207
208
209
210
# File 'lib/lasertag.rb', line 204

def project_properties(project=nil)

  project = "#{project}:" if project

  result = %x[gradle #{project}properties]
  convert_values_to_hash result
end

#push_tag(tag) ⇒ Object

PUSH TAG



135
136
137
138
139
140
141
142
143
144
# File 'lib/lasertag.rb', line 135

def push_tag(tag)
  g = Git.open(Dir.pwd)
  begin
    g.push(g.remote(@app_remote), tag)
  rescue Exception => e
    puts "Can't push tag\n reason: #{e.to_s.red}"
    delete_tag tag
    exit 1
  end
end

#tag_code(version) ⇒ Object

TAG CODE



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lasertag.rb', line 153

def tag_code(version)
  g = Git.open(Dir.pwd)
  begin
    g.add_tag(version, {:message => "Version #{version.gsub(/^v/, "")}"})
  rescue Exception => e
    expected = "'#{version}' already exists"
    if e.to_s.include? expected
      puts "Error ocurred: #{expected.red}"
    else
      puts "Can't tag the code\n reason: #{e.to_s.red}"
    end

    exit 1
  end
end