Class: Puppet::Node::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-languageserver-sidecar/puppet_modulepath_monkey_patches.rb

Instance Method Summary collapse

Instance Method Details

#create_workspace_module_object(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet-languageserver-sidecar/puppet_modulepath_monkey_patches.rb', line 55

def create_workspace_module_object(path)
  # Read the metadata to find the actual module name
  md_file = File.join(PuppetLanguageServerSidecar::Workspace.root_path, 'metadata.json')
  begin
     = workspace_load_json(File.read(md_file, encoding: 'utf-8'))
    return nil if ['name'].nil?

    # Extract the actual module name
    if Puppet::Module.is_module_directory_name?(['name'])
      module_name = ['name']
    elsif Puppet::Module.is_module_namespaced_name?(['name'])
      # Based on regex at https://github.com/puppetlabs/puppet/blob/f5ca8c05174c944f783cfd0b18582e2160b77d0e/lib/puppet/module.rb#L54
      result = /^[a-zA-Z0-9]+-([a-z][a-z0-9_]*)$/.match(['name'])
      module_name = result[1]
    else
      # TODO: This is an invalid puppet module name in the metadata.json.  Should we log an error/warning?
      return nil
    end

    # The Puppet::Module initializer was changed in
    # https://github.com/puppetlabs/puppet/commit/935c0311dbaf1df03937822525c36b26de5390ef
    # We need to switch the creation based on whether the modules_strict_semver? method is available
    return Puppet::Module.new(module_name, path, self, modules_strict_semver?) if respond_to?(:modules_strict_semver?)

    Puppet::Module.new(module_name, path, self)
  rescue StandardError
    nil
  end
end

#modulesObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/puppet-languageserver-sidecar/puppet_modulepath_monkey_patches.rb', line 85

def modules
  if @modules.nil?
    original_modules
    if PuppetLanguageServerSidecar::Workspace.has_module_metadata?
      workspace_module = create_workspace_module_object(PuppetLanguageServerSidecar::Workspace.root_path)
      @modules << workspace_module unless workspace_module.nil?
    end

    @modules
  else
    original_modules
  end
end

#modules_by_pathObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet-languageserver-sidecar/puppet_modulepath_monkey_patches.rb', line 99

def modules_by_path
  result = original_modules_by_path

  result.keys.each do |key|
    if key == PuppetLanguageServerSidecar::Workspace.root_path && PuppetLanguageServerSidecar::Workspace.has_module_metadata?
      workspace_module = create_workspace_module_object(key)
      result[key] = workspace_module.nil? ? [] : [workspace_module]
    end
  end

  result
end

#original_modulesObject



30
# File 'lib/puppet-languageserver-sidecar/puppet_modulepath_monkey_patches.rb', line 30

alias original_modules modules

#original_modules_by_pathObject



31
# File 'lib/puppet-languageserver-sidecar/puppet_modulepath_monkey_patches.rb', line 31

alias original_modules_by_path modules_by_path

#workspace_load_json(string, options = {}) ⇒ Object

The Puppet::Util::Json class doesn’t exist in all puppet version. Instead just vendor the code here as it’s a simple JSON loader only for metadata.json. github.com/puppetlabs/puppet/blob/5.5.0/lib/puppet/util/json.rb#L32-L49



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet-languageserver-sidecar/puppet_modulepath_monkey_patches.rb', line 36

def workspace_load_json(string, options = {})
  if defined? MultiJson
    begin
      MultiJson.load(string, options)
    rescue MultiJson::ParseError => e
      raise Puppet::Util::Json::ParseError.build(e, string)
    end
  else
    begin
      string = string.read if string.respond_to?(:read)

      options[:symbolize_names] = true if options.delete(:symbolize_keys)
      ::JSON.parse(string, options)
    rescue JSON::ParserError => e
      raise Puppet::Util::Json::ParseError.build(e, string)
    end
  end
end