class Signing

Resolve the RubyGems signing certificate and private key for a project.

Definitions

def self.load(root)

Load signing configuration from a project's gemspec.

Signature

parameter root String

The project root.

returns Signing

The signing configuration.

Implementation

def self.load(root)
	gemspec_path = Dir["*.gemspec", base: root].first
	gemspec = Gem::Specification.load(File.join(root, gemspec_path)) if gemspec_path
	
	return new(gemspec)
end

def initialize(gemspec)

Signature

parameter gemspec Gem::Specification | Nil

The project gemspec.

Implementation

def initialize(gemspec)
	@gemspec = gemspec
end

def certificate_path

The preferred certificate, if available.

Signature

returns String | Nil

The certificate path.

Implementation

def certificate_path
	paths = []
	
	if owner = signing_owner
		paths << File.join(GEM_CREDENTIALS_PATH, "#{owner}-release.cert")
	end
	
	paths << File.join(GEM_CREDENTIALS_PATH, "release.cert")
	return paths.find{|path| File.file?(path)}
end

def private_key_path

The private key path, preserving an existing gemspec setting.

Signature

returns String

The private key path.

Implementation

def private_key_path
	return @gemspec.signing_key if @gemspec&.signing_key
	
	if certificate_path = self.certificate_path
		return certificate_path.sub(/\.cert\z/, ".pem")
	end
	
	return File.join(GEM_CREDENTIALS_PATH, "release.pem")
end

def private_key_expression

Format a private key path for use in a gemspec.

Signature

returns String

A Ruby expression.

Implementation

def private_key_expression
	prefix = GEM_CREDENTIALS_PATH + File::SEPARATOR
	path = self.private_key_path
	
	if path.start_with?(prefix)
		relative_path = path.delete_prefix(prefix)
		path = "~/.gem/#{relative_path}"
		return "File.expand_path(#{path.inspect})"
	end
	
	return path.inspect
end