Module: Metanorma::Cli

Defined in:
lib/metanorma/cli.rb,
lib/metanorma/cli/ui.rb,
lib/metanorma/cli/errors.rb,
lib/metanorma/cli/command.rb,
lib/metanorma/cli/version.rb,
lib/metanorma/cli/compiler.rb,
lib/metanorma/cli/generator.rb,
lib/metanorma/cli/collection.rb,
lib/metanorma/cli/git_template.rb,
lib/metanorma/cli/commands/site.rb,
lib/metanorma/cli/template_repo.rb,
lib/metanorma/cli/site_generator.rb,
lib/metanorma/cli/commands/config.rb,
lib/metanorma/cli/thor_with_config.rb,
lib/metanorma/cli/commands/template_repo.rb

Defined Under Namespace

Modules: Commands, Errors Classes: Collection, Command, Compiler, Generator, GitTemplate, SiteGenerator, TemplateRepo, ThorWithConfig, UI

Constant Summary collapse

CONFIG_DIRNAME =
".metanorma"
CONFIG_FILENAME =
"config.yml"
VERSION =
"1.6.15".freeze

Class Method Summary collapse

Class Method Details

.base_templates_pathObject



46
47
48
# File 'lib/metanorma/cli.rb', line 46

def self.base_templates_path
  root_path.join("templates", "base")
end

.config_path(global = false) ⇒ Object



66
67
68
69
70
# File 'lib/metanorma/cli.rb', line 66

def self.config_path(global = false)
  return global_config_path if global

  local_config_path
end

.find_command(arguments) ⇒ Object



90
91
92
93
# File 'lib/metanorma/cli.rb', line 90

def self.find_command(arguments)
  commands = Metanorma::Cli::Command.all_commands.keys
  commands.select { |cmd| arguments.include?(cmd.gsub("_", "-")) == true }
end

.global_config_pathObject



58
59
60
# File 'lib/metanorma/cli.rb', line 58

def self.global_config_path
  home_directory.join(CONFIG_FILENAME)
end

.home_directoryObject



54
55
56
# File 'lib/metanorma/cli.rb', line 54

def self.home_directory
  Pathname.new(Dir.home).join(CONFIG_DIRNAME)
end

.load_flavorsObject



16
17
18
# File 'lib/metanorma/cli.rb', line 16

def self.load_flavors
  Metanorma::Flavor.load_flavors
end

.local_config_pathObject



62
63
64
# File 'lib/metanorma/cli.rb', line 62

def self.local_config_path
  Pathname.new(Dir.pwd).join(CONFIG_DIRNAME, CONFIG_FILENAME)
end

.rootObject



42
43
44
# File 'lib/metanorma/cli.rb', line 42

def self.root
  File.dirname(__dir__)
end

.root_pathObject



82
83
84
# File 'lib/metanorma/cli.rb', line 82

def self.root_path
  Pathname.new(Cli.root).join("..")
end

.start(arguments) ⇒ Object

Invoking commands

In the Metanorma CLI, we’ve included some custom behavior, like exposing the compiation directly from the root command.

So, for this use case we first check if the user is actually trying to compile a document or not, and based on that we’ll compile the document or show the help documentation.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metanorma/cli.rb', line 29

def self.start(arguments)
  if find_command(arguments).empty?
    arguments.unshift("compile")
  end

  Metanorma::Cli::Command.start(arguments)
rescue Interrupt, SignalException => _e
  UI.say("Process cancelled, exiting.")
rescue Errors::FileNotFoundError => error
  UI.say("Error: #{error}. \nNot sure what to run? try: metanorma help")
  exit(Errno::ENOENT::Errno)
end

.templates_pathObject



50
51
52
# File 'lib/metanorma/cli.rb', line 50

def self.templates_path
  home_directory.join("templates")
end

.with_indifferent_access(options) ⇒ Object



86
87
88
# File 'lib/metanorma/cli.rb', line 86

def self.with_indifferent_access(options)
  Thor::CoreExt::HashWithIndifferentAccess.new(options)
end

.writable_templates_path?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/metanorma/cli.rb', line 72

def self.writable_templates_path?
  parent_directory = templates_path.join("..", "..")

  unless parent_directory && parent_directory.writable?
    raise Errno::EACCES, "No permission to write in this directory"
  end

  return true
end