|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Basics |
| 14 | +import PackageLoading |
| 15 | +import PackageModel |
| 16 | +import SwiftParser |
| 17 | +import SwiftSyntax |
| 18 | +import SwiftSyntaxBuilder |
| 19 | + |
| 20 | +/// Add a target dependency to a manifest's source code. |
| 21 | +public struct AddTargetDependency { |
| 22 | + /// The set of argument labels that can occur after the "targets" |
| 23 | + /// argument in the Package initializers. |
| 24 | + /// |
| 25 | + /// TODO: Could we generate this from the the PackageDescription module, so |
| 26 | + /// we don't have keep it up-to-date manually? |
| 27 | + private static let argumentLabelsAfterTargets: Set<String> = [ |
| 28 | + "swiftLanguageVersions", |
| 29 | + "cLanguageStandard", |
| 30 | + "cxxLanguageStandard" |
| 31 | + ] |
| 32 | + |
| 33 | + /// The set of argument labels that can occur after the "dependencies" |
| 34 | + /// argument in the various target initializers. |
| 35 | + /// |
| 36 | + /// TODO: Could we generate this from the the PackageDescription module, so |
| 37 | + /// we don't have keep it up-to-date manually? |
| 38 | + private static let argumentLabelsAfterDependencies: Set<String> = [ |
| 39 | + "path", |
| 40 | + "exclude", |
| 41 | + "sources", |
| 42 | + "resources", |
| 43 | + "publicHeadersPath", |
| 44 | + "packageAccess", |
| 45 | + "cSettings", |
| 46 | + "cxxSettings", |
| 47 | + "swiftSettings", |
| 48 | + "linkerSettings", |
| 49 | + "plugins", |
| 50 | + ] |
| 51 | + |
| 52 | + /// Produce the set of source edits needed to add the given target |
| 53 | + /// dependency to the given manifest file. |
| 54 | + public static func addTargetDependency( |
| 55 | + _ dependency: TargetDescription.Dependency, |
| 56 | + targetName: String, |
| 57 | + to manifest: SourceFileSyntax |
| 58 | + ) throws -> PackageEditResult { |
| 59 | + // Make sure we have a suitable tools version in the manifest. |
| 60 | + try manifest.checkEditManifestToolsVersion() |
| 61 | + |
| 62 | + guard let packageCall = manifest.findCall(calleeName: "Package") else { |
| 63 | + throw ManifestEditError.cannotFindPackage |
| 64 | + } |
| 65 | + |
| 66 | + // Dig out the array of targets. |
| 67 | + guard let targetsArgument = packageCall.findArgument(labeled: "targets"), |
| 68 | + let targetArray = targetsArgument.expression.findArrayArgument() else { |
| 69 | + throw ManifestEditError.cannotFindTargets |
| 70 | + } |
| 71 | + |
| 72 | + // Look for a call whose name is a string literal matching the |
| 73 | + // requested target name. |
| 74 | + func matchesTargetCall(call: FunctionCallExprSyntax) -> Bool { |
| 75 | + guard let nameArgument = call.findArgument(labeled: "name") else { |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + guard let stringLiteral = nameArgument.expression.as(StringLiteralExprSyntax.self), |
| 80 | + let literalValue = stringLiteral.representedLiteralValue else { |
| 81 | + return false |
| 82 | + } |
| 83 | + |
| 84 | + return literalValue == targetName |
| 85 | + } |
| 86 | + |
| 87 | + guard let targetCall = FunctionCallExprSyntax.findFirst(in: targetArray, matching: matchesTargetCall) else { |
| 88 | + throw ManifestEditError.cannotFindTarget(targetName: targetName) |
| 89 | + } |
| 90 | + |
| 91 | + let newTargetCall = try addTargetDependencyLocal( |
| 92 | + dependency, to: targetCall |
| 93 | + ) |
| 94 | + |
| 95 | + return PackageEditResult( |
| 96 | + manifestEdits: [ |
| 97 | + .replace(targetCall, with: newTargetCall.description) |
| 98 | + ] |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + /// Implementation of adding a target dependency to an existing call. |
| 103 | + static func addTargetDependencyLocal( |
| 104 | + _ dependency: TargetDescription.Dependency, |
| 105 | + to targetCall: FunctionCallExprSyntax |
| 106 | + ) throws -> FunctionCallExprSyntax { |
| 107 | + try targetCall.appendingToArrayArgument( |
| 108 | + label: "dependencies", |
| 109 | + trailingLabels: Self.argumentLabelsAfterDependencies, |
| 110 | + newElement: dependency.asSyntax() |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
| 114 | + |
0 commit comments