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

Class Method Summary collapse

Class Method Details

.base_templates_pathObject



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

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

.config_path(global = false) ⇒ Object



68
69
70
71
72
# File 'lib/metanorma/cli.rb', line 68

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

  local_config_path
end

.find_command(arguments) ⇒ Object



92
93
94
95
# File 'lib/metanorma/cli.rb', line 92

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

.global_config_pathObject



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

def self.global_config_path
  home_directory.join(CONFIG_FILENAME)
end

.home_directoryObject



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

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



64
65
66
# File 'lib/metanorma/cli.rb', line 64

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


97
98
99
100
101
102
# File 'lib/metanorma/cli.rb', line 97

def self.print_fatal_summary(error)
  $stdout.flush
  $stderr.flush
  UI.error(error.message)
  exit(-1)
end

.rootObject



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

def self.root
  File.dirname(__dir__)
end

.root_pathObject



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

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

.templates_pathObject



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

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

.with_indifferent_access(options) ⇒ Object



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

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

.writable_templates_path?Boolean

Returns:

  • (Boolean)


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

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

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

  true
end