#!/usr/bin/env ruby18 # # Description # # Author:: Robert Sharp # Copyright:: Copyright (c) 2010 Robert Sharp # License:: Open Software Licence v3.0 # # This software is licensed for use under the Open Software Licence v. 3.0 # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php # and in the file copyright.txt. Under the terms of this licence, all derivative works # must themselves be licensed under the Open Software Licence v. 3.0 # # # replace this to point to your config class # require 'my_jeckyl_configurator' require 'rubygems' # want to find classes that might be in gems require 'jeckyl' require 'jeckyl/errors' require 'thor' require 'thor/group' require 'colored' # the Command Line Interface for jeckyl to allow users to generate # config files, generate a simple template or check a config file class JeckylCLI < Thor default_task :usage desc "usage", "You are looking at it" def usage puts "Useful tasks for managing Jeckyl config files." puts "" help end desc "list", "list all the config classes involved in a given class" def list(cfile) require cfile classes = Jeckyl::Options.descendants display_classes(classes) rescue LoadError puts "Error: could not load #{cfile}".red.bold end desc "config", "generate config file for the given config class" method_option :class, :aliases=>'-C', :type=>:numeric, :desc=>'select class by index' method_option :concat, :aliases=>'-k', :desc=>'concat all classes, starting with the oldest' def config(cfile, my_class_name=nil) require cfile # catch LoadError if cfile does not load my_class_index = options[:class] || 0 my_class_index += -1 if my_class_index > 0 classes = Jeckyl::Options.descendants if options[:concat].nil? && classes[my_class_index].nil? then $stderr.puts "Index #{my_class_index + 1} is invalid. Please select a valid index".red.bold display_classes(classes) return false end $stderr.puts "There are #{classes.length} sets of configs, use 'jeckyl list' to list them or -k to generate them all".yellow if classes.length > 1 my_classes = [] if options[:concat] then classes.each_index {|i| my_classes.unshift i} else my_classes << my_class_index end my_classes.each do |ci| puts "#" puts "# Configuration Options for: #{classes[ci].name}" puts "#" puts classes[ci].generate_config(true) end unless options[:concat] || classes.length == 1 puts "" puts "# There are also parameters in:" classes.each do |klass| puts "# #{klass.name}"unless klass == classes[my_class_index] end end rescue LoadError puts "Error: could not load #{cfile}".red.bold end desc "klass", "generate a simple class template" def klass(name, parent="Jeckyl::Config") puts < #{name} Parameter Descriptions # class Config < #{parent} def configure_a_parameter(val) default "default" comment "Comment line", "more comments" a_type_of(String) end end end EOTXT end desc "check", "check the given config file is valid for the given class" def check(klass_file, conf_file) require klass_file # catch LoadError if cfile does not load my_class_index = 0 # assume there is only one classes = Jeckyl::Options.descendants classes[my_class_index].check_config(conf_file) rescue LoadError puts "Error: could not load #{cfile}".red end desc "markdown", "display the comments from a class in markdown format" method_option :class, :aliases=>'-C', :type=>:numeric, :desc=>'select class by index' method_option :concat, :aliases=>'-k', :desc=>'concat all classes, starting with the oldest' def markdown(cfile) require cfile # catch LoadError if cfile does not load my_class_index = options[:class] || 0 my_class_index += -1 if my_class_index > 0 classes = Jeckyl::Options.descendants if options[:concat].nil? && classes[my_class_index].nil? then $stderr.puts "Index #{my_class_index + 1} is invalid. Please select a valid index".red.bold display_classes(classes) return false end $stderr.puts "There are #{classes.length} sets of configs, use 'jeckyl list' to list them or -k to generate them all".yellow if classes.length > 1 my_classes = [] if options[:concat] then classes.each_index {|i| my_classes << i} else my_classes << my_class_index end first = true sublevel = 2 my_classes.each do |ci| my_class = classes[ci] my_configurator = my_class.new(nil, :local=>true) if first then puts "# #{my_class.name} Parameters" puts "" puts "The following parameters are defined in {#{my_class.name}} and should be used" puts "in a configuration file. A default config file can be generated using:" puts "" puts " jeckyl config #{File.expand_path(cfile)}" else puts "## Additional Parameters from #{my_class.name}" puts "" puts "The following additional parameters are defined in #{my_class.name}, which" puts "is an ancestor of this config class. See separate documentation for more details." sublevel = 3 end first = false puts "" puts "#" * sublevel + " Parameters" puts "" my_configurator.comments.each do |param, comment| puts " * **#{param}**" puts " " comment.each {|line| puts " #{line}"} puts "" if my_configurator.defaults.has_key?(param) then puts " Default: #{my_configurator.defaults[param].inspect}" else puts " No default set" end puts "" end end unless options[:concat] || classes.length == 1 puts "" puts "## See Also" puts "" puts "There are also parameters in:" puts "" classes.each do |klass| puts " * #{klass.name}"unless klass == classes[my_class_index] end end rescue LoadError puts "Error: could not load #{cfile}".red end no_tasks do def display_classes(classes) $stderr.puts "The following classes are available:".green count = 1 classes.each do |klass| $stderr.puts " #{count}. #{klass.name}" count += 1 end end end end JeckylCLI.start