Skip to content

Commit 9201534

Browse files
committed
[PackageCommands] AddSetting: Extract editing logic into a method so it could be shared with other commands
1 parent a956af4 commit 9201534

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

Sources/Commands/PackageCommands/AddSetting.swift

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,47 @@ extension SwiftPackageCommand {
6767
}
6868

6969
func run(_ swiftCommandState: SwiftCommandState) throws {
70+
if !self._swiftSettings.isEmpty {
71+
try Self.editSwiftSettings(
72+
of: self.target,
73+
using: swiftCommandState,
74+
self.swiftSettings,
75+
verbose: !self.globalOptions.logging.quiet
76+
)
77+
}
78+
}
79+
80+
package static func editSwiftSettings(
81+
of target: String,
82+
using swiftCommandState: SwiftCommandState,
83+
_ settings: [(SwiftSetting, String)],
84+
verbose: Bool = false
85+
) throws {
7086
let workspace = try swiftCommandState.getActiveWorkspace()
7187
guard let packagePath = try swiftCommandState.getWorkspaceRoot().packages.first else {
7288
throw StringError("unknown package")
7389
}
7490

75-
try self.applyEdits(packagePath: packagePath, workspace: workspace)
91+
try self.applyEdits(
92+
packagePath: packagePath,
93+
workspace: workspace,
94+
target: target,
95+
swiftSettings: settings
96+
)
7697
}
7798

78-
private func applyEdits(
99+
private static func applyEdits(
79100
packagePath: Basics.AbsolutePath,
80-
workspace: Workspace
101+
workspace: Workspace,
102+
target: String,
103+
swiftSettings: [(SwiftSetting, String)],
104+
verbose: Bool = false
81105
) throws {
82106
// Load the manifest file
83107
let fileSystem = workspace.fileSystem
84108
let manifestPath = packagePath.appending(component: Manifest.filename)
85109

86-
for (setting, value) in try self.swiftSettings {
110+
for (setting, value) in swiftSettings {
87111
let manifestContents: ByteString
88112
do {
89113
manifestContents = try fileSystem.readFileContents(manifestPath)
@@ -105,13 +129,13 @@ extension SwiftPackageCommand {
105129
switch setting {
106130
case .experimentalFeature:
107131
editResult = try AddSwiftSetting.experimentalFeature(
108-
to: self.target,
132+
to: target,
109133
name: value,
110134
manifest: manifestSyntax
111135
)
112136
case .upcomingFeature:
113137
editResult = try AddSwiftSetting.upcomingFeature(
114-
to: self.target,
138+
to: target,
115139
name: value,
116140
manifest: manifestSyntax
117141
)
@@ -121,7 +145,7 @@ extension SwiftPackageCommand {
121145
}
122146

123147
editResult = try AddSwiftSetting.languageMode(
124-
to: self.target,
148+
to: target,
125149
mode: mode,
126150
manifest: manifestSyntax
127151
)
@@ -131,7 +155,7 @@ extension SwiftPackageCommand {
131155
}
132156

133157
editResult = try AddSwiftSetting.strictMemorySafety(
134-
to: self.target,
158+
to: target,
135159
manifest: manifestSyntax
136160
)
137161
}
@@ -140,7 +164,7 @@ extension SwiftPackageCommand {
140164
to: fileSystem,
141165
manifest: manifestSyntax,
142166
manifestPath: manifestPath,
143-
verbose: !self.globalOptions.logging.quiet
167+
verbose: verbose
144168
)
145169
}
146170
}

0 commit comments

Comments
 (0)