Class: Inspec::Lockfile

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/dependencies/lockfile.rb

Constant Summary collapse

MINIMUM_SUPPORTED_VERSION =

When we finalize this feature, we should set these to 1

1
CURRENT_LOCKFILE_VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lockfile_content_hash) ⇒ Lockfile

Returns a new instance of Lockfile.



53
54
55
56
57
# File 'lib/inspec/dependencies/lockfile.rb', line 53

def initialize(lockfile_content_hash)
  version = lockfile_content_hash["lockfile_version"]
  @version = version.to_i
  parse_content_hash(lockfile_content_hash)
end

Instance Attribute Details

#depsObject (readonly)

Returns the value of attribute deps.



52
53
54
# File 'lib/inspec/dependencies/lockfile.rb', line 52

def deps
  @deps
end

#versionObject (readonly)

Returns the value of attribute version.



52
53
54
# File 'lib/inspec/dependencies/lockfile.rb', line 52

def version
  @version
end

Class Method Details

.from_content(content) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/inspec/dependencies/lockfile.rb', line 17

def self.from_content(content)
  parsed_content = YAML.load(content)
  version = parsed_content["lockfile_version"]
  raise "No lockfile_version set in #{path}!" if version.nil?

  validate_lockfile_version!(version.to_i)
  new(parsed_content)
end

.from_dependency_set(dep_set) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/inspec/dependencies/lockfile.rb', line 9

def self.from_dependency_set(dep_set)
  lockfile_content = {
    "lockfile_version" => CURRENT_LOCKFILE_VERSION,
    "depends" => dep_set.to_array,
  }
  new(lockfile_content)
end

.from_file(path) ⇒ Object



26
27
28
29
# File 'lib/inspec/dependencies/lockfile.rb', line 26

def self.from_file(path)
  content = File.read(path)
  from_content(content)
end

.validate_lockfile_version!(version) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/inspec/dependencies/lockfile.rb', line 31

def self.validate_lockfile_version!(version)
  if version < MINIMUM_SUPPORTED_VERSION
    raise <<~EOF
      This lockfile specifies a lockfile_version of #{version} which is
      lower than the minimum supported version #{MINIMUM_SUPPORTED_VERSION}.

      Please create a new lockfile for this project by running:

          inspec vendor
    EOF
  elsif version > CURRENT_LOCKFILE_VERSION
    raise <<~EOF
      This lockfile claims to be version #{version} which is greater than
      the most recent lockfile version(#{CURRENT_LOCKFILE_VERSION}).

      This may happen if you are using an older version of inspec than was
      used to create the lockfile.
    EOF
  end
end

Instance Method Details

#to_yamlObject



59
60
61
62
63
64
# File 'lib/inspec/dependencies/lockfile.rb', line 59

def to_yaml
  {
    "lockfile_version" => CURRENT_LOCKFILE_VERSION,
    "depends" => @deps.map { |i| stringify_keys(i) },
  }.to_yaml
end