Class: EmergeCLI::Commands::Build::Distribution::Install

Inherits:
GlobalOptions
  • Object
show all
Defined in:
lib/commands/build/distribution/install.rb

Instance Method Summary collapse

Methods inherited from GlobalOptions

#before

Constructor Details

#initialize(network: nil) ⇒ Install

Returns a new instance of Install.



24
25
26
# File 'lib/commands/build/distribution/install.rb', line 24

def initialize(network: nil)
  @network = network
end

Instance Method Details

#call(**options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/commands/build/distribution/install.rb', line 28

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[:id]

    output_name = nil
    app_id = nil

    begin
      @network ||= EmergeCLI::Network.new(api_token:)

      Logger.info 'Getting build URL...'
      request = get_build_url(@options[:id])
      response = parse_response(request)

      platform = response['platform']
      download_url = response['downloadUrl']
      app_id = response['appId']

      extension = platform == 'ios' ? 'ipa' : 'apk'
      output_name = @options[:output] || "#{@options[:id]}.#{extension}"

      if File.exist?(output_name)
        Logger.info "Build file already exists at #{output_name}"
        prompt = TTY::Prompt.new
        choice = prompt.select('What would you like to do?', {
                                 'Install existing file' => :install,
                                 'Overwrite with new download' => :overwrite,
                                 'Cancel' => :cancel
                               })

        case choice
        when :install
          Logger.info 'Proceeding with existing file...'
        when :overwrite
          Logger.info 'Downloading new build...'
          `curl --progress-bar -L '#{download_url}' -o #{output_name}`
          Logger.info "✅ Build downloaded to #{output_name}"
        when :cancel
          raise 'Operation cancelled by user'
        end
      else
        Logger.info 'Downloading build...'
        `curl --progress-bar -L '#{download_url}' -o #{output_name}`
        Logger.info "✅ Build downloaded to #{output_name}"
      end
    rescue StandardError => e
      Logger.error "❌ Failed to download build: #{e.message}"
      raise e
    ensure
      @network&.close
    end

    begin
      if @options[:install] && !output_name.nil?
        if platform == 'ios'
          install_ios_build(output_name, app_id)
        elsif platform == 'android'
          install_android_build(output_name)
        end
      end
    rescue StandardError => e
      Logger.error "❌ Failed to install build: #{e.message}"
      raise e
    end
  end
end