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)


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

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

.check_local_certificatesObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/deploygate/xcode/export.rb', line 242

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

  conflicting_certificates = installed_distribution_conflicting_certificates
  if 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')
    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)


268
269
270
271
272
273
274
275
276
277
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
# File 'lib/deploygate/xcode/export.rb', line 268

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)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/deploygate/xcode/export.rb', line 140

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) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/deploygate/xcode/export.rb', line 200

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

  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) ⇒ LocalTeams

Parameters:

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

Returns:



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

def find_local_data(bundle_identifier, uuid = 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.each do |profile|
    entities = profile['Entitlements']
    unless entities['get-task-allow']
      team_id = entities['com.apple.developer.team-identifier']
      application_id = entities['application-identifier']
      application_id.slice!(/^#{team_id}\./)
      application_id = '.' + application_id if application_id == '*'
      if bundle_identifier.match(application_id) &&
          DateTime.now < profile['ExpirationDate'] &&
          installed_certificate?(profile['Path'])

        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)


178
179
180
181
# File 'lib/deploygate/xcode/export.rb', line 178

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)


62
63
64
65
66
67
68
69
70
71
# File 'lib/deploygate/xcode/export.rb', line 62

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)


115
116
117
118
119
120
121
122
123
124
# File 'lib/deploygate/xcode/export.rb', line 115

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)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/deploygate/xcode/export.rb', line 74

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

  ids
end

.installed_distribution_conflicting_certificatesArray

Returns:

  • (Array)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/deploygate/xcode/export.rb', line 90

def installed_distribution_conflicting_certificates
  certificates = installed_certificates()
  names = []
  certificates.each do |current|
    begin
      names << current.match(/(iPhone Distribution:.*)/)[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(/(iPhone Distribution:.*)/)[1]
      next unless conflicting_names.include?(name)
      conflicting_certificates << current
    rescue
    end
  end

  conflicting_certificates
end

.load_profile_pathsObject



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

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)


165
166
167
# File 'lib/deploygate/xcode/export.rb', line 165

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

.profile_to_plist(profile_path) ⇒ Hash

Parameters:

  • profile_path (String)

Returns:

  • (Hash)


190
191
192
193
194
195
196
197
198
# File 'lib/deploygate/xcode/export.rb', line 190

def profile_to_plist(profile_path)
  File.open(profile_path) do |profile|
    asn1 = OpenSSL::ASN1.decode(profile.read)
    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) ⇒ String

Parameters:

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

Returns:

  • (String)


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

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

  target_provisioning_profile = nil
  case local_teams.teams_count
    when 0
      target_provisioning_profile = create_provisioning(bundle_identifier, uuid)
    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)


128
129
130
131
132
133
134
135
136
# File 'lib/deploygate/xcode/export.rb', line 128

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)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/deploygate/xcode/export.rb', line 226

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