Class: AndroidInstaller::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/android-sdk-installer.rb

Overview

Installs components for building Android projects noinspection RubyClassVariableUsageInspection

Constant Summary collapse

KEY_SDK_TOOLS =
'{ANDROID_SDK_TOOLS}'
KEY_PLATFORM =
'{PLATFORM}'
SDK_URL =
'https://dl.google.com'
SDK_PATH =
'/android/repository/sdk-tools-' + KEY_PLATFORM + '-' + KEY_SDK_TOOLS + '.zip'
CONFIG_FILE =
'android-sdk-installer.yml'
REPOSITORIES_CONFIG_FILE =
'~/.android/repositories.cfg'
ANDROID_DIR =
'~/.android'
DEFAULT_PLATFORM =
'linux'
DEFAULT_VERSION =
'3859397'

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ Installer

Returns a new instance of Installer.



20
21
22
23
24
25
26
27
28
# File 'lib/android-sdk-installer.rb', line 20

def initialize(arguments)
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::WARN

  @ignore_existing = false
  @platform = DEFAULT_PLATFORM
  @version = DEFAULT_VERSION
  create_options_parser(arguments)
end

Instance Method Details

#add_license_acceptanceObject



83
84
85
86
# File 'lib/android-sdk-installer.rb', line 83

def add_license_acceptance
  `mkdir $ANDROID_HOME/licenses`
  `echo 8933bad161af4178b1185d1a37fbf41ea5269c55 > $ANDROID_HOME/licenses/android-sdk-license`
end

#create_dummy_cfgObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/android-sdk-installer.rb', line 72

def create_dummy_cfg
  # Gets rid of a warning
  # https://askubuntu.com/questions/885658/android-sdk-repositories-cfg-could-not-be-loaded
  unless Dir.exist?(ANDROID_DIR)
    `mkdir #{ANDROID_DIR}`
  end
  unless File.file?(REPOSITORIES_CONFIG_FILE)
    `touch #{REPOSITORIES_CONFIG_FILE}`
  end
end

#create_options_parser(args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/android-sdk-installer.rb', line 30

def create_options_parser(args)
  args.options do |opts|
    opts.banner = 'Usage: android-sdk-installer [OPTIONS]'
    opts.separator ''
    opts.separator 'Options'
    opts.on('-i', '--ignore', 'Ignore existing Android SDK, denoted by the existence of the ANDROID_HOME env variable') do
      @ignore_existing = true
    end
    opts.on('-p PLATFORM', '--platform PLATFORM', 'Set the platform. Must be one of linux or darwin') do |platform|
      @platform = platform
    end
    opts.on('-v VERSION', 'Set the version of the sdk to install') do |version|
      @version = version
    end
    opts.on('-h', '--help', 'Displays help') do
      puts opts.help
      exit
    end
    opts.parse!
  end
end

#installObject



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
# File 'lib/android-sdk-installer.rb', line 88

def install
  # Validation
  if File.file?(CONFIG_FILE)
    config = Psych.load_file CONFIG_FILE
  else
    config = Psych.load("foo: true\nbar: false")
  end
  if config.has_key?('version')
    @version = config['version']
  end
  if config['debug']
    @logger.level = Logger::DEBUG
    @logger.debug('We are in debug mode')
  end

  if config.has_key?('platform')
    @platform = config['platform']
  end

  if config.has_key?('ignore_existing')
    @ignore_existing = config['ignore_existing']
    @logger.debug("Ignore existing set to #{@ignore_existing}")
  end

  should_install = ENV['ANDROID_HOME'].nil? || @ignore_existing
  if should_install
    install_command_line_sdk(@platform, @version)
  else
    @logger.debug('ANDROID_HOME already set. Skipping command line tools install')
  end

  components = config['components']
  if components != nil
    components.each do |component|
      @logger.debug('Installing component ' + component)
      output = `echo y | $ANDROID_HOME/tools/bin/sdkmanager "#{component}"`
      @logger.debug(output)
      if output.include? 'Warning'
        puts "\nError installing component " + component + "\n"
        puts output
      end
    end
  end
end

#install_command_line_sdk(platform, version) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/android-sdk-installer.rb', line 52

def install_command_line_sdk(platform, version)
  sdk_path = SDK_PATH
  sdk_path[KEY_SDK_TOOLS] = version
  sdk_path[KEY_PLATFORM] = platform
  url = SDK_URL + sdk_path
  @logger.debug('Downloading version ' + version + ' for platform ' + platform + ' with url ' + url)
  `wget --quiet --output-document=android-sdk.zip #{url}`
  unless File.file?('android-sdk.zip')
    puts "\nAndroid SDK not found at url #{url}. Make sure you have the right values in your #{CONFIG_FILE}\n"
    exit(1)
  end
  @logger.debug('Unzipping android-sdk.zip')
  `unzip -q -o android-sdk.zip -d $PWD/android-sdk`
  `rm android-sdk.zip`
  `export ANDROID_HOME=$PWD/android-sdk`
  create_dummy_cfg
  add_license_acceptance
  @logger.debug('SDK base installed to ' + Dir.pwd + '/android-sdk')
end