Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Oct 19, 2024
1 parent b8b3af7 commit 0124564
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
31 changes: 26 additions & 5 deletions Sources/Merge/Intramodular/Process/SystemShell.Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
// Copyright (c) Vatsal Manot
//

import Foundation
import Combine
import Swallow

extension SystemShell {
public struct Environment {
var launchPath: String?
var deriveArguments: (_ command: String) -> [String]
let launchPath: String?
let deriveArguments: (_ command: String) -> [String]

var launchURL: URL? {
guard let launchPath else {
return nil
}

return URL(fileURLWithPath: launchPath)
}
}
}

Expand All @@ -16,7 +26,12 @@ extension SystemShell.Environment {
launchPath: String,
arguments: [String]
) async throws -> (launchPath: String, arguments: [String]) {
try await resolve(command: "\(launchPath) \(arguments.joined(separator: " "))")
/// Handle the case where the the provide path and arguments are `"/bin/zsh"` and `["-l", "-c", "..."].
if launchPath == self.launchPath, let lastArgument = arguments.last, arguments == self.deriveArguments(arguments.last!) {
return try await resolve(command: lastArgument)
} else {
return try await resolve(command: "\(launchPath) \(arguments.joined(separator: " "))")
}
}

func resolve(
Expand Down Expand Up @@ -71,11 +86,17 @@ extension SystemShell.Environment {

extension SystemShell.Environment {
public static var bash: Self {
Self(launchPath: "/bin/bash", deriveArguments: { ["-c", $0] })
Self(
launchPath: "/bin/bash",
deriveArguments: { ["-c", $0] }
)
}

public static var zsh: Self {
Self(launchPath: "/bin/zsh", deriveArguments: { ["-l", "-c", $0] })
Self(
launchPath: "/bin/zsh",
deriveArguments: { ["-l", "-c", $0] }
)
}

public static var none: Self {
Expand Down
15 changes: 15 additions & 0 deletions Sources/Merge/Intramodular/Process/SystemShell.run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import Swallow

extension SystemShell {
public func run(
shell: SystemShell.Environment,
command: String,
currentDirectoryURL: URL? = nil,
environmentVariables: [String: String] = [:]
) async throws -> _ProcessResult {
try await run(
executableURL: shell.launchURL.unwrap(),
arguments: shell.deriveArguments(command),
currentDirectoryURL: nil,
environment: shell,
environmentVariables: [:]
)
}

public func run(m
executablePath: String,
arguments: [String],
currentDirectoryURL: URL? = nil,
Expand Down

0 comments on commit 0124564

Please sign in to comment.