Skip to content

Commit

Permalink
add app and run directories to context
Browse files Browse the repository at this point in the history
  • Loading branch information
kattouf committed Sep 26, 2024
1 parent e91ac70 commit 5b74445
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
24 changes: 23 additions & 1 deletion Sources/Sake/CLI/RunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ struct RunCommand: SakeParsableCommand {
let commands = try commandsProvider.allCommands()

if let command = commands[command] {
let context = Command.Context(arguments: args, environment: ProcessInfo.processInfo.environment)
let context = Command.Context(
arguments: args,
environment: ProcessInfo.processInfo.environment,
appDirectory: Bundle.main.bundleURL.findBuildDirectory()?.deletingLastPathComponent()
.path ?? "<Could not find SakeApp directory>",
runDirectory: FileManager.default.currentDirectoryPath
)
let runner = CommandRunner(command: command, context: context)
do {
try runner.run()
Expand All @@ -46,3 +52,19 @@ struct RunCommand: SakeParsableCommand {
throw SakeAppError.commandRunFailed(command: command, error: error)
}
}

private extension URL {
func findBuildDirectory() -> URL? {
var currentURL = self

while currentURL.path != "/" {
let buildDirectory = currentURL.appendingPathComponent(".build")
if FileManager.default.fileExists(atPath: buildDirectory.path) {
return buildDirectory
}
currentURL.deleteLastPathComponent()
}

return nil
}
}
14 changes: 12 additions & 2 deletions Sources/Sake/Command+Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ public extension Command {

public extension Command.Context {
func mapArguments(_ transform: ([String]) throws -> [String]) throws -> Command.Context {
try Command.Context(arguments: transform(arguments), environment: environment)
try Command.Context(
arguments: transform(arguments),
environment: environment,
appDirectory: appDirectory,
runDirectory: runDirectory
)
}

func mapEnvironment(_ transform: ([String: String]) throws -> [String: String]) throws -> Command.Context {
try Command.Context(arguments: arguments, environment: transform(environment))
try Command.Context(
arguments: arguments,
environment: transform(environment),
appDirectory: appDirectory,
runDirectory: runDirectory
)
}
}
6 changes: 5 additions & 1 deletion Sources/Sake/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ public extension Command {
struct Context {
public let arguments: [String]
public let environment: [String: String]
public let appDirectory: String
public let runDirectory: String

public init(arguments: [String], environment: [String: String]) {
init(arguments: [String], environment: [String: String], appDirectory: String, runDirectory: String) {
self.arguments = arguments
self.environment = environment
self.appDirectory = appDirectory
self.runDirectory = runDirectory
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/SakeTests/CommandRunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ private extension Command.Context {
static var empty: Command.Context {
Command.Context(
arguments: [],
environment: [:]
environment: [:],
appDirectory: "",
runDirectory: ""
)
}
}

0 comments on commit 5b74445

Please sign in to comment.