|
| 1 | +require 'dry/cli' |
| 2 | +require 'cfpropertylist' |
| 3 | +require 'zip' |
| 4 | +require 'rbconfig' |
| 5 | + |
| 6 | +module EmergeCLI |
| 7 | + module Commands |
| 8 | + module BuildDistribution |
| 9 | + class DownloadAndInstall < EmergeCLI::Commands::GlobalOptions |
| 10 | + desc 'Download build from Build Distribution' |
| 11 | + |
| 12 | + option :api_token, type: :string, required: false, |
| 13 | + desc: 'API token for authentication, defaults to ENV[EMERGE_API_TOKEN]' |
| 14 | + option :build_id, type: :string, required: true, desc: 'Build ID to download' |
| 15 | + option :install, type: :boolean, default: true, required: false, desc: 'Install the build on the device' |
| 16 | + option :device_id, type: :string, required: false, desc: 'Device id to install the build' |
| 17 | + option :output, type: :string, required: false, desc: 'Output path for the downloaded build' |
| 18 | + |
| 19 | + def initialize(network: nil) |
| 20 | + @network = network |
| 21 | + end |
| 22 | + |
| 23 | + def call(**options) |
| 24 | + @options = options |
| 25 | + before(options) |
| 26 | + |
| 27 | + Sync do |
| 28 | + api_token = @options[:api_token] || ENV.fetch('EMERGE_API_TOKEN', nil) |
| 29 | + raise 'API token is required' unless api_token |
| 30 | + |
| 31 | + raise 'Build ID is required' unless @options[:build_id] |
| 32 | + |
| 33 | + begin |
| 34 | + @network ||= EmergeCLI::Network.new(api_token:) |
| 35 | + |
| 36 | + Logger.info 'Getting build URL...' |
| 37 | + request = get_build_url(@options[:build_id]) |
| 38 | + response = parse_response(request) |
| 39 | + |
| 40 | + platform = response['platform'] |
| 41 | + download_url = response['downloadUrl'] |
| 42 | + |
| 43 | + extension = platform == 'ios' ? 'ipa' : 'apk' |
| 44 | + Logger.info 'Downloading build...' |
| 45 | + output_name = @options[:output] || "#{@options[:build_id]}.#{extension}" |
| 46 | + `curl --progress-bar -L '#{download_url}' -o #{output_name} ` |
| 47 | + Logger.info "✅ Build downloaded to #{output_name}" |
| 48 | + |
| 49 | + if @options[:install] |
| 50 | + install_ios_build(output_name) if platform == 'ios' |
| 51 | + install_android_build(output_name) if platform == 'android' |
| 52 | + end |
| 53 | + rescue StandardError => e |
| 54 | + Logger.error "Failed to download build: #{e.message}" |
| 55 | + Logger.error 'Check your parameters and try again' |
| 56 | + raise e |
| 57 | + ensure |
| 58 | + @network&.close |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + private |
| 64 | + |
| 65 | + def get_build_url(build_id) |
| 66 | + @network.get( |
| 67 | + path: '/distribution/downloadUrl', |
| 68 | + max_retries: 3, |
| 69 | + query: { |
| 70 | + buildId: build_id |
| 71 | + } |
| 72 | + ) |
| 73 | + end |
| 74 | + |
| 75 | + def parse_response(response) |
| 76 | + case response.status |
| 77 | + when 200 |
| 78 | + JSON.parse(response.read) |
| 79 | + when 400 |
| 80 | + error_message = JSON.parse(response.read)['errorMessage'] |
| 81 | + raise "Invalid parameters: #{error_message}" |
| 82 | + when 401, 403 |
| 83 | + raise 'Invalid API token' |
| 84 | + else |
| 85 | + raise "Getting build failed with status #{response.status}" |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def install_ios_build(build_path) |
| 90 | + command = "xcrun devicectl device install app -d #{@options[:device_id]} #{build_path}" |
| 91 | + Logger.debug "Running command: #{command}" |
| 92 | + `#{command}` |
| 93 | + |
| 94 | + Logger.info '✅ Build installed' |
| 95 | + end |
| 96 | + |
| 97 | + def install_android_build(build_path) |
| 98 | + command = "adb -s #{@options[:device_id]} install #{build_path}" |
| 99 | + Logger.debug "Running command: #{command}" |
| 100 | + `#{command}` |
| 101 | + |
| 102 | + Logger.info '✅ Build installed' |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments