Skip to content

Commit

Permalink
Add command to download and install binaries from CLI (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itaybre authored Dec 25, 2024
1 parent 3369ab8 commit fe06ff3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
107 changes: 107 additions & 0 deletions lib/commands/build_distribution/download_and_install.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/emerge_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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|
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fe06ff3

Please sign in to comment.