Class: Nanoc3::CLI::Base
- Inherits:
-
Cri::Base
- Object
- Cri::Base
- Nanoc3::CLI::Base
- Defined in:
- lib/nanoc3/cli/base.rb
Class Method Summary (collapse)
-
+ (Nanoc3::CLI::Base) shared_base
Returns a fully initialised base instance.
Instance Method Summary (collapse)
-
- (Array) global_option_definitions
The list of global option definitions.
- - (Object) handle_option(option)
-
- (Base) initialize
constructor
A new instance of Base.
-
- (void) print_error(error)
Prints the given error to stderr.
-
- (void) require_site
Asserts that the current working directory contains a site (Site instance).
-
- (String) resolution_for(error)
Attempts to find a resolution for the given error, or nil if no resolution can be automatically obtained.
- - (Object) run(args)
-
- (void) set_vcs(vcs_name)
Sets the data source's VCS to the VCS with the given name.
-
- (Nanoc3::Site) site
Gets the site (Site instance) in the current directory and loads its data.
Constructor Details
- (Base) initialize
A new instance of Base
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/nanoc3/cli/base.rb', line 7 def initialize super('nanoc3') # Add help command self.help_command = Nanoc3::CLI::Commands::Help.new add_command(self.help_command) # Add other commands add_command(Nanoc3::CLI::Commands::Autocompile.new) add_command(Nanoc3::CLI::Commands::Compile.new) add_command(Nanoc3::CLI::Commands::CreateLayout.new) add_command(Nanoc3::CLI::Commands::CreateItem.new) add_command(Nanoc3::CLI::Commands::CreateSite.new) add_command(Nanoc3::CLI::Commands::Debug.new) add_command(Nanoc3::CLI::Commands::Info.new) add_command(Nanoc3::CLI::Commands::Update.new) add_command(Nanoc3::CLI::Commands::View.new) end |
Class Method Details
Returns a fully initialised base instance. It is recommended to use this
shared instance than to create new ones, as this will be the instance
that will be used when reading all code from the lib/ directory.
31 32 33 |
# File 'lib/nanoc3/cli/base.rb', line 31 def self.shared_base @shared_base ||= Nanoc3::CLI::Base.new end |
Instance Method Details
- (Array) global_option_definitions
The list of global option definitions
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/nanoc3/cli/base.rb', line 207 def global_option_definitions [ { :long => 'help', :short => 'h', :argument => :forbidden, :desc => 'show this help message and quit' }, { :long => 'no-color', :short => 'C', :argument => :forbidden, :desc => 'disable color' }, { :long => 'version', :short => 'v', :argument => :forbidden, :desc => 'show version information and quit' }, { :long => 'verbose', :short => 'V', :argument => :forbidden, :desc => 'make nanoc output more detailed' }, { :long => 'debug', :short => 'd', :argument => :forbidden, :desc => 'enable debugging (set $DEBUG to true)' }, { :long => 'warn', :short => 'w', :argument => :forbidden, :desc => 'enable warnings' } ] end |
- (Object) handle_option(option)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/nanoc3/cli/base.rb', line 237 def handle_option(option) case option when :version gem_info = defined?(Gem) ? "with RubyGems #{Gem::VERSION}" : "without RubyGems" engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby" puts "nanoc #{Nanoc3::VERSION} (c) 2007-2011 Denis Defreyne." puts "Running #{engine} #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) on #{RUBY_PLATFORM} #{gem_info}" exit 0 when :verbose Nanoc3::CLI::Logger.instance.level = :low when :debug $DEBUG = true when :warn $-w = true when :no-color' Nanoc3::CLI::Logger.instance.color = false when :help show_help exit 0 end end |
- (void) print_error(error)
This method returns an undefined value.
Prints the given error to stderr. Includes message, possible resolution (see #resolution_for), compilation stack, backtrace, etc.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/nanoc3/cli/base.rb', line 90 def print_error(error) $stderr.puts # Header $stderr.puts '+--- /!\ ERROR /!\ -------------------------------------------+' $stderr.puts '| An exception occured while running nanoc. If you think this |' $stderr.puts '| is a bug in nanoc, please do report it at |' $stderr.puts '| <http://projects.stoneship.org/trac/nanoc/newticket> -- |' $stderr.puts '| thanks in advance! |' $stderr.puts '+-------------------------------------------------------------+' # Exception and resolution (if any) $stderr.puts $stderr.puts '=== MESSAGE:' $stderr.puts $stderr.puts "#{error.class}: #{error.}" resolution = self.resolution_for(error) $stderr.puts "#{resolution}" if resolution # Compilation stack $stderr.puts $stderr.puts '=== COMPILATION STACK:' $stderr.puts if ((self.site && self.site.compiler.stack) || []).empty? $stderr.puts " (empty)" else self.site.compiler.stack.reverse.each do |obj| if obj.is_a?(Nanoc3::ItemRep) $stderr.puts " - [item] #{obj.item.identifier} (rep #{obj.name})" else # layout $stderr.puts " - [layout] #{obj.identifier}" end end end # Backtrace require 'enumerator' $stderr.puts $stderr.puts '=== BACKTRACE:' $stderr.puts $stderr.puts error.backtrace.to_enum(:each_with_index).map { |item, index| " #{index}. #{item}" }.join("\n") end |
- (void) require_site
This method returns an undefined value.
Asserts that the current working directory contains a site (Site instance). If no site is present, prints an error message and exits.
40 41 42 43 44 45 46 |
# File 'lib/nanoc3/cli/base.rb', line 40 def require_site if site.nil? $stderr.puts 'The current working directory does not seem to be a ' + 'valid/complete nanoc site directory; aborting.' exit 1 end end |
- (String) resolution_for(error)
Attempts to find a resolution for the given error, or nil if no resolution can be automatically obtained.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/nanoc3/cli/base.rb', line 139 def resolution_for(error) # FIXME this should probably go somewhere else so that 3rd-party code can add other gem names too gem_names = { 'adsf' => 'adsf', 'bluecloth' => 'bluecloth', 'builder' => 'builder', 'coderay' => 'coderay', 'cri' => 'cri', 'erubis' => 'erubis', 'haml' => 'haml', 'json' => 'json', 'less' => 'less', 'markaby' => 'markaby', 'maruku' => 'maruku', 'mime/types' => 'mime-types', 'rack' => 'rack', 'rack/cache' => 'rack-cache', 'rainpress' => 'rainpress', 'rdiscount' => 'rdiscount', 'redcloth' => 'redcloth', 'rubypants' => 'rubypants', 'sass' => 'sass', 'w3c_validators' => 'w3c_validators' } case error when LoadError # Get gem name matches = error..match(/no such file to load -- ([^\s]+)/) return nil if matches.empty? lib_name = matches[1] gem_name = gem_names[$1] # Build message if gem_name "Try installing the '#{gem_name}' gem (`gem install #{gem_name}`) and then re-running the command." end end end |
- (Object) run(args)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/nanoc3/cli/base.rb', line 67 def run(args) # Set exit handler [ 'INT', 'TERM' ].each do |signal| Signal.trap(signal) do puts exit!(0) end end super(args) rescue Interrupt => e exit(1) rescue StandardError, ScriptError => e print_error(e) exit(1) end |
- (void) set_vcs(vcs_name)
This method returns an undefined value.
Sets the data source's VCS to the VCS with the given name. Does nothing when the site's data source does not support VCSes (i.e. does not implement #vcs=).
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/nanoc3/cli/base.rb', line 186 def set_vcs(vcs_name) # Skip if not possible return if vcs_name.nil? || site.nil? # Find VCS vcs_class = Nanoc3::Extra::VCS.named(vcs_name.to_sym) if vcs_class.nil? $stderr.puts "A VCS named #{vcs_name} was not found; aborting." exit 1 end site.data_sources.each do |data_source| # Skip if not possible next if !data_source.respond_to?(:vcs=) # Set VCS data_source.vcs = vcs_class.new end end |
- (Nanoc3::Site) site
Gets the site (Site instance) in the current directory and loads its data.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nanoc3/cli/base.rb', line 52 def site # Load site if possible if File.file?('config.yaml') && (!self.instance_variable_defined?(:@site) || @site.nil?) begin @site = Nanoc3::Site.new('.') rescue Nanoc3::Errors::UnknownDataSource => e $stderr.puts "Unknown data source: #{e}" exit 1 end end @site end |