Bake ModernizeSourceBakeModernizeLicenseContributors

class Contributors

Extract contributors from a YAML file which can be generated from another repository.

Definitions

DEFAULT_PATH = "."

The default path is the root of the repository and for authors who have contributed to the entire repository or unspecified paths in the past.

def self.for(root, mailmap: nil)

Load contributors from a directory.

Implementation

def self.for(root, mailmap: nil)
	full_path = File.join(root, ".contributors.yaml")
	
	if File.exist?(full_path)
		contributors = self.new(mailmap: mailmap)
		contributors.extract(full_path)
		return contributors
	end
end

def initialize(mailmap: nil)

Create a new, empty, contributors list.

Implementation

def initialize(mailmap: nil)
	@contributions = []
	@mailmap = mailmap
end

attr :contributions

Signature

attribute Array(Hash)

The list of contributions.

def each(&block)

Iterate over each contribution.

Implementation

def each(&block)
	@contributions.each do |contribution|
		author = contribution[:author].dup
		time = contribution[:time]
		
		# Apply mailmap transformation if available
		if @mailmap && author[:email] && mapped_name = @mailmap.names[author[:email]]
			author[:name] = mapped_name
		end
		
		paths_for(contribution) do |path|
			yield path, author, time
		end
	end
end

def extract(path)

Extract the contributors from the given path.

Implementation

def extract(path)
	@contributions.concat(
		YAML.load_file(path, aliases: true, symbolize_names: true, permitted_classes: [Symbol, Date, Time])
	)
end

def paths_for(contribution)

Signature

attribute Array(Hash)

The list of paths from a given contribution.

Implementation

def paths_for(contribution)
	return to_enum(:paths_for, contribution) unless block_given?
	
	if path = contribution[:path]
		yield path
	# elsif paths = contribution[:paths]
	# 	paths.each do |path|
	# 		yield path
	# 	end
	else
		yield DEFAULT_PATH
	end
end