Class: Cert::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/cert/runner.rb

Instance Method Summary collapse

Instance Method Details

#certificate_typeObject

The kind of certificate we’re interested in



72
73
74
75
76
77
78
# File 'lib/cert/runner.rb', line 72

def certificate_type
  cert_type = Spaceship.certificate.production
  cert_type = Spaceship.certificate.development if Cert.config[:development]
  cert_type = Spaceship.certificate.in_house if Spaceship.client.in_house?

  cert_type
end

#certificatesObject

All certificates of this type



67
68
69
# File 'lib/cert/runner.rb', line 67

def certificates
  certificate_type.all
end

#create_certificateObject



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
# File 'lib/cert/runner.rb', line 80

def create_certificate
  # Create a new certificate signing request
  csr, pkey = Spaceship.certificate.create_certificate_signing_request

  # Use the signing request to create a new distribution certificate
  begin
    certificate = certificate_type.create!(csr: csr)
  rescue => ex
    Helper.log.error "Could not create another certificate, reached the maximum number of available certificates.".red
    raise ex
  end

  # Store all that onto the filesystem

  request_path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.certSigningRequest"))
  File.write(request_path, csr.to_pem)

  private_key_path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.p12"))
  File.write(private_key_path, pkey)

  cert_path = store_certificate(certificate)

  # Import all the things into the Keychain
  KeychainImporter.import_file(private_key_path)
  KeychainImporter.import_file(cert_path)

  # Environment variables for the fastlane action
  ENV["CER_CERTIFICATE_ID"] = certificate.id
  ENV["CER_FILE_PATH"] = cert_path

  Helper.log.info "Successfully generated #{certificate.id} which was imported to the local machine.".green

  return cert_path
end

#find_existing_certObject



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
# File 'lib/cert/runner.rb', line 32

def find_existing_cert
  certificates.each do |certificate|
    path = store_certificate(certificate)
    private_key_path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.p12"))

    if FastlaneCore::CertChecker.installed?(path)
      # This certificate is installed on the local machine
      ENV["CER_CERTIFICATE_ID"] = certificate.id
      ENV["CER_FILE_PATH"] = path

      Helper.log.info "Found the certificate #{certificate.id} (#{certificate.name}) which is installed on the local machine. Using this one.".green

      return path

    elsif File.exist?(private_key_path)
      KeychainImporter.import_file(private_key_path)
      KeychainImporter.import_file(path)

      ENV["CER_CERTIFICATE_ID"] = certificate.id
      ENV["CER_FILE_PATH"] = path

      Helper.log.info "Found the cached certificate #{certificate.id} (#{certificate.name}). Using this one.".green

      return path

    else
      Helper.log.info "Certificate #{certificate.id} (#{certificate.name}) can't be found on your local computer"
    end
  end

  Helper.log.info "Couldn't find an existing certificate... creating a new one"
  return nil
end

#launchObject



5
6
7
8
9
10
# File 'lib/cert/runner.rb', line 5

def launch
  run

  installed = FastlaneCore::CertChecker.installed?(ENV["CER_FILE_PATH"])
  raise "Could not find the newly generated certificate installed" unless installed
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cert/runner.rb', line 12

def run
  FileUtils.mkdir_p(Cert.config[:output_path])

  FastlaneCore::PrintTable.print_values(config: Cert.config, hide_keys: [], title: "Summary for cert #{Cert::VERSION}")

  Helper.log.info "Starting login with user '#{Cert.config[:username]}'"
  Spaceship.(Cert.config[:username], nil)
  Spaceship.select_team
  Helper.log.info "Successfully logged in"

  cert_path = find_existing_cert
  if cert_path.nil? || Cert.config[:force]
    if create_certificate # no certificate here, creating a new one
      return # success
    else
      raise "Something went wrong when trying to create a new certificate..."
    end
  end
end

#store_certificate(certificate) ⇒ Object



115
116
117
118
119
120
# File 'lib/cert/runner.rb', line 115

def store_certificate(certificate)
  path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.cer"))
  raw_data = certificate.download_raw
  File.write(path, raw_data)
  return path
end