Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
200 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Foundation | ||
import PackagePlugin | ||
|
||
extension PluginContext.Tool { | ||
func execute(arguments: [String]) async throws { | ||
let process = Process() | ||
process.executableURL = URL(fileURLWithPath: path.string) | ||
process.arguments = arguments | ||
|
||
try await withTaskCancellationHandler { | ||
try await withCheckedThrowingContinuation { continuation in | ||
DispatchQueue.global(qos: .userInitiated).async { | ||
do { | ||
try process.run() | ||
process.waitUntilExit() | ||
try process.validateTermination() | ||
|
||
continuation.resume() | ||
} catch { | ||
continuation.resume(throwing: error) | ||
} | ||
} | ||
} | ||
} onCancel: { | ||
process.terminate() | ||
} | ||
} | ||
} | ||
|
||
private extension Process { | ||
func validateTermination() throws { | ||
guard let toolName = executableURL?.lastPathComponent else { preconditionFailure() } | ||
|
||
guard terminationReason == .exit, terminationStatus == 0 else { | ||
throw PluginError.toolRunFailed( | ||
name: toolName, | ||
reason: terminationReason, | ||
status: terminationStatus) | ||
} | ||
} | ||
} | ||
|
||
enum PluginError: LocalizedError, CustomStringConvertible { | ||
case toolRunFailed(name: String, reason: Process.TerminationReason, status: Int32) | ||
|
||
var description: String { | ||
switch self { | ||
case let .toolRunFailed(name, reason, status): | ||
return """ | ||
'\(name)' invocation failed with exit code '\(status)' \ | ||
and reason '\(reason.rawValue)'. | ||
""" | ||
} | ||
} | ||
|
||
var errorDescription: String? { | ||
return description | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Foundation | ||
import PackagePlugin | ||
|
||
private enum OptionName { | ||
static let manifestPath: String = "manifest-path" | ||
} | ||
|
||
@main | ||
struct RunXCResourcePlugin: CommandPlugin { | ||
func performCommand(context: PluginContext, arguments: [String]) async throws { | ||
let tool = try context.tool(named: "xcresource") | ||
|
||
let toolArguments = toolArguments(context: context, pluginArguments: arguments) | ||
|
||
try await tool.execute(arguments: toolArguments) | ||
} | ||
|
||
private func toolArguments(context: PluginContext, pluginArguments: [String]) -> [String] { | ||
var result: [String] = [] | ||
|
||
var argExtractor = ArgumentExtractor(pluginArguments) | ||
|
||
func appendManifestPath(_ manifestPath: String) { | ||
result.append(contentsOf: ["--\(OptionName.manifestPath)", manifestPath]) | ||
} | ||
|
||
if let manifestPath = argExtractor.extractOption(named: OptionName.manifestPath).first { | ||
appendManifestPath(manifestPath) | ||
} else if let manifestPath = manifestPath(inDirectory: context.package.directory) { | ||
appendManifestPath(manifestPath) | ||
} | ||
|
||
return result | ||
} | ||
|
||
private func manifestPath(inDirectory directory: Path) -> String? { | ||
let filename = "xcresource.json" | ||
|
||
let candidates: [String] = [ | ||
"Configs/\(filename)", | ||
"Scripts/\(filename)", | ||
] | ||
|
||
for candidate in candidates { | ||
let path = directory.appending(subpath: candidate) | ||
let url = URL(fileURLWithPath: path.string) | ||
|
||
if (try? url.checkResourceIsReachable()) ?? false { | ||
return path.string | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
Samples/XCResourceSample.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "strix", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/nearfri/Strix", | ||
"state" : { | ||
"revision" : "2e309dbea51da14c4ba74b3abb3debb8ea4a7529", | ||
"version" : "2.4.5" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-argument-parser", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-argument-parser", | ||
"state" : { | ||
"revision" : "c8ed701b513cf5177118a175d85fbbbcd707ab41", | ||
"version" : "1.3.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-collections", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-collections", | ||
"state" : { | ||
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb", | ||
"version" : "1.1.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-syntax", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-syntax", | ||
"state" : { | ||
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d", | ||
"version" : "509.1.1" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.