Class: Howzit::BuildNote

Inherits:
Object
  • Object
show all
Defined in:
lib/howzit/buildnote.rb

Overview

BuildNote Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, meta: nil) ⇒ BuildNote

Initialize a build note



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/howzit/buildnote.rb', line 15

def initialize(file: nil, meta: nil)
  file ||= note_file

  @topics = []
  create_note(prompt: true) if file.nil?

  content = Util.read_file(file)
  raise "{br}No content found in build note (#{file}){x}".c if content.nil? || content.empty?

  this_meta = content.split(/^#/)[0].strip.

   = meta.nil? ? this_meta : meta.merge(this_meta)

  read_help(file)
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/howzit/buildnote.rb', line 8

def 
  
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/howzit/buildnote.rb', line 8

def title
  @title
end

#topicsObject

Returns the value of attribute topics.



6
7
8
# File 'lib/howzit/buildnote.rb', line 6

def topics
  @topics
end

Instance Method Details

#create_note(prompt: false) ⇒ Object

Create a buildnotes skeleton



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/howzit/buildnote.rb', line 269

def create_note(prompt: false)
  trap('SIGINT') do
    Howzit.console.info "\nCancelled"
    exit!
  end

  default = !$stdout.isatty || Howzit.options[:default]

  if prompt && !default
    res = Prompt.yn('No build notes file found, create one?', default: true)
    Process.exit 0 unless res
  end

  # First make sure there isn't already a buildnotes file
  if note_file
    fname = "{by}#{note_file}{bw}".c
    unless default
      res = Prompt.yn("#{fname} exists and appears to be a build note, continue anyway?", default: false)
      Process.exit 0 unless res
    end
  end

  title = File.basename(Dir.pwd)
  # prompt = TTY::Prompt.new
  if default
    input = title
  else
    title = Prompt.get_line('{bw}Project name{x}'.c, default: title)
  end
  summary = ''
  unless default
    summary = Prompt.get_line('{bw}Project summary{x}'.c)
  end

  # Template selection
  selected_templates = []
   = {}
  unless default
    selected_templates,  = select_templates_for_note(title)
  end

  fname = 'buildnotes.md'
  unless default
    fname = Prompt.get_line("{bw}Build notes filename{x}\n(must begin with 'howzit' or 'build')".c, default: fname)
  end

  # Build metadata section
   = []
  unless selected_templates.empty?
     << "template: #{selected_templates.join(',')}"
  end
  .each do |key, value|
     << "#{key}: #{value}"
  end
   = .empty? ? '' : "#{metadata_lines.join("\n")}\n\n"

  note = "    \#{metadata_section}# \#{title}\n\n    \#{summary}\n\n    ## File Structure\n\n    Where are the main editable files? Is there a dist/build folder that should be ignored?\n\n    ## Build\n\n    What build system/parameters does this use?\n\n    @run(./build command)\n\n    ## Deploy\n\n    What are the procedures/commands to deploy this project?\n\n    ## Other\n\n    Version control notes, additional gulp/rake/make/etc tasks...\n\n  EOBUILDNOTES\n\n  if File.exist?(fname) && !default\n    file = \"{by}\#{fname}\".c\n    unless Prompt.yn(\"Are you absolutely sure you want to overwrite \#{file}\", default: false)\n      Howzit.console.info('Canceled')\n      Process.exit 0\n    end\n  end\n\n  File.open(fname, 'w') do |f|\n    f.puts note\n    Howzit.console.info(\"{by}Build notes for {bw}\#{title}{by} written to {bw}\#{fname}{x}\".c)\n  end\n\n  if File.exist?(fname) && !default && Prompt.yn(\"{bg}Do you want to open {bw}\#{fname} {bg}for editing?{x}\".c,\n                                                 default: false)\n    edit_note\n  end\n\n  Process.exit 0\nend\n"

#create_template_file(file, prompt: false) ⇒ Object

Create a template file



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
265
266
# File 'lib/howzit/buildnote.rb', line 225

def create_template_file(file, prompt: false)
  trap('SIGINT') do
    Howzit.console.info "\nCancelled"
    exit!
  end

  default = !$stdout.isatty || Howzit.options[:default]

  if prompt && !default && !File.exist?(file)
    res = Prompt.yn("{bg}Template {bw}#{File.basename(file)}{bg} not found, create it?{x}".c, default: true)
    Process.exit 0 unless res
  end

  title = File.basename(file, '.md')

  note = "    # \#{title}\n\n    ## Template Topic\n\n  EOBUILDNOTES\n\n  if File.exist?(file) && !default\n    file = \"{by}\#{file}\".c\n    unless Prompt.yn(\"Are you sure you want to overwrite \#{file}\", default: false)\n      Howzit.console.info('Cancelled')\n      Process.exit 0\n    end\n  end\n\n  File.open(file, 'w') do |f|\n    f.puts note\n    Howzit.console.info(\"{by}Template {bw}\#{title}{by} written to {bw}\#{file}{x}\".c)\n  end\n\n  if File.exist?(file) && !default && Prompt.yn(\"{bg}Do you want to open {bw}\#{file} {bg}for editing?{x}\".c,\n                                                default: false)\n    edit_template_file(file)\n  end\n\n  Process.exit 0\nend\n"

#editObject

Public method to open build note in editor



50
51
52
# File 'lib/howzit/buildnote.rb', line 50

def edit
  edit_note
end

#edit_template(template) ⇒ Object

Public method to open a template in the editor



59
60
61
62
63
# File 'lib/howzit/buildnote.rb', line 59

def edit_template(template)
  file = template.sub(/(\.md)?$/i, '.md')
  file = File.join(Howzit.config.template_folder, file)
  edit_template_file(file)
end

#find_topic(term = nil) ⇒ Object

Find a topic based on a fuzzy match



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/howzit/buildnote.rb', line 70

def find_topic(term = nil)
  return @topics if term.nil?

  rx = term.to_rx

  @topics.filter do |topic|
    title = topic.title.downcase.sub(/ *\(.*?\) *$/, '')
    match = title =~ rx

    if !match && term =~ /[,:]/
      normalized = title.gsub(/\s*([,:])\s*/, '\1')
      match = normalized =~ rx
    end

    match
  end
end

#find_topic_exact(term = nil) ⇒ Array

Find a topic with an exact whole-word match



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/howzit/buildnote.rb', line 95

def find_topic_exact(term = nil)
  return [] if term.nil?

  @topics.filter do |topic|
    title = topic.title.downcase.sub(/ *\(.*?\) *$/, '').strip
    # Split both the title and search term into words
    title_words = title.split
    search_words = term.split

    # Check if all search words match the title words exactly (case-insensitive)
    search_words.map(&:downcase) == title_words.map(&:downcase)
  end
end

#grep(term) ⇒ Object

Call grep on all topics, filtering out those that don’t match



125
126
127
# File 'lib/howzit/buildnote.rb', line 125

def grep(term)
  @topics.filter { |topic| topic.grep(term) }
end

#hookObject

Copy a link to the main build note file to clipboard (macOS only)



112
113
114
115
116
117
118
# File 'lib/howzit/buildnote.rb', line 112

def hook
  title = Util.read_file(note_file).note_title(note_file, 20)
  title = "#{title} project notes"
  url = "[#{title}](file://#{note_file})"
  Util.os_copy(url)
  Howzit.console.info('Link copied to clipboard.')
end

#inspectObject

Inspect



36
37
38
# File 'lib/howzit/buildnote.rb', line 36

def inspect
  "#<Howzit::BuildNote @topics=[#{@topics.count}]>"
end

#listString

Output a list of topic titles



133
134
135
136
137
138
139
140
# File 'lib/howzit/buildnote.rb', line 133

def list
  output = []
  output.push("{bg}Topics:{x}\n".c)
  @topics.each do |topic|
    output.push("- {bw}#{topic.title}{x}".c)
  end
  output.join("\n")
end

#list_completionsString

Return a list of topic titles suitable for shell completion



160
161
162
# File 'lib/howzit/buildnote.rb', line 160

def list_completions
  list_topics.join("\n")
end

#list_runnableString

Return a formatted list of topics containing directives suitable for console output



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

def list_runnable
  output = []
  output.push(%({bg}"Runnable" Topics:{x}\n).c)

  find_topic(Howzit.options[:for_topic]).each do |topic|
    s_out = []

    topic.tasks.each { |task| s_out.push(task.to_list) }

    next if s_out.empty?

    title = topic.title
    title += " {dy}({xy}#{topic.named_args.keys.join(', ')}{dy}){x}" unless topic.named_args.empty?

    output.push("- {g}#{title}{x}".c)
    output.push(s_out.join("\n"))
  end

  output.join("\n")
end

#list_runnable_completionsString

Return a list of topics containing @directives, suitable for shell completion



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/howzit/buildnote.rb', line 171

def list_runnable_completions
  output = []
  @topics.each do |topic|
    next unless topic.tasks.count.positive?

    title = topic.title
    title += "(#{topic.named_args.keys.join(', ')})" unless topic.named_args.empty?
    output.push(title)
  end
  output.join("\n")
end

#list_topicsArray

Return an array of topic titles



147
148
149
150
151
152
153
# File 'lib/howzit/buildnote.rb', line 147

def list_topics
  @topics.map do |topic|
    title = topic.title
    title += "(#{topic.named_args.keys.join(', ')})" unless topic.named_args.empty?
    title
  end
end

#note_fileString

Accessor method for note_file (path to located build note)



376
377
378
# File 'lib/howzit/buildnote.rb', line 376

def note_file
  @note_file ||= find_note_file
end

#read_file(file) ⇒ Object

Read the help file contents



215
216
217
# File 'lib/howzit/buildnote.rb', line 215

def read_file(file)
  read_help_file(file)
end

#runObject

Public method to begin processing the build note based on command line options



43
44
45
# File 'lib/howzit/buildnote.rb', line 43

def run
  process
end