Class: EmergeCLI::Commands::Upload::Build

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

Instance Method Summary collapse

Methods inherited from GlobalOptions

#before

Constructor Details

#initialize(network: nil, git_info_provider: nil) ⇒ Build

Returns a new instance of Build.



28
29
30
31
# File 'lib/commands/upload/build.rb', line 28

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

Instance Method Details

#call(**options) ⇒ Object



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
# File 'lib/commands/upload/build.rb', line 33

def call(**options)
  @options = options
  @profiler = EmergeCLI::Profiler.new(enabled: options[:profile])
  before(options)

  start_time = Time.now

  file_path = options[:path]
  file_exists = File.exist?(file_path)
  raise "File not found at path: #{file_path}" unless file_exists

  file_extension = File.extname(file_path)
  raise "Unsupported file type: #{file_extension}" unless ['.ipa', '.apk', '.aab',
                                                           '.zip'].include?(file_extension)

  api_token = @options[:api_token] || ENV.fetch('EMERGE_API_TOKEN', nil)
  raise 'API token is required and cannot be blank' if api_token.nil? || api_token.strip.empty?

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

  Sync do
    upload_url, upload_id = fetch_upload_url

    file_size = File.size(file_path)
    Logger.info("Uploading file... (#{file_size} bytes)")

    File.open(file_path, 'rb') do |file|
      headers = {
        'Content-Type' => 'application/zip',
        'Content-Length' => file_size.to_s
      }

      response = @network.put(
        path: upload_url,
        body: file.read,
        headers: headers
      )

      unless response.status == 200
        Logger.error("Upload failed with status #{response.status}")
        Logger.error("Response body: #{response.body}")
        raise "Uploading file failed with status #{response.status}"
      end
    end

    Logger.info('Upload complete successfully!')
    Logger.info "Time taken: #{(Time.now - start_time).round(2)} seconds"
    Logger.info("✅ You can view the build analysis at https://emergetools.com/build/#{upload_id}")
  end
end