chef-utils 18.0.161 → 18.0.169
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +201 -201
- data/Rakefile +15 -15
- data/chef-utils.gemspec +50 -50
- data/lib/chef-utils/dist.rb +154 -154
- data/lib/chef-utils/dsl/architecture.rb +150 -150
- data/lib/chef-utils/dsl/cloud.rb +155 -155
- data/lib/chef-utils/dsl/default_paths.rb +60 -60
- data/lib/chef-utils/dsl/introspection.rb +134 -134
- data/lib/chef-utils/dsl/os.rb +58 -58
- data/lib/chef-utils/dsl/path_sanity.rb +39 -39
- data/lib/chef-utils/dsl/platform.rb +387 -387
- data/lib/chef-utils/dsl/platform_family.rb +360 -360
- data/lib/chef-utils/dsl/platform_version.rb +41 -41
- data/lib/chef-utils/dsl/service.rb +112 -112
- data/lib/chef-utils/dsl/train_helpers.rb +87 -87
- data/lib/chef-utils/dsl/virtualization.rb +272 -272
- data/lib/chef-utils/dsl/which.rb +123 -123
- data/lib/chef-utils/dsl/windows.rb +86 -86
- data/lib/chef-utils/internal.rb +114 -114
- data/lib/chef-utils/mash.rb +263 -263
- data/lib/chef-utils/parallel_map.rb +131 -131
- data/lib/chef-utils/version.rb +20 -20
- data/lib/chef-utils/version_string.rb +160 -160
- data/lib/chef-utils.rb +53 -53
- data/spec/spec_helper.rb +100 -100
- data/spec/unit/dsl/architecture_spec.rb +151 -151
- data/spec/unit/dsl/cloud_spec.rb +93 -93
- data/spec/unit/dsl/dsl_spec.rb +34 -34
- data/spec/unit/dsl/introspection_spec.rb +201 -201
- data/spec/unit/dsl/os_spec.rb +175 -175
- data/spec/unit/dsl/path_sanity_spec.rb +86 -86
- data/spec/unit/dsl/platform_family_spec.rb +235 -235
- data/spec/unit/dsl/platform_spec.rb +252 -252
- data/spec/unit/dsl/service_spec.rb +117 -117
- data/spec/unit/dsl/virtualization_spec.rb +75 -75
- data/spec/unit/dsl/which_spec.rb +171 -171
- data/spec/unit/dsl/windows_spec.rb +84 -84
- data/spec/unit/mash_spec.rb +51 -51
- data/spec/unit/parallel_map_spec.rb +156 -156
- metadata +3 -3
@@ -1,131 +1,131 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
#
|
3
|
-
# Copyright:: Copyright (c) Chef Software Inc.
|
4
|
-
# License:: Apache License, Version 2.0
|
5
|
-
#
|
6
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
-
# you may not use this file except in compliance with the License.
|
8
|
-
# You may obtain a copy of the License at
|
9
|
-
#
|
10
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
-
#
|
12
|
-
# Unless required by applicable law or agreed to in writing, software
|
13
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
-
# See the License for the specific language governing permissions and
|
16
|
-
# limitations under the License.
|
17
|
-
#
|
18
|
-
|
19
|
-
require "concurrent/executors"
|
20
|
-
require "concurrent/future"
|
21
|
-
require "singleton" unless defined?(Singleton)
|
22
|
-
|
23
|
-
module ChefUtils
|
24
|
-
#
|
25
|
-
# This module contains ruby refinements that adds several methods to the Enumerable
|
26
|
-
# class which are useful for parallel processing.
|
27
|
-
#
|
28
|
-
module ParallelMap
|
29
|
-
refine Enumerable do
|
30
|
-
|
31
|
-
# Enumerates through the collection in parallel using the thread pool provided
|
32
|
-
# or the default thread pool. By using the default thread pool this supports
|
33
|
-
# recursively calling the method without deadlocking while using a globally
|
34
|
-
# fixed number of workers. This method supports lazy collections. It returns
|
35
|
-
# synchronously, waiting until all the work is done. Failures are only reported
|
36
|
-
# after the collection has executed and only the first exception is raised.
|
37
|
-
#
|
38
|
-
# (0..).lazy.parallel_map { |i| i*i }.first(5)
|
39
|
-
#
|
40
|
-
# @return [Array] output results
|
41
|
-
#
|
42
|
-
def parallel_map(pool: nil)
|
43
|
-
return self unless block_given?
|
44
|
-
|
45
|
-
pool ||= ChefUtils::DefaultThreadPool.instance.pool
|
46
|
-
|
47
|
-
futures = map do |item|
|
48
|
-
Concurrent::Future.execute(executor: pool) do
|
49
|
-
yield item
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
futures.map(&:value!)
|
54
|
-
end
|
55
|
-
|
56
|
-
# This has the same behavior as parallel_map but returns the enumerator instead of
|
57
|
-
# the return values.
|
58
|
-
#
|
59
|
-
# @return [Enumerable] the enumerable for method chaining
|
60
|
-
#
|
61
|
-
def parallel_each(pool: nil, &block)
|
62
|
-
return self unless block_given?
|
63
|
-
|
64
|
-
parallel_map(pool: pool, &block)
|
65
|
-
|
66
|
-
self
|
67
|
-
end
|
68
|
-
|
69
|
-
# The flat_each method is tightly coupled to the usage of parallel_map within the
|
70
|
-
# ChefFS implementation. It is not itself a parallel method, but it is used to
|
71
|
-
# iterate through the 2nd level of nested structure, which is tied to the nested
|
72
|
-
# structures that ChefFS returns.
|
73
|
-
#
|
74
|
-
# This is different from Enumerable#flat_map because that behaves like map.flatten(1) while
|
75
|
-
# this behaves more like flatten(1).each. We need this on an Enumerable, so we have no
|
76
|
-
# Enumerable#flatten method to call.
|
77
|
-
#
|
78
|
-
# [ [ 1, 2 ], [ 3, 4 ] ].flat_each(&block) calls block four times with 1, 2, 3, 4
|
79
|
-
#
|
80
|
-
# [ [ 1, 2 ], [ 3, 4 ] ].flat_map(&block) calls block twice with [1, 2] and [3,4]
|
81
|
-
#
|
82
|
-
def flat_each(&block)
|
83
|
-
map do |value|
|
84
|
-
if value.is_a?(Enumerable)
|
85
|
-
value.each(&block)
|
86
|
-
else
|
87
|
-
yield value
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# The DefaultThreadPool has a fixed thread size and has no
|
95
|
-
# queue of work and the behavior on failure to find a thread is for the
|
96
|
-
# caller to run the work. This contract means that the thread pool can
|
97
|
-
# be called recursively without deadlocking and while keeping the fixed
|
98
|
-
# number of threads (and not exponentially growing the thread pool with
|
99
|
-
# the depth of recursion).
|
100
|
-
#
|
101
|
-
class DefaultThreadPool
|
102
|
-
include Singleton
|
103
|
-
|
104
|
-
DEFAULT_THREAD_SIZE = 10
|
105
|
-
|
106
|
-
# Size of the thread pool, must be set before getting the thread pool or
|
107
|
-
# calling parallel_map/parallel_each. Does not (but could be modified to)
|
108
|
-
# support dynamic resizing. To get fully synchronous behavior set this equal to
|
109
|
-
# zero rather than one since the caller will get work if the threads are
|
110
|
-
# busy.
|
111
|
-
#
|
112
|
-
# @return [Integer] number of threads
|
113
|
-
attr_accessor :threads
|
114
|
-
|
115
|
-
# Memoizing accessor for the thread pool
|
116
|
-
#
|
117
|
-
# @return [Concurrent::ThreadPoolExecutor] the thread pool
|
118
|
-
def pool
|
119
|
-
@pool ||= Concurrent::ThreadPoolExecutor.new(
|
120
|
-
min_threads: threads || DEFAULT_THREAD_SIZE,
|
121
|
-
max_threads: threads || DEFAULT_THREAD_SIZE,
|
122
|
-
max_queue: 0,
|
123
|
-
# "synchronous" redefines the 0 in max_queue to mean 'no queue' instead of 'infinite queue'
|
124
|
-
# it does not mean synchronous execution (no threads) but synchronous offload to the threads.
|
125
|
-
synchronous: true,
|
126
|
-
# this prevents deadlocks on recursive parallel usage
|
127
|
-
fallback_policy: :caller_runs
|
128
|
-
)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# Copyright:: Copyright (c) Chef Software Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require "concurrent/executors"
|
20
|
+
require "concurrent/future"
|
21
|
+
require "singleton" unless defined?(Singleton)
|
22
|
+
|
23
|
+
module ChefUtils
|
24
|
+
#
|
25
|
+
# This module contains ruby refinements that adds several methods to the Enumerable
|
26
|
+
# class which are useful for parallel processing.
|
27
|
+
#
|
28
|
+
module ParallelMap
|
29
|
+
refine Enumerable do
|
30
|
+
|
31
|
+
# Enumerates through the collection in parallel using the thread pool provided
|
32
|
+
# or the default thread pool. By using the default thread pool this supports
|
33
|
+
# recursively calling the method without deadlocking while using a globally
|
34
|
+
# fixed number of workers. This method supports lazy collections. It returns
|
35
|
+
# synchronously, waiting until all the work is done. Failures are only reported
|
36
|
+
# after the collection has executed and only the first exception is raised.
|
37
|
+
#
|
38
|
+
# (0..).lazy.parallel_map { |i| i*i }.first(5)
|
39
|
+
#
|
40
|
+
# @return [Array] output results
|
41
|
+
#
|
42
|
+
def parallel_map(pool: nil)
|
43
|
+
return self unless block_given?
|
44
|
+
|
45
|
+
pool ||= ChefUtils::DefaultThreadPool.instance.pool
|
46
|
+
|
47
|
+
futures = map do |item|
|
48
|
+
Concurrent::Future.execute(executor: pool) do
|
49
|
+
yield item
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
futures.map(&:value!)
|
54
|
+
end
|
55
|
+
|
56
|
+
# This has the same behavior as parallel_map but returns the enumerator instead of
|
57
|
+
# the return values.
|
58
|
+
#
|
59
|
+
# @return [Enumerable] the enumerable for method chaining
|
60
|
+
#
|
61
|
+
def parallel_each(pool: nil, &block)
|
62
|
+
return self unless block_given?
|
63
|
+
|
64
|
+
parallel_map(pool: pool, &block)
|
65
|
+
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
# The flat_each method is tightly coupled to the usage of parallel_map within the
|
70
|
+
# ChefFS implementation. It is not itself a parallel method, but it is used to
|
71
|
+
# iterate through the 2nd level of nested structure, which is tied to the nested
|
72
|
+
# structures that ChefFS returns.
|
73
|
+
#
|
74
|
+
# This is different from Enumerable#flat_map because that behaves like map.flatten(1) while
|
75
|
+
# this behaves more like flatten(1).each. We need this on an Enumerable, so we have no
|
76
|
+
# Enumerable#flatten method to call.
|
77
|
+
#
|
78
|
+
# [ [ 1, 2 ], [ 3, 4 ] ].flat_each(&block) calls block four times with 1, 2, 3, 4
|
79
|
+
#
|
80
|
+
# [ [ 1, 2 ], [ 3, 4 ] ].flat_map(&block) calls block twice with [1, 2] and [3,4]
|
81
|
+
#
|
82
|
+
def flat_each(&block)
|
83
|
+
map do |value|
|
84
|
+
if value.is_a?(Enumerable)
|
85
|
+
value.each(&block)
|
86
|
+
else
|
87
|
+
yield value
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# The DefaultThreadPool has a fixed thread size and has no
|
95
|
+
# queue of work and the behavior on failure to find a thread is for the
|
96
|
+
# caller to run the work. This contract means that the thread pool can
|
97
|
+
# be called recursively without deadlocking and while keeping the fixed
|
98
|
+
# number of threads (and not exponentially growing the thread pool with
|
99
|
+
# the depth of recursion).
|
100
|
+
#
|
101
|
+
class DefaultThreadPool
|
102
|
+
include Singleton
|
103
|
+
|
104
|
+
DEFAULT_THREAD_SIZE = 10
|
105
|
+
|
106
|
+
# Size of the thread pool, must be set before getting the thread pool or
|
107
|
+
# calling parallel_map/parallel_each. Does not (but could be modified to)
|
108
|
+
# support dynamic resizing. To get fully synchronous behavior set this equal to
|
109
|
+
# zero rather than one since the caller will get work if the threads are
|
110
|
+
# busy.
|
111
|
+
#
|
112
|
+
# @return [Integer] number of threads
|
113
|
+
attr_accessor :threads
|
114
|
+
|
115
|
+
# Memoizing accessor for the thread pool
|
116
|
+
#
|
117
|
+
# @return [Concurrent::ThreadPoolExecutor] the thread pool
|
118
|
+
def pool
|
119
|
+
@pool ||= Concurrent::ThreadPoolExecutor.new(
|
120
|
+
min_threads: threads || DEFAULT_THREAD_SIZE,
|
121
|
+
max_threads: threads || DEFAULT_THREAD_SIZE,
|
122
|
+
max_queue: 0,
|
123
|
+
# "synchronous" redefines the 0 in max_queue to mean 'no queue' instead of 'infinite queue'
|
124
|
+
# it does not mean synchronous execution (no threads) but synchronous offload to the threads.
|
125
|
+
synchronous: true,
|
126
|
+
# this prevents deadlocks on recursive parallel usage
|
127
|
+
fallback_policy: :caller_runs
|
128
|
+
)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
data/lib/chef-utils/version.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
# Copyright:: Copyright (c) Chef Software Inc.
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
module ChefUtils
|
18
|
-
CHEFUTILS_ROOT = File.expand_path("..", __dir__)
|
19
|
-
VERSION = "18.0.
|
20
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# Copyright:: Copyright (c) Chef Software Inc.
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module ChefUtils
|
18
|
+
CHEFUTILS_ROOT = File.expand_path("..", __dir__)
|
19
|
+
VERSION = "18.0.169"
|
20
|
+
end
|
@@ -1,160 +1,160 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
# Copyright:: Copyright 2017, Noah Kantrowitz
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
module ChefUtils
|
18
|
-
# String-like object for version strings.
|
19
|
-
#
|
20
|
-
# @since 13.2
|
21
|
-
# @api internal
|
22
|
-
class VersionString < String
|
23
|
-
# Parsed version object for the string.
|
24
|
-
# @return [Gem::Version]
|
25
|
-
attr_reader :parsed_version
|
26
|
-
|
27
|
-
# Create a new VersionString from an input String.
|
28
|
-
#
|
29
|
-
# @param val [String] Version string to parse.
|
30
|
-
def initialize(val)
|
31
|
-
val ||= ""
|
32
|
-
super(val)
|
33
|
-
begin
|
34
|
-
@parsed_version = ::Gem::Version.create(self)
|
35
|
-
rescue ArgumentError
|
36
|
-
@parsed_version = nil
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# @!group Compat wrappers for String
|
41
|
-
|
42
|
-
# Compat wrapper for + to behave like a normal String.
|
43
|
-
#
|
44
|
-
# @param other [String]
|
45
|
-
# @return [String]
|
46
|
-
def +(other)
|
47
|
-
to_s + other
|
48
|
-
end
|
49
|
-
|
50
|
-
# Compat wrapper for * to behave like a normal String.
|
51
|
-
#
|
52
|
-
# @param other [Integer]
|
53
|
-
# @return [String]
|
54
|
-
def *(other)
|
55
|
-
to_s * other
|
56
|
-
end
|
57
|
-
|
58
|
-
# @!group Comparison operators
|
59
|
-
|
60
|
-
# Compare a VersionString to an object. If compared to another VersionString
|
61
|
-
# then sort like `Gem::Version`, otherwise try to treat the other object as
|
62
|
-
# a version but fall back to normal string comparison.
|
63
|
-
#
|
64
|
-
# @param other [Object]
|
65
|
-
# @return [Integer]
|
66
|
-
def <=>(other)
|
67
|
-
other_ver = case other
|
68
|
-
when VersionString
|
69
|
-
other.parsed_version
|
70
|
-
else
|
71
|
-
begin
|
72
|
-
Gem::Version.create(other.to_s)
|
73
|
-
rescue ArgumentError
|
74
|
-
# Comparing to a string that isn't a version.
|
75
|
-
return super
|
76
|
-
end
|
77
|
-
end
|
78
|
-
parsed_version <=> other_ver
|
79
|
-
end
|
80
|
-
|
81
|
-
# Compat wrapper for == based on <=>.
|
82
|
-
#
|
83
|
-
# @param other [Object]
|
84
|
-
# @return [Boolean]
|
85
|
-
def ==(other)
|
86
|
-
(self <=> other) == 0
|
87
|
-
end
|
88
|
-
|
89
|
-
# Compat wrapper for != based on <=>.
|
90
|
-
#
|
91
|
-
# @param other [Object]
|
92
|
-
# @return [Boolean]
|
93
|
-
def !=(other)
|
94
|
-
(self <=> other) != 0
|
95
|
-
end
|
96
|
-
|
97
|
-
# Compat wrapper for < based on <=>.
|
98
|
-
#
|
99
|
-
# @param other [Object]
|
100
|
-
# @return [Boolean]
|
101
|
-
def <(other)
|
102
|
-
(self <=> other) < 0
|
103
|
-
end
|
104
|
-
|
105
|
-
# Compat wrapper for <= based on <=>.
|
106
|
-
#
|
107
|
-
# @param other [Object]
|
108
|
-
# @return [Boolean]
|
109
|
-
def <=(other)
|
110
|
-
(self <=> other) < 1
|
111
|
-
end
|
112
|
-
|
113
|
-
# Compat wrapper for > based on <=>.
|
114
|
-
#
|
115
|
-
# @param other [Object]
|
116
|
-
# @return [Boolean]
|
117
|
-
def >(other)
|
118
|
-
(self <=> other) > 0
|
119
|
-
end
|
120
|
-
|
121
|
-
# Compat wrapper for >= based on <=>.
|
122
|
-
#
|
123
|
-
# @param other [Object]
|
124
|
-
# @return [Boolean]
|
125
|
-
def >=(other)
|
126
|
-
(self <=> other) > -1
|
127
|
-
end
|
128
|
-
|
129
|
-
# @!group Matching operators
|
130
|
-
|
131
|
-
# Matching operator to support checking against a requirement string.
|
132
|
-
#
|
133
|
-
# @param other [Regexp, String]
|
134
|
-
# @return [Boolean]
|
135
|
-
# @example Match against a Regexp
|
136
|
-
# ChefUtils::VersionString.new('1.0.0') =~ /^1/
|
137
|
-
# @example Match against a requirement
|
138
|
-
# ChefUtils::VersionString.new('1.0.0') =~ '~> 1.0'
|
139
|
-
def =~(other)
|
140
|
-
case other
|
141
|
-
when Regexp
|
142
|
-
super
|
143
|
-
else
|
144
|
-
begin
|
145
|
-
Gem::Requirement.create(other) =~ parsed_version
|
146
|
-
rescue ArgumentError
|
147
|
-
# one side of the comparison wasn't parsable
|
148
|
-
super
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
# Back-compat API for chef-sugar. The other APIs are preferable.
|
154
|
-
#
|
155
|
-
# @api private
|
156
|
-
def satisfies?(*constraints)
|
157
|
-
Gem::Requirement.new(*constraints).satisfied_by?(@parsed_version)
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# Copyright:: Copyright 2017, Noah Kantrowitz
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module ChefUtils
|
18
|
+
# String-like object for version strings.
|
19
|
+
#
|
20
|
+
# @since 13.2
|
21
|
+
# @api internal
|
22
|
+
class VersionString < String
|
23
|
+
# Parsed version object for the string.
|
24
|
+
# @return [Gem::Version]
|
25
|
+
attr_reader :parsed_version
|
26
|
+
|
27
|
+
# Create a new VersionString from an input String.
|
28
|
+
#
|
29
|
+
# @param val [String] Version string to parse.
|
30
|
+
def initialize(val)
|
31
|
+
val ||= ""
|
32
|
+
super(val)
|
33
|
+
begin
|
34
|
+
@parsed_version = ::Gem::Version.create(self)
|
35
|
+
rescue ArgumentError
|
36
|
+
@parsed_version = nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# @!group Compat wrappers for String
|
41
|
+
|
42
|
+
# Compat wrapper for + to behave like a normal String.
|
43
|
+
#
|
44
|
+
# @param other [String]
|
45
|
+
# @return [String]
|
46
|
+
def +(other)
|
47
|
+
to_s + other
|
48
|
+
end
|
49
|
+
|
50
|
+
# Compat wrapper for * to behave like a normal String.
|
51
|
+
#
|
52
|
+
# @param other [Integer]
|
53
|
+
# @return [String]
|
54
|
+
def *(other)
|
55
|
+
to_s * other
|
56
|
+
end
|
57
|
+
|
58
|
+
# @!group Comparison operators
|
59
|
+
|
60
|
+
# Compare a VersionString to an object. If compared to another VersionString
|
61
|
+
# then sort like `Gem::Version`, otherwise try to treat the other object as
|
62
|
+
# a version but fall back to normal string comparison.
|
63
|
+
#
|
64
|
+
# @param other [Object]
|
65
|
+
# @return [Integer]
|
66
|
+
def <=>(other)
|
67
|
+
other_ver = case other
|
68
|
+
when VersionString
|
69
|
+
other.parsed_version
|
70
|
+
else
|
71
|
+
begin
|
72
|
+
Gem::Version.create(other.to_s)
|
73
|
+
rescue ArgumentError
|
74
|
+
# Comparing to a string that isn't a version.
|
75
|
+
return super
|
76
|
+
end
|
77
|
+
end
|
78
|
+
parsed_version <=> other_ver
|
79
|
+
end
|
80
|
+
|
81
|
+
# Compat wrapper for == based on <=>.
|
82
|
+
#
|
83
|
+
# @param other [Object]
|
84
|
+
# @return [Boolean]
|
85
|
+
def ==(other)
|
86
|
+
(self <=> other) == 0
|
87
|
+
end
|
88
|
+
|
89
|
+
# Compat wrapper for != based on <=>.
|
90
|
+
#
|
91
|
+
# @param other [Object]
|
92
|
+
# @return [Boolean]
|
93
|
+
def !=(other)
|
94
|
+
(self <=> other) != 0
|
95
|
+
end
|
96
|
+
|
97
|
+
# Compat wrapper for < based on <=>.
|
98
|
+
#
|
99
|
+
# @param other [Object]
|
100
|
+
# @return [Boolean]
|
101
|
+
def <(other)
|
102
|
+
(self <=> other) < 0
|
103
|
+
end
|
104
|
+
|
105
|
+
# Compat wrapper for <= based on <=>.
|
106
|
+
#
|
107
|
+
# @param other [Object]
|
108
|
+
# @return [Boolean]
|
109
|
+
def <=(other)
|
110
|
+
(self <=> other) < 1
|
111
|
+
end
|
112
|
+
|
113
|
+
# Compat wrapper for > based on <=>.
|
114
|
+
#
|
115
|
+
# @param other [Object]
|
116
|
+
# @return [Boolean]
|
117
|
+
def >(other)
|
118
|
+
(self <=> other) > 0
|
119
|
+
end
|
120
|
+
|
121
|
+
# Compat wrapper for >= based on <=>.
|
122
|
+
#
|
123
|
+
# @param other [Object]
|
124
|
+
# @return [Boolean]
|
125
|
+
def >=(other)
|
126
|
+
(self <=> other) > -1
|
127
|
+
end
|
128
|
+
|
129
|
+
# @!group Matching operators
|
130
|
+
|
131
|
+
# Matching operator to support checking against a requirement string.
|
132
|
+
#
|
133
|
+
# @param other [Regexp, String]
|
134
|
+
# @return [Boolean]
|
135
|
+
# @example Match against a Regexp
|
136
|
+
# ChefUtils::VersionString.new('1.0.0') =~ /^1/
|
137
|
+
# @example Match against a requirement
|
138
|
+
# ChefUtils::VersionString.new('1.0.0') =~ '~> 1.0'
|
139
|
+
def =~(other)
|
140
|
+
case other
|
141
|
+
when Regexp
|
142
|
+
super
|
143
|
+
else
|
144
|
+
begin
|
145
|
+
Gem::Requirement.create(other) =~ parsed_version
|
146
|
+
rescue ArgumentError
|
147
|
+
# one side of the comparison wasn't parsable
|
148
|
+
super
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Back-compat API for chef-sugar. The other APIs are preferable.
|
154
|
+
#
|
155
|
+
# @api private
|
156
|
+
def satisfies?(*constraints)
|
157
|
+
Gem::Requirement.new(*constraints).satisfied_by?(@parsed_version)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|