language: raise ShebangDetectionError rather than a generic error

This commit is contained in:
Bo Anderson 2021-04-29 17:48:09 +01:00
parent 1e567161d1
commit 728bb547a7
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
3 changed files with 12 additions and 3 deletions

View File

@ -729,3 +729,10 @@ class MacOSVersionError < RuntimeError
super "unknown or unsupported macOS version: #{version.inspect}" super "unknown or unsupported macOS version: #{version.inspect}"
end end
end end
# Raised when `detected_perl_shebang` etc cannot detect the shebang.
class ShebangDetectionError < RuntimeError
def initialize(type, reason)
super "Cannot detect #{type} shebang: #{reason}."
end
end

View File

@ -16,7 +16,7 @@ module Language
elsif formula.deps.map(&:name).include? "perl" elsif formula.deps.map(&:name).include? "perl"
Formula["perl"].opt_bin/"perl" Formula["perl"].opt_bin/"perl"
else else
raise "Cannot detect Perl shebang: formula does not depend on Perl." raise ShebangDetectionError.new("Perl", "formula does not depend on Perl")
end end
Utils::Shebang::RewriteInfo.new( Utils::Shebang::RewriteInfo.new(

View File

@ -105,8 +105,10 @@ module Language
def detected_python_shebang(formula = self) def detected_python_shebang(formula = self)
python_deps = formula.deps.map(&:name).grep(/^python(@.*)?$/) python_deps = formula.deps.map(&:name).grep(/^python(@.*)?$/)
raise "Cannot detect Python shebang: formula does not depend on Python." if python_deps.empty? raise ShebangDetectionError.new("Python", "formula does not depend on Python") if python_deps.empty?
raise "Cannot detect Python shebang: formula has multiple Python dependencies." if python_deps.length > 1 if python_deps.length > 1
raise ShebangDetectionError.new("Python", "formula has multiple Python dependencies")
end
python_shebang_rewrite_info(Formula[python_deps.first].opt_bin/"python3") python_shebang_rewrite_info(Formula[python_deps.first].opt_bin/"python3")
end end