#!/usr/bin/env ruby require 'rubygems' require 'templater' require 'cucumber/nagios' require 'highline/import' require 'pathname' module CucumberNagiosGenerators extend Templater::Manifold class ProjectGenerator < Templater::Generator def initialize(destination_root, options = {}, *args) super @readme = [] @readme << "<%= color('Your new cucumber-nagios project can be found in #{self.destination_root}.', :green) %>" @readme << "" @readme << "Next, install the necessary RubyGems with:" @readme << "" @readme << " <%= color('bundle install', :yellow) %>" @readme << "" end def self.source_root Pathname.new(__FILE__).parent.parent.join('lib', 'generators', 'project').expand_path end def destination_root options[:directory] ? options[:directory].to_s : Pathname.new(@destination_root).join(name) end @root = Pathname.new(__FILE__).parent.parent.join('lib', 'cucumber', 'nagios').expand_path desc "Generate a cucumber-nagios project." first_argument :name, :required => true, :desc => "Project name" option :directory, :desc => "specify the directory to generate the project into" file '.gitignore', '.gitignore' file '.bzrignore', '.bzrignore' directory :steps, @root.join('steps'), 'features/steps' directory :features, @root.join('support'), 'features/support' glob! def after_run git_init readme end def git_init if system("which git > /dev/null 2>&1") Dir.chdir(destination_root) do git_directory = Pathname.new(destination_root).join('.git') if not git_directory.directory? system("git init -q") system("git add .") system("git commit -qm 'Initial commit to #{name}.' .") end end @readme << "Your project has been initialised as a git repository." @readme << "" else @readme << "It's <%= color('strongly', :red) %> recommended you version control your project:" @readme << "" @readme << " # Install git using your package manager." @readme << " <%= color('git init', :yellow) %>" @readme << " <%= color('git add .', :yellow) %>" @readme << " <%= color('git commit -m \"Initial commit to #{name}.\" .', :yellow) %>" @readme << "" end end def readme puts @readme.each do |line| if line =~ /^\s*$/ puts line else say(line) end end puts unless @readme.last =~ /^\s*$/ end end add :project, ProjectGenerator # feature generator class FeatureGenerator < Templater::Generator def self.source_root Pathname.new(__FILE__).parent.parent.join('lib', 'generators', 'feature').expand_path end desc <<-DESC Generate a Cucumber feature. \n Usage: cucumber-nagios-gen feature e.g. cucumber-nagios-gen feature google.com.au search DESC first_argument :site, :required => true, :desc => "Site name" second_argument :feature, :required => true, :desc => "Feature name" template :feature do |template| template.source = "%feature_name%.feature" template.destination = "features/#{site}/#{feature}.feature" end template :step do |template| template.source = "%feature_name%_steps.rb" template.destination = "features/#{site}/steps/#{feature}_steps.rb" end end add :feature, FeatureGenerator desc "Generators for cucumber-nagios projects." end CucumberNagiosGenerators.run_cli Dir.pwd, 'cucumber-nagios-gen', Cucumber::Nagios::VERSION, ARGV