Module: MetadataJsonDeps

Defined in:
lib/metadata_json_deps.rb

Defined Under Namespace

Classes: ForgeVersions

Class Method Summary collapse

Class Method Details

.build_fixtures(filename) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/metadata_json_deps.rb', line 20

def self.build_fixtures(filename)
  require 'yaml'

  result = {}

  dependencies = PuppetMetadata.read(filename).dependencies
  if dependencies.any?
    forge = ForgeVersions.new

    repositories = {}
    result['fixtures'] = {'repositories' => repositories}

    dependencies.each do |dependency, _constraint|
      mod = forge.get_module(dependency)
      # TODO: The forge should expose the source URL directly
      repositories[mod.name] = mod.current_release.[:source]
    end
  end

  puts result.to_yaml
end

.bump_dependency(filename, module_name, upper_bound) ⇒ Array<String>

Bump a dependency in a filename

Parameters:

  • filename (String)

    A path to a metadata file. An error is raised if it’s invalid metadata.

  • module_name (String)

    The module name listed in dependencies. It must be normalized to the forge style (using a dash). It can fall back to a slash if metadata uses a slash.

  • upper_bound (String)

    The new upper bound for the module name

Returns:

  • (Array<String>)

    An array with the old and new version. Can be used to determine if a change was made.

See Also:

  • PuppetMetadata.read


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metadata_json_deps.rb', line 53

def self.bump_dependency(filename, module_name, upper_bound)
   = PuppetMetadata.read(filename)

  requirement = .dependencies[module_name]
  unless requirement
    # TODO: normalize keys in puppet_metadata so we don't need 2 lookups?
    module_name = module_name.tr('-', '/')
    requirement = .dependencies[module_name]
    raise Exception.new("Dependency #{module_name} not found") unless requirement
  end

  return [requirement.to_s, requirement.to_s] if requirement.end == upper_bound

  new = ">= #{requirement.begin} < #{upper_bound}"

   = ..clone
  ['dependencies'].each do |dependency|
    if dependency['name'] == module_name
      dependency['version_requirement'] = new
    end
  end

  File.write(filename, JSON.pretty_generate() + "\n")

  [requirement.to_s, new]
end

.run(filenames, verbose = false) ⇒ Integer

Returns the exit code.

Parameters:

  • filenames (Array[String])

    The filenames to run on

  • verbose (Boolean) (defaults to: false)

    Whether or not to run in verbose mode

Returns:

  • (Integer)

    the exit code



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/metadata_json_deps.rb', line 86

def self.run(filenames, verbose = false)
  forge = ForgeVersions.new

  exit_code = 0

  filenames.each do |filename|
    puts "Checking #{filename}"
     = PuppetMetadata.read(filename)

    .dependencies.map do |dependency, constraint|
      mod = forge.get_module(dependency)

      if mod.deprecated_at
        exit_code |= 2
        if mod.superseded_by
          puts "  #{dependency} was superseded by #{mod.superseded_by[:slug]}"
        elsif mod.deprecated_for
          puts "  #{dependency} was deprecated: #{mod.deprecated_for}"
        else
          puts "  #{dependency} was deprecated"
        end
      else
        current = mod.current_release.version

        if .satisfies_dependency?(dependency, current)
          if verbose
            puts "  #{dependency} (#{constraint}) matches #{current}"
          end
        else
          exit_code |= 1
          puts "  #{dependency} (#{constraint}) doesn't match #{current}"
        end
      end
    end
  end

  exit_code
rescue Interrupt
  0
end