bin/nanoc in nanoc-1.6.2 vs bin/nanoc in nanoc-2.0

- old
+ new

@@ -1,100 +1,204 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../lib/nanoc.rb' +# Get what we need +require File.dirname(__FILE__) + '/../lib/nanoc.rb' require 'getoptlong' -try_require 'rubygems' +# Helper functions +def usage(s) + error(s, 'Usage') +end + # Define some texts + version_text = "nanoc #{Nanoc::VERSION} (c) 2007 Denis Defreyne." -usage_text = "Usage: nanoc [options] [command] [parameters]" help_text = <<EOT -Usage: nanoc [-chv] - nanoc create_site <name> - nanoc create_page <name> [-t template] - nanoc create_template <name> +Usage: nanoc [-chvV] nanoc compile + nanoc autocompile + nanoc create_layout [layout_name] + nanoc create_page [page_name] [-t template] + nanoc create_site [site_name] + nanoc create_template [template_name] Options: - -h, --help Show this help message and quit. - -v, --version Show the nanoc version number and quit. - -t, --template Template that should be used when creating a page. - (can only be used with create_page) + -h, --help Show this help message and quit. + -p, --port Port the autocompiler should be run on. + -t, --template Template that should be used when creating a page. + (can only be used with create_page) + -V, --verbose Display more verbose log messages. + -v, --version Show the nanoc version number and quit. Description: The 'nanoc' command is used for creating nanoc-powered sites, as well as managing them: creating pages and templates and compiling the site. Read more about nanoc on the site: <http://nanoc.stoneship.org/> EOT +# Create site + +$nanoc_site = Nanoc::Site.from_cwd + # Parse options + opts = GetoptLong.new( - [ '--help', '-h', GetoptLong::NO_ARGUMENT ], - [ '--template', '-t', GetoptLong::REQUIRED_ARGUMENT ], - [ '--version', '-v', GetoptLong::NO_ARGUMENT ] + [ '--help', '-h', GetoptLong::NO_ARGUMENT ], + [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT ], + [ '--template', '-t', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', '-V', GetoptLong::NO_ARGUMENT ], + [ '--version', '-v', GetoptLong::NO_ARGUMENT ], + [ '--yes', '-y', GetoptLong::NO_ARGUMENT ] ) -unprocessed_opts = {} +$unprocessed_opts = {} begin opts.each do |opt, arg| case opt when '--help' $stderr.puts help_text exit + when '--verbose' + $log_level = :low when '--version' puts version_text exit else - unprocessed_opts[opt] = arg + $unprocessed_opts[opt] = arg end end rescue GetoptLong::InvalidOption $stderr.puts usage_text - exit + exit(1) end # Make sure we have at least one argument -if ARGV.size == 0 - $stderr.puts usage_text - exit + +usage 'nanoc [options] [command] [parameters]' if ARGV.size == 0 + +# Functions for handing commands + +def setup + # Check syntax + usage 'Usage: nanoc setup' if ARGV.size != 1 + + # Make sure we are in a nanoc site directory + unless $nanoc_site + error 'The current working directory does not seem to ' + + 'be a valid/complete nanoc site directory; aborting.' + end + + # Check for -y switch + unless $unprocessed_opts.has_key?('--yes') + error 'Are you absolutely sure you want to set up the data source for ' + + 'this site? Setting up the data source will remove existing ' + + 'data. To continue, use the -y switch, like "nanoc setup -y".' + end + + # Setup + $nanoc_site.setup end -# Handle command -command = ARGV[0] -case command +def create_site + # Check syntax + usage 'Usage: nanoc create_site [site_name] [options]' if ARGV.size != 2 + # Create site - when 'create_site', 'cs' - if ARGV.size != 2 - $stderr.puts 'Usage: nanoc create_site [site_name]' - exit - end - $nanoc_creator.create_site(ARGV[1]) + Nanoc::Site.create(ARGV[1]) +end +def create_page + # Check syntax + usage 'Usage: nanoc create_page [page_name] [options]' if ARGV.size != 2 + + # Make sure we are in a nanoc site directory + unless $nanoc_site + error 'The current working directory does not seem to ' + + 'be a valid/complete nanoc site directory; aborting.' + end + # Create page - when 'create_page', 'cp' - if ARGV.size != 2 - $stderr.puts 'Usage: nanoc create_page [page_name]' - exit - end - $nanoc_creator.create_page(ARGV[1], :template => unprocessed_opts['--template']) + if $unprocessed_opts['--template'].nil? + $nanoc_site.create_page(ARGV[1]) + else + $nanoc_site.create_page(ARGV[1], $unprocessed_opts['--template']) + end +end +def create_layout + # Check syntax + usage 'nanoc create_layout [layout_name]' if ARGV.size != 2 + + # Make sure we are in a nanoc site directory + unless $nanoc_site + error 'The current working directory does not seem to ' + + 'be a valid/complete nanoc site directory; aborting.' + end + # Create template - when 'create_template', 'ct' - if ARGV.size != 2 - $stderr.puts 'Usage: nanoc create_template [template_name]' - exit - end - $nanoc_creator.create_template(ARGV[1]) + $nanoc_site.create_layout(ARGV[1]) +end - # Process site and generate output - when 'compile', 'compile_site', 'co' - if ARGV.size != 1 - $stderr.puts 'Usage: nanoc compile' - exit - end - $nanoc_compiler.run +def create_template + # Check syntax + usage 'nanoc create_template [template_name]' if ARGV.size != 2 + # Make sure we are in a nanoc site directory + unless $nanoc_site + error 'The current working directory does not seem to ' + + 'be a valid/complete nanoc site directory; aborting.' + end + + # Create template + $nanoc_site.create_template(ARGV[1]) +end + +def compile_site + # Check syntax + usage 'nanoc compile' if ARGV.size != 1 + + # Make sure we are in a nanoc site directory + unless $nanoc_site + error 'The current working directory does not seem to ' + + 'be a valid/complete nanoc site directory; aborting.' + end + + # Compile site + $nanoc_site.compile +end + +def autocompile_site + # Check syntax + usage 'nanoc compile' if ARGV.size != 1 + + # Make sure we are in a nanoc site directory + unless $nanoc_site + error 'The current working directory does not seem to ' + + 'be a valid/complete nanoc site directory; aborting.' + end + + # Autocompile site + $nanoc_site.autocompile($unprocessed_opts['--port']) +end + +# Handle command + +case ARGV[0] + when 'autocompile_site', 'autocompile', 'aco' + autocompile_site + when 'create_site', 'cs' + create_site + when 'create_page', 'cp' + create_page + when 'create_layout', 'cl' + create_layout + when 'create_template', 'ct' + create_template + when 'compile_site', 'compile', 'co' + compile_site + when 'setup', 's' + setup else - puts 'Unrecognised command \'' + command + '\'' + error 'Unrecognised command \'' + ARGV[0] + '\'' end