Class: Plate::CLI
- Inherits:
-
Object
- Object
- Plate::CLI
- Defined in:
- lib/plate/cli.rb
Overview
The CLI class controls the behavior of plate when it is used as a command line interface.
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#destination ⇒ Object
Returns the value of attribute destination.
-
#options ⇒ Object
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #builder ⇒ Object
-
#command ⇒ Object
The current command to be run.
-
#initialize(args = []) ⇒ CLI
constructor
A new instance of CLI.
- #parse_options! ⇒ Object
-
#run ⇒ Object
Run the given command.
Constructor Details
#initialize(args = []) ⇒ CLI
Returns a new instance of CLI.
8 9 10 11 12 13 14 15 |
# File 'lib/plate/cli.rb', line 8 def initialize(args = []) @args = String === args ? args.split(' ') : args.dup @options = {} # Some defaults @source = Dir.pwd @destination = File.join(@source, 'public') end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
6 7 8 |
# File 'lib/plate/cli.rb', line 6 def args @args end |
#destination ⇒ Object
Returns the value of attribute destination.
6 7 8 |
# File 'lib/plate/cli.rb', line 6 def destination @destination end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/plate/cli.rb', line 6 def @options end |
#source ⇒ Object
Returns the value of attribute source.
6 7 8 |
# File 'lib/plate/cli.rb', line 6 def source @source end |
Class Method Details
.run! ⇒ Object
94 95 96 |
# File 'lib/plate/cli.rb', line 94 def run! new(ARGV).run end |
Instance Method Details
#builder ⇒ Object
17 18 19 |
# File 'lib/plate/cli.rb', line 17 def builder @builder ||= Builder.new(self.source, self.destination, self.) end |
#command ⇒ Object
The current command to be run. Pulled from the args attribute.
22 23 24 |
# File 'lib/plate/cli.rb', line 22 def command self.args.size > 0 ? self.args[0] : 'build' end |
#parse_options! ⇒ Object
26 27 28 29 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/plate/cli.rb', line 26 def = {} opts = OptionParser.new do |opts| = "Usage: plate [command] [options]" opts.on('--category [CATEGORY]', '-c', 'Pass in a category for creating a new post.') do |c| [:category] = c end opts.on('--config [PATH]', '-C', 'Set the config file location for the site.') do |c| [:config] = c end opts.on('--destination [PATH]', '-d', 'Set the destination directory for this build.') do |d| @destination = File.(d) end opts.on("--[no-]draft", "Create a new post as a draft.") do |v| [:draft] = v end opts.on('--layout [LAYOUT]', '-l', 'Pass in a layout for creating a new post.') do |l| [:layout] = l end opts.on('--source [PATH]', '-s', 'Set the source directory for this build.') do |s| @source = File.(s) end opts.on('--verbose', '-V', 'Show output about the generation of the site.') do [:verbose] = true end opts.on('--version', '-v', 'Show the current Plate version number.') do puts "You're running Plate version #{Plate::VERSION}!" exit 0 end opts.on('--watch', '-w', 'Watch the source directory for changes.') do [:watch] = true end end opts.parse!(self.args) @options = end |
#run ⇒ Object
Run the given command. If the command does not exist, nothing will be run.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/plate/cli.rb', line 76 def run command_name = "run_#{command}_command".to_sym if self.respond_to?(command_name) # remove command name self.args.shift self.send(command_name) else puts "Command #{command} not found" return false end true end |