Skip to content

Commit 667022a

Browse files
author
Miguel Perez
committed
Updates from feedback
1 parent 9ce6e16 commit 667022a

File tree

3 files changed

+71
-86
lines changed

3 files changed

+71
-86
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public struct SwiftPackageTool: ParsableCommand {
3838
var swiftOptions: SwiftToolOptions
3939

4040
public init() {}
41-
42-
public static var _errorLabel: String { "error" }
4341
}
4442

4543
extension SwiftPackageTool {
@@ -76,8 +74,10 @@ extension SwiftPackageTool {
7674
]
7775

7876
if InitPackage.createPackageMode == .new {
79-
subCommands.insert(Create.self, at: 6)
80-
subCommands.insert(AddTemplate.self, at: 7)
77+
if let index = subCommands.firstIndex(where: { $0 == Init.self }) {
78+
subCommands.insert(Create.self, at: index + 1)
79+
subCommands.insert(AddTemplate.self, at: index + 2)
80+
}
8181
}
8282

8383
return subCommands
@@ -300,7 +300,7 @@ extension SwiftPackageTool {
300300
@OptionGroup(_hiddenFromHelp: true)
301301
var swiftOptions: SwiftToolOptions
302302

303-
@Argument(help: "URL to template (path to template works too)")
303+
@Argument(help: "URL to template (absolute path to template works too)")
304304
var url: String
305305

306306
@Option(help: "Custom template name")

Sources/Workspace/InitPackage.swift

Lines changed: 63 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,6 @@ public final class InitPackage {
8383
moduleName
8484
}
8585
}
86-
87-
public convenience init(fileSystem: FileSystem,
88-
configPath: AbsolutePath,
89-
destinationPath: AbsolutePath,
90-
name: String,
91-
packageType: PackageType
92-
) throws {
93-
try self.init(fileSystem: fileSystem,
94-
configPath: configPath,
95-
destinationPath: destinationPath,
96-
mode: .initialize,
97-
packageName: name,
98-
packageType: packageType,
99-
packageTemplateName: nil)
100-
}
10186

10287
/// Create an instance that can create a package with given arguments.
10388
public init(fileSystem: FileSystem,
@@ -139,22 +124,22 @@ public final class InitPackage {
139124

140125
private func makePackageNew() throws {
141126
let templateHomeDirectory = configPath.appending(components: "templates", "new-package")
142-
var templateToUse: String?
127+
var template: String?
143128

144129
if let templateName = packageTemplateName {
145130
guard fileSystem.exists(templateHomeDirectory.appending(component: templateName)) else {
146131
throw InternalError("Could not find template folder: \(templateHomeDirectory.appending(component: templateName))")
147132
}
148133

149-
templateToUse = templateName
134+
template = templateName
150135
} else {
151136
// Checking if a default template is present
152137
if fileSystem.exists(templateHomeDirectory.appending(components: "default")) {
153-
templateToUse = "default"
138+
template = "default"
154139
}
155140
}
156141

157-
if let templateName = templateToUse {
142+
if let templateName = template {
158143
try fileSystem.getDirectoryContents(templateHomeDirectory.appending(component: templateName)).forEach {
159144
progressReporter?("Copying \($0)")
160145
try copyTemplate(fileSystem: fileSystem,
@@ -177,6 +162,65 @@ public final class InitPackage {
177162
try writePackageStructure(template: template)
178163
}
179164

165+
private func getSwiftPMDefaultTemplate(packageType: InitPackage.PackageType,
166+
sources: RelativePath = .init("./Sources"),
167+
tests: RelativePath? = nil,
168+
createSubDirectoryForModule: Bool = false) -> InitPackage.PackageTemplate {
169+
// Even if we are making a "classic" package that doesn't use a template we should till use templates
170+
// for consistency within the codebase
171+
switch InitPackage.createPackageMode {
172+
case .new:
173+
return InitPackage.PackageTemplate(sourcesDirectory: sources,
174+
testsDirectory: tests,
175+
createSubDirectoryForModule: createSubDirectoryForModule,
176+
packageType: packageType)
177+
case .legacy:
178+
return InitPackage.PackageTemplate(sourcesDirectory: RelativePath("./Sources"),
179+
testsDirectory: RelativePath("./Tests"),
180+
createSubDirectoryForModule: true,
181+
packageType: packageType)
182+
}
183+
}
184+
185+
private func copyTemplate(fileSystem: FileSystem, sourcePath: AbsolutePath, destinationPath: AbsolutePath, name: String) throws {
186+
// Recursively copy the template package
187+
// Currently only replaces the string literal "$NAME"
188+
if fileSystem.isDirectory(sourcePath) {
189+
if let dirName = sourcePath.pathString.split(separator: "/").last {
190+
if !fileSystem.exists(destinationPath.appending(component: String(dirName))) {
191+
try fileSystem.createDirectory(destinationPath.appending(component: String(dirName)))
192+
}
193+
194+
try fileSystem.getDirectoryContents(sourcePath).forEach {
195+
try copyTemplate(fileSystem: fileSystem,
196+
sourcePath: sourcePath.appending(component: $0),
197+
destinationPath: destinationPath.appending(components: String(dirName)),
198+
name: name)
199+
}
200+
}
201+
} else {
202+
let fileContents = try fileSystem.readFileContents(sourcePath)
203+
204+
if let validDescription = fileContents.validDescription {
205+
if let fileName = sourcePath.pathString.split(separator: "/").last {
206+
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
207+
var renamed = validDescription.replacingOccurrences(of: "___NAME___", with: name)
208+
renamed = renamed.replacingOccurrences(of: "___NAME_AS_C99___", with: name.spm_mangledToC99ExtendedIdentifier())
209+
210+
try fileSystem.writeFileContents(destinationPath.appending(component: String(fileName))) { $0 <<< renamed }
211+
}
212+
}
213+
} else {
214+
// This else takes care of things such as images
215+
if let fileName = sourcePath.pathString.split(separator: "/").last {
216+
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
217+
try fileSystem.copy(from: sourcePath, to: destinationPath.appending(component: String(fileName)))
218+
}
219+
}
220+
}
221+
}
222+
}
223+
180224
/// Actually creates the new package at the destinationPath
181225
private func writePackageStructure(template: PackageTemplate) throws {
182226
progressReporter?("Creating \(template.packageType) package: \(packageName)")
@@ -521,65 +565,6 @@ public final class InitPackage {
521565
try writeExecutableTestsFile(testClassFile)
522566
}
523567
}
524-
525-
private func getSwiftPMDefaultTemplate(packageType: InitPackage.PackageType,
526-
sources: RelativePath = .init("./Sources"),
527-
tests: RelativePath? = nil,
528-
createSubDirectoryForModule: Bool = false) -> InitPackage.PackageTemplate {
529-
// Even if we are making a "classic" package that doesn't use a template we should till use templates
530-
// for consistency within the codebase
531-
switch InitPackage.createPackageMode {
532-
case .new:
533-
return InitPackage.PackageTemplate(sourcesDirectory: sources,
534-
testsDirectory: tests,
535-
createSubDirectoryForModule: createSubDirectoryForModule,
536-
packageType: packageType)
537-
case .legacy:
538-
return InitPackage.PackageTemplate(sourcesDirectory: RelativePath("./Sources"),
539-
testsDirectory: RelativePath("./Tests"),
540-
createSubDirectoryForModule: true,
541-
packageType: packageType)
542-
}
543-
}
544-
545-
private func copyTemplate(fileSystem: FileSystem, sourcePath: AbsolutePath, destinationPath: AbsolutePath, name: String) throws {
546-
// Recursively copy the template package
547-
// Currently only replaces the string literal "$NAME"
548-
if fileSystem.isDirectory(sourcePath) {
549-
if let fileName = sourcePath.pathString.split(separator: "/").last {
550-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
551-
try fileSystem.createDirectory(destinationPath.appending(component: String(fileName)))
552-
}
553-
554-
try fileSystem.getDirectoryContents(sourcePath).forEach {
555-
try copyTemplate(fileSystem: fileSystem,
556-
sourcePath: sourcePath.appending(component: $0),
557-
destinationPath: destinationPath.appending(components: String(fileName)),
558-
name: name)
559-
}
560-
}
561-
} else {
562-
let fileContents = try fileSystem.readFileContents(sourcePath)
563-
564-
if let validDescription = fileContents.validDescription {
565-
var renamed = validDescription.replacingOccurrences(of: "___NAME___", with: name)
566-
renamed = renamed.replacingOccurrences(of: "___NAME_AS_C99___", with: name.spm_mangledToC99ExtendedIdentifier())
567-
568-
if let fileName = sourcePath.pathString.split(separator: "/").last {
569-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
570-
try fileSystem.writeFileContents(destinationPath.appending(component: String(fileName))) { $0 <<< renamed }
571-
}
572-
}
573-
} else {
574-
// This else takes care of things such as images
575-
if let fileName = sourcePath.pathString.split(separator: "/").last {
576-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
577-
try fileSystem.copy(from: sourcePath, to: destinationPath.appending(component: String(fileName)))
578-
}
579-
}
580-
}
581-
}
582-
}
583568

584569
// TEMP
585570
public enum Mode {

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ final class PackageToolTests: XCTestCase {
10101010
try execute(["config", "get-mirror", "--original-url", "[email protected]:apple/swift-package-manager.git"], packagePath: packageRoot)
10111011
}
10121012

1013-
check(stderr: "error: mirror not found\n") {
1013+
check(stderr: "Error: mirror not found\n") {
10141014
try execute(["config", "unset-mirror", "--original-url", "foo"], packagePath: packageRoot)
10151015
}
10161016
}
@@ -1097,7 +1097,7 @@ final class PackageToolTests: XCTestCase {
10971097
XCTAssertEqual(result.exitStatus, .terminated(code: 1))
10981098

10991099
let stderrOutput = try result.utf8stderrOutput()
1100-
XCTAssert(stderrOutput.contains("error: root manifest not found"), #"actual: "\#(stderrOutput)""#)
1100+
XCTAssert(stderrOutput.contains("Error: root manifest not found"), #"actual: "\#(stderrOutput)""#)
11011101
}
11021102

11031103
// Runnning with output as absolute path to existing directory
@@ -1108,7 +1108,7 @@ final class PackageToolTests: XCTestCase {
11081108

11091109
let stderrOutput = try result.utf8stderrOutput()
11101110
XCTAssert(
1111-
stderrOutput.contains("error: Couldn’t create an archive:") &&
1111+
stderrOutput.contains("Error: Couldn’t create an archive:") &&
11121112
stderrOutput.contains("fatal: could not create archive file '/': Is a directory"),
11131113
#"actual: "\#(stderrOutput)""#
11141114
)

0 commit comments

Comments
 (0)