Class: Chef::Taste::DependencyChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/taste/dependency_checker.rb

Overview

The class checks for cookbook dependencies and populates all fields for the dependent cookbook

Class Method Summary collapse

Class Method Details

.check(path = Dir.pwd) ⇒ Array<Dependency>

Check for cookbook dependencies and their versions

Parameters:

  • path (String) (defaults to: Dir.pwd)

    the path for the cookbook

Returns:

  • (Array<Dependency>)

    list of dependency objects

Raises:

  • NotACookbook the current/given path is not a cookbook



42
43
44
45
46
47
48
49
50
# File 'lib/chef/taste/dependency_checker.rb', line 42

def check(path = Dir.pwd)
  raise NotACookbookError, 'Path is not a cookbook' unless File.exists?(File.join(path, 'metadata.rb'))
  ridley = Ridley::Chef::Cookbook::Metadata.from_file(File.join(path, 'metadata.rb'))
  dependencies =
    ridley.dependencies.map do |name, version|
      Dependency.new(name, version)
    end
  populate_fields(dependencies)
end

.cookbook_exists?(name) ⇒ Boolean

Checks if a particular cookbook exists in the community site

Parameters:

  • name (String)

    the name of the cookbook

Returns:

  • (Boolean)

    whether the cookbook exists in the community site or not



99
100
101
102
# File 'lib/chef/taste/dependency_checker.rb', line 99

def cookbook_exists?(name)
  rest = Berkshelf::CommunityREST.new
  rest.get(name).status == 200
end

.populate_fields(dependencies) ⇒ Array<Dependency>

Populate various fields for all dependencies

Parameters:

  • dependencies (Array<Dependency>)

    list of dependency objects

Returns:

  • (Array<Dependency>)

    list of dependency objects with updated fields



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/taste/dependency_checker.rb', line 58

def populate_fields(dependencies)
  rest = Berkshelf::CommunityREST.new
  dependencies.each do |dependency|
    # Skip cookbooks that are not available in the community site. It might be an external cookbook.
    next unless cookbook_exists?(dependency.name)

    dependency.latest = rest.latest_version(dependency.name)

    # Obtain the version used based on the version constraint
    dependency.version_used = rest.satisfy(dependency.name, dependency.requirement)
    dependency.source_url = rest.get(dependency.name).body['external_url']

    # Calculate the status and changelog based on the version being used and the latest version
    update_status(dependency)
  end
end

.update_status(dependency) ⇒ Object

Updates the status of the dependency based on the version used and the latest version available in the community site. It also obtains the changelog of the dependency is out-of-date

Parameters:

  • dependency (Dependency)

    the cookbook dependency



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/taste/dependency_checker.rb', line 80

def update_status(dependency)
  if dependency.version_used && dependency.latest
    used_version = Solve::Version.new(dependency.version_used)
    latest_version = Solve::Version.new(dependency.latest)
    if used_version.eql?(latest_version)
      dependency.status = 'up-to-date'
    else
      dependency.status = 'out-of-date'
      dependency.changelog = Changelog.compute(dependency)
    end
  end
end