Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Oct 13, 2024
1 parent 94def3a commit 191aab8
Show file tree
Hide file tree
Showing 16 changed files with 527 additions and 363 deletions.
46 changes: 45 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import PackageDescription

let package = Package(
var package = Package(
name: "Merge",
platforms: [
.iOS(.v13),
Expand Down Expand Up @@ -67,3 +67,47 @@ let package = Package(
],
swiftLanguageVersions: [.v5]
)

/*package = Optional(package)!

extension Target.Dependency {
var name: String {
switch self {
case let .targetItem(name, _):
return name
case let .productItem(name, package, moduleAliases, condition):
return name
case let .byNameItem(name, condition):
return name
}
}
}

private func patchPackageToUsePrebuiltBinaries(in package: inout Package) {
func patchMacro(_ target: Target, _ macro: String) {
var settings = target.swiftSettings ?? []

settings.append(.unsafeFlags([
"-Xfrontend",
"-load-plugin-executable",
"-Xfrontend",
"/Users/vatsal/Downloads/GitHub/vmanot/Frameworks/Swallow/XCFrameworks/SwallowMacros-tool#\(macro)"
]))

target.swiftSettings = settings
}

if Set(package.dependencies.compactMap({ $0.name })).contains("swift-syntax") {
package.dependencies = [
.package(path: "XCFrameworks/packages/swift-collections"),
.package(path: "XCFrameworks/packages/swift-syntax"),
]
}

for target in package.targets {
patchMacro(target, "SwallowMacros")
}
}

patchPackageToUsePrebuiltBinaries(in: &package)
*/
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Combine
import Dispatch
import Foundation
import Swift
import System

extension Process {
func pipeStandardOutput<S: Subject>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Swift
import SwallowMacrosClient

#if os(macOS)

public class _OSAScriptProcess: Process, @unchecked Sendable {
/// A `Process` subclass that internally uses `AppleScript` to execute itself.
public class OSAScriptProcess: Process, @unchecked Sendable {
private var underlyingTask = Process()
private var pidFilePath: String = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString + ".pid").path
private var _terminationHandler: (@Sendable (Process) -> Void)?
Expand Down Expand Up @@ -104,7 +104,7 @@ public class _OSAScriptProcess: Process, @unchecked Sendable {
}

override public func run() throws {
let (launchPath, arguments) = try _OSAScriptProcess._osascript_launchPathAndArguments(for: (executableURL, arguments))
let (launchPath, arguments) = try OSAScriptProcess._osascript_launchPathAndArguments(for: (executableURL, arguments))

underlyingTask.launchPath = launchPath
underlyingTask.arguments = arguments
Expand Down Expand Up @@ -137,7 +137,7 @@ public class _OSAScriptProcess: Process, @unchecked Sendable {
}
}

extension _OSAScriptProcess {
extension OSAScriptProcess {
public static func _osascript_launchPathAndArguments(
for executableURLAndArguments: (executableURL: URL?, arguments: [String]?)
) throws -> (launchPath: String, arguments: [String]) {
Expand Down Expand Up @@ -216,7 +216,7 @@ extension _OSAScriptProcess {

#elseif targetEnvironment(macCatalyst)

public class _OSAScriptProcess: Process, @unchecked Sendable {
public class OSAScriptProcess: Process, @unchecked Sendable {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,85 @@
// Copyright (c) Vatsal Manot
//

import Foundation
import FoundationX
import Swift
import System

#if os(macOS)
extension Process {
// Passed as the first parameter to `sh` or `shq`, specifying where to direct the output
public enum StandardOutputSink {
/// Redirect output to the terminal
/// Redirect output to the terminal.
case terminal
/// Redirect output to the file at the given path, creating if necessary.
case file(_ path: String)
case filePath(_ path: String)
/// Redirect output and error streams to the files at the given paths, creating if necessary.
case split(_ out: String, err: String)
/// The null device, also known as `/dev/null`
/// The null device, also known as `/dev/null`.
case null
}
}

// MARK: - Initializers

extension Process.StandardOutputSink {
public static func file(
_ url: URL
) -> Self {
Self.filePath(url._fromFileURLToURL().path)
}
}

// MARK: - Supplementary

extension Process {
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
public func redirectAllOutput(
to sink: Process.StandardOutputSink
) throws {
switch sink {
case .terminal:
self.standardOutput = FileHandle.standardOutput
self.standardError = FileHandle.standardError
case .filePath(let path):
let fileHandle: FileHandle = try self._createFile(atPath: path)

self.standardOutput = fileHandle
self.standardError = fileHandle
case .split(let outputPath, let errorPath):
self.standardOutput = try self._createFile(atPath: outputPath)
self.standardError = try self._createFile(atPath: errorPath)
case .null:
self.standardOutput = FileHandle.nullDevice
self.standardError = FileHandle.nullDevice
}
}

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
private func _createFile(
atPath path: String
) throws -> FileHandle {
let directories = FilePath(path).lexicallyNormalized().removingLastComponent()

try FileManager.default.createDirectory(atPath: directories.string, withIntermediateDirectories: true)

public static func file(_ url: URL) -> Self {
Self.file(url._fromFileURLToURL().path)
guard FileManager.default.createFile(atPath: path, contents: Data()) else {
struct CouldNotCreateFile: Error {
let path: String
}

throw CouldNotCreateFile(path: path)
}

guard let fileHandle = FileHandle(forWritingAtPath: path) else {
struct CouldNotOpenFileForWriting: Error {
let path: String
}

throw CouldNotOpenFileForWriting(path: path)
}

return fileHandle
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension Shell {
try await Shell.run(
command: "echo " + commands[1...].joined(separator: " "),
environment: .bash,
progressHandler: .block {
outputHandler: .block {
arguments = $0.split(separator: " ").map(String.init)
}
)
Expand All @@ -55,7 +55,7 @@ extension Shell {

try await Shell.run(
command: "which \(x)",
progressHandler: .block {
outputHandler: .block {
result = $0
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Shell {
environment: Environment = .zsh,
environmentVariables: [String: String] = [:],
currentDirectoryURL: URL? = nil,
progressHandler: Shell.ProgressHandler = .print,
outputHandler: SystemShell.StandardOutputHandler = .print,
options: [_AsyncProcessOption]? = nil
) async throws -> _ProcessResult {
try await Shell(options: options).run(
Expand All @@ -38,7 +38,7 @@ extension Shell {
environment: environment,
environmentVariables: environmentVariables,
currentDirectoryURL: currentDirectoryURL,
progressHandler: progressHandler
outputHandler: outputHandler
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@
import Foundation
import Swallow

@globalActor
public actor _ShellActor {
public actor ActorType {
fileprivate init() {

}
}

public static let shared: ActorType = ActorType()
}
@available(*, deprecated, renamed: "SystemShell")
public typealias Shell = SystemShell

public final class Shell {
public final class SystemShell {
public var currentDirectoryURL: URL?

public let options: [_AsyncProcessOption]?

private var environmentVariables: [String: String] {
ProcessInfo.processInfo.environment
}
Expand All @@ -32,7 +24,7 @@ public final class Shell {
}

#if os(macOS)
extension Shell {
extension SystemShell {
public func run(
executableURL: URL,
arguments: [String],
Expand All @@ -41,7 +33,7 @@ extension Shell {
environmentVariables: [String: String] = [:]
) async throws -> _ProcessResult {
let (launchPath, arguments) = try await environment.resolve(launchPath: executableURL.path, arguments: arguments)

let process = try _AsyncProcess(
launchPath: launchPath,
arguments: arguments,
Expand All @@ -60,7 +52,7 @@ extension Shell {
environment: Environment = .zsh,
environmentVariables: [String: String] = [:],
currentDirectoryURL: URL? = nil,
progressHandler: Shell.ProgressHandler = .print
outputHandler: SystemShell.StandardOutputHandler = .print
) async throws -> _ProcessResult {
let options: [_AsyncProcessOption] = options ?? [
.reportCompletion,
Expand All @@ -75,7 +67,7 @@ extension Shell {
arguments: arguments,
environment: self.environmentVariables.merging(environmentVariables, uniquingKeysWith: { $1 }),
currentDirectoryURL: currentDirectoryURL ?? self.currentDirectoryURL,
progressHandler: progressHandler,
outputHandler: outputHandler,
options: options
)

Expand All @@ -88,7 +80,7 @@ extension Shell {
}
}
#else
extension Shell {
extension SystemShell {
public func run(
executableURL: URL,
arguments: [String],
Expand All @@ -106,7 +98,7 @@ extension Shell {
environment: Environment = .zsh,
environmentVariables: [String: String] = [:],
currentDirectoryURL: URL? = nil,
progressHandler: Shell.ProgressHandler = .print
outputHandler: SystemShell.StandardOutputHandler = .print
) async throws -> _ProcessResult {
throw Never.Reason.unsupported
}
Expand All @@ -115,17 +107,10 @@ extension Shell {

// MARK: - Auxiliary

extension Shell {
public enum ProgressHandler {
extension SystemShell {
public enum StandardOutputHandler {
public typealias Block = (_ text: String) -> Void

public enum ScriptOption: Equatable {
case login
case delay(_ timeinterval: TimeInterval)
case waitUntilFinish
case closeScriptInside
}

case print
case block(
output: Block,
Expand All @@ -137,3 +122,16 @@ extension Shell {
}
}
}

// MARK: - Auxiliary

@globalActor
public actor _ShellActor {
public actor ActorType {
fileprivate init() {

}
}

public static let shared: ActorType = ActorType()
}
Loading

0 comments on commit 191aab8

Please sign in to comment.