Module: Temp::Runner

Defined in:
lib/temp/runner.rb

Overview

The Runner module contains methods for Temp’s command line interface.

Class Method Summary collapse

Class Method Details

.start!(args) ⇒ Object

Run Temp as a command line program



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/temp/runner.rb', line 9

def self.start!(args)
  conf = Temp::Config.new(Temp::Config::DEFAULT_CONF_FILE)
  options = {}

  OptionParser.new do |o|
    o.banner = 'Usage: temp [options] [project] [template]'

    o.on('-v', '--version', 'Show version information') do
      puts "Temp #{Temp::VERSION}"
      exit
    end

    o.on('-l', '--list [name]', 'List all templates') do |name|
      if name
        t = Temp::Template.new(name, conf)
        puts "#{t.name} - #{t.desc}"
      else
        Temp::Template.all(conf).each do |t|
          puts "#{t.name} - #{t.desc}"
        end
      end
      exit
    end

    o.on('-t', '--template-options [opts]',
    'Pass comma-separated option flags to the template') do |opts|
      opts.split(',').each { |o| options[o.to_sym] = true }
    end
  end.parse!(args)
  conf.template_options = options

  if ARGV.size == 0
    puts 'No template specified.'
  elsif ARGV.size == 1
    template = Temp::Template.new(ARGV[0], conf)
    Temp::Project.new(Dir.pwd, template).create
  else
    template = Temp::Template.new(ARGV[1], conf)
    Temp::Project.new(ARGV[0], template).create
  end
rescue => e
  case e.exception
  when Temp::Exceptions::ProjectExistsError
    puts 'Cannot create project because file exists there.'
  when Temp::Exceptions::TemplateNonExistentError
    puts 'The specified template does not exist.'
  when OptionParser::InvalidOption
    puts e.message
  else
    raise e
  end
end