module Puppet::ModuleTool

Constants

ARTIFACTS

Directory and names that should not be checksummed.

FULL_MODULE_NAME_PATTERN
REPOSITORY_URL

Public Class Methods

artifact?(path) click to toggle source

Is this a directory that shouldn’t be checksummed?

TODO: Should this be part of Checksums? TODO: Rename this method to reflect it’s purpose? TODO: Shouldn’t this be used when building packages too?

# File lib/puppet/module_tool.rb, line 20
def self.artifact?(path)
  case File.basename(path)
  when *ARTIFACTS
    true
  else
    false
  end
end
build_tree(mods, dir) click to toggle source
# File lib/puppet/module_tool.rb, line 89
def self.build_tree(mods, dir)
  mods.each do |mod|
    version_string = mod[:version][:vstring].sub(/^(?!v)/, 'v')

    if mod[:action] == :upgrade
      previous_version = mod[:previous_version].sub(/^(?!v)/, 'v')
      version_string = "#{previous_version} -> #{version_string}"
    end

    mod[:text] = "#{mod[:module]} (#{colorize(:cyan, version_string)})"
    mod[:text] += " [#{mod[:path]}]" unless mod[:path] == dir
    build_tree(mod[:dependencies], dir)
  end
end
find_module_root(path) click to toggle source

Find the module root when given a path by checking each directory up from its current location until it finds one that contains a file called ‘Modulefile’.

@param path [Pathname, String] path to start from @return [Pathname, nil] the root path of the module directory or nil if

we cannot find one
# File lib/puppet/module_tool.rb, line 46
def self.find_module_root(path)
  path = Pathname.new(path) if path.class == String

  path.expand_path.ascend do |p|
    return p if is_module_root?(p)
  end

  nil
end
format_tree(nodes, level = 0) click to toggle source

Builds a formatted tree from a list of node hashes containing :text and :dependencies keys.

# File lib/puppet/module_tool.rb, line 69
def self.format_tree(nodes, level = 0)
  str = ''
  nodes.each_with_index do |node, i|
    last_node = nodes.length - 1 == i
    deps = node[:dependencies] || []

    str << (indent = "  " * level)
    str << (last_node ? "└" : "├")
    str << "─"
    str << (deps.empty? ? "─" : "┬")
    str << " #{node[:text]}\n"

    branch = format_tree(deps, level + 1)
    branch.gsub!(/^#{indent} /, indent + '│') unless last_node
    str << branch
  end

  return str
end
is_module_root?(path) click to toggle source

Analyse path to see if it is a module root directory by detecting a file named ‘Modulefile’ in the directory.

@param path [Pathname, String] path to analyse @return [Boolean] true if the path is a module root, false otherwise

# File lib/puppet/module_tool.rb, line 61
def self.is_module_root?(path)
  path = Pathname.new(path) if path.class == String

  FileTest.file?(path + 'Modulefile')
end
set_option_defaults(options) click to toggle source
# File lib/puppet/module_tool.rb, line 104
def self.set_option_defaults(options)
  sep = File::PATH_SEPARATOR

  if options[:environment]
    Puppet.settings[:environment] = options[:environment]
  else
    options[:environment] = Puppet.settings[:environment]
  end

  if options[:modulepath]
    Puppet.settings[:modulepath] = options[:modulepath]
  else
    # (#14872) make sure the module path of the desired environment is used
    # when determining the default value of the --target-dir option
    Puppet.settings[:modulepath] = options[:modulepath] =
      Puppet.settings.value(:modulepath, options[:environment])
  end

  if options[:target_dir]
    options[:target_dir] = File.expand_path(options[:target_dir])
    # prepend the target dir to the module path
    Puppet.settings[:modulepath] = options[:modulepath] =
      options[:target_dir] + sep + options[:modulepath]
  else
    # default to the first component of the module path
    options[:target_dir] =
      File.expand_path(options[:modulepath].split(sep).first)
  end
end
username_and_modname_from(full_module_name) click to toggle source

Return the username and modname for a given full_module_name, or raise an ArgumentError if the argument isn’t parseable.

# File lib/puppet/module_tool.rb, line 31
def self.username_and_modname_from(full_module_name)
  if matcher = full_module_name.match(FULL_MODULE_NAME_PATTERN)
    return matcher.captures
  else
    raise ArgumentError, "Not a valid full name: #{full_module_name}"
  end
end