Class: Features2Cards::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, out_stream = STDOUT, error_stream = STDERR) ⇒ CLI

Returns a new instance of CLI.



15
16
17
18
19
20
21
# File 'lib/features2cards/cli.rb', line 15

def initialize(args, out_stream = STDOUT, error_stream = STDERR)
  @args    = args  
  @out_stream   = out_stream
  @error_stream = error_stream
  @paths   = []
  @options = default_options      
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/features2cards/cli.rb', line 9

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/features2cards/cli.rb', line 8

def paths
  @paths
end

Class Method Details

.execute(args) ⇒ Object



11
12
13
# File 'lib/features2cards/cli.rb', line 11

def self.execute(args)
  new(args).execute!
end

Instance Method Details

#cardsObject



73
74
75
# File 'lib/features2cards/cli.rb', line 73

def cards
  features_to_cards(features)
end

#default_optionsObject



112
113
114
115
116
117
# File 'lib/features2cards/cli.rb', line 112

def default_options
  {
    :lang     => "en",
    :pdf_file => "cards.pdf"
  }
end

#execute!Object



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

def execute!    
  parse!(@args)
  load_cucumber(@options[:lang])
  if feature_files.empty?
    usage
    exit
  end
  
  generate_pdf(cards)
end

#feature_filesObject



83
84
85
86
87
88
89
90
# File 'lib/features2cards/cli.rb', line 83

def feature_files
  potential_feature_files = @paths.map do |path|
    path = path.gsub(/\\/, '/') # In case we're on windows. Globs don't work with backslashes.
    path = path.chomp('/')
    File.directory?(path) ? Dir["#{path}/**/*.feature"] : path
  end.flatten.uniq
  potential_feature_files
end

#featuresObject



77
78
79
80
81
# File 'lib/features2cards/cli.rb', line 77

def features
  feature_files.map do |file|
    parser.parse_file(file, {})
  end
end

#features_to_cards(features) ⇒ Object



97
98
99
100
101
# File 'lib/features2cards/cli.rb', line 97

def features_to_cards(features)
  features.map do |feature|
    [Card.for_feature(feature, @options[:lang])] 
  end.flatten
end

#generate_pdf(cards) ⇒ Object



103
104
105
# File 'lib/features2cards/cli.rb', line 103

def generate_pdf(cards)
  Prawn::Document.generate_cards(@options[:pdf_file], cards)
end

#load_cucumber(lang) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/features2cards/cli.rb', line 65

def load_cucumber(lang)
  Cucumber.load_language(lang)
  
  Cucumber::Ast::Feature.class_eval do
    attr_reader :scenarios
  end
end

#parse!(args) ⇒ Object



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
# File 'lib/features2cards/cli.rb', line 34

def parse!(args)
  @args = args
  @args.extend(::OptionParser::Arguable)      
  @args.options  do |opts|
    opts.banner = ["Usage: features2cards [options] [ [FILE|DIR] ]+", "",
      "Examples:",
      "features2cards features2cards.feature",
      "features2cards --language it examples/i18n/it",  
    ].join("\n")
    opts.on("-l [LANG]", "--language [LANG]",
      "Specify language for features (Default: #{@options[:lang]})") do |v|         
      @options[:lang] = v
    end
    opts.on("-o", "--out [FILE]",
      "Specify pdf output file (Default: #{@options[:pdf_file]}).") do |v|
      @options[:pdf_file] = v
    end
    opts.on_tail("--version", "Show version.") do
      @out_stream.puts VERSION::STRING
      Kernel.exit
    end
    opts.on_tail("-h", "--help", "You're looking at it.") do
      @out_stream.puts opts.help
      Kernel.exit
    end        
end.parse!

  # Whatever is left after option parsing is the FILE arguments
  @paths += args
end

#parserObject



93
94
95
# File 'lib/features2cards/cli.rb', line 93

def parser
  @parser ||= Cucumber::Parser::FeatureParser.new
end

#usageObject



107
108
109
110
# File 'lib/features2cards/cli.rb', line 107

def usage
  @error_stream.puts "ERROR: No feature files given"
  @error_stream.puts "Type 'features2cards --help' for usage."
end