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



56
57
58
59
60
61
62
# File 'lib/cert/runner.rb', line 56

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



51
52
53
# File 'lib/cert/runner.rb', line 51

def certificates
  certificate_type.all
end

#create_certificateObject



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

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.join(Cert.config[:output_path], 'CertCertificateSigningRequest.certSigningRequest')
  File.write(request_path, csr.to_pem)
  private_key_path = File.join(Cert.config[:output_path], 'private_key.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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cert/runner.rb', line 29

def find_existing_cert
  certificates.each do |certificate|
    path = store_certificate(certificate)

    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
    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



3
4
5
6
7
8
# File 'lib/cert/runner.rb', line 3

def launch
  run

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

#runObject



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

def run
  FastlaneCore::PrintTable.print_values(config: Cert.config, hide_keys: [], title: "Summary")

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

  if find_existing_cert
    return # success
  else
    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



96
97
98
99
100
101
# File 'lib/cert/runner.rb', line 96

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