class KBuilder::BaseBuilder

Base builder defines builder methods, build method and configuration

Convention: Setter methods (are Fluent) and use the prefix set_

Getter methods (are NOT fluent) and return the stored value
Setter methods (are NOT fluent) can be created as needed
these methods would not be prefixed with the set_

Attributes

configuration[R]
target_folders[RW]
template_folders[RW]

Public Class Methods

build() click to toggle source

Factory method that provides a builder for a specified structure runs through a configuration block and then builds the final structure

@return [type=Object] data structure

# File lib/k_builder/base_builder.rb, line 23
def self.build
  init.build
end
init(configuration = nil) { |builder| ... } click to toggle source

Create and initialize the builder.

@return [Builder] Returns the builder via fluent interface

# File lib/k_builder/base_builder.rb, line 30
def self.init(configuration = nil)
  builder = new(configuration)

  yield(builder) if block_given?

  builder
end
new(configuration = nil) click to toggle source

assigns a builder hash and defines builder methods

# File lib/k_builder/base_builder.rb, line 39
def initialize(configuration = nil)
  configuration = KBuilder.configuration if configuration.nil?

  @configuration = configuration

  @target_folders = configuration.target_folders.clone
  @template_folders = configuration.template_folders.clone
end

Public Instance Methods

add_clipboard(**opts) click to toggle source

Add content to the clipboard

@option opts [String] :content Supply the content that you want to write to the file @option opts [String] :template Supply the template that you want to write to the file, template will be processed ('nobody') From address @option opts [String] :content_file File with content, file location is based on where the program is running @option opts [String] :template_file File with handlebars templated content that will be transformed, file location is based on the configured template_path

Extra options will be used as data for templates, e.g @option opts [String] :to Recipient email @option opts [String] :body The email's body

# File lib/k_builder/base_builder.rb, line 137
def add_clipboard(**opts)
  # move to command
  content = process_any_content(**opts)

  begin
    IO.popen('pbcopy', 'w') { |f| f << content }
  rescue Errno::ENOENT => e
    if e.message == 'No such file or directory - pbcopy'
      # May want to use this GEM in the future
      # https://github.com/janlelis/clipboard
      puts 'Clipboard paste is currently only supported on MAC'
    end
  end

  self
end
Also aliased as: clipboard_copy
add_file(file, **opts) click to toggle source

Add a file to the target location

@param [String] file The file name with or without relative path, eg. my_file.json or src/my_file.json @option opts [String] :content Supply the content that you want to write to the file @option opts [String] :template Supply the template that you want to write to the file, template will be processed ('nobody') From address @option opts [String] :content_file File with content, file location is based on where the program is running @option opts [String] :template_file File with handlebars templated content that will be transformed, file location is based on the configured template_path

Extra options will be used as data for templates, e.g @option opts [String] :to Recipient email @option opts [String] :body The email's body

# File lib/k_builder/base_builder.rb, line 98
def add_file(file, **opts)
  # move to command
  full_file = opts.key?(:folder_key) ? target_file(file, folder: opts[:folder_key]) : target_file(file)

  # Need logging options that can log these internal details
  FileUtils.mkdir_p(File.dirname(full_file))

  content = process_any_content(**opts)

  File.write(full_file, content)

  # Prettier needs to work with the original file name
  run_prettier file if opts.key?(:pretty)
  # Need support for rubocop -a

  self
end
Also aliased as: touch
add_target_folder(folder_key, value) click to toggle source

Fluent adder for target folder (KBuilder::NamedFolders)

# File lib/k_builder/base_builder.rb, line 185
def add_target_folder(folder_key, value)
  target_folders.add(folder_key, value)

  self
end
add_template_folder(folder_key, value) click to toggle source

Fluent adder for template folder (KBuilder::LayeredFolders)

# File lib/k_builder/base_builder.rb, line 231
def add_template_folder(folder_key, value)
  template_folders.add(folder_key, value)

  self
end
build() click to toggle source

@return [Hash/StrongType] Returns data object, can be a hash

or strong typed object that you
have wrapped around the hash
# File lib/k_builder/base_builder.rb, line 51
def build
  raise NotImplementedError
end
cd(folder_key)
Alias for: set_current_folder
clipboard_copy(**opts)
Alias for: add_clipboard
current_folder_key() click to toggle source
# File lib/k_builder/base_builder.rb, line 191
def current_folder_key
  target_folders.current
end
debug() click to toggle source
# File lib/k_builder/base_builder.rb, line 62
def debug
  log.subheading 'kbuilder'

  log.kv 'current folder key' , current_folder_key
  log.kv 'current folder'     , target_folder
  target_folders.debug(title: 'target_folders')

  log.info ''

  template_folders.debug(title: 'template folders (search order)')
  ''
end
find_template_file(file_parts) click to toggle source

Gets a template_file relative to the template folder, looks first in local template folder and if not found, looks in global template folder

# File lib/k_builder/base_builder.rb, line 244
def find_template_file(file_parts)
  template_folders.find_file(file_parts)
end
get_template_folder(folder_key) click to toggle source

Get for template folder

# File lib/k_builder/base_builder.rb, line 238
def get_template_folder(folder_key)
  template_folders.get(folder_key)
end
make_folder(folder_key = nil, sub_path: nil) click to toggle source
# File lib/k_builder/base_builder.rb, line 117
def make_folder(folder_key = nil, sub_path: nil)
  folder_key  = current_folder_key if folder_key.nil?
  folder      = target_folder(folder_key)
  folder      = File.join(folder, sub_path) unless sub_path.nil?

  FileUtils.mkdir_p(folder)

  self
end
process_any_content(**opts) click to toggle source

Process content will take any one of the following

- Raw content
- File based content
- Raw template (translated via handlebars)
- File base template (translated via handlebars)

Process any of the above inputs to create final content output

@option opts [String] :content Supply the content that you want to write to the file @option opts [String] :template Supply the template that you want to write to the file, template will be transformed using handlebars @option opts [String] :content_file File with content, file location is based on where the program is running @option opts [String] :template_file File with handlebars templated content that will be transformed, file location is based on the configured template_path

# File lib/k_builder/base_builder.rb, line 313
def process_any_content(**opts)
  raw_content = use_content(**opts)

  return raw_content if raw_content

  template_content = use_template(**opts)

  Handlebars::Helpers::Template.render(template_content, opts) unless template_content.nil?
end
rc(command)
Alias for: run_command
run_command(command) click to toggle source
# File lib/k_builder/base_builder.rb, line 339
def run_command(command)
  # Deep path create if needed
  tf = target_folder

  FileUtils.mkdir_p(tf)

  build_command = "cd #{tf} && #{command}"

  puts build_command

  # need to support the fork process options as I was not able to run
  # k_builder_watch -n because it hid all the following output
  system(build_command)
end
Also aliased as: rc
run_cop(file, **opts) click to toggle source
# File lib/k_builder/base_builder.rb, line 323
def run_cop(file, **opts)
  command = Commands::RuboCopCommand.new(file, builder: self, **opts)
  command.execute

  self
end
run_prettier(file, log_level: :log) click to toggle source

Need to handle absolute files, see /Users/davidcruwys/dev/printspeak/reference_application/printspeak-domain/.builders/presentation/presentation_builder/commands/copy_ruby_resource_command.rb

# File lib/k_builder/base_builder.rb, line 332
def run_prettier(file, log_level: :log)
  # command = "prettier --check #{file} --write #{file}"
  command = "npx prettier --loglevel #{log_level} --write #{file}"

  run_command command
end
set_current_folder(folder_key) click to toggle source

Target folders and files


# File lib/k_builder/base_builder.rb, line 177
def set_current_folder(folder_key)
  target_folders.current = folder_key

  self
end
Also aliased as: cd
target_file(*file_parts, folder: current_folder_key) click to toggle source

Get target file

If you provide a relative folder, then it will be relative to the :folder parameter

If the :folder is not set, then it will be relative to the current folder

@examples

target_file('abc.txt')
target_file('xyz/abc.txt')
target_file('xyz', 'abc.txt')

If you provide an absolute folder, then it will ignore the :folder parameter

@examples

target_file('/abc.txt')
target_file('/xyz/abc.txt')
target_file('/xyz', 'abc.txt')
# File lib/k_builder/base_builder.rb, line 219
def target_file(*file_parts, folder: current_folder_key)
  # Absolute path
  return File.join(*file_parts) if Pathname.new(file_parts.first).absolute?

  # Relative to :folder
  File.join(target_folder(folder), *file_parts)
end
target_folder(folder_key = current_folder_key) click to toggle source

Get target folder by folder_key

If folder_key not supplied then get the current target folder

# File lib/k_builder/base_builder.rb, line 198
def target_folder(folder_key = current_folder_key)
  target_folders.get(folder_key)
end
to_h() click to toggle source
# File lib/k_builder/base_builder.rb, line 55
def to_h
  {
    target_folders: target_folders.to_h,
    template_folders: template_folders.to_h
  }
end
touch(file, **opts)
Alias for: add_file
use_content(**opts) click to toggle source

Use content from a a selection of content sources

@option opts [String] :content Just pass through the :content as is. @option opts [String] :content_file Read content from the :content_file

Future options @option opts [String] :content_loren [TODO]Create Loren Ipsum text as a :content_loren count of words @option opts [String] :content_url Read content from the :content_url

@return Returns some content

# File lib/k_builder/base_builder.rb, line 261
def use_content(**opts)
  return opts[:content] unless opts[:content].nil?

  return unless opts[:content_file]

  # NOTE: when using content file, you still want to search for it in the template folders, I THINK?
  cf = find_template_file(opts[:content_file])

  return "content not found: #{opts[:content_file]}" if cf.nil?

  # cf = opts[:content_file]

  # unless File.exist?(cf)
  #   cf_from_template_folders = find_template_file(cf)
  #   return "Content not found: #{File.expand_path(cf)}" unless File.exist?(cf_from_template_folders)

  #   cf = cf_from_template_folders
  # end

  File.read(cf)
end
use_template(**opts) click to toggle source

Use template from a a selection of template sources

@option opts [String] :template Just pass through the :template as is. @option opts [String] :template_file Read template from the :template_file

@return Returns some template

# File lib/k_builder/base_builder.rb, line 289
def use_template(**opts)
  return opts[:template] unless opts[:template].nil?

  return unless opts[:template_file]

  tf = find_template_file(opts[:template_file])

  return "template not found: #{opts[:template_file]}" if tf.nil?

  File.read(tf)
end
vscode(*file_parts, folder: current_folder_key) click to toggle source
# File lib/k_builder/base_builder.rb, line 155
def vscode(*file_parts, folder: current_folder_key)
  # move to command
  file = target_file(*file_parts, folder: folder)

  rc "code #{file}"

  self
end