class Bake::Context
Represents a context of task execution, containing all relevant state.
Attributes
The loaders which will be used to resolve recipes in this context.
The root path of this context. @returns [String | Nil]
The scope for the root {BAKEFILE}.
Public Class Methods
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. @returns [String | Nil] The path to the bakefile if it could be found.
# File lib/bake/context.rb, line 34 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 a context from the specified path. @path [String] A file-system path.
# File lib/bake/context.rb, line 62 def self.load(path = Dir.pwd) if bakefile_path = self.bakefile_path(path) scope = Scope.load(bakefile_path) working_directory = File.dirname(bakefile_path) loaders = Loaders.default(working_directory) else scope = nil working_directory = path loaders = Loaders.default(working_directory) end return self.new(loaders, scope, working_directory) end
Initialize the context with the specified loaders. @parameter loaders [Loaders]
# File lib/bake/context.rb, line 80 def initialize(loaders, scope = nil, root = nil) @loaders = loaders @stack = [] @instances = Hash.new do |hash, key| hash[key] = instance_for(key) end @scope = scope @root = root if @scope base = Base.derive base.prepend(@scope) @instances[[]] = base.new(self) end @recipes = Hash.new do |hash, key| hash[key] = recipe_for(key) end end
Public Instance Methods
Invoke recipes on the context using command line arguments. @parameter commands [Array(String)]
# File lib/bake/context.rb, line 116 def call(*commands) last_result = nil while command = commands.shift if recipe = @recipes[command] arguments, options = recipe.prepare(commands) last_result = recipe.call(*arguments, **options) else raise ArgumentError, "Could not find recipe for #{command}!" end end return last_result end
# File lib/bake/context.rb, line 145 def inspect "\#<#{self.class} #{@root}>" end
Lookup a recipe for the given command name. @parameter command [String] The command name, e.g. `bundler:release`.
# File lib/bake/context.rb, line 133 def lookup(command) @recipes[command] end
# File lib/bake/context.rb, line 137 def to_s if @root "#{self.class} #{File.basename(@root)}" else self.class.name end end
Private Instance Methods
@parameter path [Array<String>] the path for the scope.
# File lib/bake/context.rb, line 174 def base_for(path) base = nil @loaders.each do |loader| if scope = loader.scope_for(path) base ||= Base.derive(path) base.prepend(scope) end end return base end
# File lib/bake/context.rb, line 167 def instance_for(path) if base = base_for(path) return base.new(self) end end
# File lib/bake/context.rb, line 151 def recipe_for(command) path = command.split(":") if instance = @instances[path] and instance.respond_to?(path.last) return instance.recipe_for(path.last) else *path, name = *path if instance = @instances[path] and instance.respond_to?(name) return instance.recipe_for(name) end end return nil end