Skip to content

Commit d7f0325

Browse files
committed
Break out build tool executable into its own target
1 parent 7fa2de1 commit d7f0325

File tree

6 files changed

+13319
-4657
lines changed

6 files changed

+13319
-4657
lines changed

assets/test/targets/Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ let package = Package(
3333
.executableTarget(
3434
name: "ExecutableTarget"
3535
),
36+
.executableTarget(
37+
name: "BuildToolExecutableTarget"
38+
),
3639
.plugin(
3740
name: "PluginTarget",
3841
capability: .command(
@@ -42,7 +45,7 @@ let package = Package(
4245
.plugin(
4346
name: "BuildToolPlugin",
4447
capability: .buildTool(),
45-
dependencies: ["ExecutableTarget"]
48+
dependencies: ["BuildToolExecutableTarget"]
4649
),
4750
.testTarget(
4851
name: "TargetsTests",

assets/test/targets/Plugins/BuildToolPlugin/BuildToolPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct SimpleBuildToolPlugin: BuildToolPlugin {
1111
return []
1212
#endif
1313

14-
let generatorTool = try context.tool(named: "ExecutableTarget")
14+
let generatorTool = try context.tool(named: "BuildToolExecutableTarget")
1515

1616
// Construct a build command for each source file with a particular suffix.
1717
return sourceFiles.map(\.path).compactMap {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
3+
@main
4+
struct CodeGenerator {
5+
static func main() async throws {
6+
// Use swift-argument-parser or just CommandLine, here we just imply that 2 paths are passed in: input and output
7+
guard CommandLine.arguments.count == 3 else {
8+
throw CodeGeneratorError.invalidArguments
9+
}
10+
// arguments[0] is the path to this command line tool
11+
guard let input = URL(string: "file://\(CommandLine.arguments[1])"), let output = URL(string: "file://\(CommandLine.arguments[2])") else {
12+
return
13+
}
14+
let jsonData = try Data(contentsOf: input)
15+
let enumFormat = try JSONDecoder().decode(JSONFormat.self, from: jsonData)
16+
17+
let code = """
18+
enum \(enumFormat.name): CaseIterable {
19+
\t\(enumFormat.cases.map({ "case \($0)" }).joined(separator: "\n\t"))
20+
}
21+
"""
22+
guard let data = code.data(using: .utf8) else {
23+
throw CodeGeneratorError.invalidData
24+
}
25+
try data.write(to: output, options: .atomic)
26+
}
27+
}
28+
29+
struct JSONFormat: Decodable {
30+
let name: String
31+
let cases: [String]
32+
}
33+
34+
enum CodeGeneratorError: Error {
35+
case invalidArguments
36+
case invalidData
37+
}
Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1 @@
1-
#if os(Windows) && swift(<6.0)
2-
print("Executable Target Hello World!")
3-
#else
4-
import Foundation
5-
6-
@main
7-
@available(macOS 13.0.0, *)
8-
struct CodeGenerator {
9-
static func main() async throws {
10-
// Use swift-argument-parser or just CommandLine, here we just imply that 2 paths are passed in: input and output
11-
guard CommandLine.arguments.count == 3 else {
12-
throw CodeGeneratorError.invalidArguments
13-
}
14-
// arguments[0] is the path to this command line tool
15-
guard let input = URL(string: "file://\(CommandLine.arguments[1])"), let output = URL(string: "file://\(CommandLine.arguments[2])") else {
16-
return
17-
}
18-
let jsonData = try Data(contentsOf: input)
19-
let enumFormat = try JSONDecoder().decode(JSONFormat.self, from: jsonData)
20-
21-
let code = """
22-
enum \(enumFormat.name): CaseIterable {
23-
\t\(enumFormat.cases.map({ "case \($0)" }).joined(separator: "\n\t"))
24-
}
25-
"""
26-
guard let data = code.data(using: .utf8) else {
27-
throw CodeGeneratorError.invalidData
28-
}
29-
try data.write(to: output, options: .atomic)
30-
}
31-
}
32-
33-
struct JSONFormat: Decodable {
34-
let name: String
35-
let cases: [String]
36-
}
37-
38-
@available(macOS 13.00.0, *)
39-
enum CodeGeneratorError: Error {
40-
case invalidArguments
41-
case invalidData
42-
}
43-
#endif
1+
print("Executable Target Hello World!")

0 commit comments

Comments
 (0)