Class: Singer::OptionParsing

Inherits:
Object
  • Object
show all
Defined in:
lib/singer/option_parsing.rb

Overview

extracts what we need from ARGV

Constant Summary collapse

NOT_AVAILABLE =
'(not available in paths)'.freeze

Class Method Summary collapse

Class Method Details

.add_options_functional(parser) ⇒ Object

rubocop:disable Metrics/MethodLength



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/singer/option_parsing.rb', line 23

def self.add_options_functional(parser) # rubocop:disable Metrics/MethodLength
	parser.on(
		'-t', '--template-path=PATH', 'load a template from a given path - use instead of [TEMPLATE_NAME]',
	) do |path|
		Template.load_one_from_path(path)
	end
	parser.on('--dashes DASHES', NameTransformation::DASHES_OPTIONS, <<~DESCRIPTION) do |dashes|
		what to do with "-" in PROJECT_NAME when converting to Singer.configuration.project_name_camelcase:
				"modularize": convert to "::", following Ruby convention of mapping dashes to module nesting, or
				"delete": remove entirely, or
				"preserve": leave as-is
				(default is "#{Singer.configuration.dashes}" if `--dashes` isn't given)
		DESCRIPTION
		Singer.configuration.dashes = dashes
	end
end

.add_options_listing(parser) ⇒ Object

rubocop:disable Metrics/MethodLength



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/singer/option_parsing.rb', line 40

def self.add_options_listing(parser) # rubocop:disable Metrics/MethodLength
	parser.on('--list-templates', 'list all available templates') do
		list_all_templates
		Kernel.exit
	end
	parser.on('--list-paths', 'list paths used to load files') do
		list_paths
		Kernel.exit
	end
	parser.on('--list-variables', 'list all variables available to templates and in paths') do
		list_variables
		Kernel.exit
	end
end

.columnize_hash(hash_or_array) ⇒ Object



73
74
75
76
77
78
# File 'lib/singer/option_parsing.rb', line 73

def self.columnize_hash(hash_or_array)
	first_column_width = hash_or_array.map{ |k, _v| k.length }.max
	hash_or_array.each do |k, v|
		puts "#{k.to_s.ljust(first_column_width)} #{v}"
	end
end

.configure(argvies) ⇒ Object

rubocop:disable Metrics/MethodLength



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/singer/option_parsing.rb', line 4

def self.configure(argvies) # rubocop:disable Metrics/MethodLength
	options = {}
	Singer.configuration.dashes = NameTransformation::DASHES_OPTIONS.first

	OptionParser.new do |parser|
		parser.program_name = 'singer'
		parser.version = Singer::VERSION
		parser.banner = <<~USAGE
			Usage: #{parser.program_name} [OPTION]... [TEMPLATE_NAME] PROJECT_NAME
			Generates a project from a multi-file template.
			PROJECT_NAME can be in snake_case or CamelCase.

			Options:
		USAGE
		add_options_functional(parser)
		add_options_listing(parser)
	end.parse!(argvies, into: options)
end

.list_all_templatesObject



55
56
57
# File 'lib/singer/option_parsing.rb', line 55

def self.list_all_templates
	columnize_hash(Template.all.transform_values(&:path))
end

.list_pathsObject



59
60
61
# File 'lib/singer/option_parsing.rb', line 59

def self.list_paths
	columnize_hash(%w[singer_config_dir templates_from_user templates_from_gem].map{ [_1, Paths.send(_1)] })
end

.list_variablesObject



64
65
66
67
68
69
70
71
# File 'lib/singer/option_parsing.rb', line 64

def self.list_variables
	columnize_hash(Singer.configuration.members.map do |var|
		[
			var,
			CONFIGURATION_VARIABLES_FORBIDDEN_IN_PATHS.include?(var.to_s) ? NOT_AVAILABLE : "__#{var.upcase}__",
		]
	end)
end