Module: Nanoc::CLI Private

Defined in:
lib/nanoc/cli.rb,
lib/nanoc/cli/logger.rb,
lib/nanoc/cli/version.rb,
lib/nanoc/cli/transform.rb,
lib/nanoc/cli/error_handler.rb,
lib/nanoc/cli/command_runner.rb,
lib/nanoc/cli/cleaning_stream.rb,
lib/nanoc/cli/stack_trace_writer.rb,
lib/nanoc/cli/stream_cleaners/utf8.rb,
lib/nanoc/cli/ansi_string_colorizer.rb,
lib/nanoc/cli/stream_cleaners/abstract.rb,
lib/nanoc/cli/stream_cleaners/ansi_colors.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Modules: ANSIStringColorizer, Commands, CompileListeners, StreamCleaners, Transform Classes: CleaningStream, CommandRunner, ErrorHandler, Logger, StackTraceWriter

Constant Summary collapse

VERSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'4.13.3'

Class Method Summary collapse

Class Method Details

.add_after_setup_proc(proc) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



209
210
211
212
# File 'lib/nanoc/cli.rb', line 209

def self.add_after_setup_proc(proc)
  @after_setup_procs ||= []
  @after_setup_procs << proc
end

.add_command(cmd) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Adds the given command to the collection of available commands.

Parameters:

  • cmd (Cri::Command)

    The command to add



102
103
104
# File 'lib/nanoc/cli.rb', line 102

def self.add_command(cmd)
  root_command.add_command(cmd)
end

.after_setup(&block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Schedules the given block to be executed after the CLI has been set up.



109
110
111
112
# File 'lib/nanoc/cli.rb', line 109

def self.after_setup(&block)
  # TODO: decide what should happen if the CLI is already set up
  add_after_setup_proc(block)
end

.after_setup_procsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



205
206
207
# File 'lib/nanoc/cli.rb', line 205

def self.after_setup_procs
  @after_setup_procs || []
end

.debug=(boolean) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • boolean (Boolean)

    true if debug output should be enabled, false if it should not



28
29
30
# File 'lib/nanoc/cli.rb', line 28

def self.debug=(boolean)
  @debug = boolean
end

.debug?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if debug output is enabled, false if not.

Returns:

  • (Boolean)

    true if debug output is enabled, false if not



20
21
22
# File 'lib/nanoc/cli.rb', line 20

def self.debug?
  @debug || false
end

.enable_ansi_colors?(io) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if color support is present, false if not.

Returns:

  • (Boolean)

    true if color support is present, false if not



76
77
78
# File 'lib/nanoc/cli.rb', line 76

def self.enable_ansi_colors?(io)
  io.tty? && !ENV.key?('NO_COLOR')
end

.enable_utf8?(io) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if UTF-8 support is present, false if not.

Returns:

  • (Boolean)

    true if UTF-8 support is present, false if not



69
70
71
72
73
# File 'lib/nanoc/cli.rb', line 69

def self.enable_utf8?(io)
  return true unless io.tty?

  %w[LC_ALL LC_CTYPE LANG].any? { |e| ENV.fetch(e, nil) =~ /UTF/i }
end

.load_commands_at(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/nanoc/cli.rb', line 174

def self.load_commands_at(path)
  recursive_contents_of(path).each do |filename|
    # Create command
    command = Cri::Command.load_file(filename, infer_name: true)

    # Get supercommand
    pieces = filename.gsub(/^#{path}\/|\.rb$/, '').split('/')
    pieces = pieces[0, pieces.size - 1] || []
    root = Nanoc::CLI.root_command
    supercommand = pieces.reduce(root) do |cmd, piece|
      cmd&.command_named(piece)
    end

    # Add to supercommand
    if supercommand.nil?
      raise "Cannot load command at #{filename} because its supercommand cannot be found"
    end

    supercommand.add_command(command)
  end
end

.load_custom_commandsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Loads site-specific commands.



165
166
167
168
169
170
171
172
# File 'lib/nanoc/cli.rb', line 165

def self.load_custom_commands
  if Nanoc::Core::SiteLoader.cwd_is_nanoc_site?
    config = Nanoc::Core::ConfigLoader.new.new_from_cwd
    config[:commands_dirs].each do |path|
      load_commands_at(path)
    end
  end
end

.recursive_contents_of(path) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The directory contents.

Returns:

  • (Array)

    The directory contents



197
198
199
200
201
202
203
# File 'lib/nanoc/cli.rb', line 197

def self.recursive_contents_of(path)
  return [] unless File.directory?(path)

  files, dirs = *Dir[path + '/*'].sort.partition { |e| File.file?(e) }
  dirs.each { |d| files.concat recursive_contents_of(d) }
  files
end

.root_commandCri::Command

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The root command, i.e. the command-line tool itself.

Returns:

  • (Cri::Command)

    The root command, i.e. the command-line tool itself



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

def self.root_command
  @root_command
end

.run(args) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Invokes the Nanoc command-line tool with the given arguments.

Parameters:

  • args (Array<String>)

    An array of command-line arguments



85
86
87
88
89
90
# File 'lib/nanoc/cli.rb', line 85

def self.run(args)
  Nanoc::CLI::ErrorHandler.handle_while do
    setup
    root_command.run(args)
  end
end

.setupvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Makes the command-line interface ready for use.



117
118
119
120
121
122
# File 'lib/nanoc/cli.rb', line 117

def self.setup
  Nanoc::CLI.setup_cleaning_streams
  setup_commands
  load_custom_commands
  after_setup_procs.each(&:call)
end

.setup_cleaning_streamsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Wraps ‘$stdout` and `$stderr` in appropriate cleaning streams.



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

def self.setup_cleaning_streams
  $stdout = wrap_in_cleaning_stream($stdout)
  $stderr = wrap_in_cleaning_stream($stderr)
end

.setup_commandsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sets up the root command and base subcommands.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nanoc/cli.rb', line 127

def self.setup_commands
  # Reinit
  @root_command = nil

  # Add root command
  filename = __dir__ + '/cli/commands/nanoc.rb'
  @root_command = Cri::Command.load_file(filename, infer_name: true)

  # Add help command
  help_cmd = Cri::Command.new_basic_help
  add_command(help_cmd)

  # Add other commands
  cmd_filenames = Dir[__dir__ + '/cli/commands/*.rb']
  cmd_filenames.each do |cmd_filename|
    basename = File.basename(cmd_filename, '.rb')

    next if basename == 'nanoc'

    cmd = Cri::Command.load_file(cmd_filename, infer_name: true)
    add_command(cmd)
  end

  if defined?(Bundler)
    # Discover external commands through Bundler
    begin
      Bundler.require(:nanoc)
    rescue Bundler::GemfileNotFound
      # When running Nanoc with Bundler being defined but
      # no gemfile being present (rubygems automatically loads
      # Bundler when executing from command line), don't crash.
    end
  end
end

.verbosityObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/nanoc/cli.rb', line 32

def self.verbosity
  @verbosity || 0
end

.verbosity=(val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
# File 'lib/nanoc/cli.rb', line 36

def self.verbosity=(val)
  @verbosity = val
end

.wrap_in_cleaning_stream(io) ⇒ ::Nanoc::CLI::CleaningStream

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps the given stream in a cleaning stream. The cleaning streams will have the proper stream cleaners configured.

Parameters:

  • io (IO)

    The stream to wrap

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nanoc/cli.rb', line 54

def self.wrap_in_cleaning_stream(io)
  cio = ::Nanoc::CLI::CleaningStream.new(io)

  unless enable_utf8?(io)
    cio.add_stream_cleaner(Nanoc::CLI::StreamCleaners::UTF8)
  end

  unless enable_ansi_colors?(io)
    cio.add_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors)
  end

  cio
end