class Metanorma::Cli::Generator

Attributes

doctype[R]
name[R]
options[R]
template[R]
type[R]

Public Class Methods

new(name, type:, doctype:, **options) click to toggle source
# File lib/metanorma/cli/generator.rb, line 9
def initialize(name, type:, doctype:, **options)
  @name = name
  @type = type
  @doctype = doctype
  @options = options
  @template = options.fetch(:template, nil)
end
run(name, type:, doctype:, **options) click to toggle source

Generator.run

This interface find / downloads the specified template and then run the generator to create a new metanorma document.

By default it usages the default templates but user can also provide a remote git teplate repo using –template ooption, and in that case it will use that template.

# File lib/metanorma/cli/generator.rb, line 41
def self.run(name, type:, doctype:, **options)
  new(name, **options.merge(type: type, doctype: doctype)).run
end

Public Instance Methods

run() click to toggle source
# File lib/metanorma/cli/generator.rb, line 17
def run
  if Cli.writable_templates_path?
    if name && document_path.exist?
      return unless overwrite?(document_path)
      document_path.rmtree
    end

    create_metanorma_document
  end

rescue Errno::EACCES
  permission_missing_error
end

Private Instance Methods

ask_to_confirm(document) click to toggle source
# File lib/metanorma/cli/generator.rb, line 125
def ask_to_confirm(document)
  UI.ask(
    "You've an existing document with the #{document.to_s}\n" \
    "Still want to continue, and overwrite the existing one? (yes/no):",
  ).downcase
end
base_templates() click to toggle source
# File lib/metanorma/cli/generator.rb, line 75
def base_templates
  base_template_root = Cli.base_templates_path
  build_template_hash(dir_files(base_template_root), base_template_root)
end
build_template_hash(elements, source_root, type = nil) click to toggle source
# File lib/metanorma/cli/generator.rb, line 96
def build_template_hash(elements, source_root, type = nil)
  type_template_path = [source_root, type].join("/")

  Hash.new.tap do |hash|
    elements.each do |element|
      hash[element] = element.gsub(type_template_path, "")
    end
  end
end
create_file(source, destination) click to toggle source
# File lib/metanorma/cli/generator.rb, line 106
def create_file(source, destination)
  target_path = [document_path, destination].join("/")
  target_path = Pathname.new(target_path)

  unless target_path.dirname.exist?
    FileUtils.mkdir_p(target_path.dirname)
  end

  file_creation_message(name, destination)
  FileUtils.copy_entry(source, target_path)
end
create_metanorma_document() click to toggle source
# File lib/metanorma/cli/generator.rb, line 53
def create_metanorma_document
  type_template = type_specific_template

  unless type_template.empty?
    templates = base_templates.merge(type_template)
    templates.each { |source, dest| create_file(source, dest) }
  else
    UI.say(
      "Unable to generate document:\n" \
      "Templates for type #{type.to_s} cannot be found -- please provide a valid `type` or a template URL"
    )
  end
end
custom_template() click to toggle source
# File lib/metanorma/cli/generator.rb, line 86
def custom_template
  if template
    if template !~ URI::regexp
      return Pathname.new(template)
    end

    Cli::GitTemplate.download(type, repo: template)
  end
end
dir_files(*arguments) click to toggle source
# File lib/metanorma/cli/generator.rb, line 118
def dir_files(*arguments)
  paths = [*arguments, "**", "**"].join("/")
  files = Pathname.glob(paths, File::FNM_DOTMATCH) - [".", " .."]

  files.reject(&:directory?).map(&:to_s)
end
document_path() click to toggle source
# File lib/metanorma/cli/generator.rb, line 49
def document_path
  @document_path ||= Pathname.pwd.join(name)
end
file_creation_message(document, destination) click to toggle source
# File lib/metanorma/cli/generator.rb, line 132
def file_creation_message(document, destination)
  UI.say("Creating #{[document, destination].join("/").gsub("//", "/")}")
end
find_standard_template(type) click to toggle source
# File lib/metanorma/cli/generator.rb, line 67
def find_standard_template(type)
  Cli::GitTemplate.find_or_download_by(type)
end
overwrite?(document_path) click to toggle source
# File lib/metanorma/cli/generator.rb, line 71
def overwrite?(document_path)
  options[:overwrite] == true || ask_to_confirm(document_path) === "yes"
end
permission_missing_error() click to toggle source
# File lib/metanorma/cli/generator.rb, line 136
def permission_missing_error
  UI.say(
    "Unable to generate document:\n" \
    "The current user does not have permission to write to this path:\n" \
    "#{Cli.templates_path}\n" \
    "Please ensure the path is writable by the current user, or\n" \
    "run Metanorma using a different user with write permissions."
  )
end
type_specific_template() click to toggle source
# File lib/metanorma/cli/generator.rb, line 80
def type_specific_template
  type_template_path = custom_template || find_standard_template(type)
  doctype_templates  = dir_files(type_template_path, doctype)
  build_template_hash(doctype_templates, type_template_path, doctype)
end