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

0
CURRENT_LOCKFILE_VERSION =
0

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.



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

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.



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

def deps
  @deps
end

#versionObject (readonly)

Returns the value of attribute version.



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

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
46
47
48
49
50
51
52
53
# 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 == 0
    # Remove this case once this feature stablizes
    $stderr.puts <<EOF
WARNING: This is a version 0 lockfile. Thank you for trying the
experimental dependency management feature. Please be aware you may
need to regenerate this lockfile in future versions as the feature is
currently in development.
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



62
63
64
65
66
67
# File 'lib/inspec/dependencies/lockfile.rb', line 62

def to_yaml
  {
    'lockfile_version' => CURRENT_LOCKFILE_VERSION,
    'depends' => @deps,
  }.to_yaml
end