Class: Fastlane::Actions::RegisterDevicesAction

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

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, deprecated_notes, lane_context, method_missing, other_action, output, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.api_token(params) ⇒ Object



86
87
88
89
90
91
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 86

def self.api_token(params)
  params[:api_key] ||= Actions.lane_context[SharedValues::APP_STORE_CONNECT_API_KEY]
  api_token ||= Spaceship::ConnectAPI::Token.create(params[:api_key]) if params[:api_key]
  api_token ||= Spaceship::ConnectAPI::Token.from_json_file(params[:api_key_path]) if params[:api_key_path]
  return api_token
end

.authorObject



179
180
181
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 179

def self.author
  "lmirosevic"
end

.available_optionsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 101

def self.available_options
  user = CredentialsManager::AppfileConfig.try_fetch_value(:apple_dev_portal_id)
  user ||= CredentialsManager::AppfileConfig.try_fetch_value(:apple_id)
  platform = Actions.lane_context[Actions::SharedValues::PLATFORM_NAME].to_s

  [
    FastlaneCore::ConfigItem.new(key: :devices,
                                 env_name: "FL_REGISTER_DEVICES_DEVICES",
                                 description: "A hash of devices, with the name as key and the UDID as value",
                                 is_string: false,
                                 type: Hash,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :devices_file,
                                 env_name: "FL_REGISTER_DEVICES_FILE",
                                 description: "Provide a path to a file with the devices to register. For the format of the file see the examples",
                                 optional: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Could not find file '#{value}'") unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_key_path,
                                 env_name: "FL_REGISTER_DEVICES_API_KEY_PATH",
                                 description: "Path to your App Store Connect API Key JSON file (https://docs.fastlane.tools/app-store-connect-api/#using-fastlane-api-key-json-file)",
                                 optional: true,
                                 conflicting_options: [:api_key],
                                 verify_block: proc do |value|
                                   UI.user_error!("Couldn't find API key JSON file at path '#{value}'") unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_key,
                                 env_name: "FL_REGISTER_DEVICES_API_KEY",
                                 description: "Your App Store Connect API Key information (https://docs.fastlane.tools/app-store-connect-api/#use-return-value-and-pass-in-as-an-option)",
                                 type: Hash,
                                 optional: true,
                                 sensitive: true,
                                 conflicting_options: [:api_key_path]),
    FastlaneCore::ConfigItem.new(key: :team_id,
                               env_name: "REGISTER_DEVICES_TEAM_ID",
                               code_gen_sensitive: true,
                               default_value: CredentialsManager::AppfileConfig.try_fetch_value(:team_id),
                                 default_value_dynamic: true,
                               description: "The ID of your Developer Portal team if you're in multiple teams",
                               optional: true,
                               verify_block: proc do |value|
                                 ENV["FASTLANE_TEAM_ID"] = value.to_s
                               end),
    FastlaneCore::ConfigItem.new(key: :team_name,
                                 env_name: "REGISTER_DEVICES_TEAM_NAME",
                                 description: "The name of your Developer Portal team if you're in multiple teams",
                                 optional: true,
                                 code_gen_sensitive: true,
                                 default_value: CredentialsManager::AppfileConfig.try_fetch_value(:team_name),
                                 default_value_dynamic: true,
                                 verify_block: proc do |value|
                                   ENV["FASTLANE_TEAM_NAME"] = value.to_s
                                 end),
    FastlaneCore::ConfigItem.new(key: :username,
                                 env_name: "DELIVER_USER",
                                 description: "Optional: Your Apple ID",
                                 default_value: user,
                                 default_value_dynamic: true),
    FastlaneCore::ConfigItem.new(key: :platform,
                                 env_name: "REGISTER_DEVICES_PLATFORM",
                                 description: "The platform to use (optional)",
                                 optional: true,
                                 default_value: platform.empty? ? "ios" : platform,
                                 verify_block: proc do |value|
                                   UI.user_error!("The platform can only be ios or mac") unless %('ios', 'mac').include?(value)
                                 end)
  ]
end

.categoryObject



209
210
211
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 209

def self.category
  :code_signing
end

.descriptionObject



97
98
99
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 97

def self.description
  "Registers new devices to the Apple Dev Portal"
end

.detailsObject



171
172
173
174
175
176
177
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 171

def self.details
  [
    "This will register iOS/Mac devices with the Developer Portal so that you can include them in your provisioning profiles.",
    "This is an optimistic action, in that it will only ever add new devices to the member center, and never remove devices. If a device which has already been registered within the member center is not passed to this action, it will be left alone in the member center and continue to work.",
    "The action will connect to the Apple Developer Portal using the username you specified in your `Appfile` with `apple_id`, but you can override it using the `username` option, or by setting the env variable `ENV['DELIVER_USER']`."
  ].join("\n")
end

.example_codeObject



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
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 183

def self.example_code
  [
    'register_devices(
      devices: {
        "Luka iPhone 6" => "1234567890123456789012345678901234567890",
        "Felix iPad Air 2" => "abcdefghijklmnopqrstvuwxyzabcdefghijklmn"
      }
    ) # Simply provide a list of devices as a Hash',
    'register_devices(
      devices_file: "./devices.txt"
    ) # Alternatively provide a standard UDID export .txt file, see the Apple Sample (http://devimages.apple.com/downloads/devices/Multiple-Upload-Samples.zip)',
    'register_devices(
      devices_file: "./devices.txt", # You must pass in either `devices_file` or `devices`.
      team_id: "XXXXXXXXXX",         # Optional, if you"re a member of multiple teams, then you need to pass the team ID here.
      username: "[email protected]"   # Optional, lets you override the Apple Member Center username.
    )',
    'register_devices(
      devices: {
        "Luka MacBook" => "12345678-1234-1234-1234-123456789012",
        "Felix MacBook Pro" => "ABCDEFGH-ABCD-ABCD-ABCD-ABCDEFGHIJKL"
      },
      platform: "mac"
    ) # Register devices for Mac'
  ]
end

.file_column_headersObject



10
11
12
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 10

def self.file_column_headers
  ['Device ID', 'Device Name', 'Device Platform']
end

.is_supported?(platform) ⇒ Boolean

Returns:



6
7
8
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 6

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

.run(params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 14

def self.run(params)
  platform = Spaceship::ConnectAPI::BundleIdPlatform.map(params[:platform])

  if params[:devices]
    new_devices = params[:devices].map do |name, udid|
      [udid, name]
    end
  elsif params[:devices_file]
    require 'csv'

    devices_file = CSV.read(File.expand_path(File.join(params[:devices_file])), col_sep: "\t")
    unless devices_file.first == file_column_headers.first(2) || devices_file.first == file_column_headers
      UI.user_error!("Please provide a file according to the Apple Sample UDID file (https://developer.apple.com/account/resources/downloads/Multiple-Upload-Samples.zip)")
    end

    new_devices = devices_file.drop(1).map do |row|
      if row.count == 1
        UI.user_error!("Invalid device line, ensure you are using tabs (NOT spaces). See Apple's sample/spec here: https://developer.apple.com/account/resources/downloads/Multiple-Upload-Samples.zip")
      elsif !(2..3).cover?(row.count)
        UI.user_error!("Invalid device line, please provide a file according to the Apple Sample UDID file (https://developer.apple.com/account/resources/downloads/Multiple-Upload-Samples.zip)")
      end
      row
    end
  else
    UI.user_error!("You must pass either a valid `devices` or `devices_file`. Please check the readme.")
  end

  require 'spaceship'
  if (token = api_token(params))
    UI.message("Using App Store Connect API token...")
    Spaceship::ConnectAPI.token = token
  else
    UI.message("Login to App Store Connect (#{params[:username]})")
    credentials = CredentialsManager::AccountManager.new(user: params[:username])
    Spaceship::ConnectAPI.(credentials.user, credentials.password, use_portal: true, use_tunes: false)
    UI.message("Login successful")
  end

  UI.message("Fetching list of currently registered devices...")
  existing_devices = Spaceship::ConnectAPI::Device.all

  device_objs = new_devices.map do |device|
    if existing_devices.map(&:udid).map(&:downcase).include?(device[0].downcase)
      UI.verbose("UDID #{device[0]} already exists - Skipping...")
      next
    end

    device_platform = platform

    device_platform_supported = !device[2].nil? && self.is_supported?(device[2].to_sym)
    if device_platform_supported
      if device[2] == "mac"
        device_platform = Spaceship::ConnectAPI::BundleIdPlatform::MAC_OS
      else
        device_platform = Spaceship::ConnectAPI::BundleIdPlatform::IOS
      end
    end

    try_create_device(name: device[1], platform: device_platform, udid: device[0])
  end

  UI.success("Successfully registered new devices.")
  return device_objs
end

.try_create_device(name: nil, platform: nil, udid: nil) ⇒ Object



79
80
81
82
83
84
# File 'fastlane/lib/fastlane/actions/register_devices.rb', line 79

def self.try_create_device(name: nil, platform: nil, udid: nil)
  Spaceship::ConnectAPI::Device.create(name: name, platform: platform, udid: udid)
rescue => ex
  UI.error(ex.to_s)
  UI.crash!("Failed to register new device (name: #{name}, UDID: #{udid})")
end