Class: Bake::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loaders, scope = nil, root = nil) ⇒ Context

Returns a new instance of Context.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bake/context.rb', line 71

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

#loadersObject (readonly)

Returns the value of attribute loaders.



97
98
99
# File 'lib/bake/context.rb', line 97

def loaders
  @loaders
end

#rootObject (readonly)

Returns the value of attribute root.



96
97
98
# File 'lib/bake/context.rb', line 96

def root
  @root
end

#scopeObject (readonly)

Returns the value of attribute scope.



95
96
97
# File 'lib/bake/context.rb', line 95

def scope
  @scope
end

Class Method Details

.bakefile_path(path, bakefile: BAKEFILE) ⇒ String?

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.

Returns:

  • (String, nil)

    the path to the bakefile if it could be found.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bake/context.rb', line 29

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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bake/context.rb', line 55

def self.load(path)
  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



99
100
101
102
103
104
105
106
107
108
# File 'lib/bake/context.rb', line 99

def call(*commands)
  while command = commands.shift
    if recipe = @recipes[command]
      arguments, options = recipe.prepare(commands)
      recipe.call(*arguments, **options)
    else
      raise ArgumentError, "Could not find recipe for #{command}!"
    end
  end
end

#lookup(command) ⇒ Object



110
111
112
# File 'lib/bake/context.rb', line 110

def lookup(command)
  @recipes[command]
end

#to_sObject



114
115
116
117
118
119
120
# File 'lib/bake/context.rb', line 114

def to_s
  if @root
    "#{self.class} #{File.basename(@root)}"
  else
    self.class.name
  end
end