Skip to content

Commit 29fc40a

Browse files
Update init template to follow the 'package init' changes
1 parent bc8025c commit 29fc40a

File tree

3 files changed

+44
-58
lines changed

3 files changed

+44
-58
lines changed

Sources/CartonKit/Model/Template.swift

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ extension Template {
9292
) throws {
9393
try fileSystem.writeFileContents(project.path.appending(component: "Package.swift")) {
9494
var content = """
95-
// swift-tools-version:5.6
95+
// swift-tools-version:5.8
9696
import PackageDescription
9797
let package = Package(
9898
name: "\(project.name)",\n
@@ -111,11 +111,15 @@ extension Template {
111111
.executableTarget(
112112
name: "\(project.name)",
113113
dependencies: [
114+
"\(project.name)Library",
114115
\(targetDepencencies.map(\.description).joined(separator: ",\n"))
115116
]),
117+
.target(
118+
name: "\(project.name)Library",
119+
dependencies: []),
116120
.testTarget(
117-
name: "\(project.name)Tests",
118-
dependencies: ["\(project.name)"]),
121+
name: "\(project.name)LibraryTests",
122+
dependencies: ["\(project.name)Library"]),
119123
]
120124
)
121125
"""
@@ -135,8 +139,15 @@ extension Templates {
135139
project: Project,
136140
_ terminal: InteractiveWriter
137141
) async throws {
142+
// FIXME: We now create an intermediate library target to work around
143+
// an issue that prevents us from testing executable targets on Wasm.
144+
// See https://github.com/swiftwasm/swift/issues/5375
138145
try fileSystem.changeCurrentWorkingDirectory(to: project.path)
139-
try await createPackage(type: .executable, fileSystem: fileSystem, project: project, terminal)
146+
try await createPackage(
147+
type: .library, fileSystem: fileSystem,
148+
project: Project(name: project.name + "Library", path: project.path, inPlace: true),
149+
terminal
150+
)
140151
try createManifest(
141152
fileSystem: fileSystem,
142153
project: project,
@@ -151,6 +162,17 @@ extension Templates {
151162
],
152163
terminal
153164
)
165+
let sources = project.path.appending(component: "Sources")
166+
let executableTarget = sources.appending(component: project.name)
167+
// Create the executable target
168+
try fileSystem.createDirectory(executableTarget)
169+
try fileSystem.writeFileContents(executableTarget.appending(component: "main.swift")) {
170+
"""
171+
import \(project.name.spm_mangledToC99ExtendedIdentifier())Library
172+
print("Hello, world!")
173+
"""
174+
.write(to: $0)
175+
}
154176
}
155177
}
156178
}
@@ -166,9 +188,9 @@ extension Templates {
166188
) async throws {
167189
try fileSystem.changeCurrentWorkingDirectory(to: project.path)
168190
try await createPackage(
169-
type: .executable,
191+
type: .library,
170192
fileSystem: fileSystem,
171-
project: project,
193+
project: Project(name: project.name + "Library", path: project.path, inPlace: true),
172194
terminal)
173195
try createManifest(
174196
fileSystem: fileSystem,
@@ -186,18 +208,13 @@ extension Templates {
186208
terminal
187209
)
188210

189-
let sources = project.path.appending(
190-
components: "Sources",
191-
project.name
192-
)
193-
194-
for source in try fileSystem.getDirectoryContents(sources) {
195-
try fileSystem.removeFileTree(sources.appending(components: source))
196-
}
211+
let sources = project.path.appending(component: "Sources")
212+
let executableTarget = sources.appending(component: project.name)
197213

198-
try fileSystem.writeFileContents(sources.appending(components: "App.swift")) {
214+
try fileSystem.writeFileContents(executableTarget.appending(components: "App.swift")) {
199215
"""
200216
import TokamakDOM
217+
import \(project.name.spm_mangledToC99ExtendedIdentifier())Library
201218
202219
@main
203220
struct TokamakApp: App {

Sources/SwiftToolchain/Toolchain.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,11 @@ public final class Toolchain {
390390
}
391391

392392
public func runPackageInit(name: String, type: PackageType, inPlace: Bool) async throws {
393-
var initArgs = [
393+
let initArgs = [
394394
swiftPath.pathString, "package", "init",
395395
"--type", type.rawValue,
396+
"--name", name
396397
]
397-
if !inPlace {
398-
initArgs.append(contentsOf: ["--name", name])
399-
}
400398
try await TSCBasic.Process.run(initArgs, terminal)
401399
}
402400

Tests/CartonCommandTests/InitCommandTests.swift

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,24 @@ final class InitCommandTests: XCTestCase {
3232

3333
// Confirm that the files are actually in the folder
3434
XCTAssertTrue(packageDirectory.ls().contains("Package.swift"), "Package.swift does not exist")
35-
XCTAssertTrue(packageDirectory.ls().contains("README.md"), "README.md does not exist")
3635
XCTAssertTrue(packageDirectory.ls().contains(".gitignore"), ".gitignore does not exist")
3736
XCTAssertTrue(packageDirectory.ls().contains("Sources"), "Sources does not exist")
3837
XCTAssertTrue(
3938
packageDirectory.ls().contains("Sources/\(package)"),
4039
"Sources/\(package) does not exist"
4140
)
4241
XCTAssertTrue(
43-
packageDirectory.ls().contains("Sources/\(package)/\(package).swift"),
44-
"Sources/\(package)/\(package).swift does not exist"
42+
packageDirectory.ls().contains("Sources/\(package)/main.swift"),
43+
"Sources/\(package)/main.swift does not exist"
4544
)
4645
XCTAssertTrue(packageDirectory.ls().contains("Tests"), "Tests does not exist")
4746
XCTAssertTrue(
48-
packageDirectory.ls().contains("Tests/\(package)Tests"),
49-
"Tests/\(package)Tests does not exist"
47+
packageDirectory.ls().contains("Tests/\(package)LibraryTests"),
48+
"Tests/\(package)LibraryTests does not exist"
5049
)
5150
XCTAssertTrue(
52-
packageDirectory.ls().contains("Tests/\(package)Tests/\(package)Tests.swift"),
53-
"Tests/\(package)Tests/\(package)Tests.swift does not exist"
51+
packageDirectory.ls().contains("Tests/\(package)LibraryTests/\(package)LibraryTests.swift"),
52+
"Tests/\(package)LibraryTests/\(package)LibraryTests.swift does not exist"
5453
)
5554
}
5655
}
@@ -67,7 +66,6 @@ final class InitCommandTests: XCTestCase {
6766

6867
// Confirm that the files are actually in the folder
6968
XCTAssertTrue(packageDirectory.ls().contains("Package.swift"), "Package.swift does not exist")
70-
XCTAssertTrue(packageDirectory.ls().contains("README.md"), "README.md does not exist")
7169
XCTAssertTrue(packageDirectory.ls().contains(".gitignore"), ".gitignore does not exist")
7270
XCTAssertTrue(packageDirectory.ls().contains("Sources"), "Sources does not exist")
7371
XCTAssertTrue(
@@ -80,40 +78,13 @@ final class InitCommandTests: XCTestCase {
8078
)
8179
XCTAssertTrue(packageDirectory.ls().contains("Tests"), "Tests does not exist")
8280
XCTAssertTrue(
83-
packageDirectory.ls().contains("Tests/\(package)Tests"),
84-
"Tests/\(package)Tests does not exist"
81+
packageDirectory.ls().contains("Tests/\(package)LibraryTests"),
82+
"Tests/\(package)LibraryTests does not exist"
8583
)
8684
XCTAssertTrue(
87-
packageDirectory.ls().contains("Tests/\(package)Tests/\(package)Tests.swift"),
88-
"Tests/\(package)Tests/\(package)Tests.swift does not exist"
85+
packageDirectory.ls().contains("Tests/\(package)LibraryTests/\(package)LibraryTests.swift"),
86+
"Tests/\(package)LibraryTests/\(package)LibraryTests.swift does not exist"
8987
)
90-
91-
let actualTemplateSource = try String(
92-
contentsOfFile:
93-
packageDirectory
94-
.appending(components: "Sources", package, "App.swift").pathString)
95-
96-
XCTAssertEqual(expectedTemplateSource, actualTemplateSource, "Template Sources do not match")
9788
}
9889
}
99-
100-
let expectedTemplateSource =
101-
"""
102-
import TokamakDOM
103-
104-
@main
105-
struct TokamakApp: App {
106-
var body: some Scene {
107-
WindowGroup("Tokamak App") {
108-
ContentView()
109-
}
110-
}
111-
}
112-
113-
struct ContentView: View {
114-
var body: some View {
115-
Text("Hello, world!")
116-
}
117-
}
118-
"""
11990
}

0 commit comments

Comments
 (0)