Module: Puppet::ModuleTool

Extended by:
Util::Colors
Defined in:
lib/puppet/module_tool.rb,
lib/puppet/module_tool/errors.rb,
lib/puppet/module_tool/metadata.rb,
lib/puppet/module_tool/skeleton.rb,
lib/puppet/module_tool/checksums.rb,
lib/puppet/module_tool/dependency.rb,
lib/puppet/module_tool/modulefile.rb,
lib/puppet/module_tool/applications.rb,
lib/puppet/module_tool/install_directory.rb,
lib/puppet/module_tool/applications/builder.rb,
lib/puppet/module_tool/contents_description.rb,
lib/puppet/module_tool/applications/searcher.rb,
lib/puppet/module_tool/applications/unpacker.rb,
lib/puppet/module_tool/applications/upgrader.rb,
lib/puppet/module_tool/applications/generator.rb,
lib/puppet/module_tool/applications/installer.rb,
lib/puppet/module_tool/applications/application.rb,
lib/puppet/module_tool/applications/checksummer.rb,
lib/puppet/module_tool/applications/uninstaller.rb

Defined Under Namespace

Modules: Applications, Errors, Shared, Tar Classes: Checksums, ContentsDescription, Dependency, InstallDirectory, Metadata, ModulefileReader, Skeleton

Constant Summary collapse

ARTIFACTS =

Directory and names that should not be checksummed.

['pkg', /^\./, /^~/, /^#/, 'coverage', 'metadata.json', 'REVISION']
FULL_MODULE_NAME_PATTERN =
/\A([^-\/|.]+)[-|\/](.+)\z/
REPOSITORY_URL =

Constants included from Util::Colors

Util::Colors::BG_BLUE, Util::Colors::BG_CYAN, Util::Colors::BG_GREEN, Util::Colors::BG_HBLUE, Util::Colors::BG_HCYAN, Util::Colors::BG_HGREEN, Util::Colors::BG_HMAGENTA, Util::Colors::BG_HRED, Util::Colors::BG_HWHITE, Util::Colors::BG_HYELLOW, Util::Colors::BG_MAGENTA, Util::Colors::BG_RED, Util::Colors::BG_WHITE, Util::Colors::BG_YELLOW, Util::Colors::BLACK, Util::Colors::BLUE, Util::Colors::CYAN, Util::Colors::Colormap, Util::Colors::GREEN, Util::Colors::HBLACK, Util::Colors::HBLUE, Util::Colors::HCYAN, Util::Colors::HGREEN, Util::Colors::HMAGENTA, Util::Colors::HRED, Util::Colors::HWHITE, Util::Colors::HYELLOW, Util::Colors::MAGENTA, Util::Colors::RED, Util::Colors::RESET, Util::Colors::WHITE, Util::Colors::YELLOW

Class Method Summary collapse

Methods included from Util::Colors

colorize, console_color, console_has_color?, html_color

Class Method Details

.artifact?(path) ⇒ Boolean

Is this a directory that shouldn’t be checksummed?

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

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
# File 'lib/puppet/module_tool.rb', line 22

def self.artifact?(path)
  case File.basename(path)
  when *ARTIFACTS
    true
  else
    false
  end
end

.build_tree(mods, dir) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet/module_tool.rb', line 91

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) ⇒ Pathname?

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’.

Parameters:

  • path (Pathname, String)

    path to start from

Returns:

  • (Pathname, nil)

    the root path of the module directory or nil if we cannot find one



48
49
50
51
52
53
54
55
56
# File 'lib/puppet/module_tool.rb', line 48

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) ⇒ Object

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/module_tool.rb', line 71

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) ⇒ Boolean

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

Parameters:

  • path (Pathname, String)

    path to analyse

Returns:

  • (Boolean)

    true if the path is a module root, false otherwise



63
64
65
66
67
# File 'lib/puppet/module_tool.rb', line 63

def self.is_module_root?(path)
  path = Pathname.new(path) if path.class == String

  FileTest.file?(path + 'Modulefile')
end

.set_option_defaults(options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/module_tool.rb', line 106

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) ⇒ Object

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



33
34
35
36
37
38
39
# File 'lib/puppet/module_tool.rb', line 33

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