Skip to content

Commit

Permalink
Start to split up the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Jan 26, 2009
1 parent 438814e commit 6fb8b00
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 75 deletions.
4 changes: 2 additions & 2 deletions bin/cucumber
Expand Up @@ -2,5 +2,5 @@
# Add '.rb' to work around a bug in IronRuby's File#dirname
$:.unshift(File.dirname(__FILE__ + '.rb') + '/../lib') unless $:.include?(File.dirname(__FILE__ + '.rb') + '/../lib')

require 'cucumber/cli'
Cucumber::CLI.execute(ARGV.dup)
require 'cucumber/cli/main'
Cucumber::Cli::Main.execute(ARGV.dup)
4 changes: 2 additions & 2 deletions lib/cucumber.rb
Expand Up @@ -7,7 +7,7 @@
require 'cucumber/parser'
require 'cucumber/version'
require 'cucumber/step_mother'
require 'cucumber/cli'
require 'cucumber/cli/main'
require 'cucumber/broadcaster'
require 'cucumber/core_ext/exception'

Expand All @@ -24,7 +24,7 @@ def load_language(lang) #:nodoc:
Parser.load_parser(keyword_hash)
end

def language_complete?(lang)
def language_incomplete?(lang=@lang)
KEYWORD_KEYS.detect{|key| keyword_hash(lang)[key].nil?}
end

Expand Down
58 changes: 58 additions & 0 deletions lib/cucumber/cli/language_help_formatter.rb
@@ -0,0 +1,58 @@
require 'cucumber/formatter/pretty'

module Cucumber
module Cli2
class LanguageHelpFormatter < Formatter::Pretty
INCOMPLETE = %{
This language is incomplete. Please translate the missing words in
#{Cucumber::LANGUAGE_FILE}
Then contribute back to the Cucumber project. Details here:
http://wiki.github.com/aslakhellesoy/cucumber/spoken-languages
}

def self.list_languages(io)
raw = Cucumber::LANGUAGES.keys.sort.map do |lang|
[lang, Cucumber::LANGUAGES[lang]['name'], Cucumber::LANGUAGES[lang]['native']]
end
table = Ast::Table.new(raw)
new(nil, io, {:check_lang=>true}, '').visit_multiline_arg(table, :passed)
end

def self.list_keywords(io, lang)
Cucumber.load_language(lang)
raw = Cucumber::KEYWORD_KEYS.map do |key|
[key, Cucumber::LANGUAGES[lang][key]]
end
table = Ast::Table.new(raw)
new(nil, io, {:incomplete => Cucumber.language_incomplete?}, '').visit_multiline_arg(table, :passed)
end

def visit_multiline_arg(table, status)
if @options[:incomplete]
@io.puts(format_string(INCOMPLETE, :failed))
end
super
end

def visit_table_row(table_row, status)
@col = 1
super
end

def visit_table_cell_value(value, width, status)
if @col == 1
if(@options[:check_lang])
@incomplete = Cucumber.language_incomplete?(value)
end
status = :comment
elsif @incomplete
status = :failed
end

@col += 1
super(value, width, status)
end
end
end
end
51 changes: 13 additions & 38 deletions lib/cucumber/cli.rb → lib/cucumber/cli/main.rb
Expand Up @@ -3,11 +3,13 @@
require 'ostruct'
require 'cucumber/parser'
require 'cucumber/formatter'
require 'cucumber/cli/language_help_formatter'

module Cucumber
module Cli
class YmlLoadError < StandardError; end

class CLI
class Main
class << self
def step_mother=(step_mother)
@step_mother = step_mother
Expand Down Expand Up @@ -181,6 +183,10 @@ def parse_options!(args)

def execute!(step_mother)
Cucumber.load_language(@options[:lang])
if Cucumber.language_incomplete?
list_keywords(Cucumber.lang)
end

require_files
enable_diffing
features = load_plain_text_features
Expand Down Expand Up @@ -366,50 +372,19 @@ def enable_diffing
end

def list_languages
raw = Cucumber::LANGUAGES.keys.sort.map do |lang|
[lang, Cucumber::LANGUAGES[lang]['name'], Cucumber::LANGUAGES[lang]['native']]
end
print_lang_table(raw, {:check_lang=>true})
Cli2::LanguageHelpFormatter.list_languages(@out_stream)
Kernel.exit
end

def list_keywords(lang)
unless Cucumber::LANGUAGES[lang]
exit_with_error("No language with key #{v}")
end
raw = Cucumber::KEYWORD_KEYS.map do |key|
[key, Cucumber::LANGUAGES[lang][key]]
end
print_lang_table(raw, {})
end

def print_lang_table(raw, options)
table = Ast::Table.new(raw)
formatter = Formatter::Pretty.new(nil, @out_stream, options, '')

def formatter.visit_table_row(table_row, status)
@col = 1
super
exit_with_error("No language with key #{lang}")
end

def formatter.visit_table_cell_value(value, width, status)
if @col == 1
if(@options[:check_lang])
@incomplete = Cucumber.language_complete?(value)
end
status = :comment
elsif @incomplete
status = :failed
end

@col += 1
super(value, width, status)
end

formatter.indent = 0
formatter.visit_multiline_arg(table, :passed)
Cli2::LanguageHelpFormatter.list_keywords(@out_stream, lang)
Kernel.exit
end
end
end
end

Cucumber::CLI.step_mother = self
Cucumber::Cli::Main.step_mother = self
1 change: 1 addition & 0 deletions lib/cucumber/formatter/pretty.rb
Expand Up @@ -18,6 +18,7 @@ def initialize(step_mother, io, options, delim='|')
@io = io
@options = options
@delim = delim
@indent = 0
end

def visit_features(features)
Expand Down

0 comments on commit 6fb8b00

Please sign in to comment.