Class: Teapot::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ Context

Returns a new instance of Context.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/teapot/context.rb', line 41

def initialize(root, options = {})
  @root = Pathname(root)
  @options = options

  @targets = {}
  @generators = {}
  @configurations = {}

  @dependencies = []
  @selection = Set.new

  @loaded = {}

  # Load the root package:
  defined = load(root_package)

  # Find the default configuration, if it exists:
  @default_configuration = defined.default_configuration

  if options[:configuration]
    @configuration = @configurations[options[:configuration]]
  else
    @configuration = @default_configuration
  end

  # Materialize the configuration:
  @configuration = @configuration.materialize if @configuration
end

Instance Attribute Details

#configurationObject (readonly)

The context’s primary configuration.



80
81
82
# File 'lib/teapot/context.rb', line 80

def configuration
  @configuration
end

#configurationsObject (readonly)

All public configurations.



77
78
79
# File 'lib/teapot/context.rb', line 77

def configurations
  @configurations
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



82
83
84
# File 'lib/teapot/context.rb', line 82

def dependencies
  @dependencies
end

#generatorsObject (readonly)

Returns the value of attribute generators.



74
75
76
# File 'lib/teapot/context.rb', line 74

def generators
  @generators
end

#optionsObject (readonly)

Returns the value of attribute options.



71
72
73
# File 'lib/teapot/context.rb', line 71

def options
  @options
end

#rootObject (readonly)

Returns the value of attribute root.



70
71
72
# File 'lib/teapot/context.rb', line 70

def root
  @root
end

#selectionObject (readonly)

Returns the value of attribute selection.



83
84
85
# File 'lib/teapot/context.rb', line 83

def selection
  @selection
end

#targetsObject (readonly)

Returns the value of attribute targets.



73
74
75
# File 'lib/teapot/context.rb', line 73

def targets
  @targets
end

Instance Method Details

#<<(definition) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/teapot/context.rb', line 109

def << definition
  case definition
  when Target
    AlreadyDefinedError.check(definition, @targets)

    @targets[definition.name] = definition
  when Generator
    AlreadyDefinedError.check(definition, @generators)

    @generators[definition.name] = definition
  when Configuration
    # We define configurations in two cases, if they are public, or if they are part of the root package of this context.
    if definition.public? or definition.package == @root_package
      # The root package implicitly defines the default configuration.
      if definition.name == DEFAULT_CONFIGURATION_NAME
        raise AlreadyDefinedError.new(definition, root_package)
      end

      AlreadyDefinedError.check(definition, @configurations)

      @configurations[definition.name] = definition
    end
  end
end

#dependency_chain(dependency_names, configuration = @configuration) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/teapot/context.rb', line 95

def dependency_chain(dependency_names, configuration = @configuration)
  configuration.load_all

  select(dependency_names)

  Dependency::chain(@selection, @dependencies, @targets.values)
end

#direct_targets(ordered) ⇒ Object



103
104
105
106
107
# File 'lib/teapot/context.rb', line 103

def direct_targets(ordered)
  @dependencies.collect do |dependency|
    ordered.find{|(package, _)| package.provides? dependency}
  end.compact
end

#load(package) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/teapot/context.rb', line 134

def load(package)
  # In certain cases, a package record might be loaded twice. This typically occurs when multiple configurations are loaded in the same context, or if a package has already been loaded (as is typical with the root package).
  @loaded.fetch(package) do
    loader = Loader.new(self, package)

    loader.load(TEAPOT_FILE)

    # Load the definitions into the current context:
    loader.defined.each do |definition|
      self << definition
    end

    # Save the definitions per-package:
    @loaded[package] = loader.defined
  end
end

#select(names) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/teapot/context.rb', line 85

def select(names)
  names.each do |name|
    if @targets.key? name
      @selection << name
    else
      @dependencies << name
    end
  end
end

#unresolved(packages) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/teapot/context.rb', line 151

def unresolved(packages)
  failed_to_load = Set.new
  
  packages.collect do |package|
    begin
      definitions = load(package)
    rescue NonexistantTeapotError, IncompatibleTeapotError
      # If the package doesn't exist or the teapot version is too old, it failed:
      failed_to_load << package
    end
  end
  
  return failed_to_load
end