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

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: Commands, CompileListeners, StreamCleaners, Transform Classes: ANSIStringColorizer, CleaningStream, CommandRunner, ErrorHandler, Logger, StackTraceWriter

Constant Summary collapse

FORCE_COLOR_ENABLED =

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.

:enabled
FORCE_COLOR_DISABLED =

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.

:disabled
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.5'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.debugObject

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.

true if debug output is enabled, false if not



26
27
28
# File 'lib/nanoc/cli.rb', line 26

def debug
  @debug
end

.force_colorObject

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.



23
24
25
# File 'lib/nanoc/cli.rb', line 23

def force_color
  @force_color
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.



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

def verbosity
  @verbosity
end

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.



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

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



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

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.



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

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.



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

def self.after_setup_procs
  @after_setup_procs || []
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:

  • (Boolean)


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

def debug? = debug

.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



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

def self.enable_ansi_colors?(io)
  case force_color
  when FORCE_COLOR_ENABLED
    true
  when FORCE_COLOR_DISABLED
    false
  else
    io.tty? && !ENV.key?('NO_COLOR')
  end
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



63
64
65
66
67
# File 'lib/nanoc/cli.rb', line 63

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.



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

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.



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

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(File.expand_path(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



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

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



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

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



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

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.



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

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.



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

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.



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
161
# File 'lib/nanoc/cli.rb', line 128

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

.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:



52
53
54
55
56
57
58
59
60
# File 'lib/nanoc/cli.rb', line 52

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

  cio
end