Class: Appship::FirUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/appship/uploader.rb

Constant Summary collapse

FIR_API_HOST =
"api.appmeta.cn"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FirUploader

Returns a new instance of FirUploader.



165
166
167
168
169
170
171
172
173
174
# File 'lib/appship/uploader.rb', line 165

def initialize(options = {})
  @options = options
  @api_token = options[:fir_api_token] || options[:api_token]
  @api_token ||= ENV[options[:fir_api_token_env] || options[:api_token_env] || "FIR_API_TOKEN"]
  @password = options[:fir_password] || options[:password]
  @password ||= ENV[options[:fir_password_env] || options[:password_env] || "FIR_PASSWORD"]
  if @api_token.to_s.empty?
    raise ConfigurationError, "缺少 fir.im API Token,请使用 --fir-api-token 或 FIR_API_TOKEN"
  end
end

Instance Method Details

#upload!(file) ⇒ Object

Raises:



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/appship/uploader.rb', line 176

def upload!(file)
  raise ConfigurationError, "IPA/APK 文件不存在: #{file}" unless File.file?(file)

  type = package_type(file)
   = (file)
  bundle_id = [:bundle_id]
  if bundle_id.to_s.empty?
    raise ConfigurationError, "缺少应用 Bundle ID,请使用 --bundle-id 指定(fir.im 上传凭证需要该参数)"
  end

  puts "☁️ 获取 fir.im 上传凭证..."
  app = request_credentials(type, bundle_id)
  binary = app.dig("cert", "binary") || {}
  unless [binary["key"], binary["token"], binary["upload_url"]].all? { |value| !value.to_s.empty? }
    raise UploadError, "获取 fir.im 上传凭证失败: #{app}"
  end

  puts "☁️ 上传 #{File.basename(file)}#{format_size(File.size(file))})到 fir.im..."
  upload_binary!(file, binary, , type)

  icon = @options[:icon]
  upload_icon!(icon, app.dig("cert", "icon")) if icon
  update_access_password!(app["id"], @password) unless @password.to_s.empty?

  short = app["short"]
  {
    "provider" => "fir",
    "data" => {
      "id" => app["id"],
      "short" => short,
      "name" => [:app_name],
      "bundle_id" => bundle_id,
      "version" => [:version],
      "build" => [:build],
      "password_protected" => !@password.to_s.empty?,
      "type" => type
    },
    "url" => short.to_s.empty? ? "https://fir.im/" : "https://fir.im/#{short}"
  }
end