Skip to content

Commit

Permalink
Merge branch '144'
Browse files Browse the repository at this point in the history
ref. #144
  • Loading branch information
Ryota Arai committed Nov 9, 2015
2 parents a9d88c0 + 54894fd commit b21eed1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
22 changes: 12 additions & 10 deletions lib/itamae/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,41 @@ def find_recipe_in_gem(recipe)
end
end

def initialize(runner, path)
def initialize(runner, path, options = {})
@runner = runner
@path = path
@delayed_notifications = []
@children = RecipeChildren.new
@options = options
end

def dir
::File.dirname(@path)
end

def load(vars = {})
context = EvalContext.new(self, vars)
context = EvalContext.new(self, vars, @options)
context.instance_eval(File.read(path), path, 1)
end

def run(options = {})
show_banner

Itamae.logger.with_indent do
@children.run(options)
run_delayed_notifications(options)
@children.run(@options)
run_delayed_notifications
end
end

private

def run_delayed_notifications(options)
def run_delayed_notifications
@delayed_notifications.uniq! do |notification|
[notification.action, notification.action_resource]
end

while notification = @delayed_notifications.shift
notification.run(options)
notification.run(@options)
end
end

Expand All @@ -84,8 +85,9 @@ def show_banner
end

class EvalContext
def initialize(recipe, vars)
def initialize(recipe, vars, options = {})
@recipe = recipe
@options = options

vars.each do |k, v|
define_singleton_method(k) { v }
Expand All @@ -109,7 +111,7 @@ def method_missing(*args, &block)
super
end

resource = klass.new(@recipe, name, &block)
resource = klass.new(@recipe, name, @options, &block)
@recipe.children << resource
end

Expand All @@ -133,7 +135,7 @@ def include_recipe(target)
return
end

recipe = Recipe.new(runner, path)
recipe = Recipe.new(runner, path, @options)
@recipe.children << recipe
recipe.load
end
Expand All @@ -155,7 +157,7 @@ class RecipeFromDefinition < Recipe
attr_accessor :definition

def load(vars = {})
context = EvalContext.new(self, vars)
context = EvalContext.new(self, vars, @options)
context.instance_eval(&@definition.class.definition_block)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/itamae/recipe_children.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def run(options)
self.each do |resource|
case resource
when Resource::Base
resource.run(nil, dry_run: options[:dry_run])
resource.run
when Recipe
resource.run(options)
end
Expand Down
19 changes: 10 additions & 9 deletions lib/itamae/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ def define_attribute(name, options)
attr_reader :notifications
attr_reader :updated

def initialize(recipe, resource_name, &block)
def initialize(recipe, resource_name, options = {}, &block)
clear_current_attributes
@recipe = recipe
@resource_name = resource_name
@options = options
@updated = false

EvalContext.new(self).tap do |context|
Expand Down Expand Up @@ -133,11 +134,11 @@ def run(specific_action = nil, options = {})
end

[specific_action || attributes.action].flatten.each do |action|
run_action(action, options)
run_action(action)
end

verify unless options[:dry_run]
notify(options) if updated?
verify unless @options[:dry_run]
notify if updated?
end

@updated = false
Expand Down Expand Up @@ -166,7 +167,7 @@ def resource_type

alias_method :current, :current_attributes

def run_action(action, options)
def run_action(action, options = {})
original_attributes = @attributes # preserve and restore later
@current_action = action

Expand All @@ -187,12 +188,12 @@ def run_action(action, options)
show_differences

method_name = "action_#{action}"
if options[:dry_run]
if @options[:dry_run]
unless respond_to?(method_name)
Itamae.logger.error "action #{action.inspect} is unavailable"
end
else
public_send(method_name, options)
public_send(method_name, @options)
end

updated! if different?
Expand Down Expand Up @@ -327,7 +328,7 @@ def updated?
@updated
end

def notify(options)
def notify(options = {})
(notifications + recipe.children.subscribing(self)).each do |notification|
message = "Notifying #{notification.action} to #{notification.action_resource.resource_type} resource '#{notification.action_resource.resource_name}'"

Expand All @@ -346,7 +347,7 @@ def notify(options)
if notification.delayed?
@recipe.delayed_notifications << notification
elsif notification.immediately?
notification.run(options)
notification.run(@options)
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/itamae/resource/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ def pre_action
when :edit
attributes.exist = true

content = backend.receive_file(attributes.path)
attributes.block.call(content)
attributes.content = content
unless @options[:dry_run]
content = backend.receive_file(attributes.path)
attributes.block.call(content)
attributes.content = content
end
end

send_tempfile
Expand Down Expand Up @@ -163,4 +165,3 @@ def send_tempfile
end
end
end

3 changes: 1 addition & 2 deletions lib/itamae/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def load_recipes(paths)
expanded_path = gem_path if gem_path
end

recipe = Recipe.new(self, expanded_path)
recipe = Recipe.new(self, expanded_path, @options)
children << recipe
recipe.load
end
Expand Down Expand Up @@ -91,4 +91,3 @@ def create_node
end
end
end

4 changes: 3 additions & 1 deletion spec/unit/lib/itamae/resource/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ class TestResource < Itamae::Resource::Base
end

context 'with dry_run' do
subject(:dry_run_resource) { described_class.new(recipe, "name", dry_run: true) }

context 'when specified action is unavailable' do
it 'logs error' do
expect(Itamae.logger).to receive(:error).with(/action :name is unavailable/)
subject.run(nil, dry_run: true)
subject.run
end
end
end
Expand Down

0 comments on commit b21eed1

Please sign in to comment.