Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for uninstall/zap :trash. #2793

Merged
merged 2 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,14 @@ def uninstall_delete(*paths)
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)
Expand Down
32 changes: 32 additions & 0 deletions Library/Homebrew/test/utils/trash_spec.rb
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions Library/Homebrew/utils/trash.swift
Original file line number Diff line number Diff line change
@@ -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()