#!/usr/bin/env ruby require 'rubygems' require 'commander/import' require 'elf_manifesto' require 'elf_manifesto/manifest_maker' require 'ostruct' program :version, ElfManifesto::VERSION program :description, 'Creates file manifests from file collections and mustache templates' # command :create do |c| c.syntax = 'manifesto create [options]' c.summary = 'creates a manifest file' c.description = 'Create a manifest from file collections and a template' c.example 'Create a manifest from template.mustache and the jpgs in the images directory', "manifesto --template template.mustache 'images/*.jpg'" c.example 'Pass arbitrary properties to be used in the template', "manifesto --template template.mustache --properties '{ id: 1234 }' images/*.jpg'" c.option '-m', '--template STRING', String, 'Template file' c.option '-o', '--output_file STRING', String, 'Target for manifest output' c.option '-p', '--properties STRING', String, 'Property hash' c.option '-s', '--sample_template STRING', String, 'Name of a supplied template' c.option '-c', '--config_file STRING', String, 'Path to a config file. Overrides any other options' c.action do |args, options| # TODO - This logic is horrible. Refactor if ((options.sample_template.nil? && options.template.nil? && options.config_file.nil?) || (args.empty? && options.config_file.nil?)) say <<-MESSAGE Manifesto! throw me a frickin' bone here! I need some parameters Use --help to find out how MESSAGE elsif !(options.config_file.nil?) config_file = File.open(options.config_file, 'rb') config_options = eval(config_file.read) # n.b. uses OpenStruct to treat hashes as dot notation config_options[:template] = get_template_path OpenStruct.new config_options manifesto = ElfManifesto::ManifestMaker.new(config_options) else # Select a template file template = get_template_path options requested_template = options.template || options.sample_template message = "" message += "Creating manifest from #{template} with:" unless options.output_file.nil? # test for a valid templates if template.nil? || !File.exists?(template) raise "Bad template selection: #{requested_template}" end props = {} props = eval(options.properties) unless options.properties.nil? ops = { template: template, globs: args, props: props } manifesto = ElfManifesto::ManifestMaker.new(ops) end end end command "list templates" do |c| c.syntax = 'list templates' c.summary = 'Lists the built in templates available' c.description = '' c.example 'List all build in templates', 'manifesto list templates' # c.option '--some-switch', 'Some switch that does something' c.action do |args, options| path_to_templates = File.expand_path('../lib/templates', File.dirname(__FILE__)) # absolute path to the templates directory relative to this file. puts "Available Templates:" templates = Dir.glob(File.join path_to_templates, '**', '*.*' ) templates.each do |template| puts File.basename template end # Do something or c.when_called Tmp::Commands::List end command "show template" do |c| c.syntax = 'show template |template name|' c.summary = 'Shows the contents of a built in template' c.description = '' c.example 'Show the contents of the imsmanifest.mustache template', 'manifesto show template imsmanifest' # c.option '--some-switch', 'Some switch that does something' c.action do |args, options| path_to_templates = File.expand_path('../lib/templates', File.dirname(__FILE__)) # absolute path to the templates directory relative to this file. template = get_sample_template(args.first) if template.nil? raise "Template does not exist: #{args.first}" end template_file = File.open(template, "rb") puts template_file.read end end end default_command :create alias_command :list, :'list templates' alias_command :show, :'show template' private def get_template_path options if options.template.nil? template = get_sample_template options.sample_template else # test for an underscore prefix, which denotes a built-in template if options.template =~ /^_/i template = get_sample_template options.template else template = options.template end end template end def get_sample_template template_name # strip any _ prefix target = template_name.sub('_','') templates_path = File.expand_path( '../lib/templates', File.dirname(__FILE__)) templates_files = Dir.glob(File.join(templates_path, '**','*.*')) selected_template = templates_files.select do |file| File.basename(file) =~ /#{target}/i end.first end