Module: FIR::Publish

Included in:
Util::ClassMethods
Defined in:
lib/fir/util/publish.rb

Instance Method Summary collapse

Instance Method Details

#binary_informationObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fir/util/publish.rb', line 173

def binary_information
  {
    build: @app_info[:build],
    fname: File.basename(@file_path),
    key: @binary_cert[:key],
    name: @app_info[:display_name] || @app_info[:name],
    origin: 'fir-cli',
    parent_id: @app_id,
    release_tag: 'develop',
    fsize: File.size(@file_path),
    release_type: @app_info[:release_type],
    distribution_name: @app_info[:distribution_name],
    token: @binary_cert[:token],
    version: @app_info[:version],
    changelog: @changelog,
    user_id: @user_info[:id]
  }.reject { |x| x.nil? || x == '' }
end

#delete_latest_versionObject



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
# File 'lib/fir/util/publish.rb', line 40

def delete_latest_version
  # @file_type     = "ipa"
  # @token         = options[:token] || current_token
  # # @app_info = {"type": 'ipa', "identifier": 'com.sudiyi.appCourier', "name":'速递易快递', "version":'3.1.1', "build": 'haha'}
  # # @user_info      = fetch_user_info(@token)
  # # logger.info ">>>> #{@user_info}"
  # # @uploading_info = fetch_uploading_info
  # # logger.info ">>>> uploading_info #{@uploading_info}"
  # @app_id         = '5be4f90f959d69333dd5ab5a'

  logger.info ">>>> app_info #{@app_info}"
  current_version = @app_info[:version]
  current_build = @app_info[:build]

  logger.info ">>>> current_version #{current_version}"
  release_info = get(fir_api[:app_url] + "/#{@app_id}" + "/releases", api_token: @token, page: 1)
  for temp_obj in release_info[:datas]
    logger.info ">>>> current_build #{current_build} #{temp_obj[:build]}"
    if temp_obj[:build] == current_build
      next
    end

    if temp_obj[:version] == current_version
      temp_id = temp_obj[:id]
      api = fir_api[:app_url] + "/#{@app_id}" + "/releases" + "/#{temp_id}"
      logger.info ">>>> app_info #{api} #{@token}"
      RestClient.delete(fir_api[:app_url] + "/#{@app_id}" + "/releases" + "/#{temp_id}", headers={"accesstoken": "c0f6dc8a7d7704d919cbc5d69e6cb2852d1783a1c9"})
      # delete fir_api[:app_url] + "/#{@app_id}" + "/releases" + "/#{temp_id}", accesstoken: 'c0f6dc8a7d7704d919cbc5d69e6cb2852d1783a1c9'
    end
  end
end

#dingtalk(*args, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fir/util/publish.rb', line 5

def dingtalk(*args, options)
  if options[:dingtalk_access_token]
    payload = {
      "msgtype": 'markdown',
      "markdown": {
        "title": "ceshi",
        "text": options[:dingtalk_robot_msg]
      }
    }
    url = "https://oapi.dingtalk.com/robot/send?access_token=#{options[:dingtalk_access_token]}"
    DefaultRest.post(url, payload)
  end
end

#fetch_app_infoObject



241
242
243
244
245
246
247
# File 'lib/fir/util/publish.rb', line 241

def fetch_app_info
  logger.info 'Fetch app info from fir.im'

  @fir_app_info = get(fir_api[:app_url] + "/#{@app_id}", api_token: @token)
  write_app_info(id: @fir_app_info[:id], short: @fir_app_info[:short], name: @fir_app_info[:name])
  @fir_app_info
end

#fetch_release_idObject



237
238
239
# File 'lib/fir/util/publish.rb', line 237

def fetch_release_id
  get "#{fir_api[:base_url]}/apps/#{@app_id}/releases/find_release_by_key", api_token: @token, key: @binary_cert[:key]
end

#fetch_release_infoObject



250
251
252
253
254
255
256
257
258
# File 'lib/fir/util/publish.rb', line 250

def fetch_release_info
  logger.info 'Fetch release info from fir.im'

  release_info = get(fir_api[:app_url] + "/#{@app_id}" + "/releases", api_token: @token, page: 1)
  if release_info[:datas].count > 0
    @release_id = release_info[:datas][0][:id]
  end

end

#fetch_uploading_infoObject



227
228
229
230
231
232
233
234
235
# File 'lib/fir/util/publish.rb', line 227

def fetch_uploading_info
  logger.info "Fetching #{@app_info[:identifier]}@fir.im uploading info......"
  logger.info "Uploading app: #{@app_info[:name]}-#{@app_info[:version]}(Build #{@app_info[:build]})"

  post fir_api[:app_url], type: @app_info[:type],
                          bundle_id: @app_info[:identifier],
                          manual_callback: true,
                          api_token: @token
end

#icon_informationObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/fir/util/publish.rb', line 162

def icon_information
  {
    key: @icon_cert[:key],
    token: @icon_cert[:token],
    origin: 'fir-cli',
    parent_id: @app_id,
    fsize: File.size(@uncrushed_icon_path),
    fname: 'blob'
  }
end

#initialize_dtalk_options(args, options) ⇒ Object



72
73
74
75
# File 'lib/fir/util/publish.rb', line 72

def initialize_dtalk_options(args, options)
    @token         = options[:token] || current_token
    @content       = options[:content].to_s
end

#logger_info_app_short_and_qrcode(options) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
# File 'lib/fir/util/publish.rb', line 281

def logger_info_app_short_and_qrcode(options)
  @download_url = "#{fir_api[:domain]}/#{@fir_app_info[:short]}"
  @download_url += "?release_id=#{@app_uploaded_callback_data[:release_id]}" if !!options[:need_release_id]

  logger.info "Published succeed: #{@download_url}"

  @qrcode_path = "#{File.dirname(@file_path)}/fir-#{@app_info[:name]}.png"
  FIR.generate_rqrcode(@download_url, @qrcode_path)

  logger.info "Local qrcode file: #{@qrcode_path}" if @export_qrcode
end

#logger_info_publishing_messageObject



105
106
107
108
109
110
111
112
113
# File 'lib/fir/util/publish.rb', line 105

def logger_info_publishing_message
   = (@token)

  email = .fetch(:email, '')
  name  = .fetch(:name, '')

  logger.info "Publishing app via #{name}<#{email}>......."
  logger_info_dividing_line
end

#publish(*args, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fir/util/publish.rb', line 77

def publish(*args, options)
  initialize_publish_options(args, options)
  check_supported_file_and_token

  logger_info_publishing_message
  @app_info = send("#{@file_type}_info", @file_path, full_info: true)
  @user_info      = (@token)
  @uploading_info = fetch_uploading_info
  @app_id         = @uploading_info[:id]

  # if options[:delete_latest_version]
  upload_app
  delete_latest_version
  # turn_off_history
  
  logger_info_dividing_line
  logger_info_app_short_and_qrcode(options)

  # logger.info ">>>> #{options[:dingtalk_notify]}"
  if options[:dingtalk_notify]
    dingtalk_notifier(options)  
  end
  
  upload_mapping_file_with_publish(options)
  logger_info_blank_line
  clean_files
end

#turn_off_historyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fir/util/publish.rb', line 19

def turn_off_history
  # @file_type     = "ipa"
  # @token         = options[:token] || current_token
  # # @app_info = {"type": 'ipa', "identifier": 'com.sudiyi.appCourier', "name":'速递易快递', "version":'3.1.1', "build": 'haha'}
  # # @user_info      = fetch_user_info(@token)
  # # logger.info ">>>> #{@user_info}"
  # # @uploading_info = fetch_uploading_info
  # # logger.info ">>>> uploading_info #{@uploading_info}"
  # @app_id         = '5be4f90f959d69333dd5ab5a'

  release_info = get(fir_api[:app_url] + "/#{@app_id}" + "/releases", api_token: @token, page: 1)
  
  logger.info ">>>> #{@release_info}"

  for temp_obj in release_info[:datas]
    logger.info ">>>> #{@release_info}"
    temp_id = temp_obj[:id]
    patch fir_api[:app_url] + "/#{@app_id}" + "/releases" + "/#{temp_id}", api_token: @token, is_history: false
  end
end

#update_app_infoObject



217
218
219
220
221
222
223
224
225
# File 'lib/fir/util/publish.rb', line 217

def update_app_info
  update_info = { short: @short, passwd: @passwd, is_opened: @is_opened }.compact

  return if update_info.blank?

  logger.info 'Updating app info......'

  patch fir_api[:app_url] + "/#{@app_id}", update_info.merge(api_token: @token)
end

#update_release_infoObject



260
261
262
263
264
265
266
267
268
# File 'lib/fir/util/publish.rb', line 260

def update_release_info
  logger.info "Update release" ":#{@release_id}" + " info from fir.im"

  if nil != @release_id
    patch fir_api[:app_url] + "/#{@app_id}" + "/releases" + "/#{@release_id}", api_token: @token, is_history: true
  end


end

#upload_appObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fir/util/publish.rb', line 115

def upload_app
  @icon_cert   = @uploading_info[:cert][:icon]
  @binary_cert = @uploading_info[:cert][:binary]
 
  fetch_release_info
  upload_app_icon unless @app_info[:icons].blank?
  @app_uploaded_callback_data = upload_app_binary
  logger.info "App id is #{@app_id}"
  logger.info "Release id is #{@app_uploaded_callback_data[:release_id]}"
  upload_device_info
  update_app_info
  fetch_app_info
  update_release_info
end

#upload_device_infoObject



207
208
209
210
211
212
213
214
215
# File 'lib/fir/util/publish.rb', line 207

def upload_device_info
return if @app_info[:devices].blank?

logger.info 'Updating devices info......'

post fir_api[:udids_url], key: @binary_cert[:key],
                          udids: @app_info[:devices].join(','),
                          api_token: @token
end

#upload_file(postfix) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fir/util/publish.rb', line 138

def upload_file(postfix)
  logger.info "Uploading app #{postfix}......"
  url = @uploading_info[:cert][postfix.to_sym][:upload_url]
  info = send("uploading_#{postfix}_info")
  logger.debug "url = #{url}, info = #{info}"
  uploaded_info = post(url, info.merge(manual_callback: true),
                       params_to_json: false,
                       header: nil)
rescue StandardError
  logger.error "Uploading app #{postfix} failed"
  exit 1
end

#upload_mapping_file_with_publish(options) ⇒ Object



270
271
272
273
274
275
276
277
278
279
# File 'lib/fir/util/publish.rb', line 270

def upload_mapping_file_with_publish(options)
  return if !options[:mappingfile] || !options[:proj]

  logger_info_blank_line

  mapping options[:mappingfile], proj: options[:proj],
                                 build: @app_info[:build],
                                 version: @app_info[:version],
                                 token: @token
end

#uploading_binary_infoObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/fir/util/publish.rb', line 192

def uploading_binary_info
  {
    key: @binary_cert[:key],
    token: @binary_cert[:token],
    file: File.new(@file_path, 'rb'),
    # Custom variables
    'x:name' => @app_info[:display_name] || @app_info[:name],
    'x:build' => @app_info[:build],
    'x:version' => @app_info[:version],
    'x:changelog' => @changelog,
    'x:release_type' => @app_info[:release_type],
    'x:distribution_name' => @app_info[:distribution_name]
  }
end

#uploading_icon_infoObject



151
152
153
154
155
156
157
158
159
160
# File 'lib/fir/util/publish.rb', line 151

def uploading_icon_info
  large_icon_path = @app_info[:icons].max_by { |f| File.size(f) }
  @uncrushed_icon_path = convert_icon(large_icon_path)
  {
    key: @icon_cert[:key],
    token: @icon_cert[:token],
    file: File.new(@uncrushed_icon_path, 'rb'),
    'x:is_converted' => '1'
  }
end