Class: Apu::MainApp

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

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ MainApp

Returns a new instance of MainApp.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/apu.rb', line 10

def initialize(arguments)

  # defaults
  @app_path = Dir.pwd
  @package_flag = false
  @uninstall_flag = false
  @install_flag = false
  @android_home_flag = false
  @launcher_flag = false
  @run_flag = false
  @clear_flag = false

  @require_analyses = true

  # if !!arguments
  #     arguments.push '-h'
  # end

  # Parse Options
  create_options_parser(arguments)

  manage_opts

end

Instance Method Details

#android_home_is_definedObject



136
137
138
139
# File 'lib/apu.rb', line 136

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

#callObject



141
142
143
144
145
146
147
148
# File 'lib/apu.rb', line 141

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

#create_options_parser(args) ⇒ Object



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
126
127
128
129
130
131
132
133
134
# File 'lib/apu.rb', line 81

def create_options_parser(args)
  #@opt_parser = OptionParser.new do |opts|
  args.options do |opts|
    opts.banner = "Usage: apu [OPTIONS]"
    opts.separator  ''
    opts.separator  "Options"

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

    opts.on('-k', '--package', 'Retrieves package name (eg. com.example.app)') do |value|
      @package_flag = true
    end

    opts.on('-u', '--uninstall', 'Uninstalls the apk from your device') do |app_path|
      @uninstall_flag = true
    end

    opts.on('-i', '--install', 'Installs the apk on your device') do |install|
      @install_flag = true
    end

    opts.on('-a', '--android-home', 'Checks if the ANDROID_HOME variable is defined') do |home|
      @android_home_flag = true
      @require_analyses = false
    end

    opts.on('-l', '--launcher', 'Get the launcher activity path') do |app_path|
      @launcher_flag = true
    end

    opts.on('-r', '--run', 'Run the build on the device') do |flavour|
      @run_flag = true
    end

    opts.on('-c', '--clear', 'Clear app data') do |flavour|
      @clear_flag = true
    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 Apu::VERSION
      exit
    end
    opts.parse!

  end
end

#manage_optsObject

Manage options



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

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

  if @package_flag
    puts android_project.get_package_name.green
  end

  if @uninstall_flag
    android_project.uninstall_application
  end

  if @install_flag
    android_project.install
  end

  if @android_home_flag
    puts android_home_is_defined
  end

  if @launcher_flag
    puts android_project.get_launchable_activity.green
  end

  if @run_flag
    android_project.install
    system(android_project.get_execute_line)
  end

  if @clear_flag
    android_project.clear_app_data
  end
end