Class: Puppet::Parser::TypeLoader

Inherits:
Object
  • Object
show all
Includes:
Node::Environment::Helper
Defined in:
lib/puppet/parser/type_loader.rb

Defined Under Namespace

Classes: Helper

Instance Method Summary collapse

Methods included from Node::Environment::Helper

#environment, #environment=

Constructor Details

#initialize(env) ⇒ TypeLoader

Returns a new instance of TypeLoader.



97
98
99
100
# File 'lib/puppet/parser/type_loader.rb', line 97

def initialize(env)
  self.environment = env
  @loading_helper = Helper.new
end

Instance Method Details

#import(file, current_file = nil) ⇒ Object

Import our files.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppet/parser/type_loader.rb', line 62

def import(file, current_file = nil)
  return if Puppet[:ignoreimport]

  # use a path relative to the file doing the importing
  if current_file
    dir = current_file.sub(%r{[^/]+$},'').sub(/\/$/, '')
  else
    dir = "."
  end
  if dir == ""
    dir = "."
  end

  pat = file
  modname, files = Puppet::Parser::Files.find_manifests(pat, :cwd => dir, :environment => environment)
  if files.size == 0
    raise Puppet::ImportError.new("No file(s) found for import of '#{pat}'")
  end

  files.each do |file|
    unless file =~ /^#{File::SEPARATOR}/
      file = File.join(dir, file)
    end
    @loading_helper.do_once(file) do
      parse_file(file)
    end
  end

  modname
end

#known_resource_typesObject



93
94
95
# File 'lib/puppet/parser/type_loader.rb', line 93

def known_resource_types
  environment.known_resource_types
end

#load_until(namespaces, name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/puppet/parser/type_loader.rb', line 102

def load_until(namespaces, name)
  return nil if name == "" # special-case main.
  name2files(namespaces, name).each do |filename|
    modname = begin
      import(filename)
    rescue Puppet::ImportError => detail
      # We couldn't load the item
      # I'm not convienced we should just drop these errors, but this
      # preserves existing behaviours.
      nil
    end
    if result = yield(filename)
      Puppet.debug "Automatically imported #{name} from #{filename} into #{environment}"
      result.module_name = modname if modname and result.respond_to?(:module_name=)
      return result
    end
  end
  nil
end

#name2files(namespaces, name) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/puppet/parser/type_loader.rb', line 122

def name2files(namespaces, name)
  return [name.sub(/^::/, '').gsub("::", File::SEPARATOR)] if name =~ /^::/

  result = namespaces.inject([]) do |names_to_try, namespace|
    fullname = (namespace + "::#{name}").sub(/^::/, '')

    # Try to load the module init file if we're a qualified name
    names_to_try << fullname.split("::")[0] if fullname.include?("::")

    # Then the fully qualified name
    names_to_try << fullname
  end

  # Otherwise try to load the bare name on its own.  This
  # is appropriate if the class we're looking for is in a
  # module that's different from our namespace.
  result << name
  result.uniq.collect { |f| f.gsub("::", File::SEPARATOR) }
end

#parse_file(file) ⇒ Object



142
143
144
145
146
147
# File 'lib/puppet/parser/type_loader.rb', line 142

def parse_file(file)
  Puppet.debug("importing '#{file}' in environment #{environment}")
  parser = Puppet::Parser::Parser.new(environment)
  parser.file = file
  parser.parse
end