Class: DNote::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/dnote/session.rb

Overview

User session which is used by commandline interface.

By making this a class it makes it easy for external libraries to use this library just as if they were calling the commandline, but without the need to shellout.

Constant Summary collapse

DIR =

Directory relative to this script. This is used to lookup the available format templates.

File.dirname(__FILE__)
DEFAULT_FORMAT =

Default format.

"text"
DEFAULT_TITLE =

Default title.

"Developer's Notes"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Session (private)

New Session.

Yields:

  • (_self)

Yield Parameters:



73
74
75
76
77
78
# File 'lib/dnote/session.rb', line 73

def initialize(options={})
  options ||= {}
  initialize_defaults
  options.each{ |k,v| __send__("#{k}=", v) }
  yield(self) if block_given?
end

Instance Attribute Details

#colonObject

Selected labels can optionally do without the colon.



39
40
41
# File 'lib/dnote/session.rb', line 39

def colon
  @colon
end

#contextObject

Number of lines of context to display. The default is zero.



68
69
70
# File 'lib/dnote/session.rb', line 68

def context
  @context
end

#dryrunObject

If output path given, don’t actually write to disk.



58
59
60
# File 'lib/dnote/session.rb', line 58

def dryrun
  @dryrun
end

#excludeObject

Paths to exclude (match by pathname).



29
30
31
# File 'lib/dnote/session.rb', line 29

def exclude
  @exclude
end

#formatObject

Output format.



45
46
47
# File 'lib/dnote/session.rb', line 45

def format
  @format
end

#ignoreObject

Paths to ignore (match by basename).



32
33
34
# File 'lib/dnote/session.rb', line 32

def ignore
  @ignore
end

#labelsObject

Labels to lookup. By default these are TODO, FIXME and OPTIMIZE.



36
37
38
# File 'lib/dnote/session.rb', line 36

def labels
  @labels
end

#markerObject

Alternate remark marker. Useful to other languages besides Ruby.



42
43
44
# File 'lib/dnote/session.rb', line 42

def marker
  @marker
end

#outputObject

Output to a file instead of STDOUT.



55
56
57
# File 'lib/dnote/session.rb', line 55

def output
  @output
end

#pathsObject

Paths to include.



26
27
28
# File 'lib/dnote/session.rb', line 26

def paths
  @paths
end

#templateObject

If custom format, specify template file.



48
49
50
# File 'lib/dnote/session.rb', line 48

def template
  @template
end

#titleObject

Some format put a title at the top of the output. The default is “Developer’s Notes”.



52
53
54
# File 'lib/dnote/session.rb', line 52

def title
  @title
end

#urlObject

String template for line URLs (mainly for HTML format). For example, DNote uses GitHub so we could use a link template:

"https://github.com/rubyworks/dnote/blob/master/%s#L%s"


65
66
67
# File 'lib/dnote/session.rb', line 65

def url
  @url
end

Class Method Details

.main(*argv) ⇒ Object

Commandline interface.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/dnote/session.rb', line 156

def self.main(*argv)
  require 'optparse'

  session = Session.new

  opts = OptionParser.new do |opt|
    opt.banner = "DNote v#{DNote::VERSION}"

    opt.separator(" ")
    opt.separator("USAGE:\n  dnote [OPTIONS] path1 [path2 ...]")

    opt.separator(" ")
    opt.separator("OUTPUT FORMAT: (choose one)")

    opt.on("--format", "-f NAME", "select a format [text]") do |format|
      session.format = format
    end

    opt.on("--custom", "-C FILE", "use a custom ERB template") do |file|
      session.format = 'custom'
      session.template = file
    end

    opt.on("--file", "shortcut for text/file format") do
      session.format = 'text/file'
    end

    opt.on("--list", "shortcut for text/list format") do
      session.format = 'text/list'
    end

    opt.separator(" ")
    opt.separator("OTHER OPTIONS:")

    opt.on("--label", "-l LABEL", "labels to collect") do |lbl|
      session.labels.concat(lbl.split(':'))
    end

    opt.on("--[no-]colon", "match labels with/without colon suffix") do |val|
      session.colon = val
    end

    opt.on("--marker", "-m MARK", "alternative remark marker") do |mark|
       session.marker = mark 
    end

    opt.on("--url", "-u TEMPLATE", "url template for line entries (for HTML)") do |url|
       session.url = url
    end

    opt.on("--context", "-c INTEGER", "number of lines of context to display") do |int|
       session.context = int.to_i
    end

    opt.on("--exclude", "-x PATH", "exclude file or directory") do |path|
      session.exclude << path
    end

    opt.on("--ignore", "-i NAME", "ignore file based on any part of pathname") do |name|
      session.ignore << name
    end

    opt.on("--title", "-t TITLE", "title to use in header") do |title|
      session.title = title
    end

    opt.on("--output", "-o PATH", "save to file or directory") do |path|
      session.output = path
    end

    opt.on("--dryrun", "-n", "do not actually write to disk") do
      session.dryrun = true
    end

    opt.on("--debug", "debug mode") do
      $DEBUG = true
      $VERBOSE = true
    end

    opt.separator(" ")
    opt.separator("COMMAND OPTIONS:")

    opt.on_tail('--templates', "-T", "list available format templates") do
      tdir   = File.join(DIR, 'templates')
      tfiles = Dir[File.join(tdir, '**/*.erb')]
      tnames = tfiles.map{ |tname| tname.sub(tdir+'/', '').chomp('.erb') }
      groups = tnames.group_by{ |tname| tname.split('/').first }
      groups.sort.each do |(type, names)|
        puts("%-18s " * names.size % names.sort)
      end
      exit
    end

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

  begin
    opts.parse!(argv)
    session.paths.replace(argv)
    session.run
  rescue => err
    raise err if $DEBUG
    puts err
    exit 1
  end
end

Instance Method Details

#filesObject

Collect path globs and remove exclusions. This method uses #paths, #exclude and #ignore to compile the list of files.



121
122
123
124
125
126
127
128
129
# File 'lib/dnote/session.rb', line 121

def files
  list = [paths].flatten.compact
  list = ['**/*.rb'] if list.empty?
  list = glob(list)
  list = list - glob(exclude)
  list.reject do |path|
    path.split('/').any?{ |part| ignore.any?{ |ig| File.fnmatch?(ig, part) } }
  end
end

#glob(paths) ⇒ Object

Collect the file glob of each path given. If a path is a directory, inclue all content.



133
134
135
136
137
138
139
140
141
# File 'lib/dnote/session.rb', line 133

def glob(paths)
  paths.map do |path|
    if File.directory?(path)
      Dir.glob(File.join(path, '**/*'))
    else
      Dir.glob(path)
    end
  end.flatten.uniq
end

#initialize_defaultsObject (private)

Set default values for attributes.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dnote/session.rb', line 81

def initialize_defaults
  @paths   = []
  @labels  = []
  @exclude = []
  @ignore  = []
  @format  = DEFAULT_FORMAT
  @title   = DEFAULT_TITLE
  @dryrun  = false
  @marker  = nil
  @url     = nil
  @context = 0
end

#runObject

Run session.



107
108
109
110
111
112
113
114
115
116
# File 'lib/dnote/session.rb', line 107

def run
  notes = Notes.new(files, :labels=>labels, :colon=>colon, :marker=>marker, :url=>url, :context=>context)
  formatter = Format.new(notes) do |f|
    f.format   = format
    f.template = template
    f.title    = title
    f.output   = output
  end
  formatter.render
end