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 = "NAME\n  Template_builder v\#{::TemplateBuilder.version}\n\nDESCRIPTION\n  Template-builder is a handy tool that builds a template for your new Ruby\n  on Rails projects. The template contains all you need to build the RoR \n  project that you want.\n\n  Usage:\ntemplate_builder -h/--help\ntemplate_builder -v/--version\ntemplate_builder [command] [parameter type parameter type] [options]\n\n  Examples:\ntemplate_builder new <new_template_file>\ntemplate_builder edit<old_template_file>\n\n  Commands:\n  MSG\n fmt = lambda { |cmd|\n         if @all_commands[cmd] < ::TemplateBuilder::App::Command\n           msg << \"    template_builder %-15s %s\\n\" % [cmd, @all_commands[cmd].summary]\n           puts msg\n         end\n       }\n ary = [:new, :show, :add]\n ary.each(&fmt)\n (@all_commands.keys - ary).each(&fmt)\n  msg.concat <<-MSG\n\n  Further Help:\nEach command has a '--help' option that will provide detailed\ninformation for that command.\n\nhttp://github.com/alaibe/template_builder\n\n  MSG\n\nstdout.puts msg\nend\n"

#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