Skip to content

Commit

Permalink
Refactor loaders to allow for custom wrappers. (#18)
Browse files Browse the repository at this point in the history
* Move context scope to loader.

* Rename `Loaders` to `Registry::Aggregate`

* Generic `scopes_for` implementation.
  • Loading branch information
ioquatix committed Aug 21, 2024
1 parent 35c7f95 commit c18bdaf
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 247 deletions.
2 changes: 1 addition & 1 deletion guides/getting-started/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $ bundle add bake

- A `bake` executable used for invoking one or more tasks.
- A {ruby Bake::Context} instance which is bound to a project or gem and exposes a hierarchy of runnable tasks.
- A {ruby Bake::Loaders} instance which is used for on-demand loading of bake files from the current project and all available gems.
- A {ruby Bake::Loader::Aggregate} instance which is used for on-demand loading of bake files from the current project and all available gems.

## Executing Tasks

Expand Down
3 changes: 1 addition & 2 deletions lib/bake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
# Copyright, 2020-2024, by Samuel Williams.

require_relative 'bake/version'
require_relative 'bake/loaders'
require_relative 'bake/loader'
require_relative 'bake/registry'
require_relative 'bake/context'
3 changes: 1 addition & 2 deletions lib/bake/command/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

require 'samovar'

require_relative '../loaders'
require_relative '../loader'
require_relative '../registry'
require_relative '../context'

module Bake
Expand Down
14 changes: 2 additions & 12 deletions lib/bake/command/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,11 @@ def call
terminal = @parent.terminal
context = @parent.context

if scope = context.scope
printed = print_scope(terminal, context.scope) do
terminal.print_line(:context, context)
end

if printed
terminal.print_line
end
end

context.loaders.each do |loader|
context.registry.each do |loader|
printed = false

loader.each do |path|
if scope = loader.scope_for(path)
loader.scopes_for(path) do |scope|
print_scope(terminal, scope, printed: printed) do
terminal.print_line(:loader, loader)
printed = true
Expand Down
86 changes: 16 additions & 70 deletions lib/bake/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,53 +45,34 @@ def self.bakefile_path(path, bakefile: BAKEFILE)
# @path [String] A file-system path.
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)
registry = Registry.default(working_directory, bakefile_path)
instance = self.new(registry, working_directory)

return instance
end

# Initialize the context with the specified loaders.
# @parameter loaders [Loaders]
def initialize(loaders, scope = nil, root = nil)
@loaders = loaders

@wrappers = Hash.new do |hash, key|
hash[key] = []
end
# Initialize the context with the specified registry.
# @parameter registry [Registry]
def initialize(registry, root = nil)
@registry = registry
@root = root

@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

# The loaders which will be used to resolve recipes in this context.
attr :loaders

# The scope for the root {BAKEFILE}.
attr :scope
# The registry which will be used to resolve recipes in this context.
attr :registry

# The root path of this context.
# @returns [String | Nil]
Expand Down Expand Up @@ -123,35 +104,8 @@ def lookup(command)
@recipes[command]
end

class Wrapper
def initialize(wrappers, path)
@wrappers = wrappers
@path = path
end

def before(name = @path.last, &block)
wrapper = Module.new
wrapper.define_method(name) do |*arguments, **options|
instance_exec(&block)
super(*arguments, **options)
end

@wrappers[@path] << wrapper
end

def after(name = @path.last, &block)
wrapper = Module.new
wrapper.define_method(name) do |*arguments, **options|
super(*arguments, **options)
instance_exec(&block)
end

@wrappers[@path] << wrapper
end
end

def wrap(*path, &block)
Wrapper.new(@wrappers, path).instance_exec(&block)
def wrap(...)
@registry.wrap(...)
end

def to_s
Expand Down Expand Up @@ -199,17 +153,9 @@ 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:
@loaders.each do |loader|
if scope = loader.scope_for(path)
base ||= Base.derive(path)

base.prepend(scope)
end
end

# If we have any wrappers for the given path, we also prepend them to the base:
@wrappers[path].each do |wrapper|
base.prepend(wrapper)
@registry.scopes_for(path) do |scope|
base ||= Base.derive(path)
base.prepend(scope)
end

return base
Expand Down
48 changes: 0 additions & 48 deletions lib/bake/loader.rb

This file was deleted.

110 changes: 0 additions & 110 deletions lib/bake/loaders.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/bake/registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require_relative 'registry/aggregate'

module Bake
# Structured access to the working directory and loaded gems for loading bakefiles.
module Registry
def self.default(...)
Aggregate.default(...)
end
end
end
Loading

0 comments on commit c18bdaf

Please sign in to comment.