@deprecated Use {Nanoc::CLI::CommandRunner} instead
Adds the given command to the collection of available commands.
@param [Cri::Command] cmd The command to add
@return [void]
# File lib/nanoc/cli.rb, line 65 def self.add_command(cmd) self.root_command.add_command(cmd) end
@param [Boolean] boolean true if debug output should be enabled,
false if it should not
@return [void]
@since 3.2.0
# File lib/nanoc/cli.rb, line 39 def self.debug=(boolean) @debug = boolean end
@return [Boolean] true if debug output is enabled, false if not
@since 3.2.0
# File lib/nanoc/cli.rb, line 29 def self.debug? @debug || false end
@return [Cri::Command] The root command, i.e. the commandline tool itself
# File lib/nanoc/cli.rb, line 56 def self.root_command @root_command end
Invokes the nanoc commandline tool with the given arguments.
@param [Array<String>] args An array of commandline arguments
@return [void]
# File lib/nanoc/cli.rb, line 48 def self.run(args) Nanoc::CLI::ErrorHandler.handle_while do self.setup self.root_command.run(args) end end
@return [Boolean] true if color support is present, false if not
# File lib/nanoc/cli.rb, line 190 def self.enable_ansi_colors?(io) return false if !io.tty? begin require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /mswin|mingw/ rescue LoadError return false end return true end
@return [Boolean] true if UTF-8 support is present, false if not
# File lib/nanoc/cli.rb, line 183 def self.enable_utf8?(io) return true if !io.tty? %( LC_ALL LC_CTYPE LANG ).any? { |e| ENV[e] =~ /UTF/ } end
Loads the command in the file with the given filename.
@param [String] filename The name of the file that contains the command
@return [Cri::Command] The loaded command
# File lib/nanoc/cli.rb, line 133 def self.load_command_at(filename, command_name=nil) # Load code = File.read(filename) cmd = Cri::Command.define(code, filename) # Set name command_name ||= File.basename(filename, '.rb') cmd.modify { name command_name } # Done cmd end
Loads site-specific commands in `commands/`.
@return [void]
# File lib/nanoc/cli.rb, line 107 def self.load_custom_commands self.recursive_contents_of('commands').each do |filename| # Create command command = Nanoc::CLI.load_command_at(filename) # Get supercommand pieces = filename.gsub(/^commands\/|\.rb$/, '').split('/') pieces = pieces[0, pieces.size-1] || [] root = Nanoc::CLI.root_command supercommand = pieces.inject(root) do |cmd, piece| cmd.nil? ? nil : 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
@return [Array] The directory contents
# File lib/nanoc/cli.rb, line 147 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 self.recursive_contents_of(d) } files end
Makes the commandline interface ready for use.
@return [void]
# File lib/nanoc/cli.rb, line 74 def self.setup self.setup_cleaning_streams self.setup_commands self.load_custom_commands end
Wraps `$stdout` and `$stderr` in appropriate cleaning streams.
@return [void]
# File lib/nanoc/cli.rb, line 177 def self.setup_cleaning_streams $stdout = self.wrap_in_cleaning_stream($stdout) $stderr = self.wrap_in_cleaning_stream($stderr) end
Sets up the root command and base subcommands.
@return [void]
# File lib/nanoc/cli.rb, line 83 def self.setup_commands # Reinit @root_command = nil # Add root command filename = File.dirname(__FILE__) + "/cli/commands/nanoc.rb" @root_command = self.load_command_at(filename) # Add help command help_cmd = Cri::Command.new_basic_help self.add_command(help_cmd) # Add other commands cmd_filenames = Dir[File.dirname(__FILE__) + '/cli/commands/*.rb'] cmd_filenames.each do |filename| next if File.basename(filename, '.rb') == 'nanoc' cmd = self.load_command_at(filename) self.add_command(cmd) end end
Wraps the given stream in a cleaning stream. The cleaning streams will have the proper stream cleaners configured.
@param [IO] io The stream to wrap
@return [::Nanoc::CLI::CleaningStream]
# File lib/nanoc/cli.rb, line 160 def self.wrap_in_cleaning_stream(io) cio = ::Nanoc::CLI::CleaningStream.new(io) if !self.enable_utf8?(io) cio.add_stream_cleaner(Nanoc::CLI::StreamCleaners::UTF8) end if !self.enable_ansi_colors?(io) cio.add_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors) end cio end
Generated with the Darkfish Rdoc Generator 2.