Class: Stratus::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/stratus/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
# File 'lib/stratus/cli.rb', line 11

def initialize(out)
  @out = out
  @mode = :site
end

Class Method Details

.run(args, out = STDOUT) ⇒ Object



140
141
142
# File 'lib/stratus/cli.rb', line 140

def run(args, out=STDOUT)
  self.new(out).run(args)
end

Instance Method Details

#appObject

Return the Rake application object.



107
108
109
# File 'lib/stratus/cli.rb', line 107

def app
  Rake.application
end

#capture_command_line_args(args) ⇒ Object



133
134
135
136
137
# File 'lib/stratus/cli.rb', line 133

def capture_command_line_args(args)
  ::Stratus.args = args # set the args in a place where all rake tasks can read 'em
  ::Stratus.site_path = File.expand_path('.')
  args
end

#import_default_tasksObject



117
118
119
120
121
# File 'lib/stratus/cli.rb', line 117

def import_default_tasks
  path = (@mode == :app)? %w(stratus tasks app *.rake) : %w(stratus tasks site *.rake)
  Dir.glob(::Stratus.libpath(path)).sort.each {|fn| load fn}
  Dir.glob(::Stratus.libpath(%w(stratus tasks shared *.rake))).sort.each {|fn| load fn}
end

#import_website_tasksObject



123
124
125
126
# File 'lib/stratus/cli.rb', line 123

def import_website_tasks
  return if (@mode == :app)
  Dir.glob(::File.join(%w[tasks *.rake])).sort.each {|fn| load fn}
end

#init(args) ⇒ Object

Initialize the Rake application object and load the core rake tasks, the site specific rake tasks, and the site specific ruby code. Any extra command line arguments are converted into a page name and directory that might get created (depending upon the task invoked).



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stratus/cli.rb', line 74

def init( args )
  # Make sure we're in a folder with a Sitefile
  options = app.standard_rake_options
  [['--rakefile', 'StratusSite'],
   ['--no-search', nil],
   ['--silent', nil]].each {|opt, value| options.assoc(opt).last.call(value)}

  unless app.have_rakefile
    @mode = :app
  end

  import_default_tasks
  import_website_tasks
  require_lib_files
  capture_command_line_args(args)
  
  # if args.length  == 0 && @mode == :app
  #   puts "Try using:\n  stratus -T"
  # end
  
  args
end

#optionsObject

Returns the options hash from the Rake application object.



113
114
115
# File 'lib/stratus/cli.rb', line 113

def options
  app.options
end

#parse(args) ⇒ Object

Parse the command line args for options and commands to invoke.



24
25
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
# File 'lib/stratus/cli.rb', line 24

def parse( args )
  opts = OptionParser.new
  opts.banner = 'Usage: stratus [options] target [target args]'

  opts.separator ''

  desired_opts = %[--describe --prereqs --tasks --trace]
  app.standard_rake_options.each do |options|
    next unless desired_opts.include?(options.first)
    opts.on(*options)
  end

  opts.separator ''
  opts.separator 'common options:'

  opts.on_tail( '-h', '--help', 'show this message' ) do
    @out.puts opts
    exit
  end
  opts.on_tail( '-q', '--quiet', 'show compact messages' ) do
    ENV['STRATUS_LOGGING'] = 'quiet'
  end
  opts.on_tail( '--version', 'show version' ) do
    @out.puts "Stratus #{::Stratus::VERSION}"
    exit
  end
  
  begin
    opts.parse! args
  rescue OptionParser::InvalidOption => ex
    puts "!! #{ex}"
  end
  
  
  ARGV.replace Array(args.shift)
  args.delete_if do |arg|
    if %r/^[A-Z_]+=/ =~ arg
      ARGV << arg
      next true
    end
    false
  end

  args
end

#rakeObject

Execute the rake command.



99
100
101
102
103
# File 'lib/stratus/cli.rb', line 99

def rake
  app.init 'stratus'
  app.load_rakefile
  app.top_level
end

#require_lib_filesObject



128
129
130
131
# File 'lib/stratus/cli.rb', line 128

def require_lib_files
  return if (@mode == :app)
  Dir.glob(::File.join(%w[lib ** *.rb])).sort.each {|fn| require fn}
end

#run(args) ⇒ Object



16
17
18
19
20
21
# File 'lib/stratus/cli.rb', line 16

def run(args)
  args = args.dup
  parse args
  init args
  rake
end