Module: Mint::CommandLine

Defined in:
lib/mint/command_line.rb

Class Method Summary collapse

Class Method Details

.configvoid

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.

Parameters:

  • opts (Hash)

    a structured set of options to set on Mint at the specified scope

  • scope (Symbol) (defaults to: :local)

    the scope at which to apply the set of options



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.

Parameters:

  • name (String)

    the name of a layout or style to edit

  • commandline_options (Hash) (defaults to: {})

    a structured set of options, including a layout or style flag that the method will use to choose the appropriate file to edit



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, commandline_options={})
  layout = commandline_options[:layout]
  style = commandline_options[: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

Parameters:

  • message (String, #to_s)

    a message to output



67
68
69
# File 'lib/mint/command_line.rb', line 67

def self.help(message)
  puts message
end

.install(file, commandline_options = {}) ⇒ void

This method returns an undefined value.

Install the named file as a template

Parameters:

  • file (File)

    the file to install to the appropriate Mint directory

  • commandline_options (Hash) (defaults to: {})

    a structured set of options, including a scope label that the method will use to choose the appropriate installation directory



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, commandline_options={})
  opts = { scope: :local }.merge(commandline_options)
  scope = [:global, :user].
    select {|e| commandline_options[e] }.
    first || :local

  filename, ext = file.split "."

  name = commandline_options[: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.expand_path("#{destination}/..")

  puts "reading file"
  puts File.read file

  if File.exist? file
    FileUtils.cp file, destination
  else
    raise "[error] no such file"
  end
end

.optionsHash

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.)

Returns:

  • (Hash)

    a structured set of options that the commandline executable accepts



19
20
21
22
# File 'lib/mint/command_line.rb', line 19

def self.options
  options_file = "../../../config/#{Mint.files[:syntax]}"
  YAML.load_file File.expand_path(options_file, __FILE__)
end

.parse(argv, opts = {}) ⇒ Hash

Parses ARGV according to the specified or default commandline syntax

Parameters:

  • argv (Array)

    a list of arguments to parse

  • opts (Hash) (defaults to: {})

    default parsing options (to specify syntax file)

Returns:

  • (Hash)

    an object that contains parsed options, remaining arguments, and a help message



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: options }.merge(opts)
  parsed_options = {}

  parser = OptionParser.new do |cli|
    cli.banner = "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|
          parsed_options[k.to_sym] = p
        end
      else
        cli.on v[:short], v[:long], v[:description] do
          parsed_options[k.to_sym] = true
        end
      end
    end
  end

  transient_argv = argv.dup 
  parser.parse! transient_argv
  { argv: transient_argv, options: parsed_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.

Parameters:

  • files (Array, #each)

    a group of filenames

  • commandline_options (Hash, #[]) (defaults to: {})

    a structured set of configuration options that will guide Mint.publish!



211
212
213
214
215
216
# File 'lib/mint/command_line.rb', line 211

def self.publish!(files, commandline_options={})
  options = { root: Dir.getwd }.merge(Mint.configuration_with commandline_options)
  files.each_with_index do |file, idx|
    Document.new(file, options).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.

Parameters:

  • key

    the key to set

  • value

    the value to set key to

  • commandline_options (Hash, #[]) (defaults to: {})

    a structured set of options, including a scope label that the method will use to choose the appropriate scope



183
184
185
186
187
188
189
190
# File 'lib/mint/command_line.rb', line 183

def self.set(key, value, commandline_options={})
  commandline_options[:local] = true
  scope = [:global, :user, :local].
    select {|e| commandline_options[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, commandline_options={})
  scopes = Mint::SCOPE_NAMES.select do |s|
    commandline_options[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 commandline_options[:verbose]
      puts
    end
end

.uninstall(name, commandline_options = {}) ⇒ void

This method returns an undefined value.

Uninstall the named template

Parameters:

  • name (String)

    the name of the template to be uninstalled

  • commandline_options (Hash) (defaults to: {})

    a structured set of options, including a scope label that the method will use to choose the appropriate installation directory



108
109
110
111
# File 'lib/mint/command_line.rb', line 108

def self.uninstall(name, commandline_options={})
  opts = { scope: :local }.merge(commandline_options)
  FileUtils.rm_r Mint.template_path(name, :all, :scope => opts[:scope])
end