From 3e2eafb636fd745bd7f845e03a2d94018e567d03 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Tue, 15 Jul 2014 16:14:15 -0400 Subject: [PATCH] forward-compatibility to remove naming limitations Currently, Casks names are constrained by the need to form valid Ruby class names. This change enables a new syntax, in which the first line of a Cask will read like ```ruby cask :v1 => 'my-app' do ``` where :v1 refers to the version of the DSL spec. --- lib/cask/source/path_base.rb | 37 +++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/cask/source/path_base.rb b/lib/cask/source/path_base.rb index ff62fb83465f..760c56382781 100644 --- a/lib/cask/source/path_base.rb +++ b/lib/cask/source/path_base.rb @@ -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}'")