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_DEMO_LOCATION =

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

'{demo,demos}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

TODO: Should extension and profile have a common reference?



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

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

Instance Attribute Details

#extensionObject

Returns the value of attribute extension.



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

def extension
  @extension
end

#filesObject

Files to be run.



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

def files
  @files
end

#formatObject

Ouput format.



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

def format
  @format
end

#loadpathObject

Returns the value of attribute loadpath.



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

def loadpath
  @loadpath
end

#optionsObject (readonly)

Command-line options.



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

def options
  @options
end

#profileObject (readonly)

Options defined by selected profile.



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

def profile
  @profile
end

#requiresObject

Returns the value of attribute requires.



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

def requires
  @requires
end

#traceObject (readonly)

Trace execution?



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

def trace
  @trace
end

Class Method Details

.executeObject

Initialize and execute.



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

def self.execute
  new.execute
end

Instance Method Details

#demosObject



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

def demos
  files = self.files
  types = %w{qed rdoc md markdown} #Tilt.mappings.keys
  if files.empty?
    files << DEFAULT_DEMO_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.



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

def execute
  parse

  abort "No documents." if demos.empty?

  prepare_loadpath

  require_libraries
  require_profile

  session.run
end

#optsObject

Instance of OptionParser



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

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.



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

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).



234
235
236
# File 'lib/qed/command.rb', line 234

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

#profilesObject

Profile configurations.



225
226
227
228
229
230
# File 'lib/qed/command.rb', line 225

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).



240
241
242
# File 'lib/qed/command.rb', line 240

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

#require_profileObject

Require requirement file (from -e option).



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

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



262
263
264
# File 'lib/qed/command.rb', line 262

def root
  QED.root
end

#sessionObject

Session instance.



183
184
185
# File 'lib/qed/command.rb', line 183

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