Class: Fastlane::Actions::AppaloosaAction

Inherits:
Fastlane::Action show all
Defined in:
lib/fastlane/actions/appaloosa.rb

Constant Summary collapse

APPALOOSA_SERVER =
'https://www.appaloosa-store.com/api/v1'

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, output, return_value, sh, step_text

Class Method Details



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fastlane/actions/appaloosa.rb', line 152

def self.all_screenshots_links(screenshots)
  if screenshots.nil?
    screens = %w(screenshot1 screenshot2 screenshot3 screenshot4 screenshot5)
    screenshots = screens.map do |_k, _v|
      ''
    end
  else
    missings = 5 - screenshots.count
    (1..missings).map do |_i|
      screenshots << ''
    end
  end
  screenshots
end

.authorsObject



248
249
250
# File 'lib/fastlane/actions/appaloosa.rb', line 248

def self.authors
  ['Appaloosa']
end

.available_optionsObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/fastlane/actions/appaloosa.rb', line 202

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :binary,
                                 env_name: 'FL_APPALOOSA_BINARY',
                                 description: 'Path to your IPA or APK file. Optional for ipa if you use the `ipa` or `xcodebuild` action. For Mac zip the .app',
                                 default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH],
                                 verify_block: proc do |value|
                                   fail "Couldn't find ipa || apk file at path '#{value}'".red unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: 'FL_APPALOOSA_API_TOKEN',
                                 description: "Your API Token, if you don\'t have an account hit [enter]",
                                 verify_block: proc do
                                 end),
    FastlaneCore::ConfigItem.new(key: :store_id,
                                 env_name: 'FL_APPALOOSA_STORE_ID',
                                 description: "Your Store id, if you don\'t have an account hit [enter]",
                                 verify_block: proc do |_value|
                                 end),
    FastlaneCore::ConfigItem.new(key: :email,
                                 env_name: 'FL_APPALOOSA_EMAIL',
                                 description: "It's your first time? Give your email address",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :group_ids,
                                 env_name: 'FL_APPALOOSA_GROUPS',
                                 description: 'Your app is limited to special users? Give us the group ids',
                                 default_value: '',
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :screenshots,
                                 env_name: 'FL_APPALOOSA_SCREENSHOTS',
                                 description: 'Add some screenshots application to your store or hit [enter]',
                                 default_value: Actions.lane_context[SharedValues::SNAPSHOT_SCREENSHOTS_PATH]),
    FastlaneCore::ConfigItem.new(key: :locale,
                                 env_name: 'FL_APPALOOSA_LOCALE',
                                 description: 'Select the folder locale for yours screenshots',
                                 default_value: 'en-US',
                                 optional: true
                                ),
    FastlaneCore::ConfigItem.new(key: :device,
                                 env_name: 'FL_APPALOOSA_DEVICE',
                                 description: 'Select the device format for yours screenshots',
                                 optional: true
                                )
  ]
end

.create_an_account(email) ⇒ Object



80
81
82
83
# File 'lib/fastlane/actions/appaloosa.rb', line 80

def self.(email)
  response = HTTP.post("#{APPALOOSA_SERVER}/upload_services/create_an_account", form: { email: email })
  JSON.parse(response)
end

.descriptionObject



191
192
193
# File 'lib/fastlane/actions/appaloosa.rb', line 191

def self.description
  'Upload your app to Appaloosa Store'
end

.detailsObject



195
196
197
198
199
200
# File 'lib/fastlane/actions/appaloosa.rb', line 195

def self.details
  [
    "Appaloosa is a private mobile application store. This action offers a quick deployment on the platform.",
    "You can create an account, push to your existing account, or manage your user groups. We accept iOS, Mac and Android applications."
  ].join(" ")
end

.error_detected(errors) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/fastlane/actions/appaloosa.rb', line 173

def self.error_detected(errors)
  if errors
    UI.error("ERROR: #{errors}")
    true
  else
    false
  end
end


27
28
29
30
31
# File 'lib/fastlane/actions/appaloosa.rb', line 27

def self.get_binary_link(binary, api_key, store_id, group_ids)
  key_s3 = upload_on_s3(binary, api_key, store_id, group_ids)
  return if key_s3.nil?
  get_s3_url(api_key, store_id, key_s3)
end

.get_env_value(option) ⇒ Object



167
168
169
170
171
# File 'lib/fastlane/actions/appaloosa.rb', line 167

def self.get_env_value(option)
  available_options.map do |opt|
    opt if opt.key == option.to_sym
  end.compact[0].default_value
end

.get_s3_url(api_key, store_id, path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fastlane/actions/appaloosa.rb', line 52

def self.get_s3_url(api_key, store_id, path)
  binary_path = HTTP.get("#{APPALOOSA_SERVER}/#{store_id}/upload_services/url_for_download",
                         json: { store_id: store_id,
                                 api_key: api_key,
                                 key: path })
  if binary_path.status == 404
    return nil if error_detected("A problem occurred with your API token and your store id. Please try again.")
  end
  json_res = JSON.parse(binary_path)
  return if error_detected(json_res['errors'])
  json_res['binary_url']
end

.get_screenshots(screenshots_path, locale, device) ⇒ Object



113
114
115
116
117
# File 'lib/fastlane/actions/appaloosa.rb', line 113

def self.get_screenshots(screenshots_path, locale, device)
  get_env_value('screenshots').nil? ? locale = '' : locale.concat('/')
  device.nil? ? device = '' : device.concat('-')
  screenshots_path.strip.size > 0 ? screenshots_list(screenshots_path, locale, device) : nil
end


105
106
107
108
109
110
111
# File 'lib/fastlane/actions/appaloosa.rb', line 105

def self.get_screenshots_links(api_key, store_id, screenshots_path, locale, device)
  screenshots = get_screenshots(screenshots_path, locale, device)
  return if screenshots.nil?
  uploaded = upload_screenshots(screenshots, api_key, store_id)
  links = get_uploaded_links(uploaded, api_key, store_id)
  links.kind_of?(Array) ? links.flatten : nil
end


97
98
99
100
101
102
103
# File 'lib/fastlane/actions/appaloosa.rb', line 97

def self.get_uploaded_links(uploaded_screenshots, api_key, store_id)
  return if uploaded_screenshots.nil?
  urls = []
  urls << uploaded_screenshots.flatten.map do |url|
    get_s3_url(api_key, store_id, url)
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/fastlane/actions/appaloosa.rb', line 252

def self.is_supported?(platform)
  [:ios, :mac, :android].include? platform
end

.normalize_binary_name(binary) ⇒ Object



74
75
76
77
78
# File 'lib/fastlane/actions/appaloosa.rb', line 74

def self.normalize_binary_name(binary)
  binary_rename = binary.delete(' ')
  File.rename("#{binary}", "#{binary_rename}")
  binary_rename
end

.remove_extra_screenshots_file(screenshots_env) ⇒ Object



69
70
71
72
# File 'lib/fastlane/actions/appaloosa.rb', line 69

def self.remove_extra_screenshots_file(screenshots_env)
  extra_file = "#{screenshots_env}/screenshots.html"
  File.unlink(extra_file) if File.exist?(extra_file)
end

.request_email?(api_key, store_id) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/fastlane/actions/appaloosa.rb', line 85

def self.request_email?(api_key, store_id)
  api_key.size == 0 && store_id.size == 0
end

.reset_original_binary_names(current_name, original_name) ⇒ Object



65
66
67
# File 'lib/fastlane/actions/appaloosa.rb', line 65

def self.reset_original_binary_names(current_name, original_name)
  File.rename("#{current_name}", "#{original_name}")
end

.run(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fastlane/actions/appaloosa.rb', line 5

def self.run(params)
  require 'http'

  api_key = params[:api_token]
  store_id = params[:store_id]

  if request_email?(api_key, store_id)
    auth =  params[:email]
    api_key = auth['api_key']
    store_id = auth['store_id']
    return if error_detected(auth['errors'])
  end

  binary = normalize_binary_name(params[:binary])
  remove_extra_screenshots_file(params[:screenshots])
  binary_url = get_binary_link(binary, api_key, store_id, params[:group_ids])
  return if binary_url.nil?
  screenshots_url = get_screenshots_links(api_key, store_id, params[:screenshots], params[:locale], params[:device])
  upload_on_appaloosa(api_key, store_id, binary_url, screenshots_url, params[:group_ids])
  reset_original_binary_names(binary, params[:binary])
end

.screenshots_list(path, locale, device) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/fastlane/actions/appaloosa.rb', line 119

def self.screenshots_list(path, locale, device)
  return warning_detected("screenshots folder not found") unless Dir.exist?("#{path}/#{locale}")
  list = Dir.entries("#{path}/#{locale}") - ['.', '..']
  list.map do |screen|
    next if screen.match(device).nil?
    "#{path}/#{locale}#{screen}" unless Dir.exist?("#{path}/#{locale}#{screen}")
  end.compact
end

.upload_on_appaloosa(api_key, store_id, binary_path, screenshots, group_ids) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fastlane/actions/appaloosa.rb', line 128

def self.upload_on_appaloosa(api_key, store_id, binary_path, screenshots, group_ids)
  screenshots = all_screenshots_links(screenshots)
  response = HTTP.post("#{APPALOOSA_SERVER}/#{store_id}/applications/upload",
                       json: { store_id: store_id,
                               api_key: api_key,
                               application: {
                                 binary_path: binary_path,
                                 screenshot1: screenshots[0],
                                 screenshot2: screenshots[1],
                                 screenshot3: screenshots[2],
                                 screenshot4: screenshots[3],
                                 screenshot5: screenshots[4],
                                 group_ids: group_ids,
                                 provider: 'fastlane'
                               }
                             })
  json_res = JSON.parse(response)
  if json_res['errors']
    UI.error "App: #{json_res['errors']}"
  else
    UI.success "Binary processing: Check your app': #{json_res['link']}"
  end
end

.upload_on_s3(file, api_key, store_id, group_ids = '') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fastlane/actions/appaloosa.rb', line 33

def self.upload_on_s3(file, api_key, store_id, group_ids = '')
  file_name = file.split('/').last
  response = HTTP.get("#{APPALOOSA_SERVER}/upload_services/presign_form",
                      json: { file: file_name,
                              store_id: store_id,
                              group_ids: group_ids })
  json_res = JSON.parse(response)
  return if error_detected json_res['errors']
  url = json_res['s3_sign']
  path = json_res['path']
  uri = URI.parse(Base64.decode64(url))
  File.open(file, 'rb') do |f|
    Net::HTTP.start(uri.host) do |http|
      http.send_request('PUT', uri.request_uri, f.read, 'content-type' => '')
    end
  end
  path
end

.upload_screenshots(screenshots, api_key, store_id) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/fastlane/actions/appaloosa.rb', line 89

def self.upload_screenshots(screenshots, api_key, store_id)
  return if screenshots.nil?
  list = []
  list << screenshots.map do |screen|
    upload_on_s3(screen, api_key, store_id)
  end
end

.warning_detected(warning) ⇒ Object



182
183
184
185
# File 'lib/fastlane/actions/appaloosa.rb', line 182

def self.warning_detected(warning)
  UI.important("WARNING: #{warning}")
  nil
end