Class: QED::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/qed/command.rb

Overview

QED Commandline Tool

Constant Summary collapse

CONFDIR =

Configuration directory.

"{.,}config/qed"
DEFAULT_DEMOS_LOCATION =

Default location of demonstrations if no specific files or locations given. This is use in Dir.glob.

'{qed}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

TODO: Should extension and profile have a common reference?



63
64
65
66
67
68
69
70
71
# File 'lib/qed/command.rb', line 63

def initialize
  @format    = :dotprogress
  @extension = :default
  @profile   = :default
  @requires  = []
  @loadpath  = []
  @files     = []
  @options   = {}
end

Instance Attribute Details

#extensionObject

Returns the value of attribute extension.



59
60
61
# File 'lib/qed/command.rb', line 59

def extension
  @extension
end

#filesObject

Files to be run.



45
46
47
# File 'lib/qed/command.rb', line 45

def files
  @files
end

#formatObject

Ouput format.



28
29
30
# File 'lib/qed/command.rb', line 28

def format
  @format
end

#loadpathObject

Returns the value of attribute loadpath.



53
54
55
# File 'lib/qed/command.rb', line 53

def loadpath
  @loadpath
end

#optionsObject (readonly)

Command-line options.



42
43
44
# File 'lib/qed/command.rb', line 42

def options
  @options
end

#profileObject (readonly)

Options defined by selected profile.



39
40
41
# File 'lib/qed/command.rb', line 39

def profile
  @profile
end

#requiresObject

Returns the value of attribute requires.



56
57
58
# File 'lib/qed/command.rb', line 56

def requires
  @requires
end

#traceObject (readonly)

Trace execution?



36
37
38
# File 'lib/qed/command.rb', line 36

def trace
  @trace
end

Class Method Details

.executeObject

Initialize and execute.



23
24
25
# File 'lib/qed/command.rb', line 23

def self.execute
  new.execute
end

Instance Method Details

#demosObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/qed/command.rb', line 160

def demos
  files = self.files
  types = Tilt.mappings.keys
  if files.empty?
    files << DEFAULT_DEMOS_LOCATION
  end
  files = files.map do |pattern|
    Dir[pattern]
  end.flatten.uniq
  files = files.map do |file|
    if File.directory?(file)
      Dir[File.join(file,'**','*.{' + types.join(',') + '}')]
    else
      file
    end
  end
  files = files.flatten.uniq.sort
  #files = files.select do |file| 
  #  %w{.yml .yaml .rb}.include?(File.extname(file))
  #end
  files
end

#executeObject

Run demonstrations.



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/qed/command.rb', line 212

def execute
  parse

  abort "No documents." if demos.empty?

  prepare_loadpath

  require_libraries
  require_profile

  session.run
end

#optsObject

Instance of OptionParser



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
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/qed/command.rb', line 75

def opts
  @opts ||= OptionParser.new do |opt|

    opt.separator("Custom Profiles:") unless profiles.empty?

    profiles.each do |name, value|
      o = "--#{name}"
      opt.on(o, "#{name} custom profile") do
        @profile = name
      end
    end

    opt.separator("Report Formats (pick one):")

    opt.on('--dotprogress', '-d', "use dot-progress reporter [default]") do
      @options[:format] = :dotprogress
    end

    opt.on('--verbatim', '-v', "use verbatim reporter") do
      @options[:format] = :verbatim
    end

    opt.on('--bullet', '-b', "use bullet-point reporter") do
      @options[:format] = :bullet
    end

    opt.on('--html', '-h', "use underlying HTML reporter") do
      @options[:format] = :html
    end

    opt.on('--format', '-f FORMAT', "use custom reporter") do |format|
      @options[:format] = format
    end

    #opt.on('--script', "psuedo-reporter") do
    #  @options[:format] = :script  # psuedo-reporter
    #end

    opt.separator("Control Options:")

    opt.on('--ext', '-e [NAME]', "runtime extension [default]") do |name|
      @options[:extension] = name
    end

    opt.on('--loadpath', "-I PATH", "add paths to $LOAD_PATH") do |arg|
      @options[:loadpath] ||= []
      @options[:loadpath].concat(arg.split(/[:;]/).map{ |dir| File.expand_path(dir) })
    end

    opt.on('--require', "-r", "require library") do |arg|
      @options[:requires] ||= []
      @options[:requires].concat(arg.split(/[:;]/)) #.map{ |dir| File.expand_path(dir) })
    end

    opt.on('--trace', '-t', "show full backtraces for exceptions") do
      @options[:trace] = true
    end

    opt.on('--debug', "exit immediately upon raised exception") do
      $VERBOSE = true # wish this were called $WARN
      $DEBUG = true
    end

    opt.separator("Optional Commands:")

    opt.on_tail('--version', "display version") do
      puts "QED #{VERSION}"
      exit
    end

    opt.on_tail('--copyright', "display copyrights") do
      puts "Copyright (c) 2008, 2009 Thomas Sawyer, GPL License"
      exit
    end

    opt.on_tail('--help', '-h', "display this help message") do
      puts opt
      exit
    end

  end
end

#parseObject

Parse command-line options along with profile options.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/qed/command.rb', line 191

def parse
  @files = []
  argv = ARGV.dup
  opts.parse!(argv)
  @files.concat(argv)

  #if profile
  if args = profiles[profile]
    argv = Shellwords.shellwords(args)
    opts.parse!(argv)
    @files.concat(argv)
  end
  #end

  options.each do |k,v|
    __send__("#{k}=", v)
  end
end

#prepare_loadpathObject

Add to load path (from -I option).



236
237
238
# File 'lib/qed/command.rb', line 236

def prepare_loadpath
  loadpath.each{ |dir| $LOAD_PATH.unshift(dir) }
end

#profilesObject

Profile configurations.



227
228
229
230
231
232
# File 'lib/qed/command.rb', line 227

def profiles
  @profiles ||= (
    file = Dir["#{CONFDIR}/profile{,s}.{yml,yaml}"].first
    file ? YAML.load(File.new(file)) : {}
  )
end

#require_librariesObject

Require libraries (from -r option).



242
243
244
# File 'lib/qed/command.rb', line 242

def require_libraries
  requires.each{ |file| require(file) }
end

#require_profileObject

Require requirement file (from -e option).



248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/qed/command.rb', line 248

def require_profile
  return unless root

  # common environment, always loaded if present.
  #if file = Dir["#{root}/#{CONFDIR}/default.rb"].first
  #  require(file)
  #end

  #env = env() || 'default'

  if file = Dir["#{root}/#{CONFDIR}/#{extension}.rb"].first
    require(file)
  end
end

#rootObject



264
265
266
# File 'lib/qed/command.rb', line 264

def root
  QED.root
end

#sessionObject

Session instance.



185
186
187
# File 'lib/qed/command.rb', line 185

def session
  @session ||= Session.new(demos, :format=>format, :trace=>trace)
end