class Context
Represents a context of task execution, containing all relevant state.
Definitions
def self.bakefile_path(path, bakefile: BAKEFILE)
Search upwards from the specified path for a BAKEFILE = "bake.rb"
.
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.
Signature
-
returns
String | Nil
The path to the bakefile if it could be found.
Implementation
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
def self.load(path = Dir.pwd)
Load a context from the specified path.
Implementation
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
def initialize(registry, root = nil)
Initialize the context with the specified registry.
Signature
-
parameter
registry
Registry
Implementation
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
attr :registry
The registry which will be used to resolve recipes in this context.
attr :root
The root path of this context.
Signature
-
returns
String | Nil
def call(*commands)
Invoke recipes on the context using command line arguments.
e.g. context.call("gem:release:version:increment", "0,0,1")
Signature
-
parameter
commands
Array(String)
Implementation
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
def lookup(command)
Lookup a recipe for the given command name.
Signature
-
parameter
command
String
The command name, e.g.
bundler:release
.
Implementation
def lookup(command)
@recipes[command]
end
def base_for(path)
Signature
-
parameter
path
Array(String)
the path for the scope.
Implementation
def base_for(path)
base = nil
# For each loader, we check if it has a scope for the given path. If it does, we prepend it to the base:
@registry.scopes_for(path) do |scope|
base ||= Base.derive(path)
base.prepend(scope)
end
return base
end