Class: TemplateBuilder::App::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/template_builder/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Main

Create a new Main application instance. Options can be passed to configuret he stdout and stderr IO streams (very useful for testing).



26
27
28
29
30
31
32
33
# File 'lib/template_builder/app.rb', line 26

def initialize( opts = {} )
  opts[:stdout] ||= $stdout
  opts[:stderr] ||= $stderr

  @opts = opts
  @stdout = opts[:stdout]
  @stderr = opts[:stderr]
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



21
22
23
# File 'lib/template_builder/app.rb', line 21

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



20
21
22
# File 'lib/template_builder/app.rb', line 20

def stdout
  @stdout
end

Instance Method Details

#helpObject

Show the toplevel Template builder help message.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/template_builder/app.rb', line 76

def help
  msg = <<-MSG
NAME
  Template_builder v#{::TemplateBuilder.version}

DESCRIPTION
  Template-builder is a handy tool that builds a template for your new Ruby
  on Rails projects. The template contains all you need to build the RoR 
  project that you want.

  Usage:
template_builder -h/--help
template_builder -v/--version
template_builder [command] [parameter type parameter type] [options]

  Examples:
template_builder new <new_template_file>
template_builder edit<old_template_file>

  Commands:
  MSG
 fmt = lambda { |cmd|
         if @all_commands[cmd] < ::TemplateBuilder::App::Command
           msg << "    template_builder %-15s %s\n" % [cmd, @all_commands[cmd].summary]
           puts msg
         end
       }
 ary = [:new, :show, :add]
 ary.each(&fmt)
 (@all_commands.keys - ary).each(&fmt)
  msg.concat <<-MSG

  Further Help:
Each command has a '--help' option that will provide detailed
information for that command.

http://github.com/alaibe/template_builder

  MSG

stdout.puts msg
end

#run(args) ⇒ Object

Parse the desired user command and run that command object.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/template_builder/app.rb', line 37

def run( args )
  commands = []
  @all_commands = ::TemplateBuilder::App.plugins
  @all_commands.each { |k,v| commands << k.to_s if v < ::TemplateBuilder::App::Command }
  cmd_str = args.shift
  cmd = case cmd_str
    when *commands
      key = cmd_str.to_sym
      @all_commands[key].new @opts       
    when nil, '-h', '--help'
      help
    when '-v', '--version'
      stdout.puts "TemplateBuilder v#{::TemplateBuilder.version}"
    else
      help
      raise Error, "Unknown command #{cmd_str.inspect}"
    end

  if cmd
    cmd.parse args
    cmd.run
  end

rescue TemplateBuilder::App::Error => err
  stderr.puts "ERROR:  While executing template builder ..."
  stderr.puts "    #{err.message}"
  exit 1
rescue StandardError => err
  stderr.puts "ERROR:  While executing template builder ... (#{err.class})"
  stderr.puts "    #{err.to_s}"
  exit 1
rescue Exception => err
  stderr.puts "ERROR:  While executing template builder ... (#{err.class})"
  stderr.puts "    #{err.to_s}"
  exit 1
end