22require 'berkshelf'
33require 'chef/cookbook/metadata'
44require_relative 'exceptions'
5+ require_relative '../rordan_gramsay'
56
67module RordanGramsay
78 module Dependencies
@@ -36,13 +37,67 @@ def berksfile_lock
3637 end
3738 rescue FileMissing
3839 $stdout. puts ( 'berks install' )
39- out = ` berks install`
40+ RordanGramsay . stream_command ( ' berks install' )
4041 retry if $CHILD_STATUS. success?
41- raise out
4242 end
4343 end
4444
4545 # :nodoc:
46+ module Helpers
47+ def initialize
48+ super
49+ @is_fail = false
50+ end
51+
52+ def failure?
53+ @is_fail
54+ end
55+
56+ private
57+
58+ def version_from_constraint ( constraint )
59+ constraint . strip . gsub ( /[^0-9.]+/ , '' )
60+ end
61+
62+ def corrected_constraint ( cookbook , constraint )
63+ # Correct constraints `~> 0.1.0` or `~> 4.9`
64+ return constraint if constraint =~ /^\s *~>\s *[1-9][0-9]*\. \d +\s *$/
65+ return constraint if constraint =~ /^\s *~>0\. \d +\. \d +\s *$/
66+
67+ version = version_from_constraint ( constraint ) . split ( '.' )
68+ major = version . shift || '0'
69+ minor = version . shift || '0'
70+ patch = version . shift || '0'
71+
72+ if major == '0' && minor == '0' && patch == '0'
73+ # Pull in the latest version from berksfile_lock and "correct"
74+ # that version as if it is a constraint. This is so we can apply
75+ # some sort of reasonable version constraint on `0.0.0` dependencies
76+ version = berksfile_lock . graph . find ( cookbook ) . version
77+
78+ corrected_constraint ( cookbook , "~> #{ version } " )
79+ elsif major == '0'
80+ "~> #{ major } .#{ minor } .#{ patch } "
81+ else
82+ "~> #{ major } .#{ minor } "
83+ end
84+ end
85+
86+ def report_change ( cookbook , previous , now )
87+ return if previous == now
88+ $stderr. puts ( Paint % [ ' %{cookbook} : %{previous} -> %{now}' , cookbook : Paint [ cookbook , :white , :bold ] , now : Paint [ now , :green ] , previous : Paint [ previous , :yellow ] ] )
89+ end
90+
91+ def log_error ( msg , opts = { } )
92+ @is_fail = true
93+ $stderr. puts ( msg )
94+ raise NextIteration if opts [ :next ]
95+ end
96+ end
97+
98+ # This is the main entry point if you don't know/care what cookbook
99+ # type whose dependencies you're managing. It should match all methods
100+ # on the public interface of both `WrapperPinning` or `CookbookPinning`
46101 class Pinning
47102 extend Forwardable
48103 include FileAccessor
@@ -55,7 +110,7 @@ def initialize
55110 end
56111 end
57112
58- def_delegators :@obj , :call , :check , :clean , :migrate , :dependencies , :failure?
113+ def_delegators :@obj , :call , :check , :clean , :migrate , :update , : dependencies, :failure?
59114 end
60115
61116 # Most cookbooks should use this, unless you desire a layout
@@ -65,16 +120,11 @@ def initialize
65120 # `WrapperPinning` class.
66121 class CookbookPinning
67122 include FileAccessor
68-
69- def initialize
70- @is_fail = false
71- end
72-
73- def failure?
74- @is_fail
75- end
123+ include Helpers
76124
77125 def call
126+ yield if block_given?
127+
78128 File . open ( 'metadata.new.rb' , 'w' ) do |fd |
79129 File . readlines ( metadata . source_file ) . each do |line |
80130 next if line =~ /\b depends\b /i
@@ -98,6 +148,8 @@ def call
98148 end
99149
100150 def clean
151+ yield if block_given?
152+
101153 File . open ( 'metadata.new.rb' , 'w' ) do |fd |
102154 File . readlines ( metadata . source_file ) . each do |line |
103155 next if line =~ /\b depends\b /i
@@ -110,6 +162,8 @@ def clean
110162 end
111163
112164 def check
165+ yield if block_given?
166+
113167 berksfile
114168 metadata . dependencies . each do |( cookbook , constraint ) |
115169 next if constraint =~ /^\s *~>\s *[1-9][0-9]*\. \d +\s *$/
@@ -150,11 +204,43 @@ def migrate
150204 @berksfile = nil
151205 File . unlink ( berksfile_lock . source_file )
152206 @berksfile_lock = nil
153-
154207 clean
208+
209+ yield if block_given?
155210 call
156211 end
157212
213+ def update
214+ deps = metadata . dependencies . dup
215+ clean
216+ if block_given?
217+ yield
218+ else
219+ RordanGramsay . stream_command ( 'berks update' )
220+ end
221+
222+ File . open ( 'metadata.new.rb' , 'w' ) do |fd |
223+ File . readlines ( metadata . source_file ) . each do |line |
224+ next if line =~ /\b depends\b /i
225+ fd . puts ( line )
226+ end
227+
228+ dependencies . each do |( cookbook , constraint ) |
229+ old = deps [ cookbook ]
230+ if old
231+ report_change ( cookbook , old , constraint )
232+ else
233+ report_change ( cookbook , '<missing>' , constraint )
234+ end
235+
236+ fd . puts ( %(depends '#{ cookbook } ', '#{ constraint } ') )
237+ end
238+ end
239+
240+ File . rename ( 'metadata.new.rb' , metadata . source_file )
241+ @metadata = nil # Because we just modified it, clear cache
242+ end
243+
158244 def dependencies
159245 @dependencies ||= begin
160246 berksfile . dependencies . each_with_object ( { } ) do |item , deps |
@@ -167,53 +253,13 @@ def dependencies
167253 end
168254 end
169255 end
170-
171- private
172-
173- def version_from_constraint ( constraint )
174- constraint . strip . gsub ( /[^0-9.]+/ , '' )
175- end
176-
177- def corrected_constraint ( cookbook , constraint )
178- # Correct constraints `~> 0.1.0` or `~> 4.9`
179- return constraint if constraint =~ /^\s *~>\s *[1-9][0-9]*\. \d +\s *$/
180- return constraint if constraint =~ /^\s *~>0\. \d +\. \d +\s *$/
181-
182- version = version_from_constraint ( constraint ) . split ( '.' )
183- major = version . shift || '0'
184- minor = version . shift || '0'
185- patch = version . shift || '0'
186-
187- if major == '0' && minor == '0' && patch == '0'
188- # Pull in the latest version from berksfile_lock and "correct"
189- # that version as if it is a constraint. This is so we can apply
190- # some sort of reasonable version constraint on `0.0.0` dependencies
191- version = berksfile_lock . graph . find ( cookbook ) . version
192-
193- corrected_constraint ( cookbook , "~> #{ version } " )
194- elsif major == '0'
195- "~> #{ major } .#{ minor } .#{ patch } "
196- else
197- "~> #{ major } .#{ minor } "
198- end
199- end
200-
201- def report_change ( cookbook , previous , now )
202- return if previous == now
203- $stderr. puts ( Paint % [ ' %{cookbook} : %{previous} -> %{now}' , cookbook : Paint [ cookbook , :white , :bold ] , now : Paint [ now , :green ] , previous : Paint [ previous , :yellow ] ] )
204- end
205-
206- def log_error ( msg , opts = { } )
207- @is_fail = true
208- $stderr. puts ( msg )
209- raise NextIteration if opts [ :next ]
210- end
211256 end
212257
213258 # Pinning specifics for role- and wrapper-cookbooks
214259 class WrapperPinning < CookbookPinning
215260 def check
216- @is_fail = false
261+ yield if block_given?
262+
217263 deps = metadata . dependencies . each_with_object ( { } ) do |( cookbook , constraint ) , dep |
218264 dep [ cookbook ] = constraint
219265 end
0 commit comments