Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Plugins/PackageGenerator/AdvancedFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func generateExportedFiles(

let packageDir = context.package.directoryURL

for target in targets where !target.isTest {
for target in targets where !target.isTest && !target.isExecutable {
guard !target.dependencies.isEmpty else { continue }

let exportedFileName = "exported.swift"
Expand Down
3 changes: 2 additions & 1 deletion Plugins/PackageGenerator/ConfigurationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ struct ConfigurationV2: Codable {
case regular
case test
case macro
case executable

var defaultFolder: String {
switch self {
case .regular, .macro: return "Sources"
case .regular, .macro, .executable: return "Sources"
case .test: return "Tests"
}
}
Expand Down
1 change: 1 addition & 0 deletions Plugins/PackageGenerator/PackageGeneratorV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct PackageGeneratorV2 {
name: targetSpec.name,
isTest: targetSpec.type == .test,
isMacro: targetSpec.type == .macro,
isExecutable: targetSpec.type == .executable,
dependencies: filtered,
path: resolvedPath,
fullPath: context.package.directoryURL.appendingPathComponent(resolvedPath).path,
Expand Down
5 changes: 4 additions & 1 deletion Plugins/PackageGenerator/ParsedPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public struct ParsedPackage: Codable, CustomStringConvertible {
public var name: String
public var isTest: Bool
public var isMacro: Bool = false
/// An `.executableTarget` with an `.executable` product: set by the plugin from config; not from CLI JSON.
public var isExecutable: Bool = false
public var dependencies: [String]
public var path: String
public var fullPath: String
Expand Down Expand Up @@ -60,10 +62,11 @@ public struct ParsedPackage: Codable, CustomStringConvertible {
return "[\(dependencies.count)|\(localDependencies)] \(name) \(hasResources == false ? "" : "/ hasResources")"
}

public init(name: String, isTest: Bool, isMacro: Bool = false, dependencies: [String], path: String, fullPath: String, resources: String? = nil, localDependencies: Int = 0, hasBiggestNumberOfDependencies: Bool = false, exclude: [String] = [], parameters: [String]? = nil, additionalDependencies: [String]? = nil, libraryType: LibraryType? = nil, productName: String? = nil) {
public init(name: String, isTest: Bool, isMacro: Bool = false, isExecutable: Bool = false, dependencies: [String], path: String, fullPath: String, resources: String? = nil, localDependencies: Int = 0, hasBiggestNumberOfDependencies: Bool = false, exclude: [String] = [], parameters: [String]? = nil, additionalDependencies: [String]? = nil, libraryType: LibraryType? = nil, productName: String? = nil) {
self.name = name
self.isTest = isTest
self.isMacro = isMacro
self.isExecutable = isExecutable
self.dependencies = dependencies
self.path = path
self.fullPath = fullPath
Expand Down
6 changes: 5 additions & 1 deletion Plugins/PackageGenerator/ProductGeneration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
// Kept free of `PackagePlugin` so the test target can compile this file directly.

/// Generates the products `package.products.append(contentsOf: [...])` section.
/// Includes all non-test, non-macro targets sorted alphabetically.
/// Includes all non-test, non-macro targets sorted alphabetically: `.executable` for an executable target, `.library` for the rest.
func generateProductsSection(
parsedPackages: [ParsedPackage],
config: ConfigurationV2
Expand All @@ -21,6 +21,10 @@ func generateProductsSection(
// The product may be named differently from the target it wraps; `targets:` must
// always reference the real target name.
let productName = lib.productName ?? config.mappers.targets[lib.path, default: lib.name]
if lib.isExecutable {
lines.append("\(s1).executable(name: \"\(productName)\", targets: [\"\(lib.name)\"])")
continue
}
let type = (lib.libraryType ?? config.libraryType).productArgument.map { "\($0), " } ?? ""
lines.append("\(s1).library(name: \"\(productName)\", \(type)targets: [\"\(lib.name)\"])")
}
Expand Down
4 changes: 2 additions & 2 deletions Plugins/PackageGenerator/TargetRendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func pragmaMarkGroupName(_ path: String, fallback: String) -> String {
return parts.first.map(String.init) ?? fallback
}

/// Renders a single target as a `.target()`, `.testTarget()`, or `.macro()` block.
/// Renders a single target as a `.target()`, `.testTarget()`, `.executableTarget()` or `.macro()` block.
/// Matches V1's `fakeTargetToSwiftCode` format exactly.
///
/// Resolution priority per import:
Expand All @@ -42,7 +42,7 @@ func renderSingleTarget(
let s2 = String(repeating: " ", count: config.spaces * 2)
let s3 = String(repeating: " ", count: config.spaces * 3)

let blockType = target.isMacro ? "macro" : (target.isTest ? "testTarget" : "target")
let blockType = target.isMacro ? "macro" : target.isExecutable ? "executableTarget" : (target.isTest ? "testTarget" : "target")

// Build multi-line dependencies section using three-way resolution.
var depsStr = ""
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ packageDirectoryTargets:
- path: Sources/Modules
targets:
- name: ModuleA
type: regular # regular, test, or macro
type: regular # regular, test, macro, or executable
path: null # override computed path if needed
exclude: ["__Snapshots__", "Mocks"] # exclude patterns
parameters: # inline SPM target parameters
Expand All @@ -112,7 +112,7 @@ packageDirectoryTargets:

`additionalDependencies` injects strings verbatim into the target's `dependencies:` array, bypassing import analysis. Use it when a target needs a dependency that isn't imported in source (common with macros, plugins, or transitive requirements). Auto-resolved imports are emitted first (sorted), then `additionalDependencies` in declaration order.

`libraryType` sets the product linkage: `automatic` (default) omits `type:` and lets SwiftPM decide, `dynamic` and `static` emit `.library(name:, type: .dynamic/.static, targets:)`. Set it per target to override the top-level `libraryType`. Ignored for `test` and `macro` targets, which produce no product.
`libraryType` sets the product linkage: `automatic` (default) omits `type:` and lets SwiftPM decide, `dynamic` and `static` emit `.library(name:, type: .dynamic/.static, targets:)`. Set it per target to override the top-level `libraryType`. Ignored for `test` and `macro` targets, which produce no product, and for `executable` targets, which render as `.executableTarget` with an `.executable(name:targets:)` product (named by `productName` when set).

`productName` names the generated product differently from the target it wraps, emitting `.library(name: "<productName>", targets: ["<target>"])`. It takes precedence over a `mappers.targets` entry for the same target.

Expand Down
13 changes: 12 additions & 1 deletion Tests/PackageGeneratorTests/ProductGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ private func lib(
path: String? = nil,
isTest: Bool = false,
isMacro: Bool = false,
isExecutable: Bool = false,
libraryType: LibraryType? = nil,
productName: String? = nil
) -> ParsedPackage {
ParsedPackage(
name: name,
isTest: isTest,
isMacro: isMacro,
isExecutable: isExecutable,
dependencies: [],
path: path ?? "Sources/\(name)",
fullPath: "/tmp/\(name)",
Expand All @@ -36,7 +38,7 @@ private func productLines(
generateProductsSection(parsedPackages: packages, config: config)
.split(separator: "\n")
.map(String.init)
.filter { $0.contains(".library(") }
.filter { $0.contains(".library(") || $0.contains(".executable(") }
.map { $0.trimmingCharacters(in: .whitespaces).trimmingCharacters(in: CharacterSet(charactersIn: ",")) }
}

Expand Down Expand Up @@ -66,6 +68,15 @@ struct ProductGenerationRegressionTests {
#expect(productLines(packages) == [#".library(name: "Foo", targets: ["Foo"])"#])
}

@Test("An executable target produces an `.executable` product")
func executableTargetProducesAnExecutableProduct() {
let packages = [lib("tool", isExecutable: true, libraryType: .dynamic), lib("Core")]
#expect(productLines(packages) == [
#".library(name: "Core", targets: ["Core"])"#,
#".executable(name: "tool", targets: ["tool"])"#,
])
}

@Test("No library targets yields an empty section")
func emptySection() {
let out = generateProductsSection(parsedPackages: [], config: ConfigurationV2())
Expand Down
10 changes: 10 additions & 0 deletions Tests/PackageGeneratorTests/TargetRenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ private func target(
path: String? = nil,
isTest: Bool = false,
isMacro: Bool = false,
isExecutable: Bool = false,
dependencies: [String] = [],
exclude: [String] = [],
parameters: [String]? = nil,
Expand All @@ -18,6 +19,7 @@ private func target(
name: name,
isTest: isTest,
isMacro: isMacro,
isExecutable: isExecutable,
dependencies: dependencies,
path: path ?? "Sources/\(name)",
fullPath: "/tmp/\(name)",
Expand Down Expand Up @@ -113,6 +115,14 @@ struct TargetRenderingTests {
""")
}

@Test("An executable target renders as `.executableTarget` with its resolved imports")
func executableTarget() {
let (text, warnings) = render([target("tool", isExecutable: true, dependencies: ["Core"]), target("Core")])
#expect(text.contains(" .executableTarget(\n name: \"tool\",\n dependencies: [\n \"Core\"\n ],"))
#expect(text.contains(#".executable(name: "tool", targets: ["tool"])"#))
#expect(warnings.isEmpty)
}

@Test("An external product wins over an Apple SDK of the same name")
func externalProductBeatsSDK() {
let (text, warnings) = render(
Expand Down
Loading