| 
 | 1 | +#  | 
 | 2 | +# Copyright 2012-2020, Chef Software Inc.  | 
 | 3 | +#  | 
 | 4 | +# Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | +# you may not use this file except in compliance with the License.  | 
 | 6 | +# You may obtain a copy of the License at  | 
 | 7 | +#  | 
 | 8 | +#     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | +#  | 
 | 10 | +# Unless required by applicable law or agreed to in writing, software  | 
 | 11 | +# distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | +# See the License for the specific language governing permissions and  | 
 | 14 | +# limitations under the License.  | 
 | 15 | +#  | 
 | 16 | + | 
 | 17 | +#  | 
 | 18 | +# Use this software definition to fix the shebang of knife binary  | 
 | 19 | +# to point to the embedded ruby.  | 
 | 20 | +#  | 
 | 21 | + | 
 | 22 | +name "knife-shebang-fix"  | 
 | 23 | + | 
 | 24 | +default_version "0.0.1"  | 
 | 25 | + | 
 | 26 | +license :project_license  | 
 | 27 | +skip_transitive_dependency_licensing true  | 
 | 28 | + | 
 | 29 | +build do  | 
 | 30 | +  if windows?  | 
 | 31 | +    # No action needed on Windows  | 
 | 32 | +  else  | 
 | 33 | +    block "Update knife shebang to point to embedded Ruby" do  | 
 | 34 | +      # Fix the shebang for knife binary specifically  | 
 | 35 | +      # #!/usr/bin/env ruby  | 
 | 36 | +      knife_file = "#{install_dir}/embedded/bin/knife"  | 
 | 37 | +      if File.exist?(knife_file)  | 
 | 38 | +        update_shebang = false  | 
 | 39 | +        rest_of_the_file = ""  | 
 | 40 | + | 
 | 41 | +        File.open(knife_file) do |f|  | 
 | 42 | +          shebang = f.readline  | 
 | 43 | +          if shebang.start_with?("#!") &&  | 
 | 44 | +              shebang.include?("ruby") &&  | 
 | 45 | +              !shebang.include?("#{install_dir}/embedded/bin/ruby")  | 
 | 46 | +            rest_of_the_file = f.read  | 
 | 47 | +            update_shebang = true  | 
 | 48 | +          end  | 
 | 49 | +        end  | 
 | 50 | + | 
 | 51 | +        if update_shebang  | 
 | 52 | +          File.open(knife_file, "w+") do |f|  | 
 | 53 | +            f.puts("#!#{install_dir}/embedded/bin/ruby")  | 
 | 54 | +            f.puts(rest_of_the_file)  | 
 | 55 | +          end  | 
 | 56 | +        end  | 
 | 57 | +      end  | 
 | 58 | +    end  | 
 | 59 | +  end  | 
 | 60 | +end  | 
0 commit comments