Skip to content

Commit ee259bc

Browse files
author
Miguel Perez
committed
More fixes and tests for funny names
The funny name is just a folder that is space separated
1 parent f668f6a commit ee259bc

File tree

10 files changed

+108
-45
lines changed

10 files changed

+108
-45
lines changed

Fixtures/Templates/AddTemplate/GoodTemplate/.gitignore

Lines changed: 0 additions & 7 deletions
This file was deleted.

Fixtures/Templates/AddTemplateErrors/MissingManifest/.gitignore

Lines changed: 0 additions & 7 deletions
This file was deleted.

Fixtures/Templates/MakePackageWithTemplate/templates/new-package/ExeTemplate/.gitignore

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
products: [
9+
// Products define the executables and libraries a package produces, and make them visible to other packages.
10+
.library(
11+
name: "___NAME___",
12+
targets: ["___NAME___"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21+
.target(
22+
name: "___NAME___",
23+
dependencies: [],
24+
path: "Sources"),
25+
]
26+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# LibTemplate
2+
3+
A description of this package.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public struct ___NAME_AS_C99___ {
2+
public private(set) var text = "Hello, World!"
3+
4+
public init() {
5+
}
6+
}

Fixtures/Templates/MakePackageWithTemplate/templates/new-package/TestTemplate/Sources/___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() {

Sources/Commands/SwiftPackageTool.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ extension SwiftPackageTool {
226226

227227
func run(_ swiftTool: SwiftTool) throws {
228228
guard let cwd = localFileSystem.currentWorkingDirectory else {
229-
throw MakePackageErrors.currentWorkingDirectoryNotFound
229+
throw InternalError("\(MakePackageErrors.currentWorkingDirectoryNotFound)")
230230
}
231231

232232
guard let configPath = try swiftTool.getConfigPath() else {
233-
throw MakePackageErrors.configPathNotFound
233+
throw InternalError("\(MakePackageErrors.configPathNotFound)")
234234
}
235235

236236
guard packageType == nil || template == nil else {
@@ -270,11 +270,11 @@ extension SwiftPackageTool {
270270

271271
func run(_ swiftTool: SwiftTool) throws {
272272
guard let cwd = localFileSystem.currentWorkingDirectory else {
273-
throw MakePackageErrors.currentWorkingDirectoryNotFound
273+
throw InternalError("\(MakePackageErrors.currentWorkingDirectoryNotFound)")
274274
}
275275

276276
guard let configPath = try swiftTool.getConfigPath() else {
277-
throw MakePackageErrors.configPathNotFound
277+
throw InternalError("\(MakePackageErrors.configPathNotFound)")
278278
}
279279

280280
guard packageType == nil || template == nil else {
@@ -311,7 +311,7 @@ extension SwiftPackageTool {
311311

312312
func run(_ swiftTool: SwiftTool) throws {
313313
guard let configPath = try swiftTool.getConfigPath() else {
314-
throw InternalError("Could not find config path")
314+
throw InternalError("\(MakePackageErrors.configPathNotFound)")
315315
}
316316

317317
let templatePath: AbsolutePath
@@ -324,15 +324,15 @@ extension SwiftPackageTool {
324324
throw StringError("\(path) is not a valid directory")
325325
}
326326

327-
templatePath = configPath.withTemplate(template: [name ?? path.basename])
327+
templatePath = configPath.withTemplate(template: name ?? path.basename)
328328
try localFileSystem.copy(from: path, to: templatePath)
329329
} else {
330330
let provider = GitRepositoryProvider()
331331

332332
if let templateName = name {
333-
templatePath = configPath.withTemplate(template: [templateName])
333+
templatePath = configPath.withTemplate(template: templateName)
334334
} else if let templateName = url.split(separator: "/").last?.replacingOccurrences(of: ".git", with: "").flatMap({ String($0) }) {
335-
templatePath = configPath.withTemplate(template: [String(templateName)])
335+
templatePath = configPath.withTemplate(template: String(templateName))
336336
} else {
337337
throw InternalError("Could not determine template name")
338338
}
@@ -359,15 +359,15 @@ extension SwiftPackageTool {
359359

360360
func run(_ swiftTool: SwiftTool) throws {
361361
guard let configPath = try swiftTool.getConfigPath() else {
362-
throw InternalError("Could not find config path")
362+
throw InternalError("\(MakePackageErrors.configPathNotFound)")
363363
}
364364

365-
guard localFileSystem.exists(configPath.withTemplate(template: [templateName])) else {
366-
throw StringError("Could not find template: \(templateName)")
365+
guard localFileSystem.exists(configPath.withTemplate(template: templateName)) else {
366+
throw MakePackageErrors.templateNotFound(configPath.withTemplate(template: templateName))
367367
}
368368

369369
let provider = GitRepositoryProvider()
370-
let templatePath = configPath.withTemplate(template: [templateName])
370+
let templatePath = configPath.withTemplate(template: templateName)
371371

372372
guard provider.isValidDirectory(templatePath.pathString) else {
373373
throw StringError("Template: \(templateName) is not a git repo, and therefore could not be updated")

Sources/Workspace/InitPackage.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ public final class InitPackage {
136136
var templateName: String?
137137

138138
if let template = packageTemplateName {
139-
guard fileSystem.exists(configPath.withTemplate(template: [template])) else {
140-
throw InternalError("Could not find template folder: \(configPath.withTemplate(template: [template]))")
139+
guard fileSystem.exists(configPath.withTemplate(template: template)) else {
140+
throw InternalError("Could not find template folder: \(configPath.withTemplate(template: template))")
141141
}
142142

143143
templateName = template
144144
} else {
145145
// Checking if a default template is present
146-
if fileSystem.exists(configPath.withTemplate(template: ["default"])) {
146+
if fileSystem.exists(configPath.withTemplate(template: "default")) {
147147
templateName = "default"
148148
// There is a guard preventing '--type' to be used in conjunction with '--template'
149149
// In the event that the defualt template is present and '--type' was used we'll infrom
@@ -155,10 +155,10 @@ public final class InitPackage {
155155
}
156156

157157
if let template = templateName {
158-
try fileSystem.getDirectoryContents(configPath.withTemplate(template: [template])).forEach {
158+
try fileSystem.getDirectoryContents(configPath.withTemplate(template: template)).forEach {
159159
progressReporter?("Copying \($0)")
160160
try copyTemplate(fileSystem: fileSystem,
161-
sourcePath: configPath.withTemplate(template: [template, $0]),
161+
sourcePath: configPath.withTemplate(template: template).appending(component: $0),
162162
destinationPath: destinationPath,
163163
name: packageName)
164164
}
@@ -592,10 +592,8 @@ public final class InitPackage {
592592
}
593593

594594
extension AbsolutePath {
595-
public func withTemplate(template: [String]) -> AbsolutePath {
596-
var components = ["templates", "new-package"]
597-
components += template
598-
return appending(components: components)
595+
public func withTemplate(template: String) -> AbsolutePath {
596+
return appending(components: "templates", "new-package", template)
599597
}
600598
}
601599

Tests/WorkspaceTests/TemplateTests.swift

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,60 @@ class TemplateTests: XCTestCase {
144144
}
145145
}
146146

147+
func testCreateTemplateFunnyName() throws {
148+
try XCTSkipIf(InitPackage.createPackageMode == .legacy)
149+
fixture(name: "Templates/MakePackageWithTemplate") { prefix in
150+
try testWithTemporaryDirectory { tmpPath in
151+
let fs = localFileSystem
152+
let path = tmpPath.appending(component: "Foo Bar")
153+
154+
let _ = try SwiftPMProduct.SwiftPackage.executeProcess(["create", "Foo Bar", "--template", "TestTemplate", "--config-path", prefix.pathString], packagePath: tmpPath)
155+
156+
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
157+
XCTAssert(fs.exists(path.appending(components: "Sources", "Foo Bar.swift")))
158+
159+
XCTAssertBuilds(path)
160+
}
161+
}
162+
}
163+
164+
func testInitTemplateFunnyName() throws {
165+
try XCTSkipIf(InitPackage.createPackageMode == .legacy)
166+
fixture(name: "Templates/MakePackageWithTemplate") { prefix in
167+
try testWithTemporaryDirectory { tmpPath in
168+
let fs = localFileSystem
169+
let path = tmpPath.appending(component: "Foo Bar")
170+
try fs.createDirectory(path)
171+
172+
let _ = try SwiftPMProduct.SwiftPackage.executeProcess(["init", "--template", "LibTemplate","--config-path", prefix.pathString], packagePath: path)
173+
174+
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
175+
XCTAssert(fs.exists(path.appending(components: "Sources", "Foo Bar.swift")))
176+
177+
if let manifest = try fs.readFileContents(path.appending(component: "Package.swift")).validDescription {
178+
XCTAssert(manifest == transformedManifest(with: "Foo Bar"))
179+
}
180+
181+
if let swiftFile = try fs.readFileContents(path.appending(components: "Sources", "Foo Bar.swift")).validDescription {
182+
XCTAssert(swiftFile == transformedSwiftFile(with: "Foo Bar"))
183+
}
184+
185+
XCTAssertBuilds(path)
186+
}
187+
}
188+
}
189+
147190
private func transformedManifest(with name: String) -> String {
191+
// Currently the manifest will drop the patch version if it's zero
192+
// the description of Version always returns the patch version even if zero
193+
// this is just to drop the patch version
194+
var toolsVersion = "\(ToolsVersion.currentToolsVersion.major).\(ToolsVersion.currentToolsVersion.minor)"
195+
if ToolsVersion.currentToolsVersion.patch != 0 {
196+
toolsVersion += ".\(ToolsVersion.currentToolsVersion.patch)"
197+
}
198+
148199
return """
149-
// swift-tools-version:5.5
200+
// swift-tools-version:\(toolsVersion)
150201
// The swift-tools-version declares the minimum version of Swift required to build this package.
151202
152203
import PackageDescription
@@ -185,7 +236,7 @@ A description of this package.
185236

186237
private func transformedSwiftFile(with name: String) -> String {
187238
return """
188-
public struct \(name) {
239+
public struct \(name.spm_mangledToC99ExtendedIdentifier()) {
189240
public private(set) var text = "Hello, World!"
190241
191242
public init() {

0 commit comments

Comments
 (0)