Skip to content

Commit

Permalink
Added speckle executable
Browse files Browse the repository at this point in the history
  • Loading branch information
dsawardekar committed Aug 8, 2013
1 parent 54dfce2 commit 8b434d7
Show file tree
Hide file tree
Showing 9 changed files with 714 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bin/speckle
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby

require File.expand_path('../../lib/speckle/loader', __FILE__)

module Speckle

require 'speckle/cli/app'

app = Speckle::CLI::App.new
app.start(ARGV)

end
18 changes: 18 additions & 0 deletions lib/speckle/cli/app.rb
@@ -0,0 +1,18 @@
module Speckle
module CLI

require_relative 'environment'
require_relative 'router'

class App
def start(args)
env = Environment.new
options = env.load(args)

router = Router.new
router.route(options.action, options)
end
end

end
end
67 changes: 67 additions & 0 deletions lib/speckle/cli/controller.rb
@@ -0,0 +1,67 @@
module Speckle
module CLI

require 'speckle/version'

class Controller

def initialize(options, rake_app)
@options = options
@rake_app = rake_app
end

def rake(task)
@rake_app.invoke_task(task)
end

def show_version
puts VERSION
end

def show_help
puts @options.opts
end

def show_error(msg = @options.error)
puts "Error: #{msg}"
puts

show_help
end

def show_invalid_option
show_error @options.error
end

def show_missing_args
show_error @options.error
end

def show_parser_error
show_error @options.error
end

def show_no_spec_dir
show_error '"spec" directory not found'
end

def compile
rake :compile_tests
end

def compile_and_test
rake :compile_and_test
end

def test
rake :test
end

def watch
puts '--- TODO ---'
end

end

end
end
148 changes: 148 additions & 0 deletions lib/speckle/cli/environment.rb
@@ -0,0 +1,148 @@
module Speckle

module CLI

require 'optparse'
require 'ostruct'

class Environment
include Loader

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

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('-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
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

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


end
end
146 changes: 146 additions & 0 deletions lib/speckle/cli/rake_app.rb
@@ -0,0 +1,146 @@
module Speckle
module CLI

require 'rake'

class RakeApp
def initialize(options)
@options = options
end

def inputs
@options.inputs
end

def verbose
@options.verbose
end

def debug
@options.debug
end

def rake
if @rake_app
return @rake_app
end

configure_rake
Dir.chdir @options.root_dir

@rake_app = Rake.application
@rake_app.init
@rake_app.load_rakefile

Dir.chdir @options.cwd
@rake_app
end

def invoke_task(name)
rake.invoke_task("speckle:#{name.to_s}")
end

def rake_env(key, value)
unless value.nil?
ENV[key] = if value.is_a?(Array) then value.join(';') else value end
puts "rake_env: #{key} = #{ENV[key]}" if debug
end
end

def configure_rake
rake_env('TEST_SOURCES', test_sources)
rake_env('TEST_LIBS', test_libs)
rake_env('BUILD_DIR', test_build_dir)
rake_env('TEST_COMPILED', test_compiled)
rake_env('TEST_VIM', @options.vim)
rake_env('TEST_REPORTER', @options.reporter)
rake_env('SLOW_THRESHOLD', @options.slow_threshold.to_s)
rake_env('SKIP_VIMRC', to_int(@options.skip_vimrc))
rake_env('COLORIZE', to_int(@options.colorize))
rake_env('BAIL', to_int(@options.bail))

if @options.verbose
rake_env('VERBOSE', 'yes')
end

if @options.debug
rake_env('DEBUG', 'yes')
end
end

def to_int(option)
option ? '1' : '0'
end

def test_build_dir
"#{@options.cwd}/build"
end

def test_compiled
compiled = test_sources.map do |s|
s.ext('vim')
end

compiled
end

def test_sources
sources = []
grep_pattern = @options.grep_pattern
grep_invert = @options.grep_invert
unless grep_pattern.nil?
regex = Regexp.new(grep_pattern)
end

inputs.each do |input|
if File.directory?(input)
if grep_pattern.nil?
sources << "#{input}/**/*_spec.riml"
else
sources = Dir.glob("#{input}/**/*_spec.riml")
sources.keep_if do |source|
matched = regex.match(source)
if grep_invert
matched = !matched
end

matched
end
end
else
if grep_pattern.nil?
sources << input
else
matched = regex.match(input)
if grep_invert
matched = !matched
end
sources << input if matched
end
end
end

sources
end

def test_libs
input_libs = @options.libs
return nil if input_libs.nil?

input_libs = input_libs.split(':')
input_libs << 'spec'
if File.directory?(@options.speckle_lib_dir)
input_libs << @options.speckle_lib_dir
end

libs = []
input_libs.each do |lib|
libs << File.absolute_path(lib)
end

libs.join(':')
end

end
end
end

0 comments on commit 8b434d7

Please sign in to comment.