Skip to content

Commit f668f6a

Browse files
author
Miguel Perez
committed
updates from feedback
1 parent 97a0566 commit f668f6a

File tree

16 files changed

+252
-78
lines changed

16 files changed

+252
-78
lines changed

Fixtures/Templates/AddTemplate/GoodTemplate/Sources/___NAME___/___NAME___.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public struct ___NAME___ {
1+
public struct ___NAME_AS_C99___ {
22
public private(set) var text = "Hello, World!"
33

44
public init() {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import XCTest
2-
@testable import ___NAME___
2+
@testable import ___NAME_AS_C99___
33

4-
final class ___NAME___Tests: XCTestCase {
4+
final class ___NAME_AS_C99___Tests: XCTestCase {
55
func testExample() throws {
66
// This is an example of a functional test case.
77
// Use XCTAssert and related functions to verify your tests produce the correct
88
// results.
9-
XCTAssertEqual(___NAME___().text, "Hello, World!")
9+
XCTAssertEqual(___NAME_AS_C99___().text, "Hello, World!")
1010
}
1111
}

Fixtures/Templates/AddTemplateErrors/MissingManifest/Sources/___NAME___/___NAME___.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public struct ___NAME___ {
1+
public struct ___NAME_AS_C99___ {
22
public private(set) var text = "Hello, World!"
33

44
public init() {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import XCTest
2-
@testable import ___NAME___
2+
@testable import ___NAME_AS_C99___
33

4-
final class ___NAME___Tests: XCTestCase {
4+
final class ___NAME_AS_C99___Tests: XCTestCase {
55
func testExample() throws {
66
// This is an example of a functional test case.
77
// Use XCTAssert and related functions to verify your tests produce the correct
88
// results.
9-
XCTAssertEqual(___NAME___().text, "Hello, World!")
9+
XCTAssertEqual(___NAME_AS_C99___().text, "Hello, World!")
1010
}
1111
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "___NAME___",
8+
dependencies: [
9+
// Dependencies declare other packages that this package depends on.
10+
// .package(url: /* package url */, from: "1.0.0"),
11+
],
12+
targets: [
13+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
14+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
15+
.executableTarget(
16+
name: "___NAME___",
17+
dependencies: [],
18+
path: "src"),
19+
]
20+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ___NAME___
2+
3+
A description of this package.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello, world!")

Fixtures/Templates/MakePackageWithTemplate/templates/new-package/TestTemplate/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ let package = Package(
2323
dependencies: [],
2424
path: "Sources"),
2525
]
26-
)
26+
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# ___NAME___
22

3-
A description of this package.
3+
A description of this package.

Fixtures/Templates/MakePackageWithTemplate/templates/new-package/TestTemplate/Sources/___NAME___.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ public struct ___NAME___ {
33

44
public init() {
55
}
6-
}
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "___NAME___",
8+
dependencies: [
9+
// Dependencies declare other packages that this package depends on.
10+
// .package(url: /* package url */, from: "1.0.0"),
11+
],
12+
targets: [
13+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
14+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
15+
.executableTarget(
16+
name: "___NAME___",
17+
dependencies: [],
18+
path: "src"),
19+
]
20+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello, world!")

Sources/Commands/SwiftPackageTool.swift

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct SwiftPackageTool: ParsableCommand {
3131
abstract: "Perform operations on Swift packages",
3232
discussion: "SEE ALSO: swift build, swift run, swift test",
3333
version: SwiftVersion.currentVersion.completeDisplayString,
34-
subcommands: getSubCommands(),
34+
subcommands: subCommands,
3535
helpNames: [.short, .long, .customLong("help", withSingleDash: true)])
3636

3737
@OptionGroup()
@@ -43,45 +43,47 @@ public struct SwiftPackageTool: ParsableCommand {
4343
extension SwiftPackageTool {
4444
// This way the feature flag can control if the new subcommads
4545
// are visible
46-
static func getSubCommands() -> [ParsableCommand.Type] {
47-
var subCommands: [ParsableCommand.Type] = [
48-
Clean.self,
49-
PurgeCache.self,
50-
Reset.self,
51-
Update.self,
52-
Describe.self,
53-
Init.self,
54-
Format.self,
55-
56-
APIDiff.self,
57-
DumpSymbolGraph.self,
58-
DumpPIF.self,
59-
DumpPackage.self,
60-
61-
Edit.self,
62-
Unedit.self,
63-
64-
Config.self,
65-
Resolve.self,
66-
Fetch.self,
46+
static var subCommands: [ParsableCommand.Type] {
47+
get {
48+
var commands: [ParsableCommand.Type] = [
49+
Clean.self,
50+
PurgeCache.self,
51+
Reset.self,
52+
Update.self,
53+
Describe.self,
54+
Init.self,
55+
Format.self,
56+
57+
APIDiff.self,
58+
DumpSymbolGraph.self,
59+
DumpPIF.self,
60+
DumpPackage.self,
61+
62+
Edit.self,
63+
Unedit.self,
64+
65+
Config.self,
66+
Resolve.self,
67+
Fetch.self,
68+
69+
ShowDependencies.self,
70+
ToolsVersionCommand.self,
71+
GenerateXcodeProject.self,
72+
ComputeChecksum.self,
73+
ArchiveSource.self,
74+
CompletionTool.self,
75+
]
6776

68-
ShowDependencies.self,
69-
ToolsVersionCommand.self,
70-
GenerateXcodeProject.self,
71-
ComputeChecksum.self,
72-
ArchiveSource.self,
73-
CompletionTool.self,
74-
]
75-
76-
if InitPackage.createPackageMode == .new {
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-
subCommands.insert(UpdateTemplate.self, at: index + 3)
77+
if InitPackage.createPackageMode == .new {
78+
if let index = commands.firstIndex(where: { $0 == Init.self }) {
79+
commands.insert(Create.self, at: index + 1)
80+
commands.insert(AddTemplate.self, at: index + 2)
81+
commands.insert(UpdateTemplate.self, at: index + 3)
82+
}
8183
}
84+
85+
return commands
8286
}
83-
84-
return subCommands
8587
}
8688
}
8789

@@ -224,15 +226,15 @@ extension SwiftPackageTool {
224226

225227
func run(_ swiftTool: SwiftTool) throws {
226228
guard let cwd = localFileSystem.currentWorkingDirectory else {
227-
throw InternalError("Could not find the current working directory.")
229+
throw MakePackageErrors.currentWorkingDirectoryNotFound
228230
}
229231

230232
guard let configPath = try swiftTool.getConfigPath() else {
231-
throw InternalError("Could not find config path")
233+
throw MakePackageErrors.configPathNotFound
232234
}
233235

234-
guard !(packageType != nil && template != nil) else {
235-
throw InternalError("Can't use --type in conjunction with --template")
236+
guard packageType == nil || template == nil else {
237+
throw MakePackageErrors.cannotUseTypeWithTemplate
236238
}
237239

238240
let initPackage = try InitPackage(fileSystem: localFileSystem,
@@ -268,15 +270,15 @@ extension SwiftPackageTool {
268270

269271
func run(_ swiftTool: SwiftTool) throws {
270272
guard let cwd = localFileSystem.currentWorkingDirectory else {
271-
throw InternalError("Could not find the current working directory.")
273+
throw MakePackageErrors.currentWorkingDirectoryNotFound
272274
}
273275

274276
guard let configPath = try swiftTool.getConfigPath() else {
275-
throw InternalError("Could not find config path")
277+
throw MakePackageErrors.configPathNotFound
276278
}
277279

278-
guard !(packageType != nil && template != nil) else {
279-
throw InternalError("Can't use --type in conjunction with --template")
280+
guard packageType == nil || template == nil else {
281+
throw MakePackageErrors.cannotUseTypeWithTemplate
280282
}
281283

282284
let initPackage = try InitPackage(fileSystem: localFileSystem,
@@ -315,22 +317,22 @@ extension SwiftPackageTool {
315317
let templatePath: AbsolutePath
316318
if let path = try? AbsolutePath(validating: url) {
317319
guard localFileSystem.exists(path) else {
318-
throw StringError("Could not find template: \(path)")
320+
throw MakePackageErrors.templateNotFound(path)
319321
}
320322

321323
guard localFileSystem.isDirectory(path) else {
322324
throw StringError("\(path) is not a valid directory")
323325
}
324326

325-
templatePath = configPath.appending(components: "templates", "new-package", name ?? path.basename)
327+
templatePath = configPath.withTemplate(template: [name ?? path.basename])
326328
try localFileSystem.copy(from: path, to: templatePath)
327329
} else {
328330
let provider = GitRepositoryProvider()
329331

330332
if let templateName = name {
331-
templatePath = configPath.appending(components: "templates", "new-package", templateName)
333+
templatePath = configPath.withTemplate(template: [templateName])
332334
} else if let templateName = url.split(separator: "/").last?.replacingOccurrences(of: ".git", with: "").flatMap({ String($0) }) {
333-
templatePath = configPath.appending(components: "templates", "new-package", String(templateName))
335+
templatePath = configPath.withTemplate(template: [String(templateName)])
334336
} else {
335337
throw InternalError("Could not determine template name")
336338
}
@@ -360,12 +362,12 @@ extension SwiftPackageTool {
360362
throw InternalError("Could not find config path")
361363
}
362364

363-
guard localFileSystem.exists(configPath.appending(components: "templates", "new-package", templateName)) else {
365+
guard localFileSystem.exists(configPath.withTemplate(template: [templateName])) else {
364366
throw StringError("Could not find template: \(templateName)")
365367
}
366368

367369
let provider = GitRepositoryProvider()
368-
let templatePath = configPath.appending(components: "templates", "new-package", templateName)
370+
let templatePath = configPath.withTemplate(template: [templateName])
369371

370372
guard provider.isValidDirectory(templatePath.pathString) else {
371373
throw StringError("Template: \(templateName) is not a git repo, and therefore could not be updated")
@@ -893,6 +895,26 @@ extension SwiftPackageTool {
893895
}
894896
}
895897

898+
private enum MakePackageErrors: Swift.Error, CustomStringConvertible {
899+
case currentWorkingDirectoryNotFound
900+
case configPathNotFound
901+
case templateNotFound(AbsolutePath)
902+
case cannotUseTypeWithTemplate
903+
904+
var description: String {
905+
switch self {
906+
case .currentWorkingDirectoryNotFound:
907+
return "Could not find the current working directory."
908+
case .configPathNotFound:
909+
return "Could not find the config path."
910+
case .templateNotFound(let path):
911+
return "Could not find template: \(path)."
912+
case .cannotUseTypeWithTemplate:
913+
return "When using templates the package's type is derived from the template."
914+
}
915+
}
916+
}
917+
896918
extension SwiftPackageTool {
897919
struct GenerateXcodeProject: SwiftCommand {
898920
static let configuration = CommandConfiguration(

Sources/Workspace/InitPackage.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,15 @@ public final class InitPackage {
8080
let suppliedPackageType: Bool
8181

8282
private var moduleName: String {
83-
get { packageName.spm_mangledToC99ExtendedIdentifier() }
83+
get {
84+
packageName.spm_mangledToC99ExtendedIdentifier()
85+
}
8486
}
8587

8688
private var typeName: String {
87-
get { moduleName }
89+
get {
90+
moduleName
91+
}
8892
}
8993

9094
/// Create an instance that can create a package with given arguments.
@@ -129,33 +133,32 @@ public final class InitPackage {
129133
}
130134

131135
private func makePackageNew() throws {
132-
let templateHomeDirectory = configPath.appending(components: "templates", "new-package")
133136
var templateName: String?
134137

135138
if let template = packageTemplateName {
136-
guard fileSystem.exists(templateHomeDirectory.appending(component: template)) else {
137-
throw InternalError("Could not find template folder: \(templateHomeDirectory.appending(component: template))")
139+
guard fileSystem.exists(configPath.withTemplate(template: [template])) else {
140+
throw InternalError("Could not find template folder: \(configPath.withTemplate(template: [template]))")
138141
}
139142

140143
templateName = template
141144
} else {
142145
// Checking if a default template is present
143-
if fileSystem.exists(templateHomeDirectory.appending(component: "default")) {
146+
if fileSystem.exists(configPath.withTemplate(template: ["default"])) {
144147
templateName = "default"
145148
// There is a guard preventing '--type' to be used in conjunction with '--template'
146149
// In the event that the defualt template is present and '--type' was used we'll infrom
147150
// the user that the package type is coming from the template and not their supplied type.
148-
if suppliedPackageType {
149-
progressReporter?("Package type is defined by the template 'default'.")
151+
guard !suppliedPackageType else {
152+
throw StringError("Can't use '--type' when the 'default' template is defined")
150153
}
151154
}
152155
}
153156

154157
if let template = templateName {
155-
try fileSystem.getDirectoryContents(templateHomeDirectory.appending(component: template)).forEach {
158+
try fileSystem.getDirectoryContents(configPath.withTemplate(template: [template])).forEach {
156159
progressReporter?("Copying \($0)")
157160
try copyTemplate(fileSystem: fileSystem,
158-
sourcePath: templateHomeDirectory.appending(components: template, $0),
161+
sourcePath: configPath.withTemplate(template: [template, $0]),
159162
destinationPath: destinationPath,
160163
name: packageName)
161164
}
@@ -588,6 +591,14 @@ public final class InitPackage {
588591
}
589592
}
590593

594+
extension AbsolutePath {
595+
public func withTemplate(template: [String]) -> AbsolutePath {
596+
var components = ["templates", "new-package"]
597+
components += template
598+
return appending(components: components)
599+
}
600+
}
601+
591602
// Private helpers
592603

593604
private enum InitError: Swift.Error {

0 commit comments

Comments
 (0)