module Hen::CLI
Some helper methods used by the Hen executable. Also available for use in custom project skeletons.
Attributes
Collect user's answers by key, so we don't have to ask again.
Public Instance Methods
The project's namespace. (Required)
Namespaces SHOULD match the project name in SnakeCase.
# File lib/hen/cli.rb, line 95 def classname(default = default_classname) ask!("Module's/Class's name", default) end
The author's e-mail address. (Optional, but highly recommended)
# File lib/hen/cli.rb, line 105 def emailaddress(default = default_emailaddress) ask('E-mail address', default) end
The author's full name. (Required)
# File lib/hen/cli.rb, line 100 def fullname(default = default_fullname) ask!('Full name', default) end
The author's GitHub user name. (Optional)
# File lib/hen/cli.rb, line 110 def githubuser(default = default_githubuser) ask('GitHub user name', default) end
A long description of the project. (Optional)
# File lib/hen/cli.rb, line 88 def progdesc(default = nil) ask("Program's full description", default) end
The project name. (Required)
Quoting the Ruby Packaging Standard:
Project names SHOULD only contain underscores as separators in their names.
If a project is an enhancement, plugin, extension, etc. for some other project it SHOULD contain a dash in the name between the original name and the project's name.
# File lib/hen/cli.rb, line 78 def progname(default = nil) ask!("Project's name", default) end
A short summary of the project's description. (Required)
# File lib/hen/cli.rb, line 83 def progsumm(default = nil) ask!("Program's description summary", default) end
Renders the contents of sample
as an ERb template, storing the
result in target
. Returns the content.
# File lib/hen/cli.rb, line 53 def render(sample, target) abort "Sample file not found: #{sample}" unless File.readable?(sample) if File.readable?(target) abort unless agree("Target file already exists: #{target}. Overwrite? ") File.rename(target, "#{target}.bak-#{Time.now.to_i}") end content = ERB.new(File.read(sample)).result(binding) File.open(target, 'w') { |f| f.puts content unless content.empty? } content end
Private Instance Methods
Ask the user to enter an appropriate value for key
. Uses
already stored answer if present, unless cached
is false.
# File lib/hen/cli.rb, line 158 def ask(key, default = nil, cached = true) CLI.answers[key] = nil unless cached CLI.answers[key] ||= _hen_original_ask("Please enter your #{key}: ") { |q| q.default = default if default } rescue Interrupt abort '' end
Same as ask, but requires a non-empty value to be entered.
# File lib/hen/cli.rb, line 169 def ask!(key, default = nil, max = 3) msg = "#{key} is required! Please enter a non-empty value." max.times { |i| value = ask(key, default, i.zero?) return value unless value.empty? puts msg } warn "You had #{max} tries now -- giving up..." '' # default value end
Determine a suitable default namespace from the project name.
# File lib/hen/cli.rb, line 117 def default_classname name = progname name.gsub(/(?:\A|_)(.)/) { $1.upcase } if name && !name.empty? end
Determine a default e-mail address from the global config or from the Git config.
# File lib/hen/cli.rb, line 132 def default_emailaddress henconfig(:gem, :email) || gitconfig(:user, :email) end
Determine a default name from the global config or from the Git config or,
if available, from the GECOS field in the
/etc/passwd
file.
# File lib/hen/cli.rb, line 125 def default_fullname henconfig(:gem, :author) || gitconfig(:user, :name) || (pwent = Etc.getpwuid(Process.euid) and pwent.gecos.to_s[/[^,]+/]) end
Determine a default GitHub user name from the global config or from the Git config.
# File lib/hen/cli.rb, line 138 def default_githubuser henconfig(:github, :user) || gitconfig(:github, :user) end
Find key
in the Git config and return its non-empty value.
# File lib/hen/cli.rb, line 149 def gitconfig(*key) value = IO.popen(['git', 'config', key.join('.')]) { |io| io.read.chomp } value if value && !value.empty? end
Find key
in the global config and return its non-empty value.
# File lib/hen/cli.rb, line 143 def henconfig(*key) value = Hen.config(key.join('/')) value if value && !value.empty? end