diff --git a/bake.rb b/bake.rb new file mode 100644 index 0000000..eed967c --- /dev/null +++ b/bake.rb @@ -0,0 +1,10 @@ + +wrap('test') do + before do + puts "Before test..." + end + + after do + puts "After test..." + end +end diff --git a/lib/bake/bakefile_scope.rb b/lib/bake/bakefile_scope.rb new file mode 100644 index 0000000..873662d --- /dev/null +++ b/lib/bake/bakefile_scope.rb @@ -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 diff --git a/lib/bake/context.rb b/lib/bake/context.rb index 36043ed..5279e3b 100644 --- a/lib/bake/context.rb +++ b/lib/bake/context.rb @@ -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. @@ -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 diff --git a/lib/bake/registry/aggregate.rb b/lib/bake/registry/aggregate.rb index 72a6b05..c0d588a 100644 --- a/lib/bake/registry/aggregate.rb +++ b/lib/bake/registry/aggregate.rb @@ -6,7 +6,7 @@ require 'console' require_relative 'directory_loader' -require_relative 'file_loader' +require_relative 'bakefile_loader' require_relative 'wrappers' module Bake @@ -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. diff --git a/lib/bake/registry/bakefile_loader.rb b/lib/bake/registry/bakefile_loader.rb new file mode 100644 index 0000000..3211682 --- /dev/null +++ b/lib/bake/registry/bakefile_loader.rb @@ -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 \ No newline at end of file diff --git a/lib/bake/registry/file_loader.rb b/lib/bake/registry/file_loader.rb deleted file mode 100644 index 27ec173..0000000 --- a/lib/bake/registry/file_loader.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -# Released under the MIT License. -# Copyright, 2020-2024, by Samuel Williams. - -require_relative '../scope' - -module Bake - module Registry - class FileLoader - def initialize(paths) - @paths = paths - end - - def to_s - "#{self.class} #{@paths}" - end - - attr :paths - - def each(&block) - @paths.each_key(&block) - end - - def scopes_for(path) - if file_path = @paths[path] - if File.exist?(file_path) - yield Scope.load(file_path, path) - end - end - end - end - end -end \ No newline at end of file diff --git a/lib/bake/scope.rb b/lib/bake/scope.rb index d8cd652..73c622e 100644 --- a/lib/bake/scope.rb +++ b/lib/bake/scope.rb @@ -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| ...} @@ -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