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.



48
49
50
51
52
# File 'lib/inspec/dependencies/lockfile.rb', line 48

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.



47
48
49
# File 'lib/inspec/dependencies/lockfile.rb', line 47

def deps
  @deps
end

#versionObject (readonly)

Returns the value of attribute version.



47
48
49
# File 'lib/inspec/dependencies/lockfile.rb', line 47

def version
  @version
end

Class Method Details

.from_dependency_set(dep_set) ⇒ Object



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

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



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

def self.from_file(path)
  parsed_content = YAML.load(File.read(path))
  version = parsed_content['lockfile_version']
  fail "No lockfile_version set in #{path}!" if version.nil?
  validate_lockfile_version!(version.to_i)
  new(parsed_content)
end

.validate_lockfile_version!(version) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/inspec/dependencies/lockfile.rb', line 26

def self.validate_lockfile_version!(version)
  if version < MINIMUM_SUPPORTED_VERSION
    fail <<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
    fail <<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



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

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