Class: RSX::CLI

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

Overview

rsx command line tool. Useful for seeing exactly what a template compiles to, rendering a file outside of Rails, and warming the compile cache.

Constant Summary collapse

<<~TEXT
  Usage: rsx COMMAND [options] [files]

  Commands:
    compile FILE...     Print the Ruby a .rsx file compiles to
    render FILE         Render a .rsx file and print the HTML
    precompile DIR...   Compile every .rsx file under DIR and cache the result
    version             Print the RSX version

  Options:
TEXT

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(argv) ⇒ Object



21
22
23
# File 'lib/rsx/cli.rb', line 21

def self.start(argv)
  new.run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



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

def run(argv)
  options = { props: {}, cache_dir: nil }

  parser = OptionParser.new do |opts|
    opts.banner = BANNER
    opts.on("-p", "--prop NAME=VALUE", "Pass a string prop when rendering") do |pair|
      name, value = pair.split("=", 2)
      options[:props][name.to_sym] = value
    end
    opts.on("-I", "--include PATH", "Add a directory to the RSX load path") do |path|
      RSX.config.paths |= [File.expand_path(path)]
    end
    opts.on("-c", "--cache-dir DIR", "Directory for compiled output") do |dir|
      options[:cache_dir] = dir
    end
    opts.on("-h", "--help", "Show this message") { puts opts; return 0 }
  end

  arguments = parser.parse(argv)
  command = arguments.shift

  RSX.config.cache_dir = options[:cache_dir]

  case command
  when "compile" then compile(arguments)
  when "render" then render(arguments, options[:props])
  when "precompile" then precompile(arguments, options[:cache_dir])
  when "version", "-v", "--version" then puts RSX::VERSION
  when nil then puts parser
  else
    warn "rsx: unknown command #{command.inspect}"
    puts parser
    return 1
  end

  0
rescue RSX::Error, Errno::ENOENT => e
  warn "rsx: #{e.message}"
  1
end