Class: OpsWalrus::LoadPath

Inherits:
Object
  • Object
show all
Includes:
Traversable
Defined in:
lib/opswalrus/runtime_environment.rb

Overview

the assumption is that we have a bundle directory with all the packages in it and the bundle directory is the root directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traversable

#pre_order_traverse

Constructor Details

#initialize(runtime_env, dir) ⇒ LoadPath

Returns a new instance of LoadPath.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/opswalrus/runtime_environment.rb', line 121

def initialize(runtime_env, dir)
  @runtime_env = runtime_env
  @dir = dir
  @root_namespace = build_symbol_resolution_tree(@dir)
  @path_map = build_path_map(@root_namespace)

  App.instance.trace "LoadPath for #{@dir} ************************************************************"
  App.instance.trace 'root namespace ******************************************************************'
  App.instance.trace @root_namespace.to_s
  App.instance.trace 'path map ************************************************************************'
  @path_map.each do |k,v|
    App.instance.trace "#{k.to_s}: #{v.to_s}"
  end

  @dynamic_package_additions_memo = {}
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



118
119
120
# File 'lib/opswalrus/runtime_environment.rb', line 118

def dir
  @dir
end

#runtime_envObject

Returns the value of attribute runtime_env.



119
120
121
# File 'lib/opswalrus/runtime_environment.rb', line 119

def runtime_env
  @runtime_env
end

Instance Method Details

#build_path_map(root_namespace) ⇒ Object

returns a Map of path -> (Namespace | OpsFile) pairs



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/opswalrus/runtime_environment.rb', line 162

def build_path_map(root_namespace)
  path_map = {}

  pre_order_traverse(root_namespace) do |namespace_or_ops_file|
    case namespace_or_ops_file
    when Namespace
      path_map[namespace_or_ops_file.dirname] = namespace_or_ops_file
    when OpsFile
      path_map[namespace_or_ops_file.ops_file_path] = namespace_or_ops_file
    end

    namespace_or_ops_file.symbol_table.values if namespace_or_ops_file.is_a?(Namespace)
  end

  path_map
end

#build_symbol_resolution_tree(directory_path) ⇒ Object

returns a tree of Namespace -> -> {Namespace* -> …, OpsFile*, OpsFile*}



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/opswalrus/runtime_environment.rb', line 139

def build_symbol_resolution_tree(directory_path)
  namespace = Namespace.new(runtime_env, directory_path)

  directory_path.glob("*.ops").each do |ops_file_path|
    ops_file = OpsFile.new(@app, ops_file_path)
    namespace.add(ops_file.basename, ops_file)
  end

  directory_path.glob("*").
                 select(&:directory?).
                 reject {|dir| dir.basename.to_s.downcase == Bundler::BUNDLE_DIR }.
                 each do |dir_path|
    dir_basename = dir_path.basename
    unless namespace.resolve_symbol(dir_basename)
      child_namespace = build_symbol_resolution_tree(dir_path)
      namespace.add(dir_basename, child_namespace)
    end
  end

  namespace
end

#dynamically_add_new_package_dir(new_package_dir) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/opswalrus/runtime_environment.rb', line 183

def dynamically_add_new_package_dir(new_package_dir)
  # patch the symbol resolution (namespace) tree
  dir_basename = new_package_dir.basename
  unless @root_namespace.resolve_symbol(dir_basename)
    new_child_namespace = build_symbol_resolution_tree(new_package_dir)
    @root_namespace.add(dir_basename, new_child_namespace)

    # patch the path map
    new_partial_path_map = build_path_map(new_child_namespace)
    @path_map.merge!(new_partial_path_map)
  end
end

#includes_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/opswalrus/runtime_environment.rb', line 179

def includes_path?(path)
  !!@path_map[path]
end

#lookup_namespace(ops_file) ⇒ Object

returns a Namespace



197
198
199
# File 'lib/opswalrus/runtime_environment.rb', line 197

def lookup_namespace(ops_file)
  @path_map[ops_file.dirname]
end

#resolve_import_reference(origin_ops_file, import_reference) ⇒ Object

returns a Namespace | OpsFile



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/opswalrus/runtime_environment.rb', line 209

def resolve_import_reference(origin_ops_file, import_reference)
  resolved_namespace_or_ops_file = case import_reference
  when PackageDependencyReference
    # App.instance.trace "root namespace: #{@root_namespace.symbol_table}"
    @root_namespace.resolve_symbol(import_reference.package_reference.import_resolution_dirname)   # returns the Namespace associated with the bundled package import_resolution_dirname (i.e. the local name)
  when DynamicPackageImportReference
    dynamic_package_reference = import_reference.package_reference
    @dynamic_package_additions_memo[dynamic_package_reference] ||= begin
      # App.instance.trace "Downloading dynamic package: #{dynamic_package_reference.inspect}"
      App.instance.debug("Downloading dynamic package: #{dynamic_package_reference}")
      dynamically_added_package_dir = @runtime_env.app.bundler.download_git_package(dynamic_package_reference.package_uri, dynamic_package_reference.version)
      dynamically_add_new_package_dir(dynamically_added_package_dir)
      dynamically_added_package_dir
    end
    @root_namespace.resolve_symbol(import_reference.package_reference.import_resolution_dirname)   # returns the Namespace associated with the bundled package dirname (i.e. the sanitized package uri)
  when DirectoryReference
    @path_map[import_reference.dirname]
  when OpsFileReference
    @path_map[import_reference.ops_file_path]
  end
  App.instance.debug("LoadPath#resolve_import_reference(#{origin_ops_file}, #{import_reference}) -> #{resolved_namespace_or_ops_file}")
  resolved_namespace_or_ops_file
end

#resolve_symbol(origin_ops_file, symbol_name) ⇒ Object

returns a Namespace or OpsFile



202
203
204
205
206
# File 'lib/opswalrus/runtime_environment.rb', line 202

def resolve_symbol(origin_ops_file, symbol_name)
  resolved_namespace_or_ops_file = lookup_namespace(origin_ops_file)&.resolve_symbol(symbol_name)
  App.instance.debug("LoadPath#resolve_symbol(#{origin_ops_file}, #{symbol_name}) -> #{resolved_namespace_or_ops_file}")
  resolved_namespace_or_ops_file
end