Class: ChefDK::CookbookProfiler::Identifiers

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/cookbook_profiler/identifiers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookbook_path) ⇒ Identifiers

Returns a new instance of Identifiers.



28
29
30
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 28

def initialize(cookbook_path)
  @cookbook_path = cookbook_path
end

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



26
27
28
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 26

def cookbook_path
  @cookbook_path
end

Instance Method Details

#content_identifierObject



36
37
38
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 36

def content_identifier
  Digest::SHA1.new.hexdigest(fingerprint_text)
end

#cookbook_filesObject



61
62
63
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 61

def cookbook_files
  @files ||= cookbook_version.manifest_records_by_path
end

#cookbook_loaderObject



69
70
71
72
73
74
75
76
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 69

def cookbook_loader
  @cookbook_loader ||=
    begin
      loader = Chef::Cookbook::CookbookVersionLoader.new(cookbook_path)
      loader.load_cookbooks
      loader
    end
end

#cookbook_versionObject



65
66
67
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 65

def cookbook_version
  @cookbook_version ||= cookbook_loader.cookbook_version
end

#dotted_decimal_identifierObject



40
41
42
43
44
45
46
47
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 40

def dotted_decimal_identifier
  hex_id = content_identifier
  major = hex_id[0...14]
  minor = hex_id[14...28]
  patch = hex_id[28..40]
  decimal_integers =[major, minor, patch].map {|hex| hex.to_i(16) }
  decimal_integers.join(".")
end

#files_with_checksumsObject



55
56
57
58
59
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 55

def files_with_checksums
  cookbook_files.inject([]) do |files_with_checksums, (_name, file_info)|
    files_with_checksums << [file_info["path"], file_info["checksum"]]
  end
end

#fingerprint_textObject



49
50
51
52
53
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 49

def fingerprint_text
  files_with_checksums.sort {|a,b| a[0] <=> b[0]}.inject("") do |fingerprint, file_spec|
    fingerprint << "#{file_spec[0]}:#{file_spec[1]}\n"
  end
end

#semver_versionObject



32
33
34
# File 'lib/chef-dk/cookbook_profiler/identifiers.rb', line 32

def semver_version
  cookbook_version.version
end