2424require 'chef/cache/checksum'
2525require 'chef/sandbox'
2626require 'chef/cookbook_version'
27+ require 'chef/cookbook/syntax_check'
2728require 'chef/cookbook/file_system_file_vendor'
2829
2930class Chef
@@ -50,6 +51,8 @@ def run
5051 else
5152 config [ :cookbook_path ] = Chef ::Config [ :cookbook_path ]
5253 end
54+ # Ugh, manipulating globals causes bugs.
55+ @user_cookbook_path = config [ :cookbook_path ]
5356
5457 Chef ::Cookbook ::FileVendor . on_create { |manifest | Chef ::Cookbook ::FileSystemFileVendor . new ( manifest ) }
5558
@@ -70,23 +73,12 @@ def run
7073 end
7174 end
7275
73- def test_ruby ( cookbook_dir )
74- Chef ::Log . info ( "Validating ruby files" )
75- Dir [ File . join ( cookbook_dir , '**' , '*.rb' ) ] . each do |ruby_file |
76- test_ruby_file ( cookbook_dir , ruby_file )
77- end
78- end
79-
80- def test_templates ( cookbook_dir )
81- Chef ::Log . info ( "Validating templates" )
82- Dir [ File . join ( cookbook_dir , '**' , '*.erb' ) ] . each do |erb_file |
83- test_template_file ( cookbook_dir , erb_file )
84- end
85- end
86-
8776 def upload_cookbook ( cookbook )
8877 Chef ::Log . info ( "Saving #{ cookbook . name } " )
8978
79+ # Validate the cookbook before staging it or else the syntax checker's
80+ # cache will not be helpful.
81+ validate_cookbook ( cookbook )
9082 # create build directory
9183 tmp_cookbook_dir = create_build_dir ( cookbook )
9284
@@ -173,10 +165,6 @@ def create_build_dir(cookbook)
173165 end
174166 end
175167
176- # Validate ruby files and templates
177- test_ruby ( tmp_cookbook_dir )
178- test_templates ( tmp_cookbook_dir )
179-
180168 # First, generate metadata
181169 Chef ::Log . debug ( "Generating metadata" )
182170 kcm = Chef ::Knife ::CookbookMetadata . new
@@ -201,30 +189,18 @@ def catch_auth_exceptions
201189 end
202190 end
203191
204- private
205-
206- def test_template_file ( cookbook_dir , erb_file )
207- Chef ::Log . debug ( "Testing template #{ erb_file } for syntax errors..." )
208- result = shell_out ( "sh -c 'erubis -x #{ erb_file } | ruby -c'" )
209- result . error!
210- rescue Chef ::Exceptions ::ShellCommandFailed
211- file_relative_path = erb_file [ /^#{ Regexp . escape ( cookbook_dir +File ::Separator ) } (.*)/ , 1 ]
212- Chef ::Log . fatal ( "Erb template #{ file_relative_path } has a syntax error:" )
213- result . stderr . each_line { |l | Chef ::Log . fatal ( l . chomp ) }
214- exit ( 1 )
192+ def validate_cookbook ( cookbook )
193+ pp :validate_cookbook => { :cookbook_name => cookbook . name }
194+ syntax_checker = Chef ::Cookbook ::SyntaxCheck . for_cookbook ( cookbook . name , @user_cookbook_path )
195+ pp syntax_checker
196+ Chef ::Log . info ( "Validating ruby files" )
197+ exit ( 1 ) unless syntax_checker . validate_ruby_files
198+ Chef ::Log . info ( "Validating templates" )
199+ exit ( 1 ) unless syntax_checker . validate_templates
200+ Chef ::Log . info ( "Syntax OK" )
201+ true
215202 end
216203
217- def test_ruby_file ( cookbook_dir , ruby_file )
218- Chef ::Log . debug ( "Testing #{ ruby_file } for syntax errors..." )
219- result = shell_out ( "ruby -c #{ ruby_file } " )
220- result . error!
221- rescue Chef ::Exceptions ::ShellCommandFailed
222- file_relative_path = ruby_file [ /^#{ Regexp . escape ( cookbook_dir +File ::Separator ) } (.*)/ , 1 ]
223- Chef ::Log . fatal ( "Cookbook file #{ file_relative_path } has a syntax error:" )
224- result . stderr . each_line { |l | Chef ::Log . fatal ( l . chomp ) }
225- exit ( 1 )
226- end
227-
228204 end
229205 end
230206end
0 commit comments