templater 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,11 +5,11 @@ require 'highline'
5
5
  require "highline/import"
6
6
  require 'diff/lcs'
7
7
 
8
-
8
+ require path + 'discovery'
9
9
  require path + 'capture_helpers'
10
- require path + 'template'
11
- require path + 'file'
12
- require path + 'empty_directory'
10
+ require path + 'actions/template'
11
+ require path + 'actions/file'
12
+ require path + 'actions/empty_directory'
13
13
  require path + 'generator'
14
14
  require path + 'proxy'
15
15
  require path + 'manifold'
@@ -40,6 +40,6 @@ module Templater
40
40
  class MalformattedArgumentError < ArgumentError #:nodoc:
41
41
  end
42
42
 
43
- VERSION = '0.1.3'
43
+ VERSION = '0.1.4'
44
44
 
45
45
  end
@@ -0,0 +1,55 @@
1
+ module Templater
2
+ module Actions
3
+ class EmptyDirectory
4
+
5
+ attr_reader :name, :destination
6
+
7
+ def initialize(name, destination)
8
+ @name, @destination = name, destination
9
+ end
10
+
11
+ # Returns the destination path relative to Dir.pwd. This is useful for prettier output in interfaces
12
+ # where the destination root is Dir.pwd.
13
+ #
14
+ # === Returns
15
+ # String:: The destination relative to Dir.pwd
16
+ def relative_destination
17
+ @destination.sub(::Dir.pwd + ::File::SEPARATOR, '')
18
+ end
19
+
20
+ # Returns the contents of the source file as a String
21
+ #
22
+ # === Returns
23
+ # String:: The source file.
24
+ def render
25
+ ::File.read(source)
26
+ end
27
+
28
+ # Checks if the destination file already exists.
29
+ #
30
+ # === Returns
31
+ # Boolean:: true if the file exists, false otherwise.
32
+ def exists?
33
+ ::File.exists?(destination)
34
+ end
35
+
36
+ # For empty directory this is in fact alias for exists? method.
37
+ #
38
+ # === Returns
39
+ # Boolean:: true if it is identical, false otherwise.
40
+ def identical?
41
+ exists?
42
+ end
43
+
44
+ # Renders the template and copies it to the destination.
45
+ def invoke!
46
+ ::FileUtils.mkdir_p(destination)
47
+ end
48
+
49
+ # removes the destination file
50
+ def revoke!
51
+ ::FileUtils.rm_rf(::File.expand_path(destination))
52
+ end
53
+ end # EmptyDirectory
54
+ end # Actions
55
+ end # Templater
@@ -0,0 +1,65 @@
1
+ module Templater
2
+ module Actions
3
+ class File
4
+
5
+ attr_accessor :name, :source, :destination
6
+
7
+ # Builds a new file, given the name of the file and its source and destination.
8
+ #
9
+ # === Parameters
10
+ # name<Symbol>:: The name of this template
11
+ # source<String>:: Full path to the source of this template
12
+ # destination<String>:: Full path to the destination of this template
13
+ def initialize(name, source, destination)
14
+ @name = name
15
+ @source = source
16
+ @destination = destination
17
+ end
18
+
19
+ # Returns the destination path relative to Dir.pwd. This is useful for prettier output in interfaces
20
+ # where the destination root is Dir.pwd.
21
+ #
22
+ # === Returns
23
+ # String:: The destination relative to Dir.pwd
24
+ def relative_destination
25
+ @destination.sub(::Dir.pwd + ::File::SEPARATOR, '')
26
+ end
27
+
28
+ # Returns the contents of the source file as a String
29
+ #
30
+ # === Returns
31
+ # String:: The source file.
32
+ def render
33
+ ::File.read(source)
34
+ end
35
+
36
+ # Checks if the destination file already exists.
37
+ #
38
+ # === Returns
39
+ # Boolean:: true if the file exists, false otherwise.
40
+ def exists?
41
+ ::File.exists?(destination)
42
+ end
43
+
44
+ # Checks if the content of the file at the destination is identical to the rendered result.
45
+ #
46
+ # === Returns
47
+ # Boolean:: true if it is identical, false otherwise.
48
+ def identical?
49
+ exists? && ::FileUtils.identical?(source, destination)
50
+ end
51
+
52
+ # Renders the template and copies it to the destination.
53
+ def invoke!
54
+ ::FileUtils.mkdir_p(::File.dirname(destination))
55
+ ::FileUtils.copy_file(source, destination)
56
+ end
57
+
58
+ # removes the destination file
59
+ def revoke!
60
+ ::FileUtils.rm(destination)
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,69 @@
1
+ module Templater
2
+ module Actions
3
+ class Template
4
+
5
+ attr_accessor :context, :name, :source, :destination, :options
6
+
7
+ # Builds a new template, given the context (e.g. binding) in which the template will be rendered
8
+ # (usually a generator), the name of the template and its source and destination.
9
+ #
10
+ # === Parameters
11
+ # context<Object>:: Context for rendering
12
+ # name<Symbol>:: The name of this template
13
+ # source<String>:: Full path to the source of this template
14
+ # destination<String>:: Full path to the destination of this template
15
+ # render<Boolean>:: If set to false, will do a copy instead of rendering.
16
+ def initialize(context, name, source, destination)
17
+ @context = context
18
+ @name = name
19
+ @source = source
20
+ @destination = destination
21
+ end
22
+
23
+ # Returns the destination path relative to Dir.pwd. This is useful for prettier output in interfaces
24
+ # where the destination root is Dir.pwd.
25
+ #
26
+ # === Returns
27
+ # String:: The destination relative to Dir.pwd
28
+ def relative_destination
29
+ @destination.sub(::Dir.pwd + ::File::SEPARATOR, '')
30
+ end
31
+
32
+ # Renders the template using ERB and returns the result as a String.
33
+ #
34
+ # === Returns
35
+ # String:: The rendered template.
36
+ def render
37
+ ERB.new(::File.read(source), nil, '-').result(context.send(:binding))
38
+ end
39
+
40
+ # Checks if the destination file already exists.
41
+ #
42
+ # === Returns
43
+ # Boolean:: true if the file exists, false otherwise.
44
+ def exists?
45
+ ::File.exists?(destination)
46
+ end
47
+
48
+ # Checks if the content of the file at the destination is identical to the rendered result.
49
+ #
50
+ # === Returns
51
+ # Boolean:: true if it is identical, false otherwise.
52
+ def identical?
53
+ ::File.read(destination) == render if ::File.exists?(destination)
54
+ end
55
+
56
+ # Renders the template and copies it to the destination.
57
+ def invoke!
58
+ ::FileUtils.mkdir_p(::File.dirname(destination))
59
+ ::File.open(destination, 'w') {|f| f.write render }
60
+ end
61
+
62
+ # removes the destination file
63
+ def revoke!
64
+ ::FileUtils.rm(destination)
65
+ end
66
+
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,79 @@
1
+ module Templater
2
+
3
+ # This provides a hook system which programs that use Templater can use to discover generators
4
+ # installed through gems. This requires two separate things, the Templater-using progrma will
5
+ # have to call the #discover! method giving a scope, like this:
6
+ #
7
+ # Templater::Discovery.discover!("name-of-scope")
8
+ #
9
+ # Where "name-of-scope" should be a string that uniquely identifies your program. Any gem wishing
10
+ # to then add a generator, that is automatically picked up, will then need to add a Generators
11
+ # file at the root of the project (don't forget to add it to the gem's manifest of files).
12
+ #
13
+ # - lib /
14
+ # - spec /
15
+ # - Rakefile
16
+ # - Generators
17
+ #
18
+ # This file should look something like this:
19
+ #
20
+ # scope "name-of-scope" do
21
+ # require ...something...
22
+ # end
23
+ #
24
+ # Multiple scopes can be added to the same Generators file for use with different generator
25
+ # programs.
26
+ module Discovery
27
+
28
+ extend self
29
+
30
+ # Adds a block of code specific for a certain scope of generators, where the scope would
31
+ # probably be the name of the program running the generator.
32
+ #
33
+ # === Parameters
34
+ # scope<String>:: The name of the scope
35
+ # block<&Proc>:: A block of code to execute provided the scope is correct
36
+ def scope(scope, &block)
37
+ @scopes[scope] ||= []
38
+ @scopes[scope] << block
39
+ end
40
+
41
+ # Searches installed gems for Generators files and loads all code blocks in them that match
42
+ # the given scope.
43
+ #
44
+ # === Parameters
45
+ # scope<String>:: The name of the scope to search for
46
+ def discover!(scope)
47
+ @scopes = {}
48
+ generator_files.each do |file|
49
+ load file
50
+ end
51
+ @scopes[scope].each { |block| block.call } if @scopes[scope]
52
+ end
53
+
54
+ protected
55
+
56
+ def find_latest_gems
57
+ Gem::cache.inject({}) do |latest_gems, cache|
58
+ name, gem = cache
59
+ currently_latest = latest_gems[gem.name]
60
+ latest_gems[gem.name] = gem if currently_latest.nil? or gem.version > currently_latest.version
61
+ latest_gems
62
+ end.values
63
+ end
64
+
65
+ def generator_files
66
+ find_latest_gems.inject([]) do |files, gem|
67
+ path = ::File.join(gem.full_gem_path, "Generators")
68
+ files << path if ::File.exists?(path) and not ::File.directory?(path)
69
+ files
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ def scope(scope, &block) #:nodoc:
78
+ Templater::Discovery.scope(scope, &block)
79
+ end
@@ -407,7 +407,7 @@ module Templater
407
407
  # name<Symbol>:: The name of the template to look up.
408
408
  #
409
409
  # === Returns
410
- # Templater::Template:: The found template.
410
+ # Templater::Actions::Template:: The found template.
411
411
  def template(name)
412
412
  self.templates.find { |t| t.name == name }
413
413
  end
@@ -419,7 +419,7 @@ module Templater
419
419
  # name<Symbol>:: The name of the file to look up.
420
420
  #
421
421
  # === Returns
422
- # Templater::File:: The found file.
422
+ # Templater::Actions::File:: The found file.
423
423
  def file(name)
424
424
  self.files.find { |f| f.name == name }
425
425
  end
@@ -427,7 +427,7 @@ module Templater
427
427
  # Finds and returns all empty directories whose options match the generator options.
428
428
  #
429
429
  # === Returns
430
- # [Templater::EmptyDirectory]:: The found empty directories that generator creates.
430
+ # [Templater::Actions::EmptyDirectory]:: The found empty directories that generator creates.
431
431
  def empty_directory(name)
432
432
  self.empty_directories.find { |d| d.name == name }
433
433
  end
@@ -435,7 +435,7 @@ module Templater
435
435
  # Finds and returns all templates whose options match the generator options.
436
436
  #
437
437
  # === Returns
438
- # [Templater::Template]:: The found templates.
438
+ # [Templater::Actions::Template]:: The found templates.
439
439
  def templates
440
440
  templates = self.class.templates.map do |t|
441
441
  template = Templater::Proxy.new(self, t[:name], t[:source], t[:destination], &t[:block]).to_template
@@ -447,7 +447,7 @@ module Templater
447
447
  # Finds and returns all files whose options match the generator options.
448
448
  #
449
449
  # === Returns
450
- # [Templater::File]:: The found files.
450
+ # [Templater::Actions::File]:: The found files.
451
451
  def files
452
452
  files = self.class.files.map do |t|
453
453
  file = Templater::Proxy.new(self, t[:name], t[:source], t[:destination], &t[:block]).to_file
@@ -459,7 +459,7 @@ module Templater
459
459
  # Finds and returns all empty directories generator creates.
460
460
  #
461
461
  # === Returns
462
- # [Templater::File]:: The found files.
462
+ # [Templater::Actions::File]:: The found files.
463
463
  def empty_directories
464
464
  self.class.empty_directories.map do |t|
465
465
  Templater::Proxy.new(self, t[:name], nil, t[:destination], &t[:block]).to_empty_directory
@@ -490,7 +490,7 @@ module Templater
490
490
  # whose options match that generator's options.
491
491
  #
492
492
  # === Returns
493
- # [Templater::File, Templater::Template]:: The found templates and files.
493
+ # [Templater::Actions::File, Templater::Actions::Template]:: The found templates and files.
494
494
  def actions
495
495
  actions = templates + files + empty_directories
496
496
  actions += invocations.map { |i| i.actions }
@@ -17,17 +17,17 @@ module Templater
17
17
 
18
18
  def to_template
19
19
  instance_eval(&@block) if @block
20
- Templater::Template.new(@generator, @name, get_source, get_destination)
20
+ Templater::Actions::Template.new(@generator, @name, get_source, get_destination)
21
21
  end
22
22
 
23
23
  def to_file
24
24
  instance_eval(&@block) if @block
25
- Templater::File.new(@name, get_source, get_destination)
25
+ Templater::Actions::File.new(@name, get_source, get_destination)
26
26
  end
27
27
 
28
28
  def to_empty_directory
29
29
  instance_eval(&@block) if @block
30
- Templater::EmptyDirectory.new(@name, get_destination)
30
+ Templater::Actions::EmptyDirectory.new(@name, get_destination)
31
31
  end
32
32
 
33
33
  def method_missing(method, *args, &block)
@@ -1,13 +1,13 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
- describe Templater::EmptyDirectory, '.new' do
3
+ describe Templater::Actions::EmptyDirectory, '.new' do
4
4
  it "sets name and destination" do
5
- Templater::EmptyDirectory.new(:monkey, '/path/to/destination').
5
+ Templater::Actions::EmptyDirectory.new(:monkey, '/path/to/destination').
6
6
  name.should == :monkey
7
7
  end
8
8
 
9
9
  it 'sets destination' do
10
- Templater::EmptyDirectory.new(:monkey, '/path/to/destination').
10
+ Templater::Actions::EmptyDirectory.new(:monkey, '/path/to/destination').
11
11
  destination.should == '/path/to/destination'
12
12
  end
13
13
  end
@@ -15,10 +15,10 @@ end
15
15
 
16
16
 
17
17
 
18
- describe Templater::EmptyDirectory, '#relative_destination' do
18
+ describe Templater::Actions::EmptyDirectory, '#relative_destination' do
19
19
  it "returns destination relative to the pwd" do
20
20
  Dir.stub!(:pwd).and_return('/path/to')
21
- file = Templater::EmptyDirectory.new(:monkey, '/path/to/destination/with/some/more/subdirs')
21
+ file = Templater::Actions::EmptyDirectory.new(:monkey, '/path/to/destination/with/some/more/subdirs')
22
22
  file.relative_destination.should == 'destination/with/some/more/subdirs'
23
23
  end
24
24
  end
@@ -26,42 +26,42 @@ end
26
26
 
27
27
 
28
28
 
29
- describe Templater::EmptyDirectory, '#render' do
29
+ describe Templater::Actions::EmptyDirectory, '#render' do
30
30
  it 'does nothing for empty directories?'
31
31
  end
32
32
 
33
33
 
34
34
 
35
35
 
36
- describe Templater::EmptyDirectory, '#exists?' do
36
+ describe Templater::Actions::EmptyDirectory, '#exists?' do
37
37
 
38
38
  it "should exist if the destination file exists" do
39
- file = Templater::EmptyDirectory.new(:monkey, result_path('erb.rbs'))
39
+ file = Templater::Actions::EmptyDirectory.new(:monkey, result_path('erb.rbs'))
40
40
  file.should be_exists
41
41
  end
42
42
 
43
43
  it "should not exist if the destination file does not exist" do
44
- file = Templater::EmptyDirectory.new(:monkey, result_path('some_weird_file.rbs'))
44
+ file = Templater::Actions::EmptyDirectory.new(:monkey, result_path('some_weird_file.rbs'))
45
45
  file.should_not be_exists
46
46
  end
47
47
  end
48
48
 
49
49
 
50
50
 
51
- describe Templater::EmptyDirectory, '#identical' do
51
+ describe Templater::Actions::EmptyDirectory, '#identical' do
52
52
  it "should not be identical if the destination file doesn't exist" do
53
- file = Templater::EmptyDirectory.new(:monkey, result_path('some_weird/path/that_does/not_exist'))
53
+ file = Templater::Actions::EmptyDirectory.new(:monkey, result_path('some_weird/path/that_does/not_exist'))
54
54
  file.should_not be_identical
55
55
  end
56
56
 
57
57
  it "should not be identical if the destination file is not identical to the source file" do
58
- file = Templater::EmptyDirectory.new(:monkey, result_path('simple_erb.rbs'))
58
+ file = Templater::Actions::EmptyDirectory.new(:monkey, result_path('simple_erb.rbs'))
59
59
  file.should be_exists
60
60
  file.should be_identical
61
61
  end
62
62
 
63
63
  it "should be identical if the destination file is identical to the source file" do
64
- file= Templater::EmptyDirectory.new(:monkey, result_path('file.rbs'))
64
+ file= Templater::Actions::EmptyDirectory.new(:monkey, result_path('file.rbs'))
65
65
  file.should be_exists
66
66
  file.should be_identical
67
67
  end
@@ -69,9 +69,9 @@ end
69
69
 
70
70
 
71
71
 
72
- describe Templater::EmptyDirectory, '#invoke!' do
72
+ describe Templater::Actions::EmptyDirectory, '#invoke!' do
73
73
  it "should copy the source file to the destination" do
74
- file = Templater::EmptyDirectory.new(:monkey, result_path('path/to/subdir/test2.rbs'))
74
+ file = Templater::Actions::EmptyDirectory.new(:monkey, result_path('path/to/subdir/test2.rbs'))
75
75
 
76
76
  file.invoke!
77
77
 
@@ -84,9 +84,9 @@ end
84
84
 
85
85
 
86
86
 
87
- describe Templater::EmptyDirectory, '#revoke!' do
87
+ describe Templater::Actions::EmptyDirectory, '#revoke!' do
88
88
  it "removes the destination directory" do
89
- file = Templater::EmptyDirectory.new(:monkey, result_path('path/to/empty/subdir/'))
89
+ file = Templater::Actions::EmptyDirectory.new(:monkey, result_path('path/to/empty/subdir/'))
90
90
 
91
91
  file.invoke!
92
92
  File.exists?(result_path('path/to/empty/subdir/')).should be_true
@@ -1,67 +1,67 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
- describe Templater::File, '.new' do
3
+ describe Templater::Actions::File, '.new' do
4
4
  it "should set name, source and destination" do
5
- file = Templater::File.new(:monkey, '/path/to/source', '/path/to/destination')
5
+ file = Templater::Actions::File.new(:monkey, '/path/to/source', '/path/to/destination')
6
6
  file.name.should == :monkey
7
7
  file.source.should == '/path/to/source'
8
8
  file.destination.should == '/path/to/destination'
9
9
  end
10
10
  end
11
11
 
12
- describe Templater::File, '#relative_destination' do
12
+ describe Templater::Actions::File, '#relative_destination' do
13
13
  it "should get the destination relative to the pwd" do
14
14
  Dir.stub!(:pwd).and_return('/path/to')
15
- file = Templater::File.new(:monkey, '/path/to/source', '/path/to/destination/with/some/more/subdirs')
15
+ file = Templater::Actions::File.new(:monkey, '/path/to/source', '/path/to/destination/with/some/more/subdirs')
16
16
  file.relative_destination.should == 'destination/with/some/more/subdirs'
17
17
  end
18
18
  end
19
19
 
20
- describe Templater::File, '#render' do
20
+ describe Templater::Actions::File, '#render' do
21
21
  it "should output the file" do
22
- file = Templater::File.new(:monkey, template_path('simple_erb.rbt'), '/path/to/destination')
22
+ file = Templater::Actions::File.new(:monkey, template_path('simple_erb.rbt'), '/path/to/destination')
23
23
  file.render.should == "test<%= 1+1 %>test"
24
24
  end
25
25
  end
26
26
 
27
- describe Templater::File, '#exists?' do
27
+ describe Templater::Actions::File, '#exists?' do
28
28
 
29
29
  it "should exist if the destination file exists" do
30
- file = Templater::File.new(:monkey, template_path('simple.rbt'), result_path('erb.rbs'))
30
+ file = Templater::Actions::File.new(:monkey, template_path('simple.rbt'), result_path('erb.rbs'))
31
31
  file.should be_exists
32
32
  end
33
33
 
34
34
  it "should not exist if the destination file does not exist" do
35
- file = Templater::File.new(:monkey, template_path('simple.rbt'), result_path('some_weird_file.rbs'))
35
+ file = Templater::Actions::File.new(:monkey, template_path('simple.rbt'), result_path('some_weird_file.rbs'))
36
36
  file.should_not be_exists
37
37
  end
38
38
 
39
39
  end
40
40
 
41
- describe Templater::File, '#identical' do
41
+ describe Templater::Actions::File, '#identical' do
42
42
 
43
43
  it "should not be identical if the destination file doesn't exist" do
44
- file = Templater::File.new(:monkey, template_path('simple_erb.rbt'), result_path('some_weird_file.rbs'))
44
+ file = Templater::Actions::File.new(:monkey, template_path('simple_erb.rbt'), result_path('some_weird_file.rbs'))
45
45
  file.should_not be_identical
46
46
  end
47
47
 
48
48
  it "should not be identical if the destination file is not identical to the source file" do
49
- file = Templater::File.new(:monkey, template_path('simple_erb.rbt'), result_path('simple_erb.rbs'))
49
+ file = Templater::Actions::File.new(:monkey, template_path('simple_erb.rbt'), result_path('simple_erb.rbs'))
50
50
  file.should be_exists
51
51
  file.should_not be_identical
52
52
  end
53
53
 
54
54
  it "should be identical if the destination file is identical to the source file" do
55
- file= Templater::File.new(:monkey, template_path('simple_erb.rbt'), result_path('file.rbs'))
55
+ file= Templater::Actions::File.new(:monkey, template_path('simple_erb.rbt'), result_path('file.rbs'))
56
56
  file.should be_exists
57
57
  file.should be_identical
58
58
  end
59
59
  end
60
60
 
61
- describe Templater::File, '#invoke!' do
61
+ describe Templater::Actions::File, '#invoke!' do
62
62
 
63
63
  it "should copy the source file to the destination" do
64
- file = Templater::File.new(:monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'))
64
+ file = Templater::Actions::File.new(:monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'))
65
65
 
66
66
  file.invoke!
67
67
 
@@ -73,10 +73,10 @@ describe Templater::File, '#invoke!' do
73
73
  end
74
74
  end
75
75
 
76
- describe Templater::File, '#revoke!' do
76
+ describe Templater::Actions::File, '#revoke!' do
77
77
 
78
78
  it "should remove the destination file" do
79
- file = Templater::File.new(:monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'))
79
+ file = Templater::Actions::File.new(:monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test2.rbs'))
80
80
 
81
81
  file.invoke!
82
82
 
@@ -14,7 +14,7 @@ describe Templater::Generator, '.file' do
14
14
 
15
15
  @instance.file(:my_template).source.should == '/tmp/source/path/to/source.rbt'
16
16
  @instance.file(:my_template).destination.should == '/tmp/destination/path/to/destination.rb'
17
- @instance.file(:my_template).should be_an_instance_of(Templater::File)
17
+ @instance.file(:my_template).should be_an_instance_of(Templater::Actions::File)
18
18
  end
19
19
 
20
20
  it "should add a file with source and infer destination " do
@@ -25,7 +25,7 @@ describe Templater::Generator, '.file' do
25
25
 
26
26
  @instance.file(:my_template).source.should == '/tmp/source/path/to/file.rb'
27
27
  @instance.file(:my_template).destination.should == '/tmp/destination/path/to/file.rb'
28
- @instance.file(:my_template).should be_an_instance_of(Templater::File)
28
+ @instance.file(:my_template).should be_an_instance_of(Templater::Actions::File)
29
29
  end
30
30
 
31
31
  it "should add a file and convert an instruction encoded in the destination, but not one encoded in the source" do
@@ -38,7 +38,7 @@ describe Templater::Generator, '.file' do
38
38
 
39
39
  @instance.file(:my_template).source.should == '/tmp/source/template/%some_method%.rbt'
40
40
  @instance.file(:my_template).destination.should == "/tmp/destination/template/beast.rb"
41
- @instance.file(:my_template).should be_an_instance_of(Templater::File)
41
+ @instance.file(:my_template).should be_an_instance_of(Templater::Actions::File)
42
42
  end
43
43
 
44
44
  it "should add a file and leave an encoded instruction be if it doesn't exist as a method" do
@@ -48,6 +48,6 @@ describe Templater::Generator, '.file' do
48
48
  @instance.stub!(:source_root).and_return('/tmp/source')
49
49
 
50
50
  @instance.file(:my_template).destination.should == "/tmp/destination/template/%some_method%.rb"
51
- @instance.file(:my_template).should be_an_instance_of(Templater::File)
51
+ @instance.file(:my_template).should be_an_instance_of(Templater::Actions::File)
52
52
  end
53
53
  end
@@ -14,7 +14,7 @@ describe Templater::Generator, '.template' do
14
14
 
15
15
  @instance.template(:my_template).source.should == '/tmp/source/path/to/source.rbt'
16
16
  @instance.template(:my_template).destination.should == '/tmp/destination/path/to/destination.rb'
17
- @instance.template(:my_template).should be_an_instance_of(Templater::Template)
17
+ @instance.template(:my_template).should be_an_instance_of(Templater::Actions::Template)
18
18
  end
19
19
 
20
20
  it "should add a template with absolute source and destination" do
@@ -25,7 +25,7 @@ describe Templater::Generator, '.template' do
25
25
 
26
26
  @instance.template(:my_template).source.should == '/path/to/source.rbt'
27
27
  @instance.template(:my_template).destination.should == '/path/to/destination.rb'
28
- @instance.template(:my_template).should be_an_instance_of(Templater::Template)
28
+ @instance.template(:my_template).should be_an_instance_of(Templater::Actions::Template)
29
29
  end
30
30
 
31
31
  it "should add a template with destination and infer the source" do
@@ -36,7 +36,7 @@ describe Templater::Generator, '.template' do
36
36
 
37
37
  @instance.template(:my_template).source.should == '/tmp/source/path/to/destination.rbt'
38
38
  @instance.template(:my_template).destination.should == '/tmp/destination/path/to/destination.rb'
39
- @instance.template(:my_template).should be_an_instance_of(Templater::Template)
39
+ @instance.template(:my_template).should be_an_instance_of(Templater::Actions::Template)
40
40
  end
41
41
 
42
42
  it "should add a template with a block" do
@@ -50,7 +50,7 @@ describe Templater::Generator, '.template' do
50
50
 
51
51
  @instance.template(:my_template).source.should == '/tmp/source/blah.rbt'
52
52
  @instance.template(:my_template).destination.should == "/tmp/destination/gurr#{Process.pid.to_s}.rb"
53
- @instance.template(:my_template).should be_an_instance_of(Templater::Template)
53
+ @instance.template(:my_template).should be_an_instance_of(Templater::Actions::Template)
54
54
  end
55
55
 
56
56
  it "should add a template and convert an with an instruction encoded in the destination, but not one encoded in the source" do
@@ -63,7 +63,7 @@ describe Templater::Generator, '.template' do
63
63
 
64
64
  @instance.template(:my_template).source.should == '/tmp/source/template/%some_method%.rbt'
65
65
  @instance.template(:my_template).destination.should == "/tmp/destination/template/beast.rb"
66
- @instance.template(:my_template).should be_an_instance_of(Templater::Template)
66
+ @instance.template(:my_template).should be_an_instance_of(Templater::Actions::Template)
67
67
  end
68
68
 
69
69
  it "should add a template and leave an encoded instruction be if it doesn't exist as a method" do
@@ -73,7 +73,7 @@ describe Templater::Generator, '.template' do
73
73
  @instance.stub!(:source_root).and_return('/tmp/source')
74
74
 
75
75
  @instance.template(:my_template).destination.should == "/tmp/destination/template/%some_method%.rb"
76
- @instance.template(:my_template).should be_an_instance_of(Templater::Template)
76
+ @instance.template(:my_template).should be_an_instance_of(Templater::Actions::Template)
77
77
  end
78
78
 
79
79
  end
@@ -1,8 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
- describe Templater::Template, '.new' do
3
+ describe Templater::Actions::Template, '.new' do
4
4
  it "should set name, source and destination" do
5
- template = Templater::Template.new('some_context', :monkey, '/path/to/source', '/path/to/destination')
5
+ template = Templater::Actions::Template.new('some_context', :monkey, '/path/to/source', '/path/to/destination')
6
6
  template.context.should == 'some_context'
7
7
  template.name.should == :monkey
8
8
  template.source.should == '/path/to/source'
@@ -10,85 +10,85 @@ describe Templater::Template, '.new' do
10
10
  end
11
11
  end
12
12
 
13
- describe Templater::Template, '#relative_destination' do
13
+ describe Templater::Actions::Template, '#relative_destination' do
14
14
  it "should get the destination relative to the pwd" do
15
15
  Dir.stub!(:pwd).and_return('/path/to')
16
- template = Templater::Template.new('some_context', :monkey, '/path/to/source', '/path/to/destination/with/some/more/subdirs')
16
+ template = Templater::Actions::Template.new('some_context', :monkey, '/path/to/source', '/path/to/destination/with/some/more/subdirs')
17
17
  template.relative_destination.should == 'destination/with/some/more/subdirs'
18
18
  end
19
19
  end
20
20
 
21
- describe Templater::Template, '#render' do
21
+ describe Templater::Actions::Template, '#render' do
22
22
  before do
23
23
  @context = class << self; self end
24
24
  end
25
25
 
26
26
  it "should render a simple template" do
27
- template = Templater::Template.new(@context, :monkey, template_path('simple.rbt'), '/path/to/destination')
27
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple.rbt'), '/path/to/destination')
28
28
  template.render.should == "Hello World"
29
29
  end
30
30
 
31
31
  it "should render some basic erb" do
32
- template = Templater::Template.new(@context, :monkey, template_path('simple_erb.rbt'), '/path/to/destination')
32
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple_erb.rbt'), '/path/to/destination')
33
33
  template.render.should == "test2test"
34
34
  end
35
35
 
36
36
  it "should render some erb and convert erb literals" do
37
- template = Templater::Template.new(@context, :monkey, template_path('literals_erb.rbt'), '/path/to/destination')
37
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('literals_erb.rbt'), '/path/to/destination')
38
38
  template.render.should == "test2test<%= 1+1 %>blah"
39
39
  end
40
40
 
41
41
  it "should render some erb fetching stuff from the context" do
42
42
  @context.should_receive(:funkydoodle).and_return('_doodle_')
43
- template = Templater::Template.new(@context, :monkey, template_path('erb.rbt'), '/path/to/destination')
43
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('erb.rbt'), '/path/to/destination')
44
44
  template.render.should == "test_doodle_blah"
45
45
  end
46
46
  end
47
47
 
48
- describe Templater::Template, '#exists?' do
48
+ describe Templater::Actions::Template, '#exists?' do
49
49
 
50
50
  it "should exist if the destination file exists" do
51
- template = Templater::Template.new(@context, :monkey, template_path('simple.rbt'), result_path('erb.rbs'))
51
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple.rbt'), result_path('erb.rbs'))
52
52
  template.should be_exists
53
53
  end
54
54
 
55
55
  it "should not exist if the destination file does not exist" do
56
- template = Templater::Template.new(@context, :monkey, template_path('simple.rbt'), result_path('some_weird_file.rbs'))
56
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple.rbt'), result_path('some_weird_file.rbs'))
57
57
  template.should_not be_exists
58
58
  end
59
59
 
60
60
  end
61
61
 
62
- describe Templater::Template, '#identical' do
62
+ describe Templater::Actions::Template, '#identical' do
63
63
  before do
64
64
  @context = class << self; self end
65
65
  end
66
66
 
67
67
  it "should not be identical if the destination file doesn't exist" do
68
- template = Templater::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('some_weird_file.rbs'))
68
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('some_weird_file.rbs'))
69
69
  template.should_not be_identical
70
70
  end
71
71
 
72
72
  it "should not be identical if the rendered content does not match the content of the file" do
73
- template = Templater::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('random.rbs'))
73
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('random.rbs'))
74
74
  template.should be_exists
75
75
  template.should_not be_identical
76
76
  end
77
77
 
78
78
  it "should be identical if the rendered content matches the content of the file" do
79
- template = Templater::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('simple_erb.rbs'))
79
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('simple_erb.rbs'))
80
80
  template.should be_exists
81
81
  template.should be_identical
82
82
  end
83
83
  end
84
84
 
85
- describe Templater::Template, '#invoke!' do
85
+ describe Templater::Actions::Template, '#invoke!' do
86
86
  before do
87
87
  @context = class << self; self end
88
88
  end
89
89
 
90
90
  it "should render the template and copy it to the destination" do
91
- template = Templater::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('test.rbs'))
91
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('test.rbs'))
92
92
 
93
93
  template.invoke!
94
94
 
@@ -99,7 +99,7 @@ describe Templater::Template, '#invoke!' do
99
99
  end
100
100
 
101
101
  it "should render the template and copy it to the destination, creating any required subdirectories" do
102
- template = Templater::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test.rbs'))
102
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('path/to/subdir/test.rbs'))
103
103
 
104
104
  template.invoke!
105
105
 
@@ -113,10 +113,10 @@ describe Templater::Template, '#invoke!' do
113
113
  end
114
114
 
115
115
 
116
- describe Templater::Template, '#revoke!' do
116
+ describe Templater::Actions::Template, '#revoke!' do
117
117
 
118
118
  it "should remove the destination file" do
119
- template = Templater::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('test.rbs'))
119
+ template = Templater::Actions::Template.new(@context, :monkey, template_path('simple_erb.rbt'), result_path('test.rbs'))
120
120
 
121
121
  template.invoke!
122
122
 
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.1.3
4
+ version: 0.1.4
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-07-24 00:00:00 +02:00
12
+ date: 2008-08-02 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -48,6 +48,10 @@ files:
48
48
  - Rakefile
49
49
  - TODO
50
50
  - lib/templater
51
+ - lib/templater/actions
52
+ - lib/templater/actions/empty_directory.rb
53
+ - lib/templater/actions/file.rb
54
+ - lib/templater/actions/template.rb
51
55
  - lib/templater/capture_helpers.rb
52
56
  - lib/templater/cli
53
57
  - lib/templater/cli/generator.rb
@@ -56,12 +60,10 @@ files:
56
60
  - lib/templater/core_ext
57
61
  - lib/templater/core_ext/kernel.rb
58
62
  - lib/templater/core_ext/string.rb
59
- - lib/templater/empty_directory.rb
60
- - lib/templater/file.rb
63
+ - lib/templater/discovery.rb
61
64
  - lib/templater/generator.rb
62
65
  - lib/templater/manifold.rb
63
66
  - lib/templater/proxy.rb
64
- - lib/templater/template.rb
65
67
  - lib/templater.rb
66
68
  - spec/core_ext
67
69
  - spec/core_ext/string_spec.rb
@@ -1,53 +0,0 @@
1
- module Templater
2
- class EmptyDirectory
3
-
4
- attr_reader :name, :destination
5
-
6
- def initialize(name, destination)
7
- @name, @destination = name, destination
8
- end
9
-
10
- # Returns the destination path relative to Dir.pwd. This is useful for prettier output in interfaces
11
- # where the destination root is Dir.pwd.
12
- #
13
- # === Returns
14
- # String:: The destination relative to Dir.pwd
15
- def relative_destination
16
- @destination.sub(::Dir.pwd + ::File::SEPARATOR, '')
17
- end
18
-
19
- # Returns the contents of the source file as a String
20
- #
21
- # === Returns
22
- # String:: The source file.
23
- def render
24
- ::File.read(source)
25
- end
26
-
27
- # Checks if the destination file already exists.
28
- #
29
- # === Returns
30
- # Boolean:: true if the file exists, false otherwise.
31
- def exists?
32
- ::File.exists?(destination)
33
- end
34
-
35
- # For empty directory this is in fact alias for exists? method.
36
- #
37
- # === Returns
38
- # Boolean:: true if it is identical, false otherwise.
39
- def identical?
40
- exists?
41
- end
42
-
43
- # Renders the template and copies it to the destination.
44
- def invoke!
45
- ::FileUtils.mkdir_p(destination)
46
- end
47
-
48
- # removes the destination file
49
- def revoke!
50
- ::FileUtils.rm_rf(::File.expand_path(destination))
51
- end
52
- end # EmptyDirectory
53
- end # Templater
@@ -1,63 +0,0 @@
1
- module Templater
2
- class File
3
-
4
- attr_accessor :name, :source, :destination
5
-
6
- # Builds a new file, given the name of the file and its source and destination.
7
- #
8
- # === Parameters
9
- # name<Symbol>:: The name of this template
10
- # source<String>:: Full path to the source of this template
11
- # destination<String>:: Full path to the destination of this template
12
- def initialize(name, source, destination)
13
- @name = name
14
- @source = source
15
- @destination = destination
16
- end
17
-
18
- # Returns the destination path relative to Dir.pwd. This is useful for prettier output in interfaces
19
- # where the destination root is Dir.pwd.
20
- #
21
- # === Returns
22
- # String:: The destination relative to Dir.pwd
23
- def relative_destination
24
- @destination.sub(::Dir.pwd + ::File::SEPARATOR, '')
25
- end
26
-
27
- # Returns the contents of the source file as a String
28
- #
29
- # === Returns
30
- # String:: The source file.
31
- def render
32
- ::File.read(source)
33
- end
34
-
35
- # Checks if the destination file already exists.
36
- #
37
- # === Returns
38
- # Boolean:: true if the file exists, false otherwise.
39
- def exists?
40
- ::File.exists?(destination)
41
- end
42
-
43
- # Checks if the content of the file at the destination is identical to the rendered result.
44
- #
45
- # === Returns
46
- # Boolean:: true if it is identical, false otherwise.
47
- def identical?
48
- exists? && ::FileUtils.identical?(source, destination)
49
- end
50
-
51
- # Renders the template and copies it to the destination.
52
- def invoke!
53
- ::FileUtils.mkdir_p(::File.dirname(destination))
54
- ::FileUtils.copy_file(source, destination)
55
- end
56
-
57
- # removes the destination file
58
- def revoke!
59
- ::FileUtils.rm(destination)
60
- end
61
-
62
- end
63
- end
@@ -1,67 +0,0 @@
1
- module Templater
2
- class Template
3
-
4
- attr_accessor :context, :name, :source, :destination, :options
5
-
6
- # Builds a new template, given the context (e.g. binding) in which the template will be rendered
7
- # (usually a generator), the name of the template and its source and destination.
8
- #
9
- # === Parameters
10
- # context<Object>:: Context for rendering
11
- # name<Symbol>:: The name of this template
12
- # source<String>:: Full path to the source of this template
13
- # destination<String>:: Full path to the destination of this template
14
- # render<Boolean>:: If set to false, will do a copy instead of rendering.
15
- def initialize(context, name, source, destination)
16
- @context = context
17
- @name = name
18
- @source = source
19
- @destination = destination
20
- end
21
-
22
- # Returns the destination path relative to Dir.pwd. This is useful for prettier output in interfaces
23
- # where the destination root is Dir.pwd.
24
- #
25
- # === Returns
26
- # String:: The destination relative to Dir.pwd
27
- def relative_destination
28
- @destination.sub(::Dir.pwd + ::File::SEPARATOR, '')
29
- end
30
-
31
- # Renders the template using ERB and returns the result as a String.
32
- #
33
- # === Returns
34
- # String:: The rendered template.
35
- def render
36
- ERB.new(::File.read(source), nil, '-').result(context.send(:binding))
37
- end
38
-
39
- # Checks if the destination file already exists.
40
- #
41
- # === Returns
42
- # Boolean:: true if the file exists, false otherwise.
43
- def exists?
44
- ::File.exists?(destination)
45
- end
46
-
47
- # Checks if the content of the file at the destination is identical to the rendered result.
48
- #
49
- # === Returns
50
- # Boolean:: true if it is identical, false otherwise.
51
- def identical?
52
- ::File.read(destination) == render if ::File.exists?(destination)
53
- end
54
-
55
- # Renders the template and copies it to the destination.
56
- def invoke!
57
- ::FileUtils.mkdir_p(::File.dirname(destination))
58
- ::File.open(destination, 'w') {|f| f.write render }
59
- end
60
-
61
- # removes the destination file
62
- def revoke!
63
- ::FileUtils.rm(destination)
64
- end
65
-
66
- end
67
- end