marcosgz-cap-template 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.8.7-p249@capistrano-template --create
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ notifications:
7
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-template.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tony Pitluga
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Capistrano::Template
2
+
3
+ A [capistrano](http://capistranorb.com/) plugin that aids in rendering templates.
4
+
5
+
6
+ ## NOTE
7
+ Forked from [capistrano-template](https://github.com/pitluga/capistrano-template)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'capistrano-template'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install capistrano-template
22
+
23
+ ## Usage
24
+
25
+ Best explained with an example deploy.rb file.
26
+
27
+ ```ruby
28
+ require 'capistrano/template'
29
+
30
+ set :application, "example"
31
+ # ...
32
+
33
+ task :configure_apache, :roles => :web do
34
+ put(template.render('vhost.conf'), "/etc/apache2/sites-available/#{application}.conf")
35
+ end
36
+ ```
37
+
38
+ ## Supported Functions
39
+
40
+ All supported functions live under the ```template``` namespace.
41
+
42
+ ### render(template_name)
43
+
44
+ ERBs the given template and returns the result.
45
+
46
+ ## Configuration Variables
47
+
48
+ ### template_path
49
+
50
+ The location to search for templates. Relative to the current working directory of the cap command. Defaults to ```config/deploy/templates```.
51
+
52
+
53
+ ### template_bindings
54
+
55
+ An array of bindings that represent the search path for variables when rendering templates. An extension point to allow external sources for data. Defaults to ```[Bindings::CapistranoBinding.new(self)]```
56
+
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1 @@
1
+ Goodbye <%= name %>
@@ -0,0 +1 @@
1
+ Hello <%= name %>
@@ -0,0 +1,13 @@
1
+ # run with cap -f vanilla.rb
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'capistrano'
4
+ require 'capistrano/template'
5
+
6
+ set :template_path, 'templates'
7
+ set :name, 'world'
8
+
9
+
10
+ desc "place a rendered template in /tmp/hello_world"
11
+ task :test_render, :roles => :app do
12
+ puts template.render('hello_world.erb')
13
+ end
@@ -0,0 +1,20 @@
1
+ module Capistrano
2
+ module Template
3
+ module Bindings
4
+ class CapistranoBinding
5
+ def initialize(capistrano)
6
+ @capistrano = capistrano
7
+ end
8
+
9
+ def exists?(variable)
10
+ @capistrano.exists?(variable)
11
+ end
12
+
13
+ def fetch(variable)
14
+ @capistrano.fetch(variable)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module Capistrano
2
+ module Template
3
+ module Bindings
4
+ class Chain
5
+
6
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$|^binding$|^instance_eval$)/ }
7
+
8
+ def initialize(*bindings)
9
+ @bindings = bindings
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ found_binding = @bindings.detect { |b| b.exists?(name) }
14
+ super if found_binding.nil?
15
+ found_binding.fetch(name)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Capistrano
2
+ module Template
3
+ module Bindings
4
+ class HashBinding
5
+
6
+ def initialize(hash)
7
+ @hash = hash
8
+ end
9
+
10
+ def exists?(variable)
11
+ @hash.has_key?(variable)
12
+ end
13
+
14
+ def fetch(variable)
15
+ @hash.fetch(variable)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module Capistrano
2
+ module Template
3
+ module Plugin
4
+ def render(template)
5
+ content = File.read(File.join(template_path, template))
6
+ ERB.new(content).result(chain.instance_eval { binding })
7
+ end
8
+
9
+ def template_bindings
10
+ case self[:template_bindings].class.name
11
+ when 'Hash'
12
+ [Bindings::HashBinding.new(fetch(:template_bindings))]
13
+ else
14
+ [Bindings::CapistranoBinding.new(self)]
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def chain
21
+ @__template_chain ||= Bindings::Chain.new(*template_bindings)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Template
3
+ VERSION = "0.0.3"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'erb'
2
+ require 'capistrano'
3
+ require 'capistrano/template/plugin'
4
+ require 'capistrano/template/version'
5
+ require 'capistrano/template/bindings/capistrano_binding'
6
+ require 'capistrano/template/bindings/chain'
7
+ require 'capistrano/template/bindings/hash_binding'
8
+
9
+ Capistrano.plugin(:template, Capistrano::Template::Plugin)
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano/template/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "marcosgz-cap-template"
8
+ gem.version = Capistrano::Template::VERSION
9
+ gem.authors = ["Marcos G. Zimmermann", "Tony Pitluga"]
10
+ gem.email = ["mgzmaster@gmail.com", "tony.pitluga@gmail.com"]
11
+ gem.summary = %q{Capistrano Templates}
12
+ gem.homepage = "https://github.com/marcosgz/capistrano-template"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency "capistrano", "> 2.0.0"
20
+
21
+ gem.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
+
3
+ module Capistrano
4
+ module Template
5
+ module Bindings
6
+ class CapistranoBindingTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @config = Capistrano::Configuration.new
10
+ end
11
+
12
+ def test_defined_returns_false_when_variable_is_not_available
13
+ script_binding = CapistranoBinding.new(@config)
14
+ assert_equal(false, script_binding.exists?(:var))
15
+ end
16
+
17
+ def test_defined_returns_true_when_variable_is_available
18
+ @config.set(:var, 'foo')
19
+ script_binding = CapistranoBinding.new(@config)
20
+ assert_equal(true, script_binding.exists?(:var))
21
+ end
22
+
23
+ def test_fetch_returns_the_value_of_the_variable
24
+ @config.set(:var, 'foo')
25
+ script_binding = CapistranoBinding.new(@config)
26
+ assert_equal('foo', script_binding.fetch(:var))
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
+
3
+ module Capistrano
4
+ module Template
5
+ module Bindings
6
+ class ChainTest < Test::Unit::TestCase
7
+
8
+ def test_method_missing_searches_bindings
9
+ chain = Chain.new(HashBinding.new(:foo => 'bar'))
10
+ assert_equal('bar', chain.foo)
11
+ end
12
+
13
+ def test_method_missing_raises_not_found_exception
14
+ chain = Chain.new(HashBinding.new(:foo => 'bar'))
15
+ exception = assert_raise(NoMethodError) { chain.baz }
16
+ assert_match /baz/, exception.message
17
+ end
18
+
19
+ def test_chains_public_methods_are_undefined
20
+ chain = Chain.new(HashBinding.new(:foo => 'bar'))
21
+ assert_raises(NoMethodError) { chain.clone }
22
+ assert_raises(NoMethodError) { chain.to_s }
23
+ assert_raises(NoMethodError) { chain.inspect }
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
+
3
+ module Capistrano
4
+ module Template
5
+ module Bindings
6
+ class HashBindingTest < Test::Unit::TestCase
7
+
8
+ def test_defined_returns_false_when_variable_is_not_available
9
+ hash_binding = HashBinding.new({})
10
+ assert_equal(false, hash_binding.exists?(:var))
11
+ end
12
+
13
+ def test_defined_returns_true_when_variable_is_available
14
+ hash_binding = HashBinding.new(:var => 'foo')
15
+ assert_equal(true, hash_binding.exists?(:var))
16
+ end
17
+
18
+ def test_fetch_returns_the_value_of_the_variable
19
+ hash_binding = HashBinding.new(:var => 'foo')
20
+ assert_equal('foo', hash_binding.fetch(:var))
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path('../../../../test_helper', __FILE__)
2
+
3
+ module Capistrano
4
+ module Template
5
+ module Operations
6
+ class StageTest < Test::Unit::TestCase
7
+ def setup
8
+ @capistrano = MockCapistrano.new
9
+ @capistrano.set(:template_path, TEST_TEMPLATES)
10
+ end
11
+
12
+ def test_execute_creates_staging_directory
13
+ @capistrano.template.stage
14
+ assert_equal 'mkdir -p /tmp/cap-templates', @capistrano.invocations.first[:command]
15
+ end
16
+
17
+ def test_execute_uploads_files_to_the_staging_directory
18
+ @capistrano.server 'example.com', :app
19
+ @capistrano.template.publish 'hello.erb', '/usr/local/bin/hello'
20
+ @capistrano.template.stage
21
+ assert_equal :up, @capistrano.transfers.first[:direction]
22
+ assert_match /Hello from/, @capistrano.transfers.first[:from].read
23
+ assert_equal '/tmp/cap-templates/usr__local__bin__hello', @capistrano.transfers.first[:to]
24
+ end
25
+
26
+ def test_execute_uploads_files_only_to_the_given_roles
27
+ @capistrano.server 'example.com', :app
28
+ @capistrano.template.publish 'hello.erb', '/usr/local/bin/hello', :roles => :app
29
+ @capistrano.template.stage
30
+ assert_equal :up, @capistrano.transfers.first[:direction]
31
+ assert_match /Hello from/, @capistrano.transfers.first[:from].read
32
+ assert_equal '/tmp/cap-templates/usr__local__bin__hello', @capistrano.transfers.first[:to]
33
+ assert_equal :app, @capistrano.transfers.first[:options][:roles]
34
+ end
35
+
36
+ def test_execute_ignores_roles_that_are_not_defined
37
+ @capistrano.template.publish 'hello.erb', '/usr/local/bin/hello', :roles => :app
38
+ @capistrano.template.stage
39
+ assert_equal [], @capistrano.transfers
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+
3
+ class PluginTest < Test::Unit::TestCase
4
+ def setup
5
+ @config = Capistrano::Configuration.new
6
+ end
7
+
8
+ def test_template_plugin_is_available
9
+ assert_nothing_raised do
10
+ @config.template
11
+ end
12
+ end
13
+
14
+ def test_render_erbs_the_given_template
15
+ @config.set(:template_path, TEST_TEMPLATES)
16
+ result = @config.template.render("hello.erb")
17
+ assert_equal(result, "Hello from #{TEST_TEMPLATES}\n")
18
+ end
19
+
20
+ def test_publish_stores_where_templates_should_be_placed
21
+ @config.template.publish('hello.erb', '/tmp/hello')
22
+ assert_equal('hello.erb', @config.__template_published_files.first[:template])
23
+ assert_equal('/tmp/hello', @config.__template_published_files.first[:location])
24
+ end
25
+
26
+ def test_publish_fills_in_the_options_hash_for_put
27
+ @config.template.publish('hello.erb', '/tmp/hello', :mode => '0640', :roles => :app)
28
+ assert_equal(:app, @config.__template_published_files.first[:options][:roles])
29
+ assert_equal('0640', @config.__template_published_files.first[:options][:mode])
30
+ end
31
+
32
+ def test_publish_raises_an_argument_error_if_location_is_not_an_absolute_path
33
+ assert_raises(ArgumentError) do
34
+ @config.template.publish('hello.erb', 'hello', :mode => '0640', :roles => :app)
35
+ end
36
+ end
37
+
38
+ def test_without_current_task_removes_any_existing_task_call_frames
39
+ Thread.current[:task_call_frames] = :original
40
+ called = false
41
+ @config.template.without_current_task do
42
+ called = true
43
+ assert_equal [], Thread.current[:task_call_frames]
44
+ end
45
+ assert_equal true, called
46
+ assert_equal :original, Thread.current[:task_call_frames]
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+
3
+ class VersionTest < Test::Unit::TestCase
4
+ def test_version_is_set
5
+ assert_not_nil Capistrano::Template::VERSION
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ class MockCapistrano < Capistrano::Configuration
2
+
3
+ def transfer(direction, from, to, options={})
4
+ transfers.push(:direction => direction, :from => from, :to => to, :options => options)
5
+ end
6
+
7
+ def run(command, options={})
8
+ invocations.push(:command => command, :options => options)
9
+ end
10
+
11
+ def transfers
12
+ @transfers ||= []
13
+ end
14
+
15
+ def invocations
16
+ @invocations ||= []
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ Hello from <%= template_path %>
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path('..', __FILE__)
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'capistrano/template'
6
+ require 'test/unit'
7
+ require 'mocks/mock_capistrano'
8
+
9
+ TEST_TEMPLATES = File.expand_path('../templates', __FILE__)
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marcosgz-cap-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcos G. Zimmermann
9
+ - Tony Pitluga
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-24 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: capistrano
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>'
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>'
29
+ - !ruby/object:Gem::Version
30
+ version: 2.0.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description:
48
+ email:
49
+ - mgzmaster@gmail.com
50
+ - tony.pitluga@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .rvmrc
57
+ - .travis.yml
58
+ - Gemfile
59
+ - LICENSE.txt
60
+ - README.md
61
+ - Rakefile
62
+ - examples/templates/goodbye_world.erb
63
+ - examples/templates/hello_world.erb
64
+ - examples/vanilla.rb
65
+ - lib/capistrano/template.rb
66
+ - lib/capistrano/template/bindings/capistrano_binding.rb
67
+ - lib/capistrano/template/bindings/chain.rb
68
+ - lib/capistrano/template/bindings/hash_binding.rb
69
+ - lib/capistrano/template/plugin.rb
70
+ - lib/capistrano/template/version.rb
71
+ - marcosgz-cap-template.gemspec
72
+ - test/capistrano/template/bindings/capistrano_binding_test.rb
73
+ - test/capistrano/template/bindings/chain_test.rb
74
+ - test/capistrano/template/bindings/hash_binding_test.rb
75
+ - test/capistrano/template/operations/stage_test.rb
76
+ - test/capistrano/template/plugin_test.rb
77
+ - test/capistrano/template/version_test.rb
78
+ - test/mocks/mock_capistrano.rb
79
+ - test/templates/hello.erb
80
+ - test/test_helper.rb
81
+ homepage: https://github.com/marcosgz/capistrano-template
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.23
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Capistrano Templates
105
+ test_files:
106
+ - test/capistrano/template/bindings/capistrano_binding_test.rb
107
+ - test/capistrano/template/bindings/chain_test.rb
108
+ - test/capistrano/template/bindings/hash_binding_test.rb
109
+ - test/capistrano/template/operations/stage_test.rb
110
+ - test/capistrano/template/plugin_test.rb
111
+ - test/capistrano/template/version_test.rb
112
+ - test/mocks/mock_capistrano.rb
113
+ - test/templates/hello.erb
114
+ - test/test_helper.rb
115
+ has_rdoc: