berkshelf 2.0.18 → 3.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.ruby-version +1 -1
- data/.travis.yml +4 -1
- data/CHANGELOG.md +2 -26
- data/Gemfile +12 -2
- data/README.md +9 -1
- data/berkshelf.gemspec +9 -18
- data/bin/berks +3 -13
- data/features/apply_command.feature +11 -9
- data/features/berksfile.feature +8 -10
- data/features/config.feature +1 -2
- data/features/configure_command.feature +13 -14
- data/features/contingent_command.feature +13 -1
- data/features/cookbook_command.feature +2 -4
- data/features/groups_install.feature +10 -2
- data/features/help.feature +1 -1
- data/features/init_command.feature +5 -7
- data/features/install_command.feature +157 -228
- data/features/json_formatter.feature +27 -15
- data/features/licenses.feature +18 -12
- data/features/list_command.feature +6 -1
- data/features/lockfile.feature +116 -72
- data/features/outdated_command.feature +3 -47
- data/features/package_command.feature +10 -7
- data/features/shelf/show.feature +2 -2
- data/features/shelf/uninstall.feature +2 -2
- data/features/show_command.feature +10 -3
- data/features/step_definitions/chef/config_steps.rb +12 -0
- data/features/step_definitions/chef_server_steps.rb +16 -16
- data/features/step_definitions/cli_steps.rb +3 -79
- data/features/step_definitions/config_steps.rb +43 -0
- data/features/step_definitions/environment_steps.rb +7 -0
- data/features/step_definitions/filesystem_steps.rb +12 -57
- data/features/step_definitions/gem_steps.rb +1 -2
- data/features/step_definitions/json_steps.rb +3 -1
- data/features/step_definitions/lockfile_steps.rb +4 -0
- data/features/step_definitions/utility_steps.rb +0 -19
- data/features/support/aruba.rb +12 -0
- data/features/support/env.rb +52 -57
- data/features/update_command.feature +37 -23
- data/features/upload_command.feature +96 -160
- data/generator_files/Berksfile.erb +2 -1
- data/generator_files/Vagrantfile.erb +3 -0
- data/generator_files/default_test.rb.erb +1 -1
- data/generator_files/helpers.rb.erb +1 -1
- data/lib/berkshelf.rb +43 -24
- data/lib/berkshelf/api_client.rb +67 -0
- data/lib/berkshelf/api_client/remote_cookbook.rb +42 -0
- data/lib/berkshelf/berksfile.rb +232 -420
- data/lib/berkshelf/cached_cookbook.rb +22 -10
- data/lib/berkshelf/chef/config.rb +1 -0
- data/lib/berkshelf/cli.rb +66 -68
- data/lib/berkshelf/commands/shelf.rb +1 -1
- data/lib/berkshelf/community_rest.rb +10 -17
- data/lib/berkshelf/config.rb +23 -27
- data/lib/berkshelf/cookbook_generator.rb +3 -4
- data/lib/berkshelf/cookbook_store.rb +74 -17
- data/lib/berkshelf/core_ext/file.rb +2 -2
- data/lib/berkshelf/core_ext/pathname.rb +7 -5
- data/lib/berkshelf/{cookbook_source.rb → dependency.rb} +47 -67
- data/lib/berkshelf/downloader.rb +49 -106
- data/lib/berkshelf/errors.rb +64 -71
- data/lib/berkshelf/formatters.rb +11 -9
- data/lib/berkshelf/formatters/human_readable.rb +9 -9
- data/lib/berkshelf/formatters/json.rb +14 -4
- data/lib/berkshelf/init_generator.rb +3 -3
- data/lib/berkshelf/installer.rb +136 -0
- data/lib/berkshelf/location.rb +91 -131
- data/lib/berkshelf/locations/git_location.rb +9 -11
- data/lib/berkshelf/locations/github_location.rb +1 -1
- data/lib/berkshelf/locations/path_location.rb +10 -27
- data/lib/berkshelf/lockfile.rb +92 -70
- data/lib/berkshelf/logger.rb +4 -7
- data/lib/berkshelf/mixin/config.rb +21 -4
- data/lib/berkshelf/resolver.rb +60 -150
- data/lib/berkshelf/resolver/graph.rb +44 -0
- data/lib/berkshelf/source.rb +55 -0
- data/lib/berkshelf/source_uri.rb +38 -0
- data/lib/berkshelf/version.rb +1 -1
- data/spec/config/knife.rb +1 -1
- data/spec/fixtures/cassettes/Berkshelf_Resolver/_initialize/adds_the_dependencies_of_the_dependency_as_dependencies.yml +3694 -0
- data/spec/fixtures/cookbooks/example_cookbook/Berksfile.lock +1 -1
- data/spec/fixtures/lockfiles/default.lock +1 -1
- data/spec/spec_helper.rb +20 -121
- data/spec/support/chef_api.rb +3 -4
- data/spec/support/chef_server.rb +20 -11
- data/spec/support/git.rb +127 -0
- data/spec/support/kitchen.rb +12 -0
- data/spec/support/path_helpers.rb +69 -0
- data/spec/unit/berkshelf/api_client/remote_cookbook_spec.rb +23 -0
- data/spec/unit/berkshelf/api_client_spec.rb +57 -0
- data/spec/unit/berkshelf/berksfile_spec.rb +206 -324
- data/spec/unit/berkshelf/cached_cookbook_spec.rb +73 -38
- data/spec/unit/berkshelf/community_rest_spec.rb +30 -71
- data/spec/unit/berkshelf/config_spec.rb +3 -14
- data/spec/unit/berkshelf/cookbook_generator_spec.rb +1 -2
- data/spec/unit/berkshelf/cookbook_store_spec.rb +12 -7
- data/spec/unit/berkshelf/dependency_spec.rb +285 -0
- data/spec/unit/berkshelf/downloader_spec.rb +4 -183
- data/spec/unit/berkshelf/formatters/null_spec.rb +1 -1
- data/spec/unit/berkshelf/formatters_spec.rb +4 -2
- data/spec/unit/berkshelf/git_spec.rb +15 -15
- data/spec/unit/berkshelf/installer_spec.rb +39 -0
- data/spec/unit/berkshelf/location_spec.rb +87 -114
- data/spec/unit/berkshelf/locations/git_location_spec.rb +41 -53
- data/spec/unit/berkshelf/locations/path_location_spec.rb +13 -23
- data/spec/unit/berkshelf/lockfile_spec.rb +38 -40
- data/spec/unit/berkshelf/resolver/graph_spec.rb +44 -0
- data/spec/unit/berkshelf/resolver_spec.rb +34 -83
- data/spec/unit/berkshelf/source_spec.rb +23 -0
- data/spec/unit/berkshelf/source_uri_spec.rb +29 -0
- metadata +149 -188
- checksums.yaml +0 -7
- data/features/default_locations.feature +0 -127
- data/features/step_definitions/berksfile_steps.rb +0 -8
- data/features/step_definitions/configure_cli_steps.rb +0 -19
- data/features/vendor_install.feature +0 -19
- data/lib/berkshelf/core_ext/openuri.rb +0 -36
- data/lib/berkshelf/core_ext/rbzip2.rb +0 -8
- data/lib/berkshelf/locations/chef_api_location.rb +0 -228
- data/lib/berkshelf/locations/site_location.rb +0 -92
- data/lib/berkshelf/test.rb +0 -35
- data/spec/knife.rb.sample +0 -12
- data/spec/support/test_generators.rb +0 -27
- data/spec/unit/berkshelf/cli_spec.rb +0 -16
- data/spec/unit/berkshelf/cookbook_source_spec.rb +0 -358
- data/spec/unit/berkshelf/core_ext/pathname_spec.rb +0 -46
- data/spec/unit/berkshelf/locations/chef_api_location_spec.rb +0 -139
- data/spec/unit/berkshelf/locations/site_location_spec.rb +0 -19
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
module Berkshelf
|
|
2
|
-
class GitLocation
|
|
2
|
+
class GitLocation < Location::Base
|
|
3
3
|
class << self
|
|
4
4
|
# Create a temporary directory for the cloned repository within Berkshelf's
|
|
5
5
|
# temporary directory
|
|
@@ -11,8 +11,6 @@ module Berkshelf
|
|
|
11
11
|
end
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
-
include Location
|
|
15
|
-
|
|
16
14
|
set_location_key :git
|
|
17
15
|
set_valid_options :ref, :branch, :tag, :rel
|
|
18
16
|
|
|
@@ -24,7 +22,6 @@ module Berkshelf
|
|
|
24
22
|
|
|
25
23
|
alias_method :tag, :branch
|
|
26
24
|
|
|
27
|
-
# @param [#to_s] name
|
|
28
25
|
# @param [Solve::Constraint] version_constraint
|
|
29
26
|
# @param [Hash] options
|
|
30
27
|
#
|
|
@@ -38,9 +35,8 @@ module Berkshelf
|
|
|
38
35
|
# same as tag
|
|
39
36
|
# @option options [String] :rel
|
|
40
37
|
# the path within the repository to find the cookbook
|
|
41
|
-
def initialize(
|
|
42
|
-
|
|
43
|
-
@version_constraint = version_constraint
|
|
38
|
+
def initialize(dependency, options = {})
|
|
39
|
+
super
|
|
44
40
|
@uri = options[:git]
|
|
45
41
|
@branch = options[:branch] || options[:tag] || 'master'
|
|
46
42
|
@ref = options[:ref]
|
|
@@ -52,7 +48,9 @@ module Berkshelf
|
|
|
52
48
|
# @param [#to_s] destination
|
|
53
49
|
#
|
|
54
50
|
# @return [Berkshelf::CachedCookbook]
|
|
55
|
-
def
|
|
51
|
+
def do_download
|
|
52
|
+
destination = Berkshelf::CookbookStore.instance.storage_path
|
|
53
|
+
|
|
56
54
|
if cached?(destination)
|
|
57
55
|
@ref ||= Berkshelf::Git.rev_parse(revision_path(destination))
|
|
58
56
|
return local_revision(destination)
|
|
@@ -63,14 +61,14 @@ module Berkshelf
|
|
|
63
61
|
|
|
64
62
|
tmp_path = rel ? File.join(clone, rel) : clone
|
|
65
63
|
unless File.chef_cookbook?(tmp_path)
|
|
66
|
-
msg = "Cookbook '#{name}' not found at git: #{uri}"
|
|
64
|
+
msg = "Cookbook '#{dependency.name}' not found at git: #{uri}"
|
|
67
65
|
msg << " with branch '#{branch}'" if branch
|
|
68
66
|
msg << " with ref '#{ref}'" if ref
|
|
69
67
|
msg << " at path '#{rel}'" if rel
|
|
70
68
|
raise CookbookNotFound, msg
|
|
71
69
|
end
|
|
72
70
|
|
|
73
|
-
cb_path = File.join(destination, "#{name}-#{ref}")
|
|
71
|
+
cb_path = File.join(destination, "#{dependency.name}-#{ref}")
|
|
74
72
|
FileUtils.rm_rf(cb_path)
|
|
75
73
|
FileUtils.mv(tmp_path, cb_path)
|
|
76
74
|
|
|
@@ -123,7 +121,7 @@ module Berkshelf
|
|
|
123
121
|
|
|
124
122
|
def revision_path(destination)
|
|
125
123
|
return unless ref
|
|
126
|
-
File.join(destination, "#{name}-#{ref}")
|
|
124
|
+
File.join(destination, "#{dependency.name}-#{ref}")
|
|
127
125
|
end
|
|
128
126
|
end
|
|
129
127
|
end
|
|
@@ -17,7 +17,7 @@ module Berkshelf
|
|
|
17
17
|
# the GitHub repo identifier to clone
|
|
18
18
|
# @option options [#to_sym] :protocol
|
|
19
19
|
# the protocol with which to communicate with GitHub
|
|
20
|
-
def initialize(
|
|
20
|
+
def initialize(dependency, options = {})
|
|
21
21
|
@repo_identifier = options.delete(:github)
|
|
22
22
|
@protocol = (options.delete(:protocol) || DEFAULT_PROTOCOL).to_sym
|
|
23
23
|
options[:git] = github_url
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
module Berkshelf
|
|
2
|
-
class PathLocation
|
|
2
|
+
class PathLocation < Location::Base
|
|
3
3
|
class << self
|
|
4
4
|
# Expand and return a string representation of the given path if it is
|
|
5
5
|
# absolute or a path in the users home directory.
|
|
@@ -19,15 +19,12 @@ module Berkshelf
|
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
include Location
|
|
23
|
-
|
|
24
22
|
set_location_key :path
|
|
25
23
|
set_valid_options :path, :metadata
|
|
26
24
|
|
|
27
25
|
attr_accessor :path
|
|
28
|
-
attr_reader :name
|
|
29
26
|
|
|
30
|
-
# @param [#to_s]
|
|
27
|
+
# @param [#to_s] dependency
|
|
31
28
|
# @param [Solve::Constraint] version_constraint
|
|
32
29
|
# @param [Hash] options
|
|
33
30
|
#
|
|
@@ -35,19 +32,14 @@ module Berkshelf
|
|
|
35
32
|
# a filepath to the cookbook on your local disk
|
|
36
33
|
# @option options [Boolean] :metadata
|
|
37
34
|
# true if this is a metadata source
|
|
38
|
-
def initialize(
|
|
39
|
-
|
|
40
|
-
@
|
|
41
|
-
@
|
|
42
|
-
@metadata = options[:metadata]
|
|
35
|
+
def initialize(dependency, options = {})
|
|
36
|
+
super
|
|
37
|
+
@path = options[:path].to_s
|
|
38
|
+
@metadata = options[:metadata]
|
|
43
39
|
end
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
# @return [Berkshelf::CachedCookbook]
|
|
48
|
-
# the cached cookbook for this location
|
|
49
|
-
def cookbook
|
|
50
|
-
@cookbook ||= CachedCookbook.from_path(path, name: name)
|
|
41
|
+
def do_download
|
|
42
|
+
CachedCookbook.from_path(path, name: name)
|
|
51
43
|
end
|
|
52
44
|
|
|
53
45
|
# Returns true if the location is a metadata location. By default, no
|
|
@@ -80,23 +72,14 @@ module Berkshelf
|
|
|
80
72
|
super.merge(value: self.path)
|
|
81
73
|
end
|
|
82
74
|
|
|
83
|
-
# The string representation of this PathLocation
|
|
84
|
-
# is the default cookbook store, just leave it out, because
|
|
85
|
-
# it's probably just cached.
|
|
86
|
-
#
|
|
87
|
-
# @example
|
|
88
|
-
# loc.to_s #=> artifact (1.4.0)
|
|
75
|
+
# The string representation of this PathLocation
|
|
89
76
|
#
|
|
90
77
|
# @example
|
|
91
78
|
# loc.to_s #=> artifact (1.4.0) at path: '/Users/Seth/Dev/artifact'
|
|
92
79
|
#
|
|
93
80
|
# @return [String]
|
|
94
81
|
def to_s
|
|
95
|
-
|
|
96
|
-
"#{self.class.location_key}"
|
|
97
|
-
else
|
|
98
|
-
"#{self.class.location_key}: '#{path}'"
|
|
99
|
-
end
|
|
82
|
+
"#{self.class.location_key}: '#{File.expand_path(path)}'"
|
|
100
83
|
end
|
|
101
84
|
end
|
|
102
85
|
end
|
data/lib/berkshelf/lockfile.rb
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
require_relative 'dependency'
|
|
2
|
+
|
|
1
3
|
module Berkshelf
|
|
2
4
|
# The object representation of the Berkshelf lockfile. The lockfile is useful
|
|
3
5
|
# when working in teams where the same cookbook versions are desired across
|
|
4
6
|
# multiple workstations.
|
|
5
7
|
class Lockfile
|
|
6
|
-
require_relative 'cookbook_source'
|
|
7
|
-
|
|
8
8
|
# @return [Pathname]
|
|
9
9
|
# the path to this Lockfile
|
|
10
10
|
attr_reader :filepath
|
|
@@ -20,9 +20,9 @@ module Berkshelf
|
|
|
20
20
|
# @param berksfile [Berkshelf::Berksfile]
|
|
21
21
|
# the Berksfile associated with this Lockfile
|
|
22
22
|
def initialize(berksfile)
|
|
23
|
-
@berksfile
|
|
24
|
-
@filepath
|
|
25
|
-
@
|
|
23
|
+
@berksfile = berksfile
|
|
24
|
+
@filepath = File.expand_path("#{berksfile.filepath}.lock")
|
|
25
|
+
@dependencies = {}
|
|
26
26
|
|
|
27
27
|
load! if File.exists?(@filepath)
|
|
28
28
|
end
|
|
@@ -30,14 +30,14 @@ module Berkshelf
|
|
|
30
30
|
# Load the lockfile from file system.
|
|
31
31
|
def load!
|
|
32
32
|
contents = File.read(filepath).strip
|
|
33
|
-
hash
|
|
33
|
+
hash = parse(contents)
|
|
34
34
|
|
|
35
|
-
hash[:
|
|
35
|
+
hash[:dependencies].each do |name, options|
|
|
36
36
|
# Dynamically calculate paths relative to the Berksfile if a path is given
|
|
37
37
|
options[:path] &&= File.expand_path(options[:path], File.dirname(filepath))
|
|
38
38
|
|
|
39
39
|
begin
|
|
40
|
-
add(
|
|
40
|
+
add(Berkshelf::Dependency.new(berksfile, name.to_s, options))
|
|
41
41
|
rescue Berkshelf::CookbookNotFound
|
|
42
42
|
# It's possible that a source is locked that contains a path location, and
|
|
43
43
|
# that path location was renamed or no longer exists. When loading the
|
|
@@ -49,71 +49,70 @@ module Berkshelf
|
|
|
49
49
|
|
|
50
50
|
# The list of sources constrained in this lockfile.
|
|
51
51
|
#
|
|
52
|
-
# @return [Array<Berkshelf::
|
|
53
|
-
# the list of
|
|
54
|
-
def
|
|
55
|
-
@
|
|
52
|
+
# @return [Array<Berkshelf::Dependency>]
|
|
53
|
+
# the list of dependencies in this lockfile
|
|
54
|
+
def dependencies
|
|
55
|
+
@dependencies.values
|
|
56
56
|
end
|
|
57
57
|
|
|
58
|
-
# Find the given
|
|
58
|
+
# Find the given dependency in this lockfile. This method accepts a dependency
|
|
59
59
|
# attribute which may either be the name of a cookbook (String) or an
|
|
60
|
-
# actual cookbook
|
|
60
|
+
# actual cookbook dependency.
|
|
61
61
|
#
|
|
62
|
-
# @param [String, Berkshelf::
|
|
63
|
-
# the cookbook
|
|
64
|
-
# @return [
|
|
65
|
-
# the cookbook
|
|
66
|
-
def find(
|
|
67
|
-
@
|
|
62
|
+
# @param [String, Berkshelf::Dependency] dependency
|
|
63
|
+
# the cookbook dependency/name to find
|
|
64
|
+
# @return [Berkshelf::Dependency, nil]
|
|
65
|
+
# the cookbook dependency from this lockfile or nil if one was not found
|
|
66
|
+
def find(dependency)
|
|
67
|
+
@dependencies[cookbook_name(dependency).to_s]
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
-
# Determine if this lockfile contains the given
|
|
70
|
+
# Determine if this lockfile contains the given dependency.
|
|
71
71
|
#
|
|
72
|
-
# @param [String, Berkshelf::
|
|
73
|
-
# the cookbook
|
|
72
|
+
# @param [String, Berkshelf::Dependency] dependency
|
|
73
|
+
# the cookbook dependency/name to determine existence of
|
|
74
74
|
# @return [Boolean]
|
|
75
|
-
# true if the
|
|
76
|
-
def
|
|
77
|
-
!find(
|
|
75
|
+
# true if the dependency exists, false otherwise
|
|
76
|
+
def has_dependency?(dependency)
|
|
77
|
+
!find(dependency).nil?
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
# Replace the current list of
|
|
80
|
+
# Replace the current list of dependencies with `dependencies`. This method does
|
|
81
81
|
# not write out the lockfile - it only changes the state of the object.
|
|
82
82
|
#
|
|
83
|
-
# @param [Array<Berkshelf::
|
|
84
|
-
# the list of
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
sources.each { |source| append(source) }
|
|
83
|
+
# @param [Array<Berkshelf::Dependency>] dependencies
|
|
84
|
+
# the list of dependencies to update
|
|
85
|
+
def update(dependencies)
|
|
86
|
+
reset_dependencies!
|
|
87
|
+
|
|
88
|
+
dependencies.each { |dependency| append(dependency) }
|
|
90
89
|
save
|
|
91
90
|
end
|
|
92
91
|
|
|
93
|
-
# Add the given
|
|
92
|
+
# Add the given dependency to the `dependencies` list, if it doesn't already exist.
|
|
94
93
|
#
|
|
95
|
-
# @param [Berkshelf::
|
|
96
|
-
# the
|
|
97
|
-
def add(
|
|
98
|
-
@
|
|
94
|
+
# @param [Berkshelf::Dependency] dependency
|
|
95
|
+
# the dependency to append to the dependencies list
|
|
96
|
+
def add(dependency)
|
|
97
|
+
@dependencies[cookbook_name(dependency)] = dependency
|
|
99
98
|
end
|
|
100
99
|
alias_method :append, :add
|
|
101
100
|
|
|
102
|
-
# Remove the given
|
|
101
|
+
# Remove the given dependency from this lockfile. This method accepts a dependency
|
|
103
102
|
# attribute which may either be the name of a cookbook (String) or an
|
|
104
|
-
# actual cookbook
|
|
103
|
+
# actual cookbook dependency.
|
|
105
104
|
#
|
|
106
|
-
# @param [String, Berkshelf::
|
|
107
|
-
# the cookbook
|
|
105
|
+
# @param [String, Berkshelf::Dependency] dependency
|
|
106
|
+
# the cookbook dependency/name to remove
|
|
108
107
|
#
|
|
109
108
|
# @raise [Berkshelf::CookbookNotFound]
|
|
110
|
-
# if the provided
|
|
111
|
-
def remove(
|
|
112
|
-
unless
|
|
113
|
-
raise Berkshelf::CookbookNotFound, "'#{cookbook_name(
|
|
109
|
+
# if the provided dependency does not exist
|
|
110
|
+
def remove(dependency)
|
|
111
|
+
unless has_dependency?(dependency)
|
|
112
|
+
raise Berkshelf::CookbookNotFound, "'#{cookbook_name(dependency)}' does not exist in this lockfile!"
|
|
114
113
|
end
|
|
115
114
|
|
|
116
|
-
@
|
|
115
|
+
@dependencies.delete(cookbook_name(dependency))
|
|
117
116
|
end
|
|
118
117
|
alias_method :unlock, :remove
|
|
119
118
|
|
|
@@ -126,17 +125,17 @@ module Berkshelf
|
|
|
126
125
|
# @return [String]
|
|
127
126
|
# the detailed string representation of the lockfile
|
|
128
127
|
def inspect
|
|
129
|
-
"#<Berkshelf::Lockfile #{Pathname.new(filepath).basename},
|
|
128
|
+
"#<Berkshelf::Lockfile #{Pathname.new(filepath).basename}, dependencies: #{dependencies.inspect}>"
|
|
130
129
|
end
|
|
131
130
|
|
|
132
131
|
# Write the current lockfile to a hash
|
|
133
132
|
#
|
|
134
133
|
# @return [Hash]
|
|
135
134
|
# the hash representation of this lockfile
|
|
136
|
-
# * :
|
|
135
|
+
# * :dependencies [Array<Berkshelf::Dependency>] the list of dependencies
|
|
137
136
|
def to_hash
|
|
138
137
|
{
|
|
139
|
-
|
|
138
|
+
dependencies: @dependencies
|
|
140
139
|
}
|
|
141
140
|
end
|
|
142
141
|
|
|
@@ -159,12 +158,23 @@ module Berkshelf
|
|
|
159
158
|
# @return [Hash]
|
|
160
159
|
def parse(contents)
|
|
161
160
|
# Ruby's JSON.parse cannot handle an empty string/file
|
|
162
|
-
return {
|
|
161
|
+
return { dependencies: [] } if contents.strip.empty?
|
|
162
|
+
|
|
163
|
+
hash = JSON.parse(contents, symbolize_names: true)
|
|
163
164
|
|
|
164
|
-
|
|
165
|
+
# Legacy support for 2.0 lockfiles
|
|
166
|
+
# @todo Remove in 4.0
|
|
167
|
+
if hash[:sources]
|
|
168
|
+
LockfileLegacy.warn!
|
|
169
|
+
hash[:dependencies] = hash.delete(:sources)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
return hash
|
|
165
173
|
rescue Exception => e
|
|
174
|
+
# Legacy support for 1.0 lockfiles
|
|
175
|
+
# @todo Remove in 4.0
|
|
166
176
|
if e.class == JSON::ParserError && contents =~ /^cookbook ["'](.+)["']/
|
|
167
|
-
|
|
177
|
+
LockfileLegacy.warn!
|
|
168
178
|
return LockfileLegacy.parse(berksfile, contents)
|
|
169
179
|
else
|
|
170
180
|
raise Berkshelf::LockfileParserError.new(filepath, e)
|
|
@@ -178,29 +188,26 @@ module Berkshelf
|
|
|
178
188
|
end
|
|
179
189
|
end
|
|
180
190
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
@sources = {}
|
|
191
|
+
def reset_dependencies!
|
|
192
|
+
@dependencies = {}
|
|
184
193
|
end
|
|
185
194
|
|
|
186
195
|
# Return the name of this cookbook (because it's the key in our
|
|
187
196
|
# table).
|
|
188
197
|
#
|
|
189
|
-
# @param [Berkshelf::
|
|
190
|
-
# the
|
|
198
|
+
# @param [Berkshelf::Dependency, #to_s] dependency
|
|
199
|
+
# the dependency to find the name from
|
|
191
200
|
#
|
|
192
201
|
# @return [String]
|
|
193
202
|
# the name of the cookbook
|
|
194
|
-
def cookbook_name(
|
|
195
|
-
|
|
203
|
+
def cookbook_name(dependency)
|
|
204
|
+
dependency.is_a?(Berkshelf::Dependency) ? dependency.name : dependency.to_s
|
|
196
205
|
end
|
|
197
206
|
|
|
198
207
|
# Legacy support for old lockfiles
|
|
199
208
|
#
|
|
200
209
|
# @todo Remove this class in Berkshelf 3.0.0
|
|
201
210
|
class LockfileLegacy
|
|
202
|
-
require 'pathname'
|
|
203
|
-
|
|
204
211
|
class << self
|
|
205
212
|
# Read the old lockfile content and instance eval in context.
|
|
206
213
|
#
|
|
@@ -209,19 +216,34 @@ module Berkshelf
|
|
|
209
216
|
# @param [String] content
|
|
210
217
|
# the string content read from a legacy lockfile
|
|
211
218
|
def parse(berksfile, content)
|
|
212
|
-
|
|
219
|
+
dependencies = {}.tap do |hash|
|
|
213
220
|
content.split("\n").each do |line|
|
|
214
221
|
next if line.empty?
|
|
215
|
-
|
|
216
|
-
source = self.new(berksfile, line)
|
|
222
|
+
source = new(berksfile, line)
|
|
217
223
|
hash[source.name] = source.options
|
|
218
224
|
end
|
|
219
225
|
end
|
|
220
226
|
|
|
221
227
|
{
|
|
222
|
-
|
|
228
|
+
dependencies: dependencies,
|
|
223
229
|
}
|
|
224
230
|
end
|
|
231
|
+
|
|
232
|
+
# Warn the user they he/she is using an old Lockfile format.
|
|
233
|
+
#
|
|
234
|
+
# This automatically outputs to the {Berkshelf.ui}; nothing is
|
|
235
|
+
# returned.
|
|
236
|
+
#
|
|
237
|
+
# @return [nil]
|
|
238
|
+
def warn!
|
|
239
|
+
Berkshelf.ui.warn(warning_message)
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
private
|
|
243
|
+
# @return [String]
|
|
244
|
+
def warning_message
|
|
245
|
+
'You are using the old lockfile format. Attempting to convert...'
|
|
246
|
+
end
|
|
225
247
|
end
|
|
226
248
|
|
|
227
249
|
# @return [Hash]
|
|
@@ -245,19 +267,19 @@ module Berkshelf
|
|
|
245
267
|
instance_eval(content).to_hash
|
|
246
268
|
end
|
|
247
269
|
|
|
248
|
-
# Method defined in legacy lockfiles (since we are using
|
|
249
|
-
# instance_eval).
|
|
270
|
+
# Method defined in legacy lockfiles (since we are using instance_eval).
|
|
250
271
|
#
|
|
251
272
|
# @param [String] name
|
|
252
273
|
# the name of this cookbook
|
|
253
274
|
# @option options [String] :locked_version
|
|
254
275
|
# the locked version of this cookbook
|
|
255
276
|
def cookbook(name, options = {})
|
|
256
|
-
@name
|
|
277
|
+
@name = name
|
|
257
278
|
@options = manipulate(options)
|
|
258
279
|
end
|
|
259
280
|
|
|
260
281
|
private
|
|
282
|
+
|
|
261
283
|
# Perform various manipulations on the hash.
|
|
262
284
|
#
|
|
263
285
|
# @param [Hash] options
|