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

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

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

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

  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



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

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)
      puts 'Canceled'
      Process.exit 0
    end
  end

  File.open(fname, 'w') do |f|
    f.puts note
    puts "{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?



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

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)
      puts 'Cancelled'
      Process.exit 0
    end
  end

  File.open(file, 'w') do |f|
    f.puts note
    puts "{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



46
47
48
# File 'lib/howzit/buildnote.rb', line 46

def edit
  edit_note
end

#edit_template(template) ⇒ Object

Public method to open a template in the editor

Parameters:

  • template (String)

    The template title



55
56
57
58
59
# File 'lib/howzit/buildnote.rb', line 55

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



66
67
68
69
70
71
72
73
# File 'lib/howzit/buildnote.rb', line 66

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

  @topics.filter do |topic|
    rx = term.to_rx
    topic.title.downcase =~ rx
  end
end

#grep(term) ⇒ Object

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

Parameters:

  • term (String)

    The search pattern



80
81
82
# File 'lib/howzit/buildnote.rb', line 80

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

#inspectObject

Inspect

Returns:

  • description



32
33
34
# File 'lib/howzit/buildnote.rb', line 32

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

#listString

Output a list of topic titles

Returns:

  • (String)

    formatted list of topics in build note



88
89
90
91
92
93
94
95
# File 'lib/howzit/buildnote.rb', line 88

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



112
113
114
# File 'lib/howzit/buildnote.rb', line 112

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

#list_runnableString

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

Returns:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/howzit/buildnote.rb', line 137

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



123
124
125
126
127
128
129
# File 'lib/howzit/buildnote.rb', line 123

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



103
104
105
# File 'lib/howzit/buildnote.rb', line 103

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

#note_fileString

Accessor method for note_file (path to located build note)

Returns:



311
312
313
# File 'lib/howzit/buildnote.rb', line 311

def note_file
  @note_file ||= find_note_file
end

#read_file(file) ⇒ Object

Read the help file contents

Parameters:

  • file (String)

    The filepath



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

def read_file(file)
  read_help_file(file)
end

#runObject

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



39
40
41
# File 'lib/howzit/buildnote.rb', line 39

def run
  process
end