From bf99c6b0a32c53980db606165dcb0f4f569dae78 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Thu, 20 Feb 2014 17:01:45 -0500 Subject: [PATCH] Store metadata with installed Casks. Step 2: Snapshot the Cask file used at install-time, using the previously-merged metadata directory support. Using a simple filesystem-is-a-database approach, we set up a `.metadata` directory for each installed Cask in which we can record any information, starting with a copy of the Cask definition which was used at install-time. This should be useful for various cases such as: - a fallback when the Cask was renamed/removed. We currently cannot recover any uninstall info in that scenario - installation of multiple versions of the same software There might be a smarter way to discover the source filename for the Cask through introspection or some deep Ruby voodoo. All I could come up with was to pass the filename in on the constructor, which seems perfectly reasonable if voodoo is not available. The existing code was taking the title as an argument to the constructor, which is dispensable. This PR contains no code to actually make use of the metadata, but only takes care of the relevant book-keeping: creation, destruction, as well as organization of metadata according to software version number and timestamp. --- lib/cask.rb | 7 ++++--- lib/cask/installer.rb | 17 +++++++++++++++++ lib/cask/source/path_base.rb | 2 +- lib/cask/without_source.rb | 5 +++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/cask.rb b/lib/cask.rb index 32b9caa773008..f33396f1f6605 100644 --- a/lib/cask.rb +++ b/lib/cask.rb @@ -96,9 +96,10 @@ def self.nowstamp_metadata_path(container_path) end end - attr_reader :token - def initialize(token=self.class.token) - @token = token + attr_reader :token, :sourcefile_path + def initialize(sourcefile_path=nil) + @sourcefile_path = sourcefile_path + @token = self.class.token end def caskroom_path diff --git a/lib/cask/installer.rb b/lib/cask/installer.rb index 8a0de7ee14287..632b03f336a4c 100644 --- a/lib/cask/installer.rb +++ b/lib/cask/installer.rb @@ -59,6 +59,7 @@ def install(force=false) download extract_primary_container install_artifacts + save_caskfile force enable_accessibility_access rescue StandardError => e purge_versioned_files @@ -217,6 +218,22 @@ def disable_accessibility_access end end + def save_caskfile(force=false) + timestamp = :now + create = true + savedir = @cask.metadata_subdir('Casks', timestamp, create) + if Dir.entries(savedir).size > 2 + # should not happen + if force + savedir.rmtree + FileUtils.mkdir_p savedir + else + raise CaskAlreadyInstalledError.new(@cask) + end + end + FileUtils.copy(@cask.sourcefile_path, savedir) if @cask.sourcefile_path + end + def uninstall(force=false) odebug "Cask::Installer.uninstall" disable_accessibility_access diff --git a/lib/cask/source/path_base.rb b/lib/cask/source/path_base.rb index ae6d64b3d7e17..be8125271c32d 100644 --- a/lib/cask/source/path_base.rb +++ b/lib/cask/source/path_base.rb @@ -78,7 +78,7 @@ def load raise e end begin - Object.const_get(cask_class_name).new + Object.const_get(cask_class_name).new(path) rescue CaskError, StandardError, ScriptError => e # bug: e.message.concat doesn't work with CaskError exceptions e.message.concat(" while instantiating '#{cask_class_name}' from '#{path}'") diff --git a/lib/cask/without_source.rb b/lib/cask/without_source.rb index 03305fe2c8169..6655a8a2a3f4e 100644 --- a/lib/cask/without_source.rb +++ b/lib/cask/without_source.rb @@ -3,6 +3,11 @@ def staged_path caskroom_path.children.first end + def initialize(sourcefile_path=nil) + @sourcefile_path = sourcefile_path + @token = sourcefile_path + end + def to_s "#{token} (!)" end