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

Parameters:

  • file (String) (defaults to: nil)

    The path to the build note file



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.

  @metadata = 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 
  @metadata
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



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
267
268
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
# File 'lib/howzit/buildnote.rb', line 227

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.ask("{bw}Project name:{x}".c, default: title)
    printf "{bw}Project name {xg}[#{title}]{bw}: {x}".c
    input = $stdin.gets.chomp
    title = input unless input.empty?
  end
  summary = ''
  unless default
    printf '{bw}Project summary: {x}'.c
    input = $stdin.gets.chomp
    summary = input unless input.empty?
  end

  fname = 'buildnotes.md'
  unless default
    printf "{bw}Build notes filename (must begin with 'howzit' or 'build')\n{xg}[#{fname}]{bw}: {x}".c
    input = $stdin.gets.chomp
    fname = input unless input.empty?
  end

  note = <<~EOBUILDNOTES
    # #{title}

    #{summary}

    ## File Structure

    Where are the main editable files? Is there a dist/build folder that should be ignored?

    ## Build

    What build system/parameters does this use?

    @run(./build command)

    ## Deploy

    What are the procedures/commands to deploy this project?

    ## Other

    Version control notes, additional gulp/rake/make/etc tasks...

  EOBUILDNOTES

  if File.exist?(fname) && !default
    file = "{by}#{fname}".c
    unless Prompt.yn("Are you absolutely sure you want to overwrite #{file}", default: false)
      Howzit.console.info('Canceled')
      Process.exit 0
    end
  end

  File.open(fname, 'w') do |f|
    f.puts note
    Howzit.console.info("{by}Build notes for {bw}#{title}{by} written to {bw}#{fname}{x}".c)
  end

  if File.exist?(fname) && !default && Prompt.yn("{bg}Do you want to open {bw}#{fname} {bg}for editing?{x}".c,
                                                 default: false)
    edit_note
  end

  Process.exit 0
end

#create_template_file(file, prompt: false) ⇒ Object

Create a template file

Parameters:

  • file (String)

    file path

  • prompt (Boolean) (defaults to: false)

    confirm file creation?



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

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 = <<~EOBUILDNOTES
    # #{title}

    ## Template Topic

  EOBUILDNOTES

  if File.exist?(file) && !default
    file = "{by}#{file}".c
    unless Prompt.yn("Are you sure you want to overwrite #{file}", default: false)
      Howzit.console.info('Cancelled')
      Process.exit 0
    end
  end

  File.open(file, 'w') do |f|
    f.puts note
    Howzit.console.info("{by}Template {bw}#{title}{by} written to {bw}#{file}{x}".c)
  end

  if File.exist?(file) && !default && Prompt.yn("{bg}Do you want to open {bw}#{file} {bg}for editing?{x}".c,
                                                default: false)
    edit_template_file(file)
  end

  Process.exit 0
end

#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

Parameters:

  • template (String)

    The template title



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

Parameters:

  • term (String) (defaults to: nil)

    The search term



70
71
72
73
74
75
76
# File 'lib/howzit/buildnote.rb', line 70

def find_topic(term = nil)
  return @topics if term.nil?
  @topics.filter do |topic|
    rx = term.to_rx
    topic.title.downcase.sub(/ *\(.*?\) *$/, '') =~ rx
  end
end

#grep(term) ⇒ Object

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

Parameters:

  • term (String)

    The search pattern



94
95
96
# File 'lib/howzit/buildnote.rb', line 94

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

#hookObject

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



81
82
83
84
85
86
87
# File 'lib/howzit/buildnote.rb', line 81

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

Returns:

  • description



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

Returns:

  • (String)

    formatted list of topics in build note



102
103
104
105
106
107
108
109
# File 'lib/howzit/buildnote.rb', line 102

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

Returns:

  • (String)

    newline-separated list of topic titles



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

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

#list_runnableString

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

Returns:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/howzit/buildnote.rb', line 150

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?

    output.push("- {bw}#{topic.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

Returns:

  • (String)

    newline-separated list of topic titles



136
137
138
139
140
141
142
# File 'lib/howzit/buildnote.rb', line 136

def list_runnable_completions
  output = []
  @topics.each do |topic|
    output.push(topic.title) if topic.tasks.count.positive?
  end
  output.join("\n")
end

#list_topicsArray

Return an array of topic titles

Returns:

  • (Array)

    array of topic titles



116
117
118
# File 'lib/howzit/buildnote.rb', line 116

def list_topics
  @topics.map { |topic| topic.title }
end

#note_fileString

Accessor method for note_file (path to located build note)

Returns:



324
325
326
# File 'lib/howzit/buildnote.rb', line 324

def note_file
  @note_file ||= find_note_file
end

#read_file(file) ⇒ Object

Read the help file contents

Parameters:

  • file (String)

    The filepath



173
174
175
# File 'lib/howzit/buildnote.rb', line 173

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