Skip to content

Commit

Permalink
Move context scope to loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 20, 2024
1 parent 35c7f95 commit 2974369
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 88 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
1 change: 0 additions & 1 deletion lib/bake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@

require_relative 'bake/version'
require_relative 'bake/loaders'
require_relative 'bake/loader'
require_relative 'bake/context'
1 change: 0 additions & 1 deletion lib/bake/command/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'samovar'

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

module Bake
Expand Down
10 changes: 0 additions & 10 deletions lib/bake/command/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ 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|
printed = false

Expand Down
27 changes: 6 additions & 21 deletions lib/bake/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,22 @@ 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)
loaders = Loaders.default(working_directory, bakefile_path)
instance = self.new(loaders, working_directory)

return instance
end

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

@wrappers = Hash.new do |hash, key|
hash[key] = []
Expand All @@ -72,16 +70,6 @@ def initialize(loaders, scope = nil, root = nil)
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
Expand All @@ -90,9 +78,6 @@ def initialize(loaders, scope = nil, root = nil)
# The loaders which will be used to resolve recipes in this context.
attr :loaders

# The scope for the root {BAKEFILE}.
attr :scope

# The root path of this context.
# @returns [String | Nil]
attr :root
Expand Down
48 changes: 0 additions & 48 deletions lib/bake/loader.rb

This file was deleted.

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

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

require_relative '../scope'

module Bake
module Loader
# Represents a directory which contains bakefiles.
class DirectoryLoader
# Initialize the loader with the specified root path.
# @parameter root [String] A file-system path.
def initialize(root, name: nil)
@root = root
@name = name
end

def to_s
"#{self.class} #{@name || @root}"
end

# The root path for this loader.
attr :root

# Enumerate all bakefiles within the loaders root directory.
#
# You can pass the yielded path to {scope_for} to load the corresponding {Scope}.
#
# @yields {|path| ...}
# @parameter path [String] The (relative) scope path.
def each
return to_enum unless block_given?

Dir.glob("**/*.rb", base: @root) do |file_path|
yield file_path.sub(/\.rb$/, '').split(File::SEPARATOR)
end
end

# Load the {Scope} for the specified relative path within this loader, if it exists.
# @parameter path [Array(String)] A relative path.
def scope_for(path)
*directory, file = *path

file_path = File.join(@root, directory, "#{file}.rb")

if File.exist?(file_path)
return Scope.load(file_path, path)
end
end
end

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 scope_for(path)
if file_path = @paths[path]
if File.exist?(file_path)
return Scope.load(file_path, path)
end
end
end
end
end
end
34 changes: 34 additions & 0 deletions lib/bake/loader/file_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

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

require_relative '../scope'

module Bake
module Loader
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 scope_for(path)
if file_path = @paths[path]
if File.exist?(file_path)
return Scope.load(file_path, path)
end
end
end
end
end
end
23 changes: 17 additions & 6 deletions lib/bake/loaders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

require 'console'

require_relative 'loader'
require_relative 'loader/directory_loader'
require_relative 'loader/file_loader'

module Bake
# Structured access to the working directory and loaded gems for loading bakefiles.
Expand All @@ -14,9 +15,13 @@ class Loaders

# Create a loader using the specified working directory.
# @parameter working_directory [String]
def self.default(working_directory)
def self.default(working_directory, bakefile_path = nil)
loaders = self.new

if bakefile_path
loaders.append_bakefile(bakefile_path)
end

loaders.append_defaults(working_directory)

return loaders
Expand All @@ -34,6 +39,12 @@ def empty?
@ordered.empty?
end

def append_bakefile(path)
@ordered << Loader::FileLoader.new({
[] => path
})
end

# Add loaders according to the current working directory and loaded gems.
# @parameter working_directory [String]
def append_defaults(working_directory)
Expand Down Expand Up @@ -66,7 +77,7 @@ def append_path(current = Dir.pwd, **options)
# @parameter current [String] The path to start searching from.
def append_from_root(current = Dir.pwd, **options)
while current
Console.logger.debug(self) {"Checking current #{current}..."}
Console.debug(self) {"Checking current #{current}..."}

append_path(current, **options)

Expand All @@ -83,7 +94,7 @@ def append_from_root(current = Dir.pwd, **options)
# Enumerate all loaded gems and add them.
def append_from_gems
::Gem.loaded_specs.each do |name, spec|
Console.logger.debug(self) {"Checking gem #{name}: #{spec.full_gem_path}..."}
Console.debug(self) {"Checking gem #{name}: #{spec.full_gem_path}..."}

if path = spec.full_gem_path and File.directory?(path)
append_path(path, name: spec.full_name)
Expand All @@ -95,9 +106,9 @@ def append_from_gems

def insert(directory, **options)
unless @roots.key?(directory)
Console.logger.debug(self) {"Adding #{directory.inspect}"}
Console.debug(self) {"Adding #{directory.inspect}"}

loader = Loader.new(directory, **options)
loader = Loader::DirectoryLoader.new(directory, **options)
@roots[directory] = loader
@ordered << loader

Expand Down
5 changes: 5 additions & 0 deletions lib/bake/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def recipes
end

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

# The path of the scope, relative to the root of the context.
def path
self.const_get(:PATH)
end
Expand Down

0 comments on commit 2974369

Please sign in to comment.