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.5.6".freeze

Class Method Summary collapse

Class Method Details

.base_templates_pathObject



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

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

.config_path(global = false) ⇒ Object



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

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

  local_config_path
end

.find_command(arguments) ⇒ Object



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

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

.global_config_pathObject



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

def self.global_config_path
  home_directory.join(CONFIG_FILENAME)
end

.home_directoryObject



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

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



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

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

.rootObject



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

def self.root
  File.dirname(__dir__)
end

.root_pathObject



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

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
# 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 Errors::FileNotFoundError => error
  UI.say("Error: #{error}. \nNot sure what to run? try: metanorma help")
  exit(Errno::ENOENT::Errno)
end

.templates_pathObject



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

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

.with_indifferent_access(options) ⇒ Object



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

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

.writable_templates_path?Boolean

Returns:

  • (Boolean)


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

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