templater 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/templater.rb CHANGED
@@ -43,6 +43,6 @@ module Templater
43
43
  class MalformattedArgumentError < ArgumentError #:nodoc:
44
44
  end
45
45
 
46
- VERSION = '0.3.0'
46
+ VERSION = '0.3.1'
47
47
 
48
48
  end
@@ -73,6 +73,11 @@ module Templater
73
73
  puts "Generating with #{@generator_name} generator:"
74
74
  end
75
75
  step_through_templates
76
+
77
+ # Run hooks
78
+ @generator.after_run
79
+ @generator.after_generation unless @options[:delete]
80
+ @generator.after_deletion if @options[:delete]
76
81
  end
77
82
 
78
83
  def step_through_templates
@@ -165,9 +170,6 @@ module Templater
165
170
  say "#{diff.action} #{diff.element.chomp}"
166
171
  end
167
172
  end
168
-
169
173
  end
170
-
171
174
  end
172
-
173
- end
175
+ end
@@ -15,7 +15,7 @@ module Templater
15
15
 
16
16
  def self.run(destination_root, manifold, name, version, arguments)
17
17
 
18
- if arguments.first and not arguments.first =~ /^-/ and not arguments.first == "help"
18
+ unless ["help", "-h", "--help"].include?(arguments.first)
19
19
  generator_name = arguments.shift
20
20
  if generator_class = manifold.generator(generator_name)
21
21
  Generator.new(generator_name, generator_class, destination_root, name, version).run(arguments)
@@ -54,4 +54,4 @@ module Templater
54
54
 
55
55
  end
56
56
 
57
- end
57
+ end
@@ -1,22 +1,22 @@
1
1
  module Templater
2
-
2
+
3
3
  ACTION_RESERVED_OPTIONS = [:before, :after].freeze
4
4
 
5
5
  class Generator
6
-
6
+
7
7
  include Templater::CaptureHelpers
8
-
8
+
9
9
  class << self
10
-
10
+
11
11
  attr_accessor :manifold
12
12
 
13
-
13
+
14
14
  # Returns an array of hashes, where each hash describes a single argument.
15
15
  #
16
16
  # === Returns
17
17
  # Array[Hash{Symbol=>Object}]:: A list of arguments
18
18
  def arguments; @arguments ||= []; end
19
-
19
+
20
20
  # A shorthand method for adding the first argument, see +Templater::Generator.argument+
21
21
  def first_argument(*args); argument(0, *args); end
22
22
 
@@ -25,7 +25,7 @@ module Templater
25
25
 
26
26
  # A shorthand method for adding the third argument, see +Templater::Generator.argument+
27
27
  def third_argument(*args); argument(2, *args); end
28
-
28
+
29
29
  # A shorthand method for adding the fourth argument, see +Templater::Generator.argument+
30
30
  def fourth_argument(*args); argument(3, *args); end
31
31
 
@@ -42,7 +42,7 @@ module Templater
42
42
  # Array[Hash{Symbol=>Object}]:: A list of invocations
43
43
  def invocations; @invocations ||= []; end
44
44
 
45
-
45
+
46
46
  # Returns an Hash that maps the type of action to a list of ActionDescriptions.
47
47
  #
48
48
  # ==== Returns
@@ -54,7 +54,7 @@ module Templater
54
54
  # === Returns
55
55
  # Array[Templater::ActionDescription]:: A list of template descriptions.
56
56
  def templates; actions[:templates] ||= []; end
57
-
57
+
58
58
  # Returns an array of ActionDescriptions, where each describes a single file.
59
59
  #
60
60
  # === Returns
@@ -66,7 +66,7 @@ module Templater
66
66
  # === Returns
67
67
  # Array[Templater::ActionDescription]:: A list of file descriptions.
68
68
  def directories; actions[:directories] ||= []; end
69
-
69
+
70
70
  # Returns an array of ActionDescriptions, where each describes a single empty directory created by generator.
71
71
  #
72
72
  # ==== Returns
@@ -98,7 +98,7 @@ module Templater
98
98
  # name<Symbol>:: The name of this argument, an accessor with this name will be created for the argument
99
99
  # options<Hash>:: Options for this argument
100
100
  # &block<Proc>:: Is evaluated on assignment to check the validity of the argument
101
- #
101
+ #
102
102
  # ==== Options (opts)
103
103
  # :default<Object>:: Specify a default value for this argument
104
104
  # :as<Symbol>:: If set to :hash or :array, this argument will 'consume' all remaining arguments and bundle them
@@ -111,20 +111,20 @@ module Templater
111
111
  def #{name}
112
112
  get_argument(#{n})
113
113
  end
114
-
114
+
115
115
  def #{name}=(arg)
116
116
  set_argument(#{n}, arg)
117
117
  end
118
118
  CLASS
119
119
  end
120
-
120
+
121
121
  # Adds an accessor with the given name to this generator, also automatically fills that value through
122
122
  # the options hash that is provided when the generator is initialized.
123
123
  #
124
124
  # === Parameters
125
125
  # name<Symbol>:: The name of this option, an accessor with this name will be created for the option
126
126
  # options<Hash>:: Options for this option (how meta!)
127
- #
127
+ #
128
128
  # ==== Options (opts)
129
129
  # :default<Object>:: Specify a default value for this option
130
130
  # :as<Symbol>:: If set to :boolean provides a hint to the interface using this generator.
@@ -135,13 +135,13 @@ module Templater
135
135
  def #{name}
136
136
  get_option(:#{name})
137
137
  end
138
-
138
+
139
139
  def #{name}=(arg)
140
140
  set_option(:#{name}, arg)
141
141
  end
142
142
  CLASS
143
143
  end
144
-
144
+
145
145
  # Adds an invocation of another generator to this generator. This allows the interface to invoke
146
146
  # any templates in that target generator. This requires that the generator is part of a manifold. The name
147
147
  # provided is the name of the target generator in this generator's manifold.
@@ -169,12 +169,12 @@ module Templater
169
169
  # rand(100000).to_s
170
170
  # end
171
171
  #
172
- # # invoke :other_generator with some
172
+ # # invoke :other_generator with some
173
173
  # invoke :other_generator do |generator|
174
174
  # generator.new(destination_root, options, random)
175
175
  # end
176
176
  # end
177
- #
177
+ #
178
178
  # class MyGenerator < Templater::Generator
179
179
  # option :animal
180
180
  # # other_generator will be invoked only if the option 'animal' is set to 'bear'
@@ -183,7 +183,7 @@ module Templater
183
183
  def invoke(name, options={}, &block)
184
184
  self.invocations << InvocationDescription.new(name.to_sym, options, &block)
185
185
  end
186
-
186
+
187
187
  # Adds a template to this generator. Templates are named and can later be retrieved by that name.
188
188
  # Templates have a source and a destination. When a template is invoked, the source file is rendered,
189
189
  # passing through ERB, and the result is copied to the destination. Source and destination can be
@@ -227,14 +227,14 @@ module Templater
227
227
  options = args.last.is_a?(Hash) ? args.pop : {}
228
228
  source, destination = args
229
229
  source, destination = source + 't', source if args.size == 1
230
-
230
+
231
231
  templates << ActionDescription.new(name, options) do |generator|
232
232
  template = Actions::Template.new(generator, name, source, destination, options)
233
233
  generator.instance_exec(template, &block) if block
234
234
  template
235
235
  end
236
236
  end
237
-
237
+
238
238
  # Adds a template that is not rendered using ERB, but copied directly. Unlike Templater::Generator.template
239
239
  # this will not append a 't' to the source, otherwise it works identically.
240
240
  #
@@ -252,7 +252,7 @@ module Templater
252
252
  options = args.last.is_a?(Hash) ? args.pop : {}
253
253
  source, destination = args
254
254
  source, destination = source, source if args.size == 1
255
-
255
+
256
256
  files << ActionDescription.new(name, options) do |generator|
257
257
  file = Actions::File.new(generator, name, source, destination, options)
258
258
  generator.instance_exec(file, &block) if block
@@ -278,14 +278,14 @@ module Templater
278
278
  options = args.last.is_a?(Hash) ? args.pop : {}
279
279
  source, destination = args
280
280
  source, destination = source, source if args.size == 1
281
-
281
+
282
282
  directories << ActionDescription.new(name, options) do |generator|
283
283
  directory = Actions::Directory.new(generator, name, source, destination, options)
284
284
  generator.instance_exec(directory, &block) if block
285
285
  directory
286
286
  end
287
287
  end
288
-
288
+
289
289
  # Adds an empty directory that will be created when the generator is run.
290
290
  #
291
291
  # === Parameters
@@ -300,21 +300,21 @@ module Templater
300
300
  def empty_directory(name, *args, &block)
301
301
  options = args.last.is_a?(Hash) ? args.pop : {}
302
302
  destination = args.first
303
-
303
+
304
304
  empty_directories << ActionDescription.new(name, options) do |generator|
305
305
  directory = Actions::EmptyDirectory.new(generator, name, destination, options)
306
306
  generator.instance_exec(directory, &block) if block
307
307
  directory
308
308
  end
309
309
  end
310
-
310
+
311
311
  # An easy way to add many templates to a generator, each item in the list is added as a
312
312
  # template. The provided list can be either an array of Strings or a Here-Doc with templates
313
313
  # on individual lines.
314
314
  #
315
315
  # === Parameters
316
316
  # list<String|Array>:: A list of templates to be added to this generator
317
- #
317
+ #
318
318
  # === Examples
319
319
  #
320
320
  # class MyGenerator < Templater::Generator
@@ -323,20 +323,20 @@ module Templater
323
323
  # another/template.css
324
324
  # LIST
325
325
  # template_list ['a/third/template.rb', 'and/a/fourth.js']
326
- # end
326
+ # end
327
327
  def template_list(list)
328
328
  list.to_a.each do |item|
329
329
  item = item.to_s.chomp.strip
330
330
  self.template(item.gsub(/[\.\/]/, '_').to_sym, item)
331
331
  end
332
332
  end
333
-
333
+
334
334
  # An easy way to add many non-rendering templates to a generator. The provided list can be either an
335
335
  # array of Strings or a Here-Doc with templates on individual lines.
336
336
  #
337
337
  # === Parameters
338
338
  # list<String|Array>:: A list of non-rendering templates to be added to this generator
339
- #
339
+ #
340
340
  # === Examples
341
341
  #
342
342
  # class MyGenerator < Templater::Generator
@@ -358,7 +358,7 @@ module Templater
358
358
  #
359
359
  # === Parameters
360
360
  # list<String|Array>:: A list of non-rendering templates to be added to this generator
361
- #
361
+ #
362
362
  # === Examples
363
363
  #
364
364
  # class MyGenerator < Templater::Generator
@@ -375,12 +375,12 @@ module Templater
375
375
  end
376
376
  end
377
377
 
378
-
378
+
379
379
  # Search a directory for templates and files and add them to this generator. Any file
380
380
  # whose extension matches one of those provided in the template_extensions parameter
381
381
  # is considered a template and will be rendered with ERB, all others are considered
382
382
  # normal files and are simply copied.
383
- #
383
+ #
384
384
  # A hash of options can be passed which will be assigned to each file and template.
385
385
  # All of these options are matched against the options passed to the generator.
386
386
  #
@@ -394,7 +394,7 @@ module Templater
394
394
  ::Dir[::File.join(source_root, dir.to_s, '**/*')].each do |action|
395
395
  unless ::File.directory?(action)
396
396
  action = action.sub("#{source_root}/", '')
397
-
397
+
398
398
  if template_extensions.include?(::File.extname(action.sub(/\.%.+%$/,''))[1..-1]) or template_extensions.include?(::File.basename(action))
399
399
  template(action.downcase.gsub(/[^a-z0-9]+/, '_').to_sym, action, action)
400
400
  else
@@ -403,9 +403,9 @@ module Templater
403
403
  end
404
404
  end
405
405
  end
406
-
406
+
407
407
  # Returns a list of the classes of all generators (recursively) that are invoked together with this one.
408
- #
408
+ #
409
409
  # === Returns
410
410
  # Array[Templater::Generator]:: an array of generator classes.
411
411
  def generators
@@ -418,7 +418,7 @@ module Templater
418
418
  end
419
419
  generators.flatten.compact
420
420
  end
421
-
421
+
422
422
  # This should return the directory where source templates are located. This method must be overridden in
423
423
  # any Generator inheriting from Templater::Source.
424
424
  #
@@ -427,16 +427,16 @@ module Templater
427
427
  def source_root
428
428
  raise Templater::SourceNotSpecifiedError, "Subclasses of Templater::Generator must override the source_root method, to specify where source templates are located."
429
429
  end
430
-
430
+
431
431
  end # end of eigenclass
432
432
 
433
433
 
434
434
  #
435
435
  # ==== Instance methods
436
436
  #
437
-
437
+
438
438
  attr_accessor :destination_root, :arguments, :options
439
-
439
+
440
440
  # Create a new generator. Checks the list of arguments agains the requirements set using +argument+.
441
441
  #
442
442
  # === Parameters
@@ -450,12 +450,12 @@ module Templater
450
450
  @destination_root = destination_root
451
451
  @arguments = []
452
452
  @options = options
453
-
453
+
454
454
  # Initialize options to their default values.
455
455
  self.class.options.each do |option|
456
456
  @options[option.name] ||= option.options[:default]
457
457
  end
458
-
458
+
459
459
  args.each_with_index do |arg, n|
460
460
  set_argument(n, arg)
461
461
  end
@@ -467,7 +467,7 @@ module Templater
467
467
  argument.valid?(@arguments[i])
468
468
  end
469
469
  end
470
-
470
+
471
471
  # Finds and returns the template of the given name. If that template's options don't match the generator
472
472
  # options, returns nil.
473
473
  #
@@ -479,7 +479,7 @@ module Templater
479
479
  def template(name)
480
480
  templates.find { |t| t.name == name }
481
481
  end
482
-
482
+
483
483
  # Finds and returns the file of the given name. If that file's options don't match the generator
484
484
  # options, returns nil.
485
485
  #
@@ -499,7 +499,7 @@ module Templater
499
499
  def empty_directory(name)
500
500
  empty_directories.find { |d| d.name == name }
501
501
  end
502
-
502
+
503
503
  # Finds and returns all templates whose options match the generator options.
504
504
  #
505
505
  # === Returns
@@ -507,7 +507,7 @@ module Templater
507
507
  def templates
508
508
  actions(:templates)
509
509
  end
510
-
510
+
511
511
  # Finds and returns all files whose options match the generator options.
512
512
  #
513
513
  # === Returns
@@ -522,20 +522,20 @@ module Templater
522
522
  # [Templater::Actions::File]:: The found files.
523
523
  def empty_directories
524
524
  actions(:empty_directories)
525
- end
526
-
525
+ end
526
+
527
527
  # Finds and returns all templates whose options match the generator options.
528
528
  #
529
529
  # === Returns
530
530
  # [Templater::Generator]:: The found templates.
531
531
  def invocations
532
532
  return [] unless self.class.manifold
533
-
533
+
534
534
  self.class.invocations.map do |invocation|
535
535
  invocation.get(self) if match_options?(invocation.options)
536
536
  end.compact
537
537
  end
538
-
538
+
539
539
  # Finds and returns all templates and files for this generators whose options match its options.
540
540
  #
541
541
  # === Returns
@@ -547,7 +547,7 @@ module Templater
547
547
  actions
548
548
  end
549
549
  end
550
-
550
+
551
551
  # Finds and returns all templates and files for this generators and any of those generators it invokes,
552
552
  # whose options match that generator's options.
553
553
  #
@@ -558,12 +558,12 @@ module Templater
558
558
  all_actions += invocations.map { |i| i.all_actions(type) }
559
559
  all_actions.flatten
560
560
  end
561
-
561
+
562
562
  # Invokes the templates for this generator
563
563
  def invoke!
564
564
  all_actions.each { |t| t.invoke! }
565
565
  end
566
-
566
+
567
567
  # Renders all actions in this generator. Use this to verify that rendering templates raises no errors.
568
568
  #
569
569
  # === Returns
@@ -571,7 +571,7 @@ module Templater
571
571
  def render!
572
572
  all_actions.map { |t| t.render }
573
573
  end
574
-
574
+
575
575
  # Returns this generator's source root
576
576
  #
577
577
  # === Returns
@@ -582,7 +582,7 @@ module Templater
582
582
  def source_root
583
583
  self.class.source_root
584
584
  end
585
-
585
+
586
586
  # Returns the destination root that is given to the generator on initialization. If the generator is a
587
587
  # command line program, this would usually be Dir.pwd.
588
588
  #
@@ -591,9 +591,21 @@ module Templater
591
591
  def destination_root
592
592
  @destination_root # just here so it can be documented.
593
593
  end
594
-
594
+
595
+ def after_run
596
+ # override in subclasses if necessary
597
+ end
598
+
599
+ def after_generation
600
+ # override in subclasses if necessary
601
+ end
602
+
603
+ def after_deletion
604
+ # override in subclasses if necessary
605
+ end
606
+
595
607
  protected
596
-
608
+
597
609
  def set_argument(n, value)
598
610
  expected = self.class.arguments[n]
599
611
  raise Templater::TooManyArgumentsError, "This generator does not take this many Arguments" if expected.nil?
@@ -603,19 +615,19 @@ module Templater
603
615
  expected.valid?(value)
604
616
  @arguments[n] = value
605
617
  end
606
-
618
+
607
619
  def get_argument(n)
608
620
  @arguments[n]
609
621
  end
610
-
622
+
611
623
  def set_option(name, value)
612
624
  @options[name] = value
613
625
  end
614
-
626
+
615
627
  def get_option(name)
616
628
  @options[name]
617
629
  end
618
-
630
+
619
631
  def match_options?(options)
620
632
  options.all? do |key, value|
621
633
  key.to_sym.in?(Templater::ACTION_RESERVED_OPTIONS) or self.send(key) == value
@@ -623,5 +635,5 @@ module Templater
623
635
  end
624
636
 
625
637
  end
626
-
638
+
627
639
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: templater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
@@ -9,7 +9,7 @@ autorequire: templater
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-02 00:00:00 +03:00
12
+ date: 2008-10-08 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -57,73 +57,73 @@ files:
57
57
  - README
58
58
  - Rakefile
59
59
  - TODO
60
- - lib/templater.rb
61
60
  - lib/templater
62
- - lib/templater/description.rb
63
- - lib/templater/generator.rb
64
- - lib/templater/manifold.rb
65
- - lib/templater/core_ext
66
- - lib/templater/core_ext/kernel.rb
67
- - lib/templater/core_ext/string.rb
68
- - lib/templater/spec
69
- - lib/templater/spec/helpers.rb
61
+ - lib/templater/actions
62
+ - lib/templater/actions/action.rb
63
+ - lib/templater/actions/directory.rb
64
+ - lib/templater/actions/empty_directory.rb
65
+ - lib/templater/actions/file.rb
66
+ - lib/templater/actions/template.rb
70
67
  - lib/templater/capture_helpers.rb
71
68
  - lib/templater/cli
72
69
  - lib/templater/cli/generator.rb
73
70
  - lib/templater/cli/manifold.rb
74
71
  - lib/templater/cli/parser.rb
72
+ - lib/templater/core_ext
73
+ - lib/templater/core_ext/kernel.rb
74
+ - lib/templater/core_ext/string.rb
75
+ - lib/templater/description.rb
75
76
  - lib/templater/discovery.rb
76
- - lib/templater/actions
77
- - lib/templater/actions/directory.rb
78
- - lib/templater/actions/empty_directory.rb
79
- - lib/templater/actions/template.rb
80
- - lib/templater/actions/action.rb
81
- - lib/templater/actions/file.rb
82
- - spec/spec_helper.rb
77
+ - lib/templater/generator.rb
78
+ - lib/templater/manifold.rb
79
+ - lib/templater/spec
80
+ - lib/templater/spec/helpers.rb
81
+ - lib/templater.rb
82
+ - spec/actions
83
+ - spec/actions/directory_spec.rb
84
+ - spec/actions/empty_directory_spec.rb
85
+ - spec/actions/file_spec.rb
86
+ - spec/actions/template_spec.rb
87
+ - spec/core_ext
88
+ - spec/core_ext/string_spec.rb
83
89
  - spec/generator
84
- - spec/generator/invoke_spec.rb
85
- - spec/generator/invocations_spec.rb
86
- - spec/generator/files_spec.rb
87
- - spec/generator/glob_spec.rb
88
- - spec/generator/templates_spec.rb
89
- - spec/generator/empty_directories_spec.rb
90
90
  - spec/generator/actions_spec.rb
91
+ - spec/generator/arguments_spec.rb
91
92
  - spec/generator/desc_spec.rb
93
+ - spec/generator/destination_root_spec.rb
94
+ - spec/generator/empty_directories_spec.rb
95
+ - spec/generator/files_spec.rb
92
96
  - spec/generator/generators_spec.rb
93
- - spec/generator/arguments_spec.rb
94
- - spec/generator/source_root_spec.rb
97
+ - spec/generator/glob_spec.rb
98
+ - spec/generator/invocations_spec.rb
99
+ - spec/generator/invoke_spec.rb
95
100
  - spec/generator/options_spec.rb
96
- - spec/generator/destination_root_spec.rb
97
101
  - spec/generator/render_spec.rb
102
+ - spec/generator/source_root_spec.rb
103
+ - spec/generator/templates_spec.rb
104
+ - spec/manifold_spec.rb
105
+ - spec/results
106
+ - spec/results/erb.rbs
107
+ - spec/results/file.rbs
108
+ - spec/results/random.rbs
109
+ - spec/results/simple_erb.rbs
110
+ - spec/spec_helper.rb
111
+ - spec/spec_helpers_spec.rb
112
+ - spec/templater_spec.rb
98
113
  - spec/templates
114
+ - spec/templates/erb.rbt
99
115
  - spec/templates/glob
100
- - spec/templates/glob/test.rb
101
- - spec/templates/glob/hellothar.%feh%
102
- - spec/templates/glob/README
103
116
  - spec/templates/glob/arg.js
117
+ - spec/templates/glob/hellothar.%feh%
104
118
  - spec/templates/glob/hellothar.html.%feh%
119
+ - spec/templates/glob/README
105
120
  - spec/templates/glob/subfolder
106
- - spec/templates/glob/subfolder/monkey.rb
107
121
  - spec/templates/glob/subfolder/jessica_alba.jpg
122
+ - spec/templates/glob/subfolder/monkey.rb
123
+ - spec/templates/glob/test.rb
108
124
  - spec/templates/literals_erb.rbt
109
125
  - spec/templates/simple.rbt
110
126
  - spec/templates/simple_erb.rbt
111
- - spec/templates/erb.rbt
112
- - spec/core_ext
113
- - spec/core_ext/string_spec.rb
114
- - spec/templater_spec.rb
115
- - spec/spec_helpers_spec.rb
116
- - spec/manifold_spec.rb
117
- - spec/results
118
- - spec/results/simple_erb.rbs
119
- - spec/results/erb.rbs
120
- - spec/results/file.rbs
121
- - spec/results/random.rbs
122
- - spec/actions
123
- - spec/actions/file_spec.rb
124
- - spec/actions/empty_directory_spec.rb
125
- - spec/actions/directory_spec.rb
126
- - spec/actions/template_spec.rb
127
127
  has_rdoc: true
128
128
  homepage: http://templater.rubyforge.org/
129
129
  post_install_message: