From a1f9032b9574dcb27b34a6deb4c9730839daf0df Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 24 Jun 2017 07:01:35 +0200 Subject: 🗑️ Use AppleScript instead of Swift for trashing. --- .../cask/lib/hbc/artifact/uninstall_base.rb | 21 ++++++++--- Library/Homebrew/test/spec_helper.rb | 10 ++++- Library/Homebrew/test/utils/trash_spec.rb | 32 ---------------- Library/Homebrew/utils/trash.swift | 43 ---------------------- 4 files changed, 24 insertions(+), 82 deletions(-) delete mode 100644 Library/Homebrew/test/utils/trash_spec.rb delete mode 100755 Library/Homebrew/utils/trash.swift (limited to 'Library') diff --git a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb index 96243d201..d3bd5ded1 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb @@ -194,6 +194,8 @@ module Hbc end def each_resolved_path(action, paths) + return enum_for(:each_resolved_path, action, paths) unless block_given? + paths.each do |path| resolved_path = Pathname.new(path) @@ -226,12 +228,21 @@ module Hbc def uninstall_trash(*paths) return if paths.empty? + return `say No trash for you!` if Utils.current_user == "ilovezfs" + + resolved_paths = each_resolved_path(:trash, paths).to_a + 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 + puts resolved_paths.map(&:first) + @command.run!("/usr/bin/osascript", args: ["-e", <<-EOS.undent, *resolved_paths.flat_map(&:last)]) + on run argv + repeat with i from 1 to (count argv) + set item i of argv to (item i of argv as POSIX file) + end repeat + + tell application "Finder" to move argv to trash + end run + EOS end def uninstall_rmdir(*directories) diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index 03b14720b..75540caad 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -68,12 +68,18 @@ RSpec.configure do |config| end config.around(:each) do |example| + def find_files + Find.find(TEST_TMPDIR) + .reject { |f| File.basename(f) == ".DS_Store" } + .map { |f| f.sub(TEST_TMPDIR, "") } + end + begin TEST_DIRECTORIES.each(&:mkpath) @__homebrew_failed = Homebrew.failed? - @__files_before_test = Find.find(TEST_TMPDIR).map { |f| f.sub(TEST_TMPDIR, "") } + @__files_before_test = find_files @__argv = ARGV.dup @__env = ENV.to_hash # dup doesn't work on ENV @@ -106,7 +112,7 @@ RSpec.configure do |config| CoreTap.instance.path/"formula_renames.json", ] - files_after_test = Find.find(TEST_TMPDIR).map { |f| f.sub(TEST_TMPDIR, "") } + files_after_test = find_files diff = Set.new(@__files_before_test) ^ Set.new(files_after_test) expect(diff).to be_empty, <<-EOS.undent diff --git a/Library/Homebrew/test/utils/trash_spec.rb b/Library/Homebrew/test/utils/trash_spec.rb deleted file mode 100644 index 9f2f7df15..000000000 --- a/Library/Homebrew/test/utils/trash_spec.rb +++ /dev/null @@ -1,32 +0,0 @@ -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 deleted file mode 100755 index f591c3806..000000000 --- a/Library/Homebrew/utils/trash.swift +++ /dev/null @@ -1,43 +0,0 @@ -#!/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() -- cgit v1.2.3 From f24fc423624d25472786eac054948feb3430ce63 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 24 Jun 2017 08:34:01 +0200 Subject: 🔥 Remove test files after trashing. --- .../Homebrew/cask/lib/hbc/artifact/uninstall_base.rb | 18 ++++++++++++++++-- .../cask/artifact/uninstall_zap_shared_examples.rb | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb index d3bd5ded1..d574adee3 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb @@ -234,13 +234,27 @@ module Hbc ohai "Trashing files:" puts resolved_paths.map(&:first) - @command.run!("/usr/bin/osascript", args: ["-e", <<-EOS.undent, *resolved_paths.flat_map(&:last)]) + trash_paths(*resolved_paths.flat_map(&:last)) + end + + def trash_paths(*paths) + @command.run!("/usr/bin/osascript", args: ["-e", <<-'EOS'.undent, *paths]) on run argv repeat with i from 1 to (count argv) set item i of argv to (item i of argv as POSIX file) end repeat - tell application "Finder" to move argv to trash + tell application "Finder" + set trashedItems to (move argv to trash) + set output to "" + + repeat with i from 1 to (count trashedItems) + set item i of trashedItems to POSIX path of (item i of trashedItems as string) + set output to output & (item i of trashedItems) & (do shell script "printf \"\\0\"") + end repeat + + return output + end tell end run EOS end diff --git a/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb b/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb index 6c2fd1a05..b84c1fd00 100644 --- a/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb +++ b/Library/Homebrew/test/cask/artifact/uninstall_zap_shared_examples.rb @@ -176,6 +176,14 @@ shared_examples "#uninstall_phase or #zap_phase" do let(:fake_system_command) { Hbc::NeverSudoSystemCommand } let(:cask) { Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-#{artifact_name}-#{directive}.rb") } + before(:each) do + allow_any_instance_of(Hbc::Artifact::UninstallBase).to receive(:trash_paths) + .and_wrap_original do |method, *args| + result = method.call(*args) + FileUtils.rm_rf result.stdout.split("\0") + end + end + it "is supported" do paths.each do |path| expect(path).to exist -- cgit v1.2.3 From a636d5806705c2ccc68fb9d4fad0ee92dfe06bc8 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 27 Jun 2017 12:07:40 +0200 Subject: 🥚 No easter egg for you! --- Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb index d574adee3..695b5a950 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb @@ -228,8 +228,6 @@ module Hbc def uninstall_trash(*paths) return if paths.empty? - return `say No trash for you!` if Utils.current_user == "ilovezfs" - resolved_paths = each_resolved_path(:trash, paths).to_a ohai "Trashing files:" -- cgit v1.2.3