Class: InstallTheme::CLI

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

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/install_theme/cli.rb', line 5

def self.execute(stdout, arguments=[])
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      Use any HTML template as a theme generator for your Rails app.
      
      Usage: #{File.basename($0)} path/to/rails_app path/to/template content_path [options]
      
      Options are:
    BANNER
    opts.separator ""
    opts.on("-p", "--partial KEY_AND_PATH", String,
            "Replace the inner HTML of an element with <%= yield :key %>",
            "Example using CSS path: --partial header:#header",
            "Example using XPath: --partial \"header://div[@id='header']\"") do |arg|
              options[:partials] ||= {}
              key, path = arg.split(/\s*:\s*/)[0..1]
              if key && path
                options[:partials][key.strip] = path.strip
              else
                stdout.puts partial_format_error(arg)
                exit
              end
            end
    opts.on("-l", "--layout application", String,
            "Set the layout file name.",
            "Layout files are stored in app/views/layouts and will be suffixed by .html.erb or .html.haml",
            "Default: application") { |arg| options[:layout] = arg }
    opts.on("--erb",
            "Generate ERb templates.",
            "Default: auto-detect") { |arg| options[:template_type] = 'erb' }
    opts.on("--haml",
            "Generate HAML templates.",
            "Default: auto-detect") { |arg| options[:template_type] = 'haml' }
    opts.on("--index_path index.html", String,
            "HTML page to use for application layout.",
            "Default: index.html") { |arg| options[:index_path] = arg }
    opts.on("--defaults_file install_theme.yml", String,
            "Select an alternate YAML file containing defaults for the theme.",
            "Default: install_theme.yml") { |arg| options[:defaults_file] = arg }
    opts.on("-h", "--help",
            "Show this help message.") { |arg| stdout.puts opts; exit }
    opts.on("-v", "--version",
            "Show the version (which is #{InstallTheme::VERSION})."
            ) { |arg| stdout.puts InstallTheme::VERSION; exit }
    opts.parse!(arguments)
  end
  options[:rails_root]    = arguments.shift
  options[:template_root] = arguments.shift
  options[:content_path]  = arguments.shift
  theme = InstallTheme.new(options)
  unless theme.valid?
    stdout.puts parser; exit
  end
  theme.apply_to_target
end

.partial_format_error(partial_argument) ⇒ Object



62
63
64
65
66
67
# File 'lib/install_theme/cli.rb', line 62

def self.partial_format_error(partial_argument)
  <<-EOS.gsub(/^      /, '')
  ERROR: Incorrect format for --partial argument "#{partial_argument}".
  Correct format is: --partial label:path
  EOS
end