|
| 1 | +require 'claide' |
| 2 | +require 'spaceship' |
| 3 | + |
| 4 | +module XcodeInstall |
| 5 | + class Command |
| 6 | + class Tools < Command |
| 7 | + self.command = 'tools' |
| 8 | + self.summary = 'List or install Xcode CLI tools.' |
| 9 | + |
| 10 | + def self.options |
| 11 | + [['--install=name', 'Install simulator beginning with name, e.g. \'iOS 8.4\', \'tvOS 9.0\'.'], |
| 12 | + ['--force', 'Install even if the same version is already installed.'], |
| 13 | + ['--no-install', 'Only download DMG, but do not install it.'], |
| 14 | + ['--no-progress', 'Don’t show download progress.']].concat(super) |
| 15 | + end |
| 16 | + |
| 17 | + def initialize(argv) |
| 18 | + @install = argv.option('install') |
| 19 | + @force = argv.flag?('force', false) |
| 20 | + @should_install = argv.flag?('install', true) |
| 21 | + @progress = argv.flag?('progress', true) |
| 22 | + @installer = XcodeInstall::Installer.new |
| 23 | + super |
| 24 | + end |
| 25 | + |
| 26 | + def run |
| 27 | + @install ? install : list |
| 28 | + end |
| 29 | + |
| 30 | + :private |
| 31 | + |
| 32 | + def download(package) |
| 33 | + puts("Downloading #{package}") |
| 34 | + url = 'https://developer.apple.com/devcenter/download.action?path=' + package |
| 35 | + dmg_file = File.basename(url) |
| 36 | + Curl.new.fetch( |
| 37 | + url: url, |
| 38 | + directory: XcodeInstall::CACHE_DIR, |
| 39 | + cookies: @installer.spaceship.cookie, |
| 40 | + output: dmg_file, |
| 41 | + progress: false |
| 42 | + ) |
| 43 | + XcodeInstall::CACHE_DIR + dmg_file |
| 44 | + end |
| 45 | + |
| 46 | + def install |
| 47 | + dmg_path = download(@install) |
| 48 | + puts("Downloaded to from #{dmg_path}") |
| 49 | + mount_dir = @installer.mount(dmg_path) |
| 50 | + puts("Mounted to #{mount_dir}") |
| 51 | + pkg_path = Dir.glob(File.join(mount_dir, '*.pkg')).first |
| 52 | + puts("Installing from #{pkg_path}") |
| 53 | + prompt = "Please authenticate to install Command Line Tools.\nPassword: " |
| 54 | + `sudo -p "#{prompt}" installer -verbose -pkg "#{pkg_path}" -target /` |
| 55 | + end |
| 56 | + |
| 57 | + def list |
| 58 | + raise NotImplementedError, 'Listing is not implemented' |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments