Module: MotionProvisioning

Defined in:
lib/motion-provisioning.rb,
lib/motion-provisioning/utils.rb,
lib/motion-provisioning/version.rb,
lib/motion-provisioning/application.rb,
lib/motion-provisioning/certificate.rb,
lib/motion-provisioning/mobileprovision.rb,
lib/motion-provisioning/provisioning_profile.rb

Defined Under Namespace

Modules: Utils Classes: Application, Certificate, MobileProvision, ProvisioningProfile

Constant Summary collapse

VERSION =
"1.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.freeObject

Returns the value of attribute free.



24
25
26
# File 'lib/motion-provisioning.rb', line 24

def free
  @free
end

.teamObject

Returns the value of attribute team.



24
25
26
# File 'lib/motion-provisioning.rb', line 24

def team
  @team
end

Class Method Details

.certificate(opts = {}) ⇒ Object



172
173
174
175
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
# File 'lib/motion-provisioning.rb', line 172

def self.certificate(opts = {})
  unless opts[:platform]
    Utils.log("Error", "Certificate 'platform' is required")
    exit(1)
  end

  unless opts[:type]
    Utils.log("Error", "Certificate 'type' is required")
    exit(1)
  end

  if opts[:free] == true && opts[:type] != :development
    Utils.log("Error", "You can only create a 'free' certificate for type 'development'. You selected type '#{opts[:type].to_s}'")
    exit(1)
  end

  opts[:platform] = :ios if opts[:platform] == :tvos

  supported_platforms = [:ios, :mac]
  unless supported_platforms.include?(opts[:platform])
    Utils.log("Error", "Invalid value'#{opts[:platform]}'for 'platorm'. Supported values: #{supported_platforms}")
    exit(1)
  end

  supported_types = [:distribution, :development, :developer_id]
  unless supported_types.include?(opts[:type])
    Utils.log("Error", "Invalid value '#{opts[:type]}'for 'type'. Supported values: #{supported_types}")
    exit(1)
  end

  MotionProvisioning.free = opts[:free]
  Certificate.new.certificate_name(opts[:type], opts[:platform])
end

.clientObject



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
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
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
# File 'lib/motion-provisioning.rb', line 27

def self.client
  Spaceship::Portal.client ||= begin

    FileUtils.mkdir_p(MotionProvisioning.output_path)

    if File.exist?('.gitignore') && File.read('.gitignore').match(/^provisioning$/).nil?
      answer = Utils.ask("Info", "Do you want to add the 'provisioning' folder fo your '.gitignore' file? (Recommended) (Y/n):")
      `echo provisioning >> .gitignore` if answer.yes?
    end

    client = if free
      Spaceship::FreePortalClient.new
    else
      Spaceship::PortalClient.new
    end

    email = ENV['MOTION_PROVISIONING_EMAIL'] || MotionProvisioning.config['email'] || Utils.ask("Info", "Your Apple ID email:").answer

    config_path = File.join(MotionProvisioning.output_path, 'config.yaml')

    if ENV['MOTION_PROVISIONING_EMAIL'].nil? && !File.exist?(config_path)
      answer = Utils.ask("Info", "Do you want to save the email to the config file ('#{MotionProvisioning.output_path}/config.yaml') so you dont have to type it again? (Y/n):")
      if answer.yes?
        File.write(config_path, { 'email' => email }.to_yaml)
      end
    end

    password = ENV['MOTION_PROVISIONING_PASSWORD']

    server_name = "motionprovisioning.#{email}"
    item = Security::InternetPassword.find(server: server_name)
    password ||= item.password if item

    if password.nil?
      Utils.log("Info", "The login information you enter will be stored safely in the macOS keychain.")
      password = Utils.ask_password("Info", "Password for #{email}:")
      Security::InternetPassword.add(server_name, email, password)
    end

    Utils.log("Info", "Logging into the Developer Portal with email '#{email}'.")
    begin
      client.user = email
      client.(email, password)
    rescue Spaceship::Client::InvalidUserCredentialsError => ex
      Utils.log("Error", "There was an error logging into your account. Your password may be wrong.")

      if Utils.ask("Info", 'Do you want to reenter your password? (Y/n):').yes?

        # The 'delete' method is very verbose, temporarily disable output
        orig_stdout = $stdout.dup
        $stdout.reopen('/dev/null', 'w')
        Security::InternetPassword.delete(server: server_name)
        $stdout.reopen(orig_stdout)

        password = Utils.ask_password("Info", "Password for #{email}:")
        Security::InternetPassword.add(server_name, email, password)
        retry
      else
        abort
      end
    end

    if self.free
      client.teams.each do |team|
        if team['currentTeamMember']['roles'].include?('XCODE_FREE_USER')
          client.team_id = team['teamId']
          self.team = team
        end
      end

      if client.team_id.nil?
        raise "The current user does not belong to a free team."
      end
    else
      if team_id = MotionProvisioning.config['team_id'] || ENV['MOTION_PROVISIONING_TEAM_ID']
        found = false
        client.teams.each do |team|
          if team_id == team['teamId']
            client.team_id = team_id
            self.team = team
            found = true
          end
        end

        if found == false
          raise "The current user does not belong to team with ID '#{team_id}' selected in config.yml."
        end
      else
        team_id = client.select_team
        self.team = client.teams.detect { |team| team['teamId'] == team_id }
      end
    end

    Utils.log("Info", "Selected team \"#{team['name']}\" (#{team['teamId']}).")
    if File.exist?(config_path) && ENV['MOTION_PROVISIONING_TEAM_ID'].nil? && MotionProvisioning.config['team_id'].nil?
      answer = Utils.ask("Info", "Do you want to save the team \"#{team['name']}\" (#{team['teamId']}) in the config file ('#{MotionProvisioning.output_path}/config.yaml') so you dont have to select it again? (Y/n):")
      if answer.yes?
        config = YAML.load(File.read(config_path))
        config['team_id'] = team['teamId']
        File.write(config_path, config.to_yaml)
      end
    end

    Spaceship::App.set_client(client)
    Spaceship::AppGroup.set_client(client)
    Spaceship::Device.set_client(client)
    Spaceship::Certificate.set_client(client)
    Spaceship::ProvisioningProfile.set_client(client)

    client
  end
end

.configObject



140
141
142
143
144
145
146
147
148
# File 'lib/motion-provisioning.rb', line 140

def self.config
  return @config if @config
  config_path = File.join(MotionProvisioning.output_path, 'config.yaml')
  if File.exist?(config_path)
    @config = YAML.load(File.read(config_path)) || {}
  else
    @config = {}
  end
end

.entitlementsObject



168
169
170
# File 'lib/motion-provisioning.rb', line 168

def self.entitlements
  self.services.map(&:to_hash).inject({}, &:merge!)
end

.output_pathObject



156
157
158
# File 'lib/motion-provisioning.rb', line 156

def self.output_path
  @output_path ||= File.expand_path('provisioning')
end

.output_path=(path) ⇒ Object



150
151
152
153
154
# File 'lib/motion-provisioning.rb', line 150

def self.output_path=(path)
  path = File.expand_path(path)
  Utils.log('Info', "Output directory for MotionProvisioning set to '#{path}'.")
  @output_path = path
end

.profile(opts = {}) ⇒ Object



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
# File 'lib/motion-provisioning.rb', line 206

def self.profile(opts = {})
  unless opts[:bundle_identifier]
    Utils.log("Error", "'bundle_identifier' is required")
    exit(1)
  end

  unless opts[:app_name]
    Utils.log("Error", "'app_name' is required")
    exit(1)
  end

  supported_platforms = [:ios, :tvos, :mac]
  unless supported_platforms.include?(opts[:platform])
    Utils.log("Error", "Invalid value'#{opts[:platform]}'for 'platorm'. Supported values: #{supported_platforms}")
    exit(1)
  end

  supported_types = [:distribution, :adhoc, :development]
  unless supported_types.include?(opts[:type])
    Utils.log("Error", "Invalid value '#{opts[:type]}'for 'type'. Supported values: #{supported_types}")
    exit(1)
  end

  if opts[:free] == true && opts[:type] != :development
    Utils.log("Error", "You can only create a 'free' provisioning profile for type 'development'. You selected type '#{opts[:type].to_s}'")
    exit(1)
  end

  MotionProvisioning.free = opts[:free]
  ProvisioningProfile.new.provisioning_profile(opts[:bundle_identifier], opts[:app_name], opts[:platform], opts[:type])
end

.servicesObject



160
161
162
# File 'lib/motion-provisioning.rb', line 160

def self.services
  @services ||= []
end

.services=(services) ⇒ Object



164
165
166
# File 'lib/motion-provisioning.rb', line 164

def self.services=(services)
  @services = services
end