bocuse 0.1.2.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/bocuse CHANGED
@@ -1,8 +1,6 @@
1
1
  #!/usr/bin/env ruby -W0
2
2
  # Encoding: UTF-8
3
3
 
4
- $VERBOSE=nil
5
-
6
4
  require 'thor'
7
5
 
8
6
  $:.unshift File.expand_path '../../lib', __FILE__
@@ -0,0 +1,14 @@
1
+ module Bocuse
2
+ module ContextDelegation
3
+ # Method delegation to the context of this context. Every method not
4
+ # defined here may be defined there instead.
5
+ #
6
+ def respond_to?(sym)
7
+ super || @context.respond_to?(sym)
8
+ end
9
+ def method_missing(sym, *args, &block)
10
+ return @context.send(sym, *args, &block) if @context.respond_to?(sym)
11
+ super
12
+ end
13
+ end
14
+ end
data/lib/bocuse/file.rb CHANGED
@@ -1,4 +1,6 @@
1
1
 
2
+ require 'bocuse/node_context'
3
+
2
4
  # Represents a bocuse File.
3
5
  #
4
6
  # This is usually a file that contains one or more of the
@@ -11,12 +13,9 @@
11
13
  class Bocuse::File
12
14
  attr_reader :path
13
15
 
14
- # When inside a construct that has a configuration associated, this will
15
- # return that configuration instance.
16
- attr_reader :current_configuration
17
-
18
- def initialize(path, context)
16
+ def initialize(path, project, context)
19
17
  @path = path
18
+ @project = project
20
19
  @context = context
21
20
  end
22
21
 
@@ -36,37 +35,22 @@ class Bocuse::File
36
35
  # The files read by #evaluate will trigger these methods.
37
36
  #
38
37
  def node name
39
- unit = Bocuse::Unit.new(Proc.new, @context)
38
+ node_context = Bocuse::NodeContext.new(name, @context)
39
+ configuration = Bocuse::Configuration.new
40
40
 
41
- configuration_block do |configuration|
42
- unit.call(configuration)
43
- @context.register_node name, configuration
44
- end
41
+ unit = Bocuse::Unit.new(Proc.new, @project, node_context)
42
+ unit.call(configuration)
43
+
44
+ @project.register_node name, configuration
45
+
46
+ return configuration
45
47
  end
46
48
  def template
47
49
  # Delay template evaluation until someone tries to call include_template.
48
50
  # At that time, we'll have a configuration object to have the template
49
51
  # manipulate.
50
- unit = Bocuse::Unit.new(Proc.new, @context)
51
- @context.register_template path, unit
52
+ unit = Bocuse::Unit.new(Proc.new, @project, @context)
53
+ @project.register_template path, unit
52
54
  unit
53
55
  end
54
- private
55
- def configuration_block
56
- # NOTE since thread-safety is not an issue here (who would use threads
57
- # to define configuration?), we can use a simple file-global state to
58
- # keep track of which configuration is in progress. We use the begin-end
59
- # construct to make sure that configurations are not used beyond the end
60
- # of the node block.
61
-
62
- configuration = @current_configuration = Bocuse::Configuration.new
63
-
64
- begin
65
- yield configuration
66
- ensure
67
- @current_configuration = nil
68
- end
69
-
70
- configuration
71
- end
72
56
  end
@@ -0,0 +1,19 @@
1
+ require 'bocuse/context_delegation'
2
+
3
+ module Bocuse
4
+ # A context for a node. This wraps the project level context and provides
5
+ # node specific context functions.
6
+ #
7
+ class NodeContext
8
+ include ContextDelegation
9
+
10
+ def initialize(node_name, context)
11
+ @context = context
12
+ @node_name = node_name
13
+ end
14
+
15
+ def bocuse
16
+ @context.bocuse.merge(node_name: @node_name)
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,7 @@
1
1
  require 'pathname'
2
2
 
3
+ require 'bocuse/project_context'
4
+
3
5
  module Bocuse
4
6
  # A bocuse project comes in two flavours: simple and co-located with chef.
5
7
  # The simple project is a directory with the subdirectories
@@ -53,7 +55,8 @@ module Bocuse
53
55
  path = Pathname.new(path)
54
56
  path = base_path.join(path) unless path.absolute?
55
57
 
56
- Bocuse::File.new(path, self)
58
+ ctx = ProjectContext.new
59
+ Bocuse::File.new(path, self, ctx)
57
60
  end
58
61
 
59
62
  # Evaluates a file given by path.
@@ -92,7 +95,7 @@ module Bocuse
92
95
  # foo -> templates/foo.rb
93
96
  # /templates/bar -> /usr/templates/bar.rb
94
97
  #
95
- def template name
98
+ def lookup_template name
96
99
  unless @templates.has_key?(name.to_sym)
97
100
  file_name = name.to_s
98
101
  file_name += '.rb' unless file_name.end_with?('.rb')
@@ -0,0 +1,14 @@
1
+ require 'bocuse/context_delegation'
2
+
3
+ module Bocuse
4
+ # Project wide evaluation context.
5
+ #
6
+ class ProjectContext
7
+ def initialize
8
+ end
9
+
10
+ def bocuse
11
+ {}
12
+ end
13
+ end
14
+ end
data/lib/bocuse/unit.rb CHANGED
@@ -5,9 +5,10 @@ module Bocuse
5
5
  # Returns the current configuration, but only during a call to this unit.
6
6
  attr_reader :current_configuration
7
7
 
8
- def initialize(block, context)
8
+ def initialize(block, project, context)
9
9
  @block = block
10
10
  @context = context
11
+ @project = project
11
12
  end
12
13
 
13
14
  def call(configuration)
@@ -37,9 +38,17 @@ module Bocuse
37
38
  # Note: This could be pushed to the configuration.
38
39
  #
39
40
  def include_template identifier
40
- template_block = @context.template identifier
41
+ template_block = @project.lookup_template identifier
41
42
 
42
43
  template_block.call(current_configuration)
43
44
  end
45
+
46
+ # Exposes some of bocuses internal variables to the node that is currently
47
+ # compiled. This allows using node name (, etc...) for formulating clever
48
+ # node configurations.
49
+ #
50
+ def bocuse
51
+ @context.bocuse
52
+ end
44
53
  end
45
54
  end
@@ -41,7 +41,7 @@ describe 'complex example with templates' do
41
41
  :address => "1.1.1.1"
42
42
  },
43
43
  :empty => true,
44
- :recipes => ["nginx", "git", "app::install", "app::deploy"]
44
+ :recipes => ["app::install", "app::deploy"]
45
45
  }
46
46
  end
47
47
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe "bocuse environment" do
4
+ let(:project) { Bocuse::Project.new(fixture('environment')) }
5
+
6
+ it "exports node_name" do
7
+ project.nodes['A'].to_h[:name].should == 'A'
8
+ project.nodes['B'].to_h[:name].should == 'B'
9
+ end
10
+ end
@@ -8,7 +8,7 @@ describe 'Templates' do
8
8
  describe 'loading' do
9
9
  it 'works' do
10
10
  config = Bocuse::Configuration.new
11
- project.template(:users).call(config)
11
+ project.lookup_template(:users).call(config)
12
12
 
13
13
  config.to_h.should == {
14
14
  :user => "root",
@@ -4,10 +4,11 @@ require 'bocuse/file'
4
4
 
5
5
  describe Bocuse::File do
6
6
  let(:context) { flexmock('context') }
7
- let(:file) { described_class.new('path', context) }
7
+ let(:project) { flexmock('project') }
8
+ let(:file) { described_class.new('path', project, context) }
8
9
 
9
10
  before(:each) {
10
- context.
11
+ project.
11
12
  should_receive(
12
13
  :register_node, :register_template).by_default
13
14
  }
@@ -26,7 +27,7 @@ describe Bocuse::File do
26
27
  configuration.to_h.should == { :something => :value, :something_else => { :key => "value" } }
27
28
  end
28
29
  it "registers the node in the context" do
29
- context.should_receive(:register_node).
30
+ project.should_receive(:register_node).
30
31
  with('name', Bocuse::Configuration).once
31
32
 
32
33
  file.node('name') { }
@@ -36,7 +36,7 @@ describe Bocuse::Project do
36
36
  project.register_template template, :configuration }
37
37
 
38
38
  it "returns the template" do
39
- project.template('foo/bar').should == :configuration
39
+ project.lookup_template('foo/bar').should == :configuration
40
40
  end
41
41
  end
42
42
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bocuse::Unit do
4
+ let(:context) { flexmock('context', bocuse: {node_name: 'test.of.bocuse'}) }
5
+ let(:project) { flexmock('project') }
6
+ def unit(configuration)
7
+ described_class.new(Proc.new, project, context).
8
+ tap { |unit| unit.call(configuration) }
9
+ end
10
+
11
+ describe "#bocuse environment hash" do
12
+ it "contains the current node name" do
13
+ configuration = {}
14
+ unit(configuration) do |cfg|
15
+ cfg[:node_name] = bocuse[:node_name]
16
+ end
17
+
18
+ configuration[:node_name].should == 'test.of.bocuse'
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bocuse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-09-24 00:00:00.000000000 Z
14
+ date: 2012-10-01 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec
@@ -140,19 +140,24 @@ extensions: []
140
140
  extra_rdoc_files: []
141
141
  files:
142
142
  - lib/bocuse/configuration.rb
143
+ - lib/bocuse/context_delegation.rb
143
144
  - lib/bocuse/file.rb
145
+ - lib/bocuse/node_context.rb
144
146
  - lib/bocuse/project.rb
147
+ - lib/bocuse/project_context.rb
145
148
  - lib/bocuse/unit.rb
146
149
  - lib/bocuse/value.rb
147
150
  - lib/bocuse.rb
148
151
  - spec/integration/cli_spec.rb
149
152
  - spec/integration/complex_spec.rb
153
+ - spec/integration/environment_spec.rb
150
154
  - spec/integration/helpers_spec.rb
151
155
  - spec/integration/node_finding_spec.rb
152
156
  - spec/integration/templates_spec.rb
153
157
  - spec/lib/bocuse/configuration_spec.rb
154
158
  - spec/lib/bocuse/file_spec.rb
155
159
  - spec/lib/bocuse/project_spec.rb
160
+ - spec/lib/bocuse/unit_spec.rb
156
161
  - spec/lib/bocuse/value_spec.rb
157
162
  - bin/bocuse
158
163
  homepage: http://github.com/mobino/bocuse
@@ -182,11 +187,12 @@ summary: A front-end language to chef-solo.
182
187
  test_files:
183
188
  - spec/integration/cli_spec.rb
184
189
  - spec/integration/complex_spec.rb
190
+ - spec/integration/environment_spec.rb
185
191
  - spec/integration/helpers_spec.rb
186
192
  - spec/integration/node_finding_spec.rb
187
193
  - spec/integration/templates_spec.rb
188
194
  - spec/lib/bocuse/configuration_spec.rb
189
195
  - spec/lib/bocuse/file_spec.rb
190
196
  - spec/lib/bocuse/project_spec.rb
197
+ - spec/lib/bocuse/unit_spec.rb
191
198
  - spec/lib/bocuse/value_spec.rb
192
- has_rdoc: