aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMarkus Reiter2017-06-20 16:30:09 +0200
committerGitHub2017-06-20 16:30:09 +0200
commit96f8452e17f7a530b4cc823346b673f7626e6e95 (patch)
tree5fc28c2f31cbdedbaf06b3ce3785b57337d0b2b0 /Library
parent80ce43dff1a196d021f64277a2d3b6aa3e2898f7 (diff)
parentb2daed584d6f8b737cc2b500eb19a38d02dbc8a6 (diff)
downloadbrew-96f8452e17f7a530b4cc823346b673f7626e6e95.tar.bz2
Merge pull request #2793 from reitermarkus/trash
Add support for `uninstall/zap :trash`.
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb11
-rw-r--r--Library/Homebrew/test/utils/trash_spec.rb32
-rwxr-xr-xLibrary/Homebrew/utils/trash.swift43
3 files changed, 83 insertions, 3 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb
index 7dc772380..96243d201 100644
--- a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb
+++ b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb
@@ -224,9 +224,14 @@ module Hbc
end
def uninstall_trash(*paths)
- # :trash functionality is stubbed as a synonym for :delete
- # TODO: make :trash work differently, moving files to the Trash
- uninstall_delete(*paths)
+ return if paths.empty?
+
+ ohai "Trashing files:"
+ each_resolved_path(:trash, paths) do |path, resolved_paths|
+ puts path
+ resolved_paths.each { |resolved_path| Utils.gain_permissions(resolved_path, ["-R"], @command) }
+ @command.run!("/usr/bin/xargs", args: ["-0", "--", HOMEBREW_LIBRARY_PATH/"utils/trash.swift"], input: resolved_paths.join("\0"))
+ end
end
def uninstall_rmdir(*directories)
diff --git a/Library/Homebrew/test/utils/trash_spec.rb b/Library/Homebrew/test/utils/trash_spec.rb
new file mode 100644
index 000000000..9f2f7df15
--- /dev/null
+++ b/Library/Homebrew/test/utils/trash_spec.rb
@@ -0,0 +1,32 @@
+require "open3"
+
+describe "trash", :needs_macos do
+ let(:executable) { HOMEBREW_LIBRARY_PATH/"utils/trash.swift" }
+ let(:dir) { mktmpdir }
+ let(:file) { dir/"new_file" }
+
+ it "moves existing files to the trash" do
+ FileUtils.touch file
+
+ expect(file).to exist
+
+ out, err, status = Open3.capture3(executable, file)
+
+ expect(out).to match %r{moved #{file} to .*/\.Trash/\.*}
+ expect(err).to be_empty
+ expect(status).to be_a_success
+
+ expect(file).not_to exist
+
+ trashed_path = out.sub(/^moved #{Regexp.escape(file.to_s)} to (.*)\n$/, '\1')
+ FileUtils.rm_f trashed_path
+ end
+
+ it "fails when files don't exist" do
+ out, err, status = Open3.capture3(executable, file)
+
+ expect(out).to be_empty
+ expect(err).to eq "could not move #{file} to trash\n"
+ expect(status).to be_a_failure
+ end
+end
diff --git a/Library/Homebrew/utils/trash.swift b/Library/Homebrew/utils/trash.swift
new file mode 100755
index 000000000..f591c3806
--- /dev/null
+++ b/Library/Homebrew/utils/trash.swift
@@ -0,0 +1,43 @@
+#!/usr/bin/swift
+
+import Cocoa
+
+DispatchQueue.main.async {
+ let arguments = CommandLine.arguments.dropFirst().filter { !$0.isEmpty }
+ let URLs = arguments.map { URL(fileURLWithPath: $0) }
+
+ #if swift(>=4.0)
+ let workspace = NSWorkspace.shared
+ #else
+ let workspace = NSWorkspace.shared()
+ #endif
+
+ workspace.recycle(URLs) { (dict, error) in
+ dict.forEach {
+ #if swift(>=4.0)
+ let originalPath = $0.0.path
+ let trashPath = $0.1.path
+ #else
+ let originalPath = $0.path
+ let trashPath = $1.path
+ #endif
+
+ print("moved \(originalPath) to \(trashPath)")
+ }
+
+ if error == nil {
+ exit(0)
+ }
+
+ let trashedURLs = dict.keys
+ let untrashedURLs = URLs.filter { !trashedURLs.contains($0) }
+
+ untrashedURLs.forEach {
+ fputs("could not move \($0.path) to trash\n", stderr)
+ }
+
+ exit(1)
+ }
+}
+
+RunLoop.main.run()