Module: Mint::CommandLine
- Defined in:
- lib/mint/command_line.rb
Class Method Summary collapse
-
.config ⇒ void
Displays the sum of all active configurations, where local configurations override global ones.
-
.configure(opts, scope = :local) ⇒ void
Updates configuration options persistently in the appropriate scope, which defaults to local.
-
.edit(name, commandline_options = {}) ⇒ void
Retrieve named template file (probably a built-in or installed template) and shell out that file to the user’s favorite editor.
-
.help(message) ⇒ void
Prints a help banner.
-
.install(file, commandline_options = {}) ⇒ void
Install the named file as a template.
-
.options ⇒ Hash
Returns a map of all options that mint allows by default.
-
.parse(argv, opts = {}) ⇒ Hash
Parses ARGV according to the specified or default commandline syntax.
-
.publish!(files, commandline_options = {}) ⇒ void
Renders and writes to file all resources described by a document.
-
.set(key, value, commandline_options = {}) ⇒ void
Tries to set a config option (at the specified scope) per the user’s command.
-
.templates(filter = nil, commandline_options = {}) ⇒ void
List the installed templates.
-
.uninstall(name, commandline_options = {}) ⇒ void
Uninstall the named template.
Class Method Details
.config ⇒ void
This method returns an undefined value.
Displays the sum of all active configurations, where local configurations override global ones.
196 197 198 |
# File 'lib/mint/command_line.rb', line 196 def self.config puts YAML.dump(Mint.configuration) end |
.configure(opts, scope = :local) ⇒ void
This method returns an undefined value.
Updates configuration options persistently in the appropriate scope, which defaults to local.
168 169 170 171 172 |
# File 'lib/mint/command_line.rb', line 168 def self.configure(opts, scope=:local) config_directory = Mint.path_for_scope(scope, true) FileUtils.mkdir_p config_directory Helpers.update_yaml! "#{config_directory}/#{Mint.files[:defaults]}", opts end |
.edit(name, commandline_options = {}) ⇒ void
This method returns an undefined value.
Retrieve named template file (probably a built-in or installed template) and shell out that file to the user’s favorite editor.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/mint/command_line.rb', line 139 def self.edit(name, ={}) layout = [:layout] style = [:style] # Allow for convenient editing (edit "default" works just as well # as edit :style => "default") if style name, layout_or_style = style, :style elsif layout name, layout_or_style = layout, :layout else layout_or_style = :style end abort "[error] no template specified" if name.nil? || name.empty? file = Mint.lookup_template name, layout_or_style editor = ENV["EDITOR"] || "vi" system "#{editor} #{file}" end |
.help(message) ⇒ void
This method returns an undefined value.
Prints a help banner
67 68 69 |
# File 'lib/mint/command_line.rb', line 67 def self.help() puts end |
.install(file, commandline_options = {}) ⇒ void
This method returns an undefined value.
Install the named file as a template
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mint/command_line.rb', line 78 def self.install(file, ={}) opts = { scope: :local }.merge() scope = [:global, :user]. select {|e| [e] }. first || :local filename, ext = file.split "." name = [:template] || filename type = Mint.css_formats.include?(ext) ? :style : :layout destination = Mint.template_path(name, type, :scope => opts[:scope], :ext => ext) FileUtils.mkdir_p File.("#{destination}/..") puts "reading file" puts File.read file if File.exist? file FileUtils.cp file, destination else raise "[error] no such file" end end |
.options ⇒ Hash
Returns a map of all options that mint allows by default. Mint will consume these arguments, with optional parameters, from the commandline. (All other arguments are taken to be filenames.)
19 20 21 22 |
# File 'lib/mint/command_line.rb', line 19 def self. = "../../../config/#{Mint.files[:syntax]}" YAML.load_file File.(, __FILE__) end |
.parse(argv, opts = {}) ⇒ Hash
Parses ARGV according to the specified or default commandline syntax
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mint/command_line.rb', line 30 def self.parse(argv, opts={}) opts = { syntax: }.merge(opts) = {} parser = OptionParser.new do |cli| cli. = "Usage: mint [command] files [options]" Helpers.symbolize_keys(opts[:syntax]).each do |k,v| has_param = v[:parameter] v[:short] = "-#{v[:short]}" v[:long] = "--#{v[:long]}" if has_param v[:long] << " PARAM" cli.on v[:short], v[:long], v[:description] do |p| [k.to_sym] = p end else cli.on v[:short], v[:long], v[:description] do [k.to_sym] = true end end end end transient_argv = argv.dup parser.parse! transient_argv { argv: transient_argv, options: , help: parser.help } end |
.publish!(files, commandline_options = {}) ⇒ void
This method returns an undefined value.
Renders and writes to file all resources described by a document. Specifically: it publishes a document, using the document’s accessors to determine file placement and naming, and then renders its style. This method will overwrite any existing content in a document’s destination files. The ‘render_style` option provides an easy way to stop Mint from rendering a style, even if the document’s style is not nil.
211 212 213 214 215 216 |
# File 'lib/mint/command_line.rb', line 211 def self.publish!(files, ={}) = { root: Dir.getwd }.merge(Mint.configuration_with ) files.each_with_index do |file, idx| Document.new(file, ).publish!(:render_style => (idx == 0)) end end |
.set(key, value, commandline_options = {}) ⇒ void
This method returns an undefined value.
Tries to set a config option (at the specified scope) per the user’s command.
183 184 185 186 187 188 189 190 |
# File 'lib/mint/command_line.rb', line 183 def self.set(key, value, ={}) [:local] = true scope = [:global, :user, :local]. select {|e| [e] }. first configure({ key => value }, scope) end |
.templates(filter = nil, commandline_options = {}) ⇒ void
This method returns an undefined value.
List the installed templates
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mint/command_line.rb', line 116 def self.templates(filter=nil, ={}) scopes = Mint::SCOPE_NAMES.select do |s| [s] end.presence || Mint::SCOPE_NAMES Mint.templates(:scopes => scopes). grep(Regexp.new(filter || "")). sort. each do |template| print File.basename template print " [#{template}]" if [:verbose] puts end end |
.uninstall(name, commandline_options = {}) ⇒ void
This method returns an undefined value.
Uninstall the named template
108 109 110 111 |
# File 'lib/mint/command_line.rb', line 108 def self.uninstall(name, ={}) opts = { scope: :local }.merge() FileUtils.rm_r Mint.template_path(name, :all, :scope => opts[:scope]) end |