templater 0.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +13 -1
- data/lib/templater/actions/empty_directory.rb +4 -4
- data/lib/templater/actions/template.rb +2 -2
- data/lib/templater/description.rb +17 -0
- data/lib/templater/generator.rb +12 -43
- data/lib/templater.rb +1 -1
- data/spec/actions/empty_directory_spec.rb +7 -4
- data/spec/actions/file_spec.rb +2 -2
- data/spec/actions/template_spec.rb +2 -2
- data/spec/generator/arguments_spec.rb +5 -5
- metadata +2 -5
data/Rakefile
CHANGED
@@ -14,6 +14,19 @@ HOMEPAGE = "http://templater.rubyforge.org/"
|
|
14
14
|
SUMMARY = "File generation system"
|
15
15
|
|
16
16
|
|
17
|
+
# Used by release task
|
18
|
+
RUBY_FORGE_PROJECT = "templater"
|
19
|
+
GEM_NAME = NAME
|
20
|
+
PROJECT_URL = HOMEPAGE
|
21
|
+
PROJECT_SUMMARY = SUMMARY
|
22
|
+
PROJECT_DESCRIPTION = SUMMARY
|
23
|
+
|
24
|
+
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
25
|
+
GEM_VERSION = Templater::VERSION + PKG_BUILD
|
26
|
+
RELEASE_NAME = "REL #{GEM_VERSION}"
|
27
|
+
|
28
|
+
require "extlib/tasks/release"
|
29
|
+
|
17
30
|
#
|
18
31
|
# ==== Gemspec and installation
|
19
32
|
#
|
@@ -124,7 +137,6 @@ namespace :spec do
|
|
124
137
|
t.spec_opts = ["--format", "html:doc/reports/specs.html"]
|
125
138
|
t.fail_on_error = false
|
126
139
|
end
|
127
|
-
|
128
140
|
end
|
129
141
|
|
130
142
|
desc 'Default: run unit tests.'
|
@@ -16,12 +16,12 @@ module Templater
|
|
16
16
|
self.options = options
|
17
17
|
end
|
18
18
|
|
19
|
-
# Returns
|
19
|
+
# Returns an empty String: there's nothing to read from.
|
20
20
|
#
|
21
21
|
# === Returns
|
22
22
|
# String:: The source file.
|
23
23
|
def render
|
24
|
-
|
24
|
+
''
|
25
25
|
end
|
26
26
|
|
27
27
|
# Checks if the destination file already exists.
|
@@ -42,9 +42,9 @@ module Templater
|
|
42
42
|
|
43
43
|
# Renders the template and copies it to the destination.
|
44
44
|
def invoke!
|
45
|
-
|
45
|
+
callback(:before)
|
46
46
|
::FileUtils.mkdir_p(destination)
|
47
|
-
|
47
|
+
callback(:after)
|
48
48
|
end
|
49
49
|
|
50
50
|
# removes the destination file
|
@@ -44,10 +44,10 @@ module Templater
|
|
44
44
|
|
45
45
|
# Renders the template and copies it to the destination.
|
46
46
|
def invoke!
|
47
|
-
|
47
|
+
callback(:before)
|
48
48
|
::FileUtils.mkdir_p(::File.dirname(destination))
|
49
49
|
::File.open(destination, 'w') {|f| f.write render }
|
50
|
-
|
50
|
+
callback(:after)
|
51
51
|
end
|
52
52
|
|
53
53
|
# removes the destination file
|
@@ -44,6 +44,23 @@ module Templater
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
def extract(argument)
|
48
|
+
case options[:as]
|
49
|
+
when :hash
|
50
|
+
if argument.is_a?(String)
|
51
|
+
return argument.split(',').inject({}) do |h, pair|
|
52
|
+
key, value = pair.split(':')
|
53
|
+
raise Templater::MalformattedArgumentError, "Expected '#{argument.inspect}' to be a key/value pair" unless key and value
|
54
|
+
h[key] = value
|
55
|
+
h
|
56
|
+
end
|
57
|
+
end
|
58
|
+
when :array
|
59
|
+
return argument.split(',') if argument.is_a?(String)
|
60
|
+
end
|
61
|
+
return argument
|
62
|
+
end
|
63
|
+
|
47
64
|
end
|
48
65
|
|
49
66
|
class InvocationDescription < Description
|
data/lib/templater/generator.rb
CHANGED
@@ -401,14 +401,16 @@ module Templater
|
|
401
401
|
@options[option.name] ||= option.options[:default]
|
402
402
|
end
|
403
403
|
|
404
|
-
|
404
|
+
args.each_with_index do |arg, n|
|
405
|
+
set_argument(n, arg)
|
406
|
+
end
|
405
407
|
|
406
|
-
# Initialize arguments to their default values.
|
407
408
|
self.class.arguments.each_with_index do |argument, i|
|
409
|
+
# Initialize arguments to their default values.
|
408
410
|
@arguments[i] ||= argument.options[:default]
|
411
|
+
# Check if all arguments are valid.
|
412
|
+
argument.valid?(@arguments[i])
|
409
413
|
end
|
410
|
-
|
411
|
-
valid_arguments?
|
412
414
|
end
|
413
415
|
|
414
416
|
# Finds and returns the template of the given name. If that template's options don't match the generator
|
@@ -538,7 +540,12 @@ module Templater
|
|
538
540
|
protected
|
539
541
|
|
540
542
|
def set_argument(n, value)
|
541
|
-
self.class.arguments[n]
|
543
|
+
expected = self.class.arguments[n]
|
544
|
+
raise Templater::TooManyArgumentsError, "This generator does not take this many Arguments" if expected.nil?
|
545
|
+
|
546
|
+
value = expected.extract(value)
|
547
|
+
|
548
|
+
expected.valid?(value)
|
542
549
|
@arguments[n] = value
|
543
550
|
end
|
544
551
|
|
@@ -559,45 +566,7 @@ module Templater
|
|
559
566
|
key.to_sym.in?(Templater::ACTION_RESERVED_OPTIONS) or self.send(key) == value
|
560
567
|
end
|
561
568
|
end
|
562
|
-
|
563
|
-
def valid_arguments?
|
564
|
-
self.class.arguments.each_with_index do |arg, i|
|
565
|
-
arg.valid?(@arguments[i])
|
566
|
-
end
|
567
|
-
end
|
568
569
|
|
569
|
-
# from a list of arguments, walk through that list and assign it to this generator, taking into account
|
570
|
-
# that an argument could be a hash or array that consumes the remaining arguments.
|
571
|
-
def extract_arguments(*args)
|
572
|
-
args.each_with_index do |arg, i|
|
573
|
-
expected = self.class.arguments[i]
|
574
|
-
raise Templater::TooManyArgumentsError, "This generator does not take this many Arguments" if expected.nil?
|
575
|
-
|
576
|
-
# When one of the arguments has :as set to :hash or :list, the remaining arguments should be consumed
|
577
|
-
# and converted to a Hash or an Array respectively
|
578
|
-
case expected.options[:as]
|
579
|
-
when :hash
|
580
|
-
if arg.is_a?(String)
|
581
|
-
pairs = args[i..-1]
|
582
|
-
|
583
|
-
hash = pairs.inject({}) do |h, pair|
|
584
|
-
key, value = pair.split(':')
|
585
|
-
raise Templater::MalformattedArgumentError, "Expected '#{arg.inspect}' to be a key/value pair" unless key and value
|
586
|
-
h[key] = value
|
587
|
-
h
|
588
|
-
end
|
589
|
-
|
590
|
-
set_argument(i, hash) and return
|
591
|
-
else
|
592
|
-
set_argument(i, arg)
|
593
|
-
end
|
594
|
-
when :array
|
595
|
-
set_argument(i, args[i..-1].flatten) and return
|
596
|
-
else
|
597
|
-
set_argument(i, arg)
|
598
|
-
end
|
599
|
-
end
|
600
|
-
end
|
601
570
|
end
|
602
571
|
|
603
572
|
end
|
data/lib/templater.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require File.dirname(__FILE__)
|
2
|
-
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
3
|
describe Templater::Actions::EmptyDirectory do
|
4
4
|
|
5
5
|
before do
|
@@ -29,7 +29,10 @@ describe Templater::Actions::EmptyDirectory do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
describe '#render' do
|
32
|
-
it '
|
32
|
+
it 'should return an empty string' do
|
33
|
+
file = Templater::Actions::EmptyDirectory.new(@generator, :monkey, '/path/to/destination')
|
34
|
+
file.render.should == ''
|
35
|
+
end
|
33
36
|
end
|
34
37
|
|
35
38
|
describe '#exists?' do
|
@@ -102,4 +105,4 @@ describe Templater::Actions::EmptyDirectory do
|
|
102
105
|
end
|
103
106
|
end
|
104
107
|
|
105
|
-
end
|
108
|
+
end
|
data/spec/actions/file_spec.rb
CHANGED
@@ -160,8 +160,8 @@ describe Templater::Generator, '.argument as array' do
|
|
160
160
|
instance.llama[0].should == 'test'
|
161
161
|
end
|
162
162
|
|
163
|
-
it "should
|
164
|
-
instance = @generator_class.new('/tmp', {}, 'a monkey', 'test
|
163
|
+
it "should split the remaining arguments by comma and convert them to an array" do
|
164
|
+
instance = @generator_class.new('/tmp', {}, 'a monkey', 'test,silver,river')
|
165
165
|
instance.llama[0].should == 'test'
|
166
166
|
instance.llama[1].should == 'silver'
|
167
167
|
instance.llama[2].should == 'river'
|
@@ -197,15 +197,15 @@ describe Templater::Generator, '.argument as hash' do
|
|
197
197
|
instance.llama['test'].should == 'unit'
|
198
198
|
end
|
199
199
|
|
200
|
-
it "should
|
201
|
-
instance = @generator_class.new('/tmp', {}, 'a monkey', 'test:unit
|
200
|
+
it "should split by comma and convert them to a hash if they are key/value pairs" do
|
201
|
+
instance = @generator_class.new('/tmp', {}, 'a monkey', 'test:unit,john:silver,river:road')
|
202
202
|
instance.llama['test'].should == 'unit'
|
203
203
|
instance.llama['john'].should == 'silver'
|
204
204
|
instance.llama['river'].should == 'road'
|
205
205
|
end
|
206
206
|
|
207
207
|
it "should raise an error if one of the remaining arguments is not a key/value pair" do
|
208
|
-
lambda { @generator_class.new('/tmp', {}, 'a monkey', 'a:llama
|
208
|
+
lambda { @generator_class.new('/tmp', {}, 'a monkey', 'a:llama,duck:llama,not_a_pair,pair:blah') }.should raise_error(Templater::MalformattedArgumentError)
|
209
209
|
end
|
210
210
|
|
211
211
|
it "should raise error if the argument is neither a hash nor a key/value pair" do
|
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:
|
4
|
+
version: 0.2.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-09-
|
12
|
+
date: 2008-09-28 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -103,9 +103,6 @@ files:
|
|
103
103
|
- spec/results
|
104
104
|
- spec/results/erb.rbs
|
105
105
|
- spec/results/file.rbs
|
106
|
-
- spec/results/path
|
107
|
-
- spec/results/path/to
|
108
|
-
- spec/results/path/to/subdir
|
109
106
|
- spec/results/random.rbs
|
110
107
|
- spec/results/simple_erb.rbs
|
111
108
|
- spec/spec_helper.rb
|