mvz-dnote 1.7.2 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,15 @@
1
- module DNote
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec/core/rake_task"
2
4
 
5
+ module DNote
3
6
  # = Developmer's Notes Rake Task
4
7
  #
5
8
  class RakeTask < Rake::TaskLib
6
-
7
- require 'rake/clean'
9
+ require "rake/clean"
8
10
 
9
11
  # Default note labels to looked for in source code.
10
- DEFAULT_LABELS = ['TODO', 'FIXME', 'OPTIMIZE', 'DEPRECATE']
12
+ DEFAULT_LABELS = %w[TODO FIXME OPTIMIZE DEPRECATE].freeze
11
13
 
12
14
  # File paths to search.
13
15
  attr_accessor :files
@@ -26,91 +28,87 @@ module DNote
26
28
 
27
29
  # Output directory to save notes file. Defaults to <tt>dnote/</tt> under
28
30
  # the project log directory (eg. <tt>log/dnote/</tt>).
29
- attr_accessor :output
31
+ attr_reader :output
30
32
 
31
33
  # Title to use if temaplte can use it.
32
34
  attr_accessor :title
33
35
 
34
- #
35
36
  def output=(path)
36
37
  @output = Pathname.new(path)
37
38
  end
38
39
 
39
- #
40
40
  def init
41
- require 'dnote'
42
- require 'dnote/format'
43
- @files = "**/*.rb"
44
- @output = 'log/dnote'
45
- @formats = ['index']
46
- @labels = nil #DEFAULT_LABELS
41
+ require "dnote"
42
+ require "dnote/format"
43
+ @files = "**/*.rb"
44
+ @output = "log/dnote"
45
+ @formats = ["index"]
46
+ @labels = nil
47
47
  end
48
48
 
49
- #
50
49
  def define
51
50
  desc "Collect Developer's Notes"
52
- task 'dnote' do
51
+ task "dnote" do
53
52
  document
54
53
  end
55
- task 'dnote:clobber' do
54
+ task "dnote:clobber" do
56
55
  clean
57
56
  end
58
- task :clobber => ['dnote:clobber']
57
+ task clobber: ["dnote:clobber"]
59
58
  end
60
59
 
61
60
  # Generate notes document(s).
62
61
  def document
63
62
  abort "dnote: #{output} is not a directory" unless output.directory?
64
63
 
65
- session = ::DNote::Session.new do |s|
66
- s.paths = files
67
- s.exclude = exclude
68
- s.ignore = ignore
69
- s.labels = labels #|| DEFAULT_LABELS
70
- s.title = title
71
- s.output = output
72
- s.dryrun = application.options.dryrun #trial?
64
+ session = new_session
65
+
66
+ formats.each do |format|
67
+ generate_document_for_format(session, format)
73
68
  end
69
+ end
74
70
 
71
+ # Remove output files.
72
+ def clean
75
73
  formats.each do |format|
76
- if format == 'index'
77
- session.format = 'html'
78
- session.output = File.join(self.output, 'index.html')
79
- else
80
- session.format = format
81
- end
82
- session.run
83
- report "Updated #{output.to_s.sub(Dir.pwd+'/','')}" unless trial?
74
+ clean_format format
84
75
  end
85
76
  end
86
77
 
87
- # Reset output directory, marking it as out-of-date.
88
- def reset
89
- #if File.directory?(output)
90
- File.utime(0,0,output) unless $NOOP
91
- puts "Marked #{output} as out-of-date"
92
- #end
78
+ private
79
+
80
+ def new_session
81
+ ::DNote::Session.new do |s|
82
+ s.paths = files
83
+ s.exclude = exclude
84
+ s.ignore = ignore
85
+ s.labels = labels
86
+ s.title = title
87
+ s.output = output
88
+ s.dryrun = application.options.dryrun # trial?
89
+ end
93
90
  end
94
91
 
95
- # Remove output files.
96
- def clean
97
- #if File.directory?(output)
98
- formats.each do |format|
99
- if format == 'index'
100
- file = (output + "index.html").to_s
101
- else
102
- ext = ::DNote::Format::EXTENSIONS[format] || format
103
- file = (output + "notes.#{ext}").to_s
104
- end
105
- rm(file)
106
- report "Removed #{output}"
107
- end
108
- #else
109
- # rm(output)
110
- # report "Removed #{output}"
111
- #end
92
+ def generate_document_for_format(session, format)
93
+ if format == "index"
94
+ session.format = "html"
95
+ session.output = File.join(output, "index.html")
96
+ else
97
+ session.format = format
98
+ end
99
+ session.run
100
+ report "Updated #{output.to_s.sub("#{Dir.pwd}/", "")}" unless trial?
112
101
  end
113
102
 
103
+ def clean_format(format)
104
+ if format == "index"
105
+ file = "#{output}index.html".to_s
106
+ else
107
+ ext = ::DNote::Format::EXTENSIONS[format] || format
108
+ file = (output + "notes.#{ext}").to_s
109
+ end
110
+ rm(file)
111
+ report "Removed #{output}"
112
+ end
114
113
  end
115
-
116
114
  end
data/lib/dnote/session.rb CHANGED
@@ -1,9 +1,11 @@
1
- module DNote
1
+ # frozen_string_literal: true
2
2
 
3
- require 'dnote/core_ext'
4
- require 'dnote/notes'
5
- require 'dnote/format'
3
+ require "dnote/core_ext"
4
+ require "dnote/notes"
5
+ require "dnote/format"
6
+ require "dnote/options"
6
7
 
8
+ module DNote
7
9
  # User session which is used by commandline interface.
8
10
  #
9
11
  # By making this a class it makes it easy for external
@@ -11,13 +13,12 @@ module DNote
11
13
  # calling the commandline, but without the need to shellout.
12
14
  #
13
15
  class Session
14
-
15
16
  # Directory relative to this script. This is used
16
17
  # to lookup the available format templates.
17
18
  DIR = File.dirname(__FILE__)
18
19
 
19
20
  # Default format.
20
- DEFAULT_FORMAT = "text"
21
+ DEFAULT_FORMAT = "text"
21
22
 
22
23
  # Default title.
23
24
  DEFAULT_TITLE = "Developer's Notes"
@@ -59,7 +60,7 @@ module DNote
59
60
 
60
61
  # String template for line URLs (mainly for HTML format). For example,
61
62
  # DNote uses GitHub so we could use a link template:
62
- #
63
+ #
63
64
  # "https://github.com/rubyworks/dnote/blob/master/%s#L%s"
64
65
  #
65
66
  attr_accessor :url
@@ -67,31 +68,31 @@ module DNote
67
68
  # Number of lines of context to display. The default is zero.
68
69
  attr_accessor :context
69
70
 
70
- private
71
+ private
71
72
 
72
73
  # New Session.
73
- def initialize(options={})
74
+ def initialize(options = {})
74
75
  options ||= {}
75
76
  initialize_defaults
76
- options.each{ |k,v| __send__("#{k}=", v) }
77
+ options.each { |k, v| __send__("#{k}=", v) }
77
78
  yield(self) if block_given?
78
79
  end
79
80
 
80
81
  # Set default values for attributes.
81
82
  def initialize_defaults
82
- @paths = []
83
- @labels = []
83
+ @paths = []
84
+ @labels = []
84
85
  @exclude = []
85
- @ignore = []
86
- @format = DEFAULT_FORMAT
87
- @title = DEFAULT_TITLE
88
- @dryrun = false
89
- @marker = nil
90
- @url = nil
86
+ @ignore = []
87
+ @format = DEFAULT_FORMAT
88
+ @title = DEFAULT_TITLE
89
+ @dryrun = false
90
+ @marker = nil
91
+ @url = nil
91
92
  @context = 0
92
93
  end
93
94
 
94
- public
95
+ public
95
96
 
96
97
  # Set exclude list ensuring that the value is an array.
97
98
  def exclude=(list)
@@ -105,13 +106,18 @@ module DNote
105
106
 
106
107
  # Run session.
107
108
  def run
108
- notes = Notes.new(files, :labels=>labels, :colon=>colon, :marker=>marker, :url=>url, :context=>context)
109
- formatter = Format.new(notes) do |f|
110
- f.format = format
111
- f.template = template
112
- f.title = title
113
- f.output = output
114
- end
109
+ notes = Notes.new(files,
110
+ labels: labels,
111
+ colon: colon,
112
+ marker: marker,
113
+ url: url,
114
+ context: context)
115
+ collection = notes.notes_collection
116
+ formatter = Format.new(collection,
117
+ format: format,
118
+ template: template,
119
+ title: title,
120
+ output: output)
115
121
  formatter.render
116
122
  end
117
123
 
@@ -120,150 +126,41 @@ module DNote
120
126
  # compile the list of files.
121
127
  def files
122
128
  list = [paths].flatten.compact
123
- list = ['**/*.rb'] if list.empty?
129
+ list = ["**/*.rb"] if list.empty?
124
130
  list = glob(list)
125
- list = list - glob(exclude)
131
+ list -= glob(exclude)
126
132
  list.reject do |path|
127
- path.split('/').any?{ |part| ignore.any?{ |ig| File.fnmatch?(ig, part) } }
133
+ path.split("/").any? { |part| ignore.any? { |ig| File.fnmatch?(ig, part) } }
128
134
  end
129
135
  end
130
136
 
131
- # Collect the file glob of each path given. If
137
+ # Collect the file glob of each path given. If
132
138
  # a path is a directory, inclue all content.
133
139
  def glob(paths)
134
140
  paths.map do |path|
135
141
  if File.directory?(path)
136
- Dir.glob(File.join(path, '**/*'))
142
+ Dir.glob(File.join(path, "**/*"))
137
143
  else
138
144
  Dir.glob(path)
139
145
  end
140
146
  end.flatten.uniq
141
147
  end
142
148
 
143
- # Set special labels.
144
- #def labels=(labels)
145
- # @labels = (
146
- # case labels
147
- # when String
148
- # labels.split(/[:;,]/)
149
- # else
150
- # labels = [labels].flatten.compact.uniq.map{ |s| s.to_s }
151
- # end
152
- # )
153
- #end
149
+ # List availble format templates
150
+ def list_templates
151
+ tdir = File.join(DIR, "templates")
152
+ tfiles = Dir[File.join(tdir, "**/*.erb")]
153
+ tnames = tfiles.map { |tname| tname.sub("#{tdir}/", "").chomp(".erb") }
154
+ groups = tnames.group_by { |tname| tname.split("/").first }
155
+ groups.sort.each do |(_type, names)|
156
+ puts("%-18s " * names.size % names.sort)
157
+ end
158
+ end
154
159
 
155
160
  # Commandline interface.
156
161
  def self.main(*argv)
157
- require 'optparse'
158
-
159
- session = Session.new
160
-
161
- opts = OptionParser.new do |opt|
162
- opt.banner = "DNote v#{DNote::VERSION}"
163
-
164
- opt.separator(" ")
165
- opt.separator("USAGE:\n dnote [OPTIONS] path1 [path2 ...]")
166
-
167
- opt.separator(" ")
168
- opt.separator("OUTPUT FORMAT: (choose one)")
169
-
170
- opt.on("--format", "-f NAME", "select a format [text]") do |format|
171
- session.format = format
172
- end
173
-
174
- opt.on("--custom", "-C FILE", "use a custom ERB template") do |file|
175
- session.format = 'custom'
176
- session.template = file
177
- end
178
-
179
- opt.on("--file", "shortcut for text/file format") do
180
- session.format = 'text/file'
181
- end
182
-
183
- opt.on("--list", "shortcut for text/list format") do
184
- session.format = 'text/list'
185
- end
186
-
187
- opt.separator(" ")
188
- opt.separator("OTHER OPTIONS:")
189
-
190
- opt.on("--label", "-l LABEL", "labels to collect") do |lbl|
191
- session.labels.concat(lbl.split(':'))
192
- end
193
-
194
- opt.on("--[no-]colon", "match labels with/without colon suffix") do |val|
195
- session.colon = val
196
- end
197
-
198
- opt.on("--marker", "-m MARK", "alternative remark marker") do |mark|
199
- session.marker = mark
200
- end
201
-
202
- opt.on("--url", "-u TEMPLATE", "url template for line entries (for HTML)") do |url|
203
- session.url = url
204
- end
205
-
206
- opt.on("--context", "-c INTEGER", "number of lines of context to display") do |int|
207
- session.context = int.to_i
208
- end
209
-
210
- opt.on("--exclude", "-x PATH", "exclude file or directory") do |path|
211
- session.exclude << path
212
- end
213
-
214
- opt.on("--ignore", "-i NAME", "ignore file based on any part of pathname") do |name|
215
- session.ignore << name
216
- end
217
-
218
- opt.on("--title", "-t TITLE", "title to use in header") do |title|
219
- session.title = title
220
- end
221
-
222
- opt.on("--output", "-o PATH", "save to file or directory") do |path|
223
- session.output = path
224
- end
225
-
226
- opt.on("--dryrun", "-n", "do not actually write to disk") do
227
- session.dryrun = true
228
- end
229
-
230
- opt.on("--debug", "debug mode") do
231
- $DEBUG = true
232
- $VERBOSE = true
233
- end
234
-
235
- opt.separator(" ")
236
- opt.separator("COMMAND OPTIONS:")
237
-
238
- opt.on_tail('--templates', "-T", "list available format templates") do
239
- tdir = File.join(DIR, 'templates')
240
- tfiles = Dir[File.join(tdir, '**/*.erb')]
241
- tnames = tfiles.map{ |tname| tname.sub(tdir+'/', '').chomp('.erb') }
242
- groups = tnames.group_by{ |tname| tname.split('/').first }
243
- groups.sort.each do |(type, names)|
244
- puts("%-18s " * names.size % names.sort)
245
- end
246
- exit
247
- end
248
-
249
- opt.on_tail('--help', '-h', "show this help information") do
250
- puts opt
251
- exit
252
- end
253
- end
254
-
255
- begin
256
- opts.parse!(argv)
257
- session.paths.replace(argv)
258
- session.run
259
- rescue => err
260
- raise err if $DEBUG
261
- puts err
262
- exit 1
263
- end
162
+ session = Options.parse(*argv)
163
+ session.run
264
164
  end
265
-
266
165
  end
267
-
268
166
  end
269
-
data/lib/dnote/version.rb CHANGED
@@ -1,17 +1,5 @@
1
- module DNote
2
- VERSION = '1.7.2'
3
-
4
- #
5
- def self.metadata
6
- @metadata ||= (
7
- require 'yaml'
8
- YAML.load_file(File.dirname(__FILE__) + '/../dnote.yml')
9
- )
10
- end
11
-
12
- #
13
- def self.const_missing(name)
14
- metadata[name.to_s.downcase] || super(name)
15
- end
1
+ # frozen_string_literal: true
16
2
 
3
+ module DNote
4
+ VERSION = "1.10.0"
17
5
  end
data/lib/dnote.rb CHANGED
@@ -1,4 +1,6 @@
1
- require 'dnote/version'
2
- require 'dnote/session'
1
+ # frozen_string_literal: true
2
+
3
+ require "dnote/version"
4
+ require "dnote/session"
3
5
 
4
6
  # TEST: This is a test of an arbitrary label.