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

def initialize(arguments)

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

  @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?



182
183
184
185
# File 'lib/lasertag.rb', line 182

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

#assemble_commandObject



272
273
274
# File 'lib/lasertag.rb', line 272

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

#callObject



187
188
189
190
191
192
193
194
# File 'lib/lasertag.rb', line 187

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/lasertag.rb', line 206

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



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

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('-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



159
160
161
162
# File 'lib/lasertag.rb', line 159

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

#get_app_infoObject



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

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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/lasertag.rb', line 249

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



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
130
131
132
133
134
135
136
137
# File 'lib/lasertag.rb', line 90

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

  #$ 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?



141
142
143
144
# File 'lib/lasertag.rb', line 141

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

#is_valid(settings_path = @settings_gradle_path) ⇒ Object



200
201
202
# File 'lib/lasertag.rb', line 200

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

#manage_optsObject

Manage options



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lasertag.rb', line 68

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



223
224
225
226
227
228
229
# File 'lib/lasertag.rb', line 223

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



147
148
149
150
151
152
153
154
155
156
# File 'lib/lasertag.rb', line 147

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



196
197
198
# File 'lib/lasertag.rb', line 196

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

#tag_code(version) ⇒ Object

TAG CODE



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lasertag.rb', line 165

def tag_code(version)
  g = Git.open(Dir.pwd)
  begin
    g.add_tag(version)
  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