Skip to content

Commit 3d9ea33

Browse files
committed
Merge pull request #5365 from rolandwalker/remove_caskname_limitations
forward-compatibility to remove naming limitations on Casks
2 parents 583890a + 3e2eafb commit 3d9ea33

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

lib/cask/source/path_base.rb

+36-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,42 @@ def load
1919
raise CaskError.new "File '#{path}' is not readable" unless path.readable?
2020
raise CaskError.new "File '#{path}' is not a plain file" unless path.file?
2121
begin
22-
require path
22+
23+
# forward compatibility hack: convert first lines of the new form
24+
#
25+
# cask :v1 => 'google-chrome' do
26+
#
27+
# to the old form
28+
#
29+
# class GoogleChrome < Cask
30+
#
31+
# limitation: does not support Ruby extended quoting such as %Q{}
32+
#
33+
# in the future, this can be pared down to an "eval"
34+
35+
# read Cask
36+
cask_string = File.open(path, 'rb') do |handle|
37+
contents = handle.read
38+
if defined?(Encoding)
39+
contents.force_encoding('UTF-8')
40+
else
41+
contents
42+
end
43+
end
44+
45+
# munge text
46+
cask_string.sub!(%r{\A(\s*\#[^\n]*\n)+}, '');
47+
if %r{\A\s*cask\s+:v[\d_]+\s+=>\s+([\'\"])(\S+?)\1(?:\s*,\s*|\s+)do\s*\n}.match(cask_string)
48+
cask_string.sub!(%r{\A[^\n]+\n}, "class #{cask_class_name} < Cask\n")
49+
end
50+
51+
# simulate "require"
52+
begin
53+
Cask.const_get(cask_class_name)
54+
rescue NameError
55+
eval(cask_string, TOPLEVEL_BINDING)
56+
end
57+
2358
rescue CaskError, StandardError, ScriptError => e
2459
# bug: e.message.concat doesn't work with CaskError exceptions
2560
e.message.concat(" while loading '#{path}'")

0 commit comments

Comments
 (0)