Port file trashing to Swift
This avoids hitting AppleScript timeouts in CI.
This commit is contained in:
parent
1406ee7eac
commit
93b2c29612
@ -24,6 +24,8 @@ module Cask
|
||||
:rmdir,
|
||||
].freeze
|
||||
|
||||
TRASH_SCRIPT = (HOMEBREW_LIBRARY_PATH/"cask/utils/trash.swift").freeze
|
||||
|
||||
def self.from_args(cask, **directives)
|
||||
new(cask, directives)
|
||||
end
|
||||
@ -318,35 +320,7 @@ module Cask
|
||||
end
|
||||
|
||||
def trash_paths(*paths, command: nil, **_)
|
||||
result = command.run!("osascript", args: ["-e", <<~APPLESCRIPT, *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
|
||||
|
||||
try
|
||||
with timeout of 30 seconds
|
||||
tell application "Finder"
|
||||
set trashedItems to (move argv to trash)
|
||||
set output to ""
|
||||
|
||||
repeat with i from 1 to (count trashedItems)
|
||||
set trashedItem to POSIX path of (item i of trashedItems as string)
|
||||
set output to output & trashedItem
|
||||
if i < count trashedItems then
|
||||
set output to output & character id 0
|
||||
end if
|
||||
end repeat
|
||||
|
||||
return output
|
||||
end tell
|
||||
end timeout
|
||||
on error
|
||||
-- Ignore errors (probably running under Azure)
|
||||
return 0
|
||||
end try
|
||||
end run
|
||||
APPLESCRIPT
|
||||
result = command.run!("/usr/bin/swift", args: [TRASH_SCRIPT, *paths])
|
||||
|
||||
# Remove AppleScript's automatic newline.
|
||||
result.tap { |r| r.stdout.sub!(/\n$/, "") }
|
||||
|
||||
22
Library/Homebrew/cask/utils/trash.swift
Normal file
22
Library/Homebrew/cask/utils/trash.swift
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/swift
|
||||
|
||||
import Foundation
|
||||
|
||||
if (CommandLine.arguments.count < 2) {
|
||||
exit(2)
|
||||
}
|
||||
|
||||
let manager: FileManager = FileManager()
|
||||
|
||||
for item in CommandLine.arguments[1...] {
|
||||
do {
|
||||
let path: URL = URL(fileURLWithPath: item)
|
||||
try manager.trashItem(at: path, resultingItemURL: nil)
|
||||
print(path)
|
||||
}
|
||||
catch {
|
||||
print("\0")
|
||||
}
|
||||
}
|
||||
|
||||
exit(0)
|
||||
Loading…
x
Reference in New Issue
Block a user