diff --git a/lib/commands/build_distribution/download_and_install.rb b/lib/commands/build_distribution/download_and_install.rb new file mode 100644 index 0000000..540a261 --- /dev/null +++ b/lib/commands/build_distribution/download_and_install.rb @@ -0,0 +1,107 @@ +require 'dry/cli' +require 'cfpropertylist' +require 'zip' +require 'rbconfig' + +module EmergeCLI + module Commands + module BuildDistribution + class DownloadAndInstall < EmergeCLI::Commands::GlobalOptions + desc 'Download build from Build Distribution' + + option :api_token, type: :string, required: false, + desc: 'API token for authentication, defaults to ENV[EMERGE_API_TOKEN]' + option :build_id, type: :string, required: true, desc: 'Build ID to download' + option :install, type: :boolean, default: true, required: false, desc: 'Install the build on the device' + option :device_id, type: :string, required: false, desc: 'Device id to install the build' + option :output, type: :string, required: false, desc: 'Output path for the downloaded build' + + def initialize(network: nil) + @network = network + end + + def call(**options) + @options = options + before(options) + + Sync do + api_token = @options[:api_token] || ENV.fetch('EMERGE_API_TOKEN', nil) + raise 'API token is required' unless api_token + + raise 'Build ID is required' unless @options[:build_id] + + begin + @network ||= EmergeCLI::Network.new(api_token:) + + Logger.info 'Getting build URL...' + request = get_build_url(@options[:build_id]) + response = parse_response(request) + + platform = response['platform'] + download_url = response['downloadUrl'] + + extension = platform == 'ios' ? 'ipa' : 'apk' + Logger.info 'Downloading build...' + output_name = @options[:output] || "#{@options[:build_id]}.#{extension}" + `curl --progress-bar -L '#{download_url}' -o #{output_name} ` + Logger.info "✅ Build downloaded to #{output_name}" + + if @options[:install] + install_ios_build(output_name) if platform == 'ios' + install_android_build(output_name) if platform == 'android' + end + rescue StandardError => e + Logger.error "Failed to download build: #{e.message}" + Logger.error 'Check your parameters and try again' + raise e + ensure + @network&.close + end + end + end + + private + + def get_build_url(build_id) + @network.get( + path: '/distribution/downloadUrl', + max_retries: 3, + query: { + buildId: build_id + } + ) + end + + def parse_response(response) + case response.status + when 200 + JSON.parse(response.read) + when 400 + error_message = JSON.parse(response.read)['errorMessage'] + raise "Invalid parameters: #{error_message}" + when 401, 403 + raise 'Invalid API token' + else + raise "Getting build failed with status #{response.status}" + end + end + + def install_ios_build(build_path) + command = "xcrun devicectl device install app -d #{@options[:device_id]} #{build_path}" + Logger.debug "Running command: #{command}" + `#{command}` + + Logger.info '✅ Build installed' + end + + def install_android_build(build_path) + command = "adb -s #{@options[:device_id]} install #{build_path}" + Logger.debug "Running command: #{command}" + `#{command}` + + Logger.info '✅ Build installed' + end + end + end + end +end diff --git a/lib/emerge_cli.rb b/lib/emerge_cli.rb index b0c4811..d193ead 100644 --- a/lib/emerge_cli.rb +++ b/lib/emerge_cli.rb @@ -16,6 +16,7 @@ require_relative 'commands/order_files/validate_xcode_project' require_relative 'commands/upload/build' require_relative 'commands/build_distribution/validate_app' +require_relative 'commands/build_distribution/download_and_install' require_relative 'commands/autofixes/minify_strings' require_relative 'reaper/ast_parser' @@ -66,6 +67,7 @@ module EmergeCLI register 'build-distribution' do |prefix| prefix.register 'validate-app', Commands::BuildDistribution::ValidateApp + prefix.register 'install', Commands::BuildDistribution::DownloadAndInstall end register 'autofix' do |prefix| diff --git a/lib/utils/network.rb b/lib/utils/network.rb index 0685e49..444e83e 100644 --- a/lib/utils/network.rb +++ b/lib/utils/network.rb @@ -17,8 +17,8 @@ def initialize(api_token: nil, base_url: EMERGE_API_PROD_URL) @internet = Async::HTTP::Internet.new end - def get(path:, headers: {}, max_retries: MAX_RETRIES) - request(:get, path, nil, headers, nil, max_retries) + def get(path:, headers: {}, query: nil, max_retries: MAX_RETRIES) + request(:get, path, nil, headers, query, max_retries) end def post(path:, body:, headers: {}, query: nil, max_retries: MAX_RETRIES)