Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forward-compatibility to remove naming limitations on Casks #5365

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion lib/cask/source/path_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,42 @@ def load
raise CaskError.new "File '#{path}' is not readable" unless path.readable?
raise CaskError.new "File '#{path}' is not a plain file" unless path.file?
begin
require path

# forward compatibility hack: convert first lines of the new form
#
# cask :v1 => 'google-chrome' do
#
# to the old form
#
# class GoogleChrome < Cask
#
# limitation: does not support Ruby extended quoting such as %Q{}
#
# in the future, this can be pared down to an "eval"

# read Cask
cask_string = File.open(path, 'rb') do |handle|
contents = handle.read
if defined?(Encoding)
contents.force_encoding('UTF-8')
else
contents
end
end

# munge text
cask_string.sub!(%r{\A(\s*\#[^\n]*\n)+}, '');
if %r{\A\s*cask\s+:v[\d_]+\s+=>\s+([\'\"])(\S+?)\1(?:\s*,\s*|\s+)do\s*\n}.match(cask_string)
cask_string.sub!(%r{\A[^\n]+\n}, "class #{cask_class_name} < Cask\n")
end

# simulate "require"
begin
Cask.const_get(cask_class_name)
rescue NameError
eval(cask_string, TOPLEVEL_BINDING)
end

rescue CaskError, StandardError, ScriptError => e
# bug: e.message.concat doesn't work with CaskError exceptions
e.message.concat(" while loading '#{path}'")
Expand Down