Skip to content

Commit

Permalink
Working wrappers...
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 21, 2024
1 parent c18bdaf commit b5ecd8a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 44 deletions.
10 changes: 10 additions & 0 deletions bake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

wrap('test') do
before do
puts "Before test..."
end

after do
puts "After test..."
end
end
15 changes: 15 additions & 0 deletions lib/bake/bakefile_scope.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.

module Bake
# A special scope used for the root `bake.rb` file.
module BakefileScope
attr_accessor :wrappers

def wrap(...)
@wrappers.wrap(...)
end
end
end
10 changes: 8 additions & 2 deletions lib/bake/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ def self.load(path = Dir.pwd)
end

registry = Registry.default(working_directory, bakefile_path)
instance = self.new(registry, working_directory)
context = self.new(registry, working_directory)

return instance
context.bakefile

return context
end

# Initialize the context with the specified registry.
Expand All @@ -71,6 +73,10 @@ def initialize(registry, root = nil)
end
end

def bakefile
@instances[[]]
end

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

Expand Down
6 changes: 2 additions & 4 deletions lib/bake/registry/aggregate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'console'

require_relative 'directory_loader'
require_relative 'file_loader'
require_relative 'bakefile_loader'
require_relative 'wrappers'

module Bake
Expand Down Expand Up @@ -65,9 +65,7 @@ def scopes_for(path, &block)
end

def append_bakefile(path)
@ordered << FileLoader.new({
[] => path
})
@ordered << BakefileLoader.new(path, @wrappers)
end

# Append a specific project path to the search path for recipes.
Expand Down
39 changes: 39 additions & 0 deletions lib/bake/registry/bakefile_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

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

require_relative '../scope'
require_relative '../bakefile_scope'

module Bake
module Registry
class BakefileLoader
def initialize(path, wrappers)
@path = path
@wrappers = wrappers
end

def to_s
"#{self.class} #{@path}"
end

attr :path

def each(&block)
yield []
end

def scopes_for(path)
if path == []
scope = Scope.load(@path, []) do |scope|
scope.extend(BakefileScope)
scope.wrappers = @wrappers
end

yield scope
end
end
end
end
end
34 changes: 0 additions & 34 deletions lib/bake/registry/file_loader.rb

This file was deleted.

12 changes: 8 additions & 4 deletions lib/bake/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ module Scope
# Load the specified file into a unique scope module, which can then be included into a {Base} instance.
def self.load(file_path, path = [])
scope = Module.new

if scope.respond_to?(:set_temporary_name)
scope.set_temporary_name("#{self.name}[#{file_path}]")
end

scope.extend(self)

scope.const_set(:FILE_PATH, file_path)
scope.const_set(:PATH, path)

yield scope if block_given?

scope.module_eval(File.read(file_path), file_path)

return scope
end

def self.inspect
"Bake::Scope<#{self.const_get(:FILE_PATH)}>"
end

# Recipes defined in this scope.
#
# @yields {|recipe| ...}
Expand All @@ -42,6 +45,7 @@ def recipes

# The path of the file that was used to {load} this scope.
def file_path
pp file_path_self: self
self.const_get(:FILE_PATH)
end

Expand Down

0 comments on commit b5ecd8a

Please sign in to comment.