Class: MoCo::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/moco/options.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.args(args) ⇒ Object



14
15
16
17
# File 'lib/moco/options.rb', line 14

def self.args(args)
  @args ||= []
  @args += args.shellsplit
end

.moco_filesObject



8
9
10
11
12
# File 'lib/moco/options.rb', line 8

def self.moco_files
  files = ['~/.moco', '~/moco.rb', './.moco', './moco.rb']
  files = files.map { |file| File.expand_path(file) }
  files.select { |file| File.file?(file) }
end

.parse(args) ⇒ Object



19
20
21
# File 'lib/moco/options.rb', line 19

def self.parse(args)
  new.parse(args)
end

Instance Method Details

#default_optionsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/moco/options.rb', line 52

def default_options
  {
    :monitor        => true,
    :compile        => true,
    :compile_exts   => MoCo.compilers.keys.sort,
    :force          => false,
    :source_map     => false,
    :reload         => true,
    :reload_exts    => Browser.extensions,
    :browsers       => Browser.browsers,
    :urls           => Browser.localhost,
    :quiet          => false,
    :source_files   => [],
    :source_dirs    => [],
    :compiled_files => {},
    :compiled_dirs  => {}
  }
end

#helpObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/moco/options.rb', line 71

def help
  <<-EOF.gsub(/^ {8}/, '').gsub(/`(.+?)`/, AnsiEscape.green('\1'))
    Usage:
      `moco [options] SOURCE ...`
      `moco [options] SOURCE:COMPILED ...`

    Description:
      MoCo monitors web templates. On updates the templates are compiled and
      the browser reloaded. MoCo currently supports CoffeeScript, Sass, LESS,
      Markdown and Haml.

    Files and directories:
      The given source files and directories will be monitored for updates.
      Use the SOURCE:COMPILED format to save the compiled files to another
      directory or to change the compiled filename:
      `moco .:/www sass:/www/css README.md:/www/index.html`

    Options:
          --monitor             Keep running until Ctrl-C is pressed [DEFAULT]
          --no-monitor          Exit after the initial compilation

      -c, --compile             Compile all the supported file types [DEFAULT]
      -c, --compile EXT,EXT     Compile the given file types
          --no-compile          Disable compilation
                                `moco -c coffee -c sass,scss .`

      -f, --force               Force recompilation at startup
          --no-force            Do not compile up-to-date files [DEFAULT]

      -m, --source-map          Make source maps if the compiler supports it
          --no-source-map       Do not make source maps [DEFAULT]

      -o, --option EXT:KEY:VAL  Set a compiler option
                                `moco -o coffee:header`       # header = true
                                `     -o haml:ugly:false`     # ugly   = false
                                `     -o haml:format::xhtml`  # format = :xhtml
                                `     -o md:layout:md.html`   # layout = 'md.html'
                                `     -o less:paths:css: .`   # paths  = ['css']

      -r, --reload              Reload after css/html/js file updates [DEFAULT]
      -r, --reload EXT,EXT      Set the file types that triggers reloading
          --no-reload           Disable reloading
                                `moco -r rb -r css,html,js .`

      -b, --browser BRO,BRO     The browsers to reload [all by DEFAULT]
                                `moco -b safari -b chrome,canary .`

      -u, --url all             Reload all active tabs
      -u, --url localhost       Reload active tabs with localhost urls [DEFAULT]
      -u, --url URL,URL         Reload active tabs where the url starts with URL
                                `moco -u localhost -u http://app.dev/ .`

          --require LIB         Require the library
                                `moco --require path/to/compiler.rb .`

      -q, --quiet               Log errors only
          --no-quiet            Log errors and file updates [DEFAULT]

      -l, --list                List the supported file types and browsers

      -h, --help                Display this message

    The moco file:
      MoCo looks for files named '.moco' and 'moco.rb' in the working directory
      and in the home directory. The purpose of these files is to set options
      and to define new compilers. The command line options have precedence.

    More information:
      https://github.com/asharbitz/moco#readme
  EOF
end

#option_parserObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/moco/options.rb', line 34

def option_parser
  OptionParser.new do |op|
    op.version = MoCo::VERSION
    op.on(      '--[no-]monitor',           method(:monitor))
    op.on('-c', '--[no-]compile [EXT,EXT]', method(:compile), Array)
    op.on('-f', '--[no-]force',             method(:force))
    op.on('-m', '--[no-]source-map',        method(:source_map))
    op.on('-o', '--option EXT:KEY:VAL',     method(:compiler_option))
    op.on('-r', '--[no-]reload [EXT,EXT]',  method(:reload), Array)
    op.on('-b', '--browser BRO,BRO',        method(:browsers), Array)
    op.on('-u', '--url URL,URL',            method(:urls), Array)
    op.on(      '--require LIB',            method(:require_lib))
    op.on('-q', '--[no-]quiet',             method(:quiet))
    op.on('-l', '--list',                   method(:display_list))
    op.on('-h', '--help',                   method(:display_help))
  end
end

#parse(command_line_args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/moco/options.rb', line 23

def parse(command_line_args)
  @options = {}
  args = moco_file_args + command_line_args
  display_help if args.empty?
  option_parser.order(args) { |option| path(option) }
  validate_options
  default_options.merge(@options)
rescue OptionParser::ParseError => e
  raise OptionError.new(e)
end