Class: Fastlane::Helper::CertsHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/connected/helper/connected_helper.rb

Class Method Summary collapse

Class Method Details

.download_provisioning_profiles_for_app(app_store_connect, app_id) ⇒ Object

class methods that you define here become available in your action as ‘Helper::CertsHelper.your_method`



13
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
# File 'lib/fastlane/plugin/connected/helper/connected_helper.rb', line 13

def self.download_provisioning_profiles_for_app(app_store_connect, app_id)
  # Get profiles for bundle id
  response = app_store_connect.profiles(
    include: 'bundleId',
    fields: {
      profiles: 'bundleId',
      bundle_ids: 'identifier'
    }
  )

  unless response['errors'].nil?
    response['errors'].each do |e|
      UI.error("ERROR: #{e['title']} - #{e['detail']}")
    end
    raise "Connected Fastlane plugin couldn't fetch your provisioning profiles"
  end

  bundle_id = response['included']
              .select { |i| i['type'] == 'bundleIds' }
              .detect({}) { |i| i.dig('attributes', 'identifier') == app_id }

  profiles_shallow = response['data']
                     .select { |d| bundle_id['id'] == d.dig('relationships', 'bundleId', 'data', 'id') }

  profiles = []
  profiles_shallow.each do |p|
    # Get provisioning profile file content
    profile_data = app_store_connect.profile(id: p['id'])
    profile_id = profile_data['data']['id']
    profile_name = profile_data['data']['attributes']['name'].gsub(/\s/, '')
    profile_content = Base64.decode64(profile_data['data']['attributes']['profileContent'])

    # Save provisioning profile to file
    directory = ".temp"
    Dir.mkdir(directory) unless File.exist?(directory)
    profile_path = File.join(directory, "#{profile_name}_#{profile_id}.mobileprovision")
    out_file = File.new(profile_path, "w+")
    out_file.puts(profile_content)
    out_file.close

    profiles.push({ 'id' => profile_id, 'name' => profile_name, 'path' => profile_path })
  end

  return profiles
end

.install_certificate(certificate_path) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fastlane/plugin/connected/helper/connected_helper.rb', line 97

def self.install_certificate(certificate_path)
  certificate_name = File.basename(certificate_path)
  Fastlane::Actions.sh("security", "import", certificate_path, "-k", File.expand_path("~/Library/Keychains/login.keychain-db"))
  UI.success("Successfully installed Certificate: #{certificate_name}")
rescue StandardError => e
  if e.message.include?("already exists in the keychain.")
    UI.message("Certificate already exists in keychain: #{certificate_name}")
  else
    UI.error("Failed to install Certificate: #{certificate_name}")
    raise e
  end
end

.install_certificates_from_provisioning_profile(app_store_connect, installed_profile) ⇒ Object



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
# File 'lib/fastlane/plugin/connected/helper/connected_helper.rb', line 67

def self.install_certificates_from_provisioning_profile(app_store_connect, installed_profile)
  profile_name = installed_profile['name']
  profile_path = installed_profile['path']

  # Decode profile
  readable_profile_path = "#{profile_path}.readable"
  decoded_profile = Fastlane::Actions.sh("security", "cms", "-D", "-i", profile_path)
  out_file = File.new(readable_profile_path, "w+")
  out_file.puts(decoded_profile)
  out_file.close

  profile_plist = Plist.parse_xml(readable_profile_path)

  # Get certificates from profile
  certificates = profile_plist["DeveloperCertificates"]

  # Install certificates
  directory = ".temp"
  i = 1
  certificates.each do |certificate|
    certificate_name = "#{profile_name}_cert_#{i}.cer"
    certificate_path = File.join(directory, certificate_name)
    out_file = File.new(certificate_path, "w+")
    out_file.puts(certificate.string)
    out_file.close
    self.install_certificate(certificate_path)
    i += 1
  end
end

.install_provisioning_profile(app_store_connect, profile) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/fastlane/plugin/connected/helper/connected_helper.rb', line 59

def self.install_provisioning_profile(app_store_connect, profile)
  # Copy to XCode Provisioning Profiles Directory
  destination = File.join(ENV['HOME'], "Library/MobileDevice/Provisioning Profiles", "#{profile['id']}.mobileprovision")
  FileUtils.copy_file(profile['path'], destination)

  return { 'id' => profile['id'], 'name' => profile['name'], 'path' => destination }
end