Class: Speckle::CLI::Environment

Inherits:
Object
  • Object
show all
Includes:
Loader
Defined in:
lib/speckle/cli/environment.rb

Constant Summary

Constants included from Loader

Loader::LIB_DIR, Loader::ROOT_DIR

Instance Method Summary collapse

Instance Method Details

#action_needs_args?(action) ⇒ Boolean



146
147
148
# File 'lib/speckle/cli/environment.rb', line 146

def action_needs_args?(action)
  [:compile_and_test, :compile, :test].include? action
end

#load(args) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
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
142
143
144
# File 'lib/speckle/cli/environment.rb', line 11

def load(args)
  options = OpenStruct.new
  options.libs = nil
  options.grep_pattern = nil
  options.grep_invert = false
  options.reporter = 'dot'
  options.args = args
  options.verbose = false
  options.vim = 'vim'
  options.cwd = Dir.pwd
  options.root_dir = ROOT_DIR
  options.speckle_build_dir = "#{ROOT_DIR}/build"
  options.speckle_lib_dir = "#{ROOT_DIR}/lib"
  options.skip_vimrc = false
  options.slow_threshold = 10
  options.colorize = true
  options.bail = false
  options.tag = nil

  parser = OptionParser.new do |opts|
    options.opts = opts

    opts.banner = "Usage: speckle [options] [file(s) OR directory]"
    opts.separator ''
    opts.separator 'Options:'
    opts.separator ''

    opts.on_head('-c', '--compile', 'Only compile tests') do
      options.action = :compile
    end

    opts.on_head('-t', '--test', 'Only run tests') do
      options.action = :test
    end

    opts.on_head('-a', '--all', 'Compile and run tests (default)') do
      options.action = :compile_and_test
    end

    opts.on('-I', '--libs <libs>', 'Specify additional riml library path(s)') do |libs|
      options.libs = libs
    end

    opts.on('-g', '--grep <pattern>', 'Only run tests matching the pattern') do |pattern|
      options.grep_pattern = pattern.strip
    end

    opts.on('-i', '--invert', 'Inverts --grep matches') do
      options.grep_invert = true
    end

    opts.on('--tag <tag>', 'Only run tests matching tag') do |tag|
      options.tag = tag
    end

    opts.on('-r', '--reporter <reporter>', 'Specify the reporter to use (spec, min, dot, tap)') do |reporter|
      options.reporter = reporter.strip
    end

    opts.on('-b', '--bail', 'Bail on first test failure') do
      options.bail = true
    end

    opts.on('-w', '--watch', 'Watch tests for changes') do
      options.action = :watch
    end

    opts.on('-m', '--vim <vim>', 'Vim program used to test, default(vim)') do |vim|
      options.vim = vim.strip
    end

    opts.on('-s', '--slow-threshold <ms>', Integer, 'Threshold in milliseconds to indicate slow tests') do |ms|
      options.slow_threshold = ms
    end

    opts.on('-k', '--skip-vimrc', 'Does not load ~/.vimrc file') do
      options.skip_vimrc = true
    end

    opts.on('-C', '--no-colors', 'Disable color output') do
      options.colorize = false
    end

    opts.on('-v', '--verbose', 'Display verbose output') do
      options.verbose = true
    end
    
    opts.on('-D', '--debug', 'Display debug output') do
      options.verbose = true
      options.debug = true
    end

    opts.on_tail('-V', '--version', 'Print Speckle version') do
      options.action = :show_version
    end

    opts.on_tail('-h', '--help', 'Print Speckle help') do
      options.action = :show_help
    end
  end

  begin
    parser.parse!(args)

    if options.action.nil?
      spec_dir = "#{options.cwd}/spec"
      if File.directory?(spec_dir)
        args << 'spec'
        options.action = :compile_and_test
      else
        options.action = :show_no_spec_dir
      end
    elsif action_needs_args?(options.action) and args.empty?
      spec_dir = "#{options.cwd}/spec"
      if File.directory?(spec_dir)
        args << 'spec'
        options.action = :compile_and_test
      end
    end

    options.inputs = args.uniq
  rescue OptionParser::InvalidOption => e
    options.error = e
    options.action = :show_invalid_option
  rescue OptionParser::MissingArgument => e
    options.error = e
    options.action = :show_missing_args
  rescue OptionParser::ParseError => e
    options.error = e
    options.action = :show_parser_error
  end

  options
end