bin/cog

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
require 'gli'
require 'cog'

include GLI::App

program_desc 'This is a utility to help you write code generators.'

desc 'Current version of cog'
version Cog::VERSION

desc 'When listing files, show the full path to the file'
switch :fullpaths

desc 'Set the Cogfile explicitly'
arg_name 'path'
flag :cogfile

desc 'Suppress the use of color in output'
switch :colorless

desc 'Add cog to the project in the present working directory'
command :init do |c|
  c.action do |gopt, opt, args|
    Cog.initialize_project
  end
end

desc 'Run and manage project generators'
long_desc 'Omit generator name to run all of them'
arg_name 'name'
command [:generator, :gen] do |c|
  
  c.default_desc 'Run generators'
  c.action do |gopt, opt, args|
    Cog::Embeds.gather_from_project
    if args.empty?
      Cog::Controllers::GeneratorController.run_all
    else
      args.each do |name|
        Cog::Controllers::GeneratorController.run name
      end
    end
  end

  c.desc 'List project generators'
  c.command :list do |sub|
    sub.action do |gopt, opt, args|
      x = Cog::Controllers::GeneratorController.list
      puts x.join "\n" unless x.empty?
    end
  end
  
  c.desc 'Create a new project generator'
  c.arg_name 'name'
  c.command :new do |sub|
    sub.desc 'Specify a target language other than the default'
    sub.default_value 'in Cogfile'
    sub.flag [:language, :l]
    
    sub.desc 'Specify the plugin which will be used to create the generator'
    sub.arg_name 'name'
    sub.default_value 'basic'
    sub.flag [:plugin, :p]
    
    sub.action do |gopt, opt, args|
      args.each do |name|
        if opt[:language] && opt[:language] != 'in Cogfile'
          lang = Cog::Languages.get_language opt[:language]
          Cog.target_language = lang
        end
        Cog::Controllers::GeneratorController.create name, :plugin_name => opt[:plugin]
      end
    end
  end
end

desc 'List or create plugins'
command [:plugin, :pl] do |c|
    
  c.default_desc 'List the available plugins'
  c.action do |gopt, opt, args|
    x = Cog::Controllers::PluginController.list
    puts x.join "\n" unless x.empty?
  end
  
  c.desc 'Create a new plugin'
  c.arg_name 'plugin_name'
  c.command :new do |sub|
    sub.action do |gopt, opt, args|
      args.each do |name|
        Cog::Controllers::PluginController.create name
      end
    end
  end
end

desc 'List or create ERB templates'
command [:template, :tm] do |c|
  
  c.default_desc 'List the available templates'
  c.action do |gopt, opt, args|
    x = Cog::Controllers::TemplateController.list
    puts x.join "\n" unless x.empty?
  end
  
  c.desc 'Create a new project template'
  c.command :new do |sub|
    sub.action do |gopt, opt, args|
      args.each do |name|
        Cog::Controllers::TemplateController.create name, :force_override => opt[:f]
      end
    end
  end
end

desc 'List supported languages'
command :lang do |c|
  c.action do
    summary = Cog.language_summary
    w = summary.collect {|info| info.name.length}.max
    summary.each do |info|
      puts info.to_s(w)
    end
  end
end

pre do |gopt, command, opt, args|
  Rainbow.enabled = false if gopt[:colorless]
  if gopt[:cogfile] && !File.exists?(gopt[:cogfile])
    STDERR.write "No such Cogfile at #{gopt[:cogfile]}\n"
    false
  else
    Cog.prepare(
      :minimal => command.name == :init,
      :project_cogfile_path => gopt[:cogfile],
      :fullpaths => gopt[:fullpaths]
      )
    true
  end
end

post do |global, command, options, args|
  # Post logic here
  # Use skips_post before a command to skip this
  # block on that command only
end

on_error do |exception|
  # Error logic here
  # return false to skip default error handling
  true
end

exit run(ARGV)