Class: Bake::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/context.rb

Overview

Represents a context of task execution, containing all relevant state.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry, root = nil) ⇒ Context

Initialize the context with the specified registry.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bake/context.rb', line 64

def initialize(registry, root = nil)
	@registry = registry
	@root = root
	
	@instances = Hash.new do |hash, key|
		hash[key] = instance_for(key)
	end
	
	@recipes = Hash.new do |hash, key|
		hash[key] = recipe_for(key)
	end
end

Instance Attribute Details

#registryObject (readonly)

The registry which will be used to resolve recipes in this context.



82
83
84
# File 'lib/bake/context.rb', line 82

def registry
  @registry
end

#rootObject (readonly)

The root path of this context.



86
87
88
# File 'lib/bake/context.rb', line 86

def root
  @root
end

Class Method Details

.bakefile_path(path, bakefile: BAKEFILE) ⇒ Object

Search upwards from the specified path for a BAKEFILE. If path points to a file, assume it’s a ‘bake.rb` file. Otherwise, recursively search up the directory tree starting from `path` to find the specified bakefile.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bake/context.rb', line 19

def self.bakefile_path(path, bakefile: BAKEFILE)
	if File.file?(path)
		return path
	end
	
	current = path
	
	while current
		bakefile_path = File.join(current, BAKEFILE)
		
		if File.exist?(bakefile_path)
			return bakefile_path
		end
		
		parent = File.dirname(current)
		
		if current == parent
			break
		else
			current = parent
		end
	end
	
	return nil
end

.load(path = Dir.pwd) ⇒ Object

Load a context from the specified path.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bake/context.rb', line 47

def self.load(path = Dir.pwd)
	if bakefile_path = self.bakefile_path(path)
		working_directory = File.dirname(bakefile_path)
	else
		working_directory = path
	end
	
	registry = Registry.default(working_directory, bakefile_path)
	context = self.new(registry, working_directory)
	
	context.bakefile
	
	return context
end

Instance Method Details

#bakefileObject



77
78
79
# File 'lib/bake/context.rb', line 77

def bakefile
	@instances[[]]
end

#call(*commands) ⇒ Object

Invoke recipes on the context using command line arguments.

e.g. ‘context.call(“gem:release:version:increment”, “0,0,1”)`



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bake/context.rb', line 93

def call(*commands)
	last_result = nil
	
	while command = commands.shift
		if recipe = @recipes[command]
			arguments, options = recipe.prepare(commands, last_result)
			last_result = recipe.call(*arguments, **options)
		else
			raise ArgumentError, "Could not find recipe for #{command}!"
		end
	end
	
	return last_result
end

#inspectObject



124
125
126
# File 'lib/bake/context.rb', line 124

def inspect
	"\#<#{self.class} #{@root}>"
end

#lookup(command) ⇒ Object Also known as: []

Lookup a recipe for the given command name.



110
111
112
# File 'lib/bake/context.rb', line 110

def lookup(command)
	@recipes[command]
end

#to_sObject



116
117
118
119
120
121
122
# File 'lib/bake/context.rb', line 116

def to_s
	if @root
		"#{self.class} #{File.basename(@root)}"
	else
		self.class.name
	end
end