Class: DeployGate::Xcode::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/deploygate/xcode/export.rb

Constant Summary collapse

AD_HOC =
'ad-hoc'
ENTERPRISE =
'enterprise'
SUPPORT_EXPORT_METHOD =
[AD_HOC, ENTERPRISE]
PROFILE_EXTNAME =
'.mobileprovision'

Class Method Summary collapse

Class Method Details

.adhoc?(profile_path) ⇒ Boolean

Parameters:

  • profile_path (String)

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/deploygate/xcode/export.rb', line 176

def adhoc?(profile_path)
  profile = profile_to_plist(profile_path)
  !profile['Entitlements']['get-task-allow'] && profile['ProvisionsAllDevices'].nil?
end

.check_local_certificatesObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/deploygate/xcode/export.rb', line 248

def check_local_certificates
  if installed_distribution_certificate_ids.count == 0
    # not local install certificate
    puts HighLine.color(I18n.t('xcode.export.check_local_certificates.not_local_install_certificate.error_message'), HighLine::RED)
    puts ''
    puts I18n.t('xcode.export.check_local_certificates.not_local_install_certificate.note')
    puts ''
    exit
  end

  iphone_conflicting_certificates = installed_distribution_conflicting_certificates_by('iPhone Distribution')
  apple_conflicting_certificates = installed_distribution_conflicting_certificates_by('Apple Distribution')
  if iphone_conflicting_certificates.count > 0 || apple_conflicting_certificates.count > 0
    puts HighLine.color(I18n.t('xcode.export.check_local_certificates.conflict_certificate.error_message'), HighLine::RED)
    puts ''
    puts I18n.t('xcode.export.check_local_certificates.conflict_certificate.note')
    iphone_conflicting_certificates.each do |certificate|
      puts certificate
    end
    apple_conflicting_certificates.each do |certificate|
      puts certificate
    end
    puts ""

    exit
  end
end

.clean_provisioning_profiles(bundle_identifier, team) ⇒ void

This method returns an undefined value.

Parameters:

  • bundle_identifier (String)


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/deploygate/xcode/export.rb', line 278

def clean_provisioning_profiles(bundle_identifier, team)
  puts I18n.t('xcode.export.clean_provisioning_profiles.start')
  puts ''

  profile_paths = []
  profile_paths = load_profile_paths
  profiles = profile_paths.map{|p| profile_to_plist(p)}

  profiles.each do |profile|
    entities = profile['Entitlements']
    unless entities['get-task-allow']
      team = entities['com.apple.developer.team-identifier']
      application_id = entities['application-identifier']
      if "#{team}.#{bundle_identifier}" == application_id &&
          DateTime.now < profile['ExpirationDate'] &&
          installed_certificate?(profile['Path'])

        profile_paths.push(profile['Path'])
      end
    end
  end

  most_new_profile_path = profile_paths.first
  profile_paths.each do |path|
    most_new_profile_path = path if File.ctime(path) > File.ctime(most_new_profile_path)
  end

  profile_paths.delete(most_new_profile_path)
  profile_paths.each do |path|
    next unless File.exist?(path)
    File.delete(path)
    puts I18n.t('xcode.export.clean_provisioning_profiles.delete', path: path)
  end

  puts ''
  puts I18n.t('xcode.export.clean_provisioning_profiles.finish')
end

.codesigning_identity(profile_path) ⇒ String

Parameters:

  • profile_path (String)

Returns:

  • (String)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/deploygate/xcode/export.rb', line 145

def codesigning_identity(profile_path)
  profile = profile_to_plist(profile_path)
  identity = nil

  profile['DeveloperCertificates'].each do |cert|
    certificate_str = cert.read
    certificate =  OpenSSL::X509::Certificate.new certificate_str
    id = OpenSSL::Digest::SHA1.new(certificate.to_der).to_s.upcase!

    available = `security find-identity -v -p codesigning`
    available.split("\n").each do |current|
      next if current.include? "REVOKED"
      begin
        search = current.match(/.*\) (.*) \"(.*)\"/)
        identity = search[2] if id == search[1]
      rescue
      end
    end
  end

  identity
end

.create_provisioning(identifier, uuid, team_id) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/deploygate/xcode/export.rb', line 205

def create_provisioning(identifier, uuid, team_id)
  member_center = Xcode::MemberCenter.new(team_id)
  app = MemberCenters::App.new(identifier, member_center)
  provisioning_prifile = MemberCenters::ProvisioningProfile.new(identifier, member_center)

  begin
    unless app.created?
      app.create!
      puts I18n.t('xcode.export.create_provisioning.created', identifier: identifier)
    end
  rescue => e
    puts HighLine.color(I18n.t('xcode.export.create_provisioning.error.failed_to_create.app_id'), HighLine::RED)
    raise e
  end

  begin
    provisioning_profiles = provisioning_prifile.create!(uuid)
  rescue => e
    puts HighLine.color(I18n.t('xcode.export.create_provisioning.error.failed_to_create.provisioning_profile'), HighLine::RED)
    raise e
  end

  select_profile(provisioning_profiles)
end

.find_local_data(bundle_identifier, uuid = nil, provisioning_team = nil, specifier_name = nil) ⇒ LocalTeams

Parameters:

  • bundle_identifier (String)
  • uuid (String) (defaults to: nil)
  • provisioning_team (String) (defaults to: nil)

Returns:



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
# File 'lib/deploygate/xcode/export.rb', line 35

def find_local_data(bundle_identifier, uuid = nil, provisioning_team = nil, specifier_name = nil)
  local_teams = LocalTeams.new

  profile_paths = load_profile_paths
  profiles = profile_paths.map{|p| profile_to_plist(p)}
  profiles.reject! {|profile| profile['UUID'] != uuid} unless uuid.nil?
  profiles.reject! {|profile| profile['Name'] != specifier_name} unless specifier_name.nil?

  profiles.each do |profile|
    next if DateTime.now >= profile['ExpirationDate'] || !installed_certificate?(profile['Path'])

    entities = profile['Entitlements']
    unless entities['get-task-allow']
      team_id = entities['com.apple.developer.team-identifier']
      next if provisioning_team != nil && team_id != provisioning_team

      application_id = entities['application-identifier']
      application_id.slice!(/^#{team_id}\./)
      application_id = '.' + application_id if application_id == '*'
      if match = bundle_identifier.match(application_id)
        next if match[0] != bundle_identifier

        local_teams.add(team_id, profile['TeamName'], profile['Path'])
      end
    end
  end

  local_teams
end

.inhouse?(profile_path) ⇒ Boolean

Parameters:

  • profile_path (String)

Returns:

  • (Boolean)


183
184
185
186
# File 'lib/deploygate/xcode/export.rb', line 183

def inhouse?(profile_path)
  profile = profile_to_plist(profile_path)
  !profile['Entitlements']['get-task-allow'] && !profile['ProvisionsAllDevices'].nil?
end

.installed_certificate?(profile_path) ⇒ Boolean

Parameters:

  • profile_path (String)

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
# File 'lib/deploygate/xcode/export.rb', line 67

def installed_certificate?(profile_path)
  profile = profile_to_plist(profile_path)
  certs = profile['DeveloperCertificates'].map do |cert|
    certificate_str = cert.read
    certificate =  OpenSSL::X509::Certificate.new certificate_str
    id = OpenSSL::Digest::SHA1.new(certificate.to_der).to_s.upcase!
    installed_distribution_certificate_ids.include?(id)
  end
  certs.include?(true)
end

.installed_certificatesArray

Returns:

  • (Array)


120
121
122
123
124
125
126
127
128
129
# File 'lib/deploygate/xcode/export.rb', line 120

def installed_certificates
  available = `security find-identity -v -p codesigning`
  certificates = []
  available.split("\n").each do |current|
    next if current.include? "REVOKED"
    certificates << current
  end

  certificates
end

.installed_distribution_certificate_idsArray

Returns:

  • (Array)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/deploygate/xcode/export.rb', line 79

def installed_distribution_certificate_ids
  certificates = installed_certificates()
  ids = []
  certificates.each do |current|
    next unless current.match(/iPhone Distribution:/) || current.match(/Apple Distribution:/)
    begin
      (ids << current.match(/.*\) (.*) \".*/)[1])
    rescue
      # the last line does not match
    end
  end

  ids
end

.installed_distribution_conflicting_certificates_by(distribution_name) ⇒ Array

Returns:

  • (Array)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/deploygate/xcode/export.rb', line 95

def installed_distribution_conflicting_certificates_by(distribution_name)
  certificates = installed_certificates()
  names = []
  certificates.each do |current|
    begin
      names << current.match(/(#{distribution_name}:.*)/)[1]
    rescue
    end
  end

  conflicting_names = names.select{|e| names.index(e) != names.rindex(e)}.uniq
  conflicting_certificates = []
  certificates.each do |current|
    begin
      name = current.match(/(#{distribution_name}:.*)/)[1]
      next unless conflicting_names.include?(name)
      conflicting_certificates << current
    rescue
    end
  end

  conflicting_certificates
end

.load_profile_pathsObject



188
189
190
191
# File 'lib/deploygate/xcode/export.rb', line 188

def load_profile_paths
  profiles_path = File.expand_path("~") + "/Library/MobileDevice/Provisioning Profiles/*.mobileprovision"
  Dir[profiles_path]
end

.method(profile_path) ⇒ String

Parameters:

  • profile_path (String)

Returns:

  • (String)


170
171
172
# File 'lib/deploygate/xcode/export.rb', line 170

def method(profile_path)
  adhoc?(profile_path) ? AD_HOC : ENTERPRISE
end

.profile_to_plist(profile_path) ⇒ Hash

Parameters:

  • profile_path (String)

Returns:

  • (Hash)


195
196
197
198
199
200
201
202
203
# File 'lib/deploygate/xcode/export.rb', line 195

def profile_to_plist(profile_path)
  File.open(profile_path) do |profile|
    asn1 = OpenSSL::ASN1.decode_all(profile.read).first
    plist_str = asn1.value[1].value[0].value[2].value[1].value[0].value
    plist = Plist.parse_xml plist_str.force_encoding('UTF-8')
    plist['Path'] = profile_path
    return plist
  end
end

.provisioning_profile(bundle_identifier, uuid = nil, provisioning_team = nil, specifier_name = nil) ⇒ String

Parameters:

  • bundle_identifier (String)
  • uuid (String) (defaults to: nil)
  • provisioning_team (String) (defaults to: nil)

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deploygate/xcode/export.rb', line 15

def provisioning_profile(bundle_identifier, uuid = nil, provisioning_team = nil, specifier_name = nil)
  local_teams = DeployGate::Xcode::Export.find_local_data(bundle_identifier, uuid, provisioning_team, specifier_name)

  case local_teams.teams_count
    when 0
      target_provisioning_profile = create_provisioning(bundle_identifier, uuid, provisioning_team)
    when 1
      target_provisioning_profile = select_profile(local_teams.first_team_profile_paths)
    else
      # when many teams
      target_provisioning_profile = select_teams(local_teams)
  end

  target_provisioning_profile
end

.select_profile(profile_paths) ⇒ String

Parameters:

  • profile_paths (Array)

Returns:

  • (String)


133
134
135
136
137
138
139
140
141
# File 'lib/deploygate/xcode/export.rb', line 133

def select_profile(profile_paths)
  select = nil

  profile_paths.each do |path|
    select = path if adhoc?(path) && select.nil?
    select = path if inhouse?(path)
  end
  select
end

.select_teams(local_teams) ⇒ String

Parameters:

Returns:

  • (String)


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/deploygate/xcode/export.rb', line 232

def select_teams(local_teams)
  result = nil
  cli = HighLine.new
  cli.choose do |menu|
    menu.prompt = I18n.t('xcode.export.select_teams.prompt')
    local_teams.teams.each do |team|
      menu.choice(I18n.t('xcode.export.select_teams.choice', team_name: team[:name], team_id: team[:id])) {
        profile_paths = local_teams.profile_paths(team[:id])
        result = DeployGate::Xcode::Export.select_profile(profile_paths)
      }
    end
  end

  result
end