#!/usr/bin/env ruby # Get what we need require File.dirname(__FILE__) + '/../lib/nanoc.rb' require 'getoptlong' # Helper functions def usage(s) error(s, 'Usage') end # Define some texts version_text = "nanoc #{Nanoc::VERSION} (c) 2007-2008 Denis Defreyne." help_text = < EOT # Create site $nanoc_site = Nanoc::Site.from_cwd # Parse options opts = GetoptLong.new( [ '--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 = {} 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 end end rescue GetoptLong::InvalidOption $stderr.puts usage_text exit(1) end # Make sure we have at least one argument 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 def create_site # Check syntax usage 'Usage: nanoc create_site [site_name] [options]' if ARGV.size != 2 # Create site 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 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 $nanoc_site.create_layout(ARGV[1]) end 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 error 'Unrecognised command \'' + ARGV[0] + '\'' end