-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support vending products that are backed by binaryTargets #5810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"schemaVersion": "1.0", | ||
"artifacts": { | ||
"mytool": { | ||
"type": "executable", | ||
"version": "1.2.3", | ||
"variants": [ | ||
{ | ||
"path": "mytool-macos/mytool", | ||
"supportedTriples": ["x86_64-apple-macosx", "arm64-apple-macosx"] | ||
}, | ||
{ | ||
"path": "mytool-linux/mytool", | ||
"supportedTriples": ["x86_64-unknown-linux-gnu"] | ||
} | ||
] | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/bash | ||
|
||
print_usage() { | ||
echo "usage: ${0##*/} [--verbose] <in> <out>" | ||
} | ||
|
||
# Parse arguments until we find '--' or an argument that isn't an option. | ||
until [ $# -eq 0 ] | ||
do | ||
case "$1" in | ||
--verbose) verbose=1; shift;; | ||
--) shift; break;; | ||
-*) echo "unknown option: ${1}"; print_usage; exit 1; shift;; | ||
*) break;; | ||
esac | ||
done | ||
|
||
# Print usage and leave if we don't have exactly two arguments. | ||
if [ $# -ne 2 ]; then | ||
print_usage | ||
exit 1 | ||
fi | ||
|
||
# For our sample tool we just copy from one to the other. | ||
if [ $verbose != 0 ]; then | ||
echo "[${0##*/}-linux] '$1' '$2'" | ||
fi | ||
|
||
cp "$1" "$2" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/bash | ||
|
||
print_usage() { | ||
echo "usage: ${0##*/} [--verbose] <in> <out>" | ||
} | ||
|
||
# Parse arguments until we find '--' or an argument that isn't an option. | ||
until [ $# -eq 0 ] | ||
do | ||
case "$1" in | ||
--verbose) verbose=1; shift;; | ||
--) shift; break;; | ||
-*) echo "unknown option: ${1}"; print_usage; exit 1; shift;; | ||
*) break;; | ||
esac | ||
done | ||
|
||
# Print usage and leave if we don't have exactly two arguments. | ||
if [ $# -ne 2 ]; then | ||
print_usage | ||
exit 1 | ||
fi | ||
|
||
# For our sample tool we just copy from one to the other. | ||
if [ $verbose != 0 ]; then | ||
echo "[${0##*/}-macosx] '$1' '$2'" | ||
fi | ||
|
||
cp "$1" "$2" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// swift-tools-version: 5.6 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MyBinaryProduct", | ||
products: [ | ||
.executable( | ||
name: "MyVendedSourceGenBuildTool", | ||
targets: ["MyVendedSourceGenBuildTool"] | ||
), | ||
], | ||
targets: [ | ||
.binaryTarget( | ||
name: "MyVendedSourceGenBuildTool", | ||
path: "Binaries/MyVendedSourceGenBuildTool.artifactbundle" | ||
), | ||
] | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// swift-tools-version: 5.6 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MyBinaryToolPlugin", | ||
dependencies: [ | ||
.package(path: "Dependency"), | ||
], | ||
targets: [ | ||
// A local tool that uses a build tool plugin. | ||
.executableTarget( | ||
name: "MyLocalTool", | ||
plugins: [ | ||
"MySourceGenBuildToolPlugin", | ||
] | ||
), | ||
// The plugin that generates build tool commands to invoke MySourceGenBuildTool. | ||
.plugin( | ||
name: "MySourceGenBuildToolPlugin", | ||
capability: .buildTool(), | ||
dependencies: [ | ||
.product(name: "MyVendedSourceGenBuildTool", package: "Dependency"), | ||
] | ||
), | ||
] | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import PackagePlugin | ||
|
||
@main | ||
struct MyPlugin: BuildToolPlugin { | ||
|
||
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] { | ||
print("Hello from the Build Tool Plugin!") | ||
guard let target = target as? SourceModuleTarget else { return [] } | ||
let inputFiles = target.sourceFiles.filter({ $0.path.extension == "dat" }) | ||
return try inputFiles.map { | ||
let inputFile = $0 | ||
let inputPath = inputFile.path | ||
let outputName = inputPath.stem + ".swift" | ||
let outputPath = context.pluginWorkDirectory.appending(outputName) | ||
return .buildCommand( | ||
displayName: | ||
"Generating \(outputName) from \(inputPath.lastComponent)", | ||
executable: | ||
try context.tool(named: "mytool").path, | ||
arguments: [ | ||
"--verbose", | ||
"\(inputPath)", | ||
"\(outputPath)" | ||
], | ||
inputFiles: [ | ||
inputPath, | ||
], | ||
outputFiles: [ | ||
outputPath | ||
] | ||
) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let foo = "I am Foo!" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("Generated string Foo: '\(foo)'") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -638,6 +638,11 @@ public final class BinaryTarget: Target { | |
} | ||
} | ||
|
||
public var containsExecutable: Bool { | ||
// FIXME: needs to be revisited once libraries are supported in artifact bundles | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opted to not open this can of worms here since right now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fair, but would be nice if there was some kind of associated data on the artifactsArchive that would allow us to throw an error if it is not an executable. eg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yah, that was my approach, but it did require a bunch of changes 🪱 Could be a good change for a separate PR, though. |
||
return self.kind == .artifactsArchive | ||
} | ||
|
||
public enum Origin: Equatable, Codable { | ||
|
||
/// Represents an artifact that was downloaded from a remote URL. | ||
|
@@ -799,3 +804,18 @@ public enum PluginPermission: Hashable, Codable { | |
} | ||
} | ||
} | ||
|
||
public extension Sequence where Iterator.Element == Target { | ||
var executables: [Target] { | ||
return filter { | ||
switch $0.type { | ||
case .binary: | ||
return ($0 as? BinaryTarget)?.containsExecutable == true | ||
case .executable: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.