Class: Bake::Context
- Inherits:
-
Object
- Object
- Bake::Context
- Defined in:
- lib/bake/context.rb
Overview
Represents a context of task execution, containing all relevant state.
Instance Attribute Summary collapse
-
#loaders ⇒ Object
readonly
The loaders which will be used to resolve recipes in this context.
-
#root ⇒ String | Nil
readonly
The root path of this context.
-
#scope ⇒ Object
readonly
The scope for the root BAKEFILE.
Class Method Summary collapse
-
.bakefile_path(path, bakefile: BAKEFILE) ⇒ String | Nil
Search upwards from the specified path for a BAKEFILE.
-
.load(path = Dir.pwd) ⇒ Object
Load a context from the specified path.
Instance Method Summary collapse
-
#call(*commands) ⇒ Object
Invoke recipes on the context using command line arguments.
-
#initialize(loaders, scope = nil, root = nil) ⇒ Context
constructor
Initialize the context with the specified loaders.
-
#lookup(command) ⇒ Object
Lookup a recipe for the given command name.
- #to_s ⇒ Object
Constructor Details
#initialize(loaders, scope = nil, root = nil) ⇒ Context
Initialize the context with the specified loaders.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bake/context.rb', line 78 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 |
Instance Attribute Details
#loaders ⇒ Object (readonly)
The loaders which will be used to resolve recipes in this context.
103 104 105 |
# File 'lib/bake/context.rb', line 103 def loaders @loaders end |
#root ⇒ String | Nil (readonly)
The root path of this context.
110 111 112 |
# File 'lib/bake/context.rb', line 110 def root @root end |
#scope ⇒ Object (readonly)
The scope for the root BAKEFILE.
106 107 108 |
# File 'lib/bake/context.rb', line 106 def scope @scope end |
Class Method Details
.bakefile_path(path, bakefile: BAKEFILE) ⇒ String | Nil
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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bake/context.rb', line 32 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bake/context.rb', line 60 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 |
Instance Method Details
#call(*commands) ⇒ Object
Invoke recipes on the context using command line arguments.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/bake/context.rb', line 114 def call(*commands) last_result = nil while command = commands.shift if recipe = @recipes[command] arguments, = recipe.prepare(commands) last_result = recipe.call(*arguments, **) else raise ArgumentError, "Could not find recipe for #{command}!" end end return last_result end |
#lookup(command) ⇒ Object
Lookup a recipe for the given command name.
131 132 133 |
# File 'lib/bake/context.rb', line 131 def lookup(command) @recipes[command] end |
#to_s ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/bake/context.rb', line 135 def to_s if @root "#{self.class} #{File.basename(@root)}" else self.class.name end end |