-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSwiftPackageCommand.swift
151 lines (131 loc) · 4.82 KB
/
SwiftPackageCommand.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import ArgumentParser
import Basics
import CoreCommands
import Foundation
import PackageGraph
import PackageLoading
import PackageModel
import SourceControl
import SPMBuildCore
import Workspace
import XCBuildSupport
import enum TSCUtility.Diagnostics
/// swift-package tool namespace
public struct SwiftPackageCommand: AsyncParsableCommand {
public static var configuration = CommandConfiguration(
commandName: "package",
_superCommandName: "swift",
abstract: "Perform operations on Swift packages",
discussion: "SEE ALSO: swift build, swift run, swift test",
version: SwiftVersion.current.completeDisplayString,
subcommands: [
AddDependency.self,
AddProduct.self,
AddTarget.self,
AddTargetDependency.self,
AddTargetPlugin.self,
Clean.self,
PurgeCache.self,
Reset.self,
Update.self,
Describe.self,
Init.self,
Format.self,
Install.self,
Uninstall.self,
APIDiff.self,
DeprecatedAPIDiff.self,
DumpSymbolGraph.self,
DumpPIF.self,
DumpPackage.self,
Edit.self,
Unedit.self,
Config.self,
Resolve.self,
Fetch.self,
ShowDependencies.self,
ShowExecutables.self,
ToolsVersionCommand.self,
ComputeChecksum.self,
ArchiveSource.self,
CompletionCommand.self,
PluginCommand.self,
DefaultCommand.self,
]
+ (ProcessInfo.processInfo.environment["SWIFTPM_ENABLE_SNIPPETS"] == "1" ? [Learn.self] : []),
defaultSubcommand: DefaultCommand.self,
helpNames: [.short, .long, .customLong("help", withSingleDash: true)]
)
@OptionGroup()
var globalOptions: GlobalOptions
public static var _errorLabel: String { "error" }
public init() {}
}
extension SwiftPackageCommand {
// This command is the default when no other subcommand is passed. It is not shown in the help and is never invoked
// directly.
struct DefaultCommand: AsyncSwiftCommand {
static let configuration = CommandConfiguration(
commandName: nil,
shouldDisplay: false
)
@OptionGroup(visibility: .hidden)
var globalOptions: GlobalOptions
@OptionGroup()
var pluginOptions: PluginCommand.PluginOptions
@Argument(parsing: .captureForPassthrough)
var remaining: [String] = []
func run(_ swiftCommandState: SwiftCommandState) async throws {
// See if have a possible plugin command.
guard let command = remaining.first else {
print(SwiftPackageCommand.helpMessage())
return
}
// Check for edge cases and unknown options to match the behavior in the absence of plugins.
if command.isEmpty {
throw ValidationError("Unknown argument '\(command)'")
} else if command.starts(with: "-") {
throw ValidationError("Unknown option '\(command)'")
}
// Otherwise see if we can find a plugin.
try await PluginCommand.run(
command: command,
options: self.pluginOptions,
arguments: self.remaining,
swiftCommandState: swiftCommandState
)
}
}
}
extension PluginCommand.PluginOptions {
func merged(with other: Self) -> Self {
// validate against developer mistake
assert(
Mirror(reflecting: self).children.count == 4,
"Property added to PluginOptions without updating merged(with:)!"
)
// actual merge
var merged = self
merged.allowWritingToPackageDirectory = merged.allowWritingToPackageDirectory || other
.allowWritingToPackageDirectory
merged.additionalAllowedWritableDirectories.append(contentsOf: other.additionalAllowedWritableDirectories)
if other.allowNetworkConnections != .none {
merged.allowNetworkConnections = other.allowNetworkConnections
}
if other.packageIdentity != nil {
merged.packageIdentity = other.packageIdentity
}
return merged
}
}