Skip to content

Commit c0bc7b4

Browse files
author
ndr
committed
Merge pull request #6117 from rolandwalker/metadata_directories
Metadata directory support
2 parents 8491b10 + 0574662 commit c0bc7b4

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

lib/cask.rb

+58-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ def self.title
126126
self.name.gsub(/([a-zA-Z\d])([A-Z])/,'\1-\2').gsub(/([a-zA-Z\d])([A-Z])/,'\1-\2').downcase
127127
end
128128

129+
def self.nowstamp_metadata_path(container_path)
130+
@timenow ||= Time.now.gmtime
131+
if container_path.respond_to?(:join)
132+
precision = 3
133+
timestamp = @timenow.strftime('%Y%m%d%H%M%S')
134+
fraction = ("%.#{precision}f" % (@timenow.to_f - @timenow.to_i))[1..-1]
135+
timestamp.concat(fraction)
136+
container_path.join(timestamp)
137+
end
138+
end
139+
129140
attr_reader :title
130141
def initialize(title=self.class.title)
131142
@title = title
@@ -136,7 +147,53 @@ def caskroom_path
136147
end
137148

138149
def destination_path
139-
caskroom_path.join(version.to_s)
150+
cask_version = version ? version : :unknown
151+
caskroom_path.join(cask_version.to_s)
152+
end
153+
154+
def metadata_master_container_path
155+
caskroom_path.join(self.class.metadata_subdir)
156+
end
157+
158+
def metadata_versioned_container_path
159+
cask_version = version ? version : :unknown
160+
metadata_master_container_path.join(cask_version.to_s)
161+
end
162+
163+
def metadata_path(timestamp=:latest, create=false)
164+
return nil unless metadata_versioned_container_path.respond_to?(:join)
165+
if create and timestamp == :latest
166+
raise CaskError.new('Cannot create metadata path when timestamp is :latest')
167+
end
168+
if timestamp == :latest
169+
path = Pathname.glob(metadata_versioned_container_path.join('*')).sort.last
170+
elsif timestamp == :now
171+
path = self.class.nowstamp_metadata_path(metadata_versioned_container_path)
172+
else
173+
path = metadata_versioned_container_path.join(timestamp)
174+
end
175+
if create
176+
odebug "Creating metadata directory #{path}"
177+
FileUtils.mkdir_p path
178+
end
179+
path
180+
end
181+
182+
def metadata_subdir(leaf, timestamp=:latest, create=false)
183+
if create and timestamp == :latest
184+
raise CaskError.new('Cannot create metadata subdir when timestamp is :latest')
185+
end
186+
unless leaf.respond_to?(:length) and leaf.length > 0
187+
raise CaskError.new('Cannot create metadata subdir for empty leaf')
188+
end
189+
parent = metadata_path(timestamp, create)
190+
return nil unless parent.respond_to?(:join)
191+
subdir = parent.join(leaf)
192+
if create
193+
odebug "Creating metadata subdirectory #{subdir}"
194+
FileUtils.mkdir_p subdir
195+
end
196+
subdir
140197
end
141198

142199
def installed?

lib/cask/installer.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class Cask::Installer
2+
3+
PERSISTENT_METADATA_SUBDIRS = [ 'gpg' ]
4+
25
def initialize(cask, command=Cask::SystemCommand)
36
@cask = cask
47
@command = command
@@ -137,7 +140,9 @@ def uninstall_artifacts
137140

138141
def purge_files
139142
odebug "Purging files"
140-
if @cask.destination_path.exist?
143+
144+
# versioned staged distribution
145+
if @cask.destination_path.respond_to?(:rmtree) and @cask.destination_path.exist?
141146
begin
142147
@cask.destination_path.rmtree
143148
rescue
@@ -149,6 +154,20 @@ def purge_files
149154
end
150155
end
151156
end
157+
158+
if @cask.metadata_versioned_container_path.respond_to?(:children) and @cask.metadata_versioned_container_path.exist?
159+
@cask.metadata_versioned_container_path.children.each do |subdir|
160+
subdir.rmtree unless PERSISTENT_METADATA_SUBDIRS.include?(subdir.basename)
161+
end
162+
end
163+
if @cask.metadata_versioned_container_path.respond_to?(:rmdir_if_possible)
164+
@cask.metadata_versioned_container_path.rmdir_if_possible
165+
end
166+
if @cask.metadata_master_container_path.respond_to?(:rmdir_if_possible)
167+
@cask.metadata_master_container_path.rmdir_if_possible
168+
end
169+
170+
# toplevel staged distribution
152171
@cask.caskroom_path.rmdir_if_possible
153172
end
154173
end

lib/cask/locations.rb

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ def tapspath
88
HOMEBREW_REPOSITORY.join "Library", "Taps"
99
end
1010

11+
def metadata_subdir
12+
'.metadata'
13+
end
14+
1115
def caskroom
1216
@@caskroom ||= Pathname('/opt/homebrew-cask/Caskroom')
1317
end

test/cask/installer_test.rb

+25
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@
274274
dest_path = Cask.appdir/'MyNestedApp.app'
275275
TestHelper.valid_alias?(dest_path).must_equal true
276276
end
277+
278+
it "generates and finds a timestamped metadata directory for an installed Cask" do
279+
caffeine = Cask.load('local-caffeine')
280+
281+
shutup do
282+
Cask::Installer.new(caffeine).install
283+
end
284+
285+
m_path = caffeine.metadata_path(:now, true)
286+
caffeine.metadata_path(:now, false).must_equal(m_path)
287+
caffeine.metadata_path(:latest).must_equal(m_path)
288+
end
289+
290+
it "generates and finds a metadata subdirectory for an installed Cask" do
291+
caffeine = Cask.load('local-caffeine')
292+
293+
shutup do
294+
Cask::Installer.new(caffeine).install
295+
end
296+
297+
subdir_name = 'Casks'
298+
m_subdir = caffeine.metadata_subdir(subdir_name, :now, true)
299+
caffeine.metadata_subdir(subdir_name, :now, false).must_equal(m_subdir)
300+
caffeine.metadata_subdir(subdir_name, :latest).must_equal(m_subdir)
301+
end
277302
end
278303

279304
describe "uninstall" do

test/cask_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,13 @@
8787
GamesXChange.title.must_equal 'games-x-change'
8888
end
8989
end
90+
91+
describe "metadata" do
92+
it "proposes a versioned metadata directory name for each instance" do
93+
cask_name = "adium"
94+
c = Cask.load(cask_name)
95+
metadata_path = Cask.caskroom.join(cask_name, '.metadata', c.version)
96+
c.metadata_versioned_container_path.to_s.must_equal(metadata_path.to_s)
97+
end
98+
end
9099
end

0 commit comments

Comments
 (0)