Class: Plate::CLI

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/plate/cli.rb', line 6

def args
  @args
end

#destinationObject

Returns the value of attribute destination.



6
7
8
# File 'lib/plate/cli.rb', line 6

def destination
  @destination
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/plate/cli.rb', line 6

def options
  @options
end

#sourceObject

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

#builderObject



17
18
19
# File 'lib/plate/cli.rb', line 17

def builder
  @builder ||= Builder.new(self.source, self.destination, self.options)
end

#commandObject

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 parse_options!
  options = {}

  opts = OptionParser.new do |opts|
    banner = "Usage: plate [command] [options]"

    opts.on('--category [CATEGORY]', '-c', 'Pass in a category for creating a new post.') do |c|
      options[:category] = c
    end

    opts.on('--config [PATH]', '-C', 'Set the config file location for the site.') do |c|
      options[:config] = c
    end

    opts.on('--destination [PATH]', '-d', 'Set the destination directory for this build.') do |d|
      @destination = File.expand_path(d)
    end

    opts.on("--[no-]draft", "Create a new post as a draft.") do |v|
      options[:draft] = v
    end

    opts.on('--layout [LAYOUT]', '-l', 'Pass in a layout for creating a new post.') do |l|
      options[:layout] = l
    end

    opts.on('--source [PATH]', '-s', 'Set the source directory for this build.') do |s|
      @source = File.expand_path(s)
    end

    opts.on('--verbose', '-V', 'Show output about the generation of the site.') do
      options[: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
      options[:watch] = true
    end
  end

  opts.parse!(self.args)

  @options = options
end

#runObject

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
  parse_options!

  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