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?



191
192
193
194
# File 'lib/lasertag.rb', line 191

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

#assemble_commandObject



279
280
281
# File 'lib/lasertag.rb', line 279

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

#callObject



196
197
198
199
200
201
202
203
# File 'lib/lasertag.rb', line 196

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



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

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



28
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
# File 'lib/lasertag.rb', line 28

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



168
169
170
171
# File 'lib/lasertag.rb', line 168

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

#get_app_infoObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/lasertag.rb', line 239

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/lasertag.rb', line 256

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

#handle_it_allObject



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

def handle_it_all

  Dir.chdir @app_path

  ### dont let uncommited stuffgit commit
  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

  puts "\n"

  ### Find app info
  app_info = get_app_info

  puts "For 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 v1.5
  push_tag "v#{version}"

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

end

#has_uncommited_codeObject

HAS UNCOMMITED CODE?



150
151
152
153
# File 'lib/lasertag.rb', line 150

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

#is_valid(settings_path = @settings_gradle_path) ⇒ Object



209
210
211
# File 'lib/lasertag.rb', line 209

def is_valid(settings_path = @settings_gradle_path)
  File.exist?(settings_path)
end

#manage_optsObject

Manage options



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lasertag.rb', line 73

def manage_opts

  if @require_analyses
    # instatiate android project
    # android_project = AndroidProject.new(@app_path)

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

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

  handle_it_all

end

#project_properties(project = nil) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/lasertag.rb', line 230

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



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

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

#settings_gradle_file(path = @base_path) ⇒ Object



205
206
207
# File 'lib/lasertag.rb', line 205

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

#tag_code(version) ⇒ Object

TAG CODE



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/lasertag.rb', line 174

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