Skip to content

Commit fe06ff3

Browse files
authored
Add command to download and install binaries from CLI (#44)
1 parent 3369ab8 commit fe06ff3

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

lib/emerge_cli.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_relative 'commands/order_files/validate_xcode_project'
1717
require_relative 'commands/upload/build'
1818
require_relative 'commands/build_distribution/validate_app'
19+
require_relative 'commands/build_distribution/download_and_install'
1920
require_relative 'commands/autofixes/minify_strings'
2021

2122
require_relative 'reaper/ast_parser'
@@ -66,6 +67,7 @@ module EmergeCLI
6667

6768
register 'build-distribution' do |prefix|
6869
prefix.register 'validate-app', Commands::BuildDistribution::ValidateApp
70+
prefix.register 'install', Commands::BuildDistribution::DownloadAndInstall
6971
end
7072

7173
register 'autofix' do |prefix|

lib/utils/network.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def initialize(api_token: nil, base_url: EMERGE_API_PROD_URL)
1717
@internet = Async::HTTP::Internet.new
1818
end
1919

20-
def get(path:, headers: {}, max_retries: MAX_RETRIES)
21-
request(:get, path, nil, headers, nil, max_retries)
20+
def get(path:, headers: {}, query: nil, max_retries: MAX_RETRIES)
21+
request(:get, path, nil, headers, query, max_retries)
2222
end
2323

2424
def post(path:, body:, headers: {}, query: nil, max_retries: MAX_RETRIES)

0 commit comments

Comments
 (0)