From bc8025cfcbeb3deda6617f5d4e5da44e02e2a8b0 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 4 May 2023 10:44:55 +0000 Subject: [PATCH 1/3] Update default toolchain version to 5.8 channel snapshot --- Dockerfile | 2 +- Sources/CartonHelpers/DefaultToolchain.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index d71f962c..474975d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && ap rm -r /var/lib/apt/lists/* ENV CARTON_ROOT=/root/.carton -ENV CARTON_DEFAULT_TOOLCHAIN=wasm-5.7.1-RELEASE +ENV CARTON_DEFAULT_TOOLCHAIN=wasm-5.8-SNAPSHOT-2023-04-30-a RUN mkdir -p $CARTON_ROOT/sdk && \ mkdir -p $CARTON_ROOT/sdk/$CARTON_DEFAULT_TOOLCHAIN && \ diff --git a/Sources/CartonHelpers/DefaultToolchain.swift b/Sources/CartonHelpers/DefaultToolchain.swift index 415f75cd..b6f1560e 100644 --- a/Sources/CartonHelpers/DefaultToolchain.swift +++ b/Sources/CartonHelpers/DefaultToolchain.swift @@ -12,4 +12,4 @@ // See the License for the specific language governing permissions and // limitations under the License. -public let defaultToolchainVersion = "wasm-5.7.1-RELEASE" +public let defaultToolchainVersion = "wasm-5.8-SNAPSHOT-2023-04-30-a" From 29fc40a7191a43a38506fbf72a804b218641815c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 4 May 2023 14:50:24 +0000 Subject: [PATCH 2/3] Update init template to follow the 'package init' changes --- Sources/CartonKit/Model/Template.swift | 47 ++++++++++++------ Sources/SwiftToolchain/Toolchain.swift | 6 +-- .../CartonCommandTests/InitCommandTests.swift | 49 ++++--------------- 3 files changed, 44 insertions(+), 58 deletions(-) diff --git a/Sources/CartonKit/Model/Template.swift b/Sources/CartonKit/Model/Template.swift index 0b099bf9..f56f27ed 100644 --- a/Sources/CartonKit/Model/Template.swift +++ b/Sources/CartonKit/Model/Template.swift @@ -92,7 +92,7 @@ extension Template { ) throws { try fileSystem.writeFileContents(project.path.appending(component: "Package.swift")) { var content = """ - // swift-tools-version:5.6 + // swift-tools-version:5.8 import PackageDescription let package = Package( name: "\(project.name)",\n @@ -111,11 +111,15 @@ extension Template { .executableTarget( name: "\(project.name)", dependencies: [ + "\(project.name)Library", \(targetDepencencies.map(\.description).joined(separator: ",\n")) ]), + .target( + name: "\(project.name)Library", + dependencies: []), .testTarget( - name: "\(project.name)Tests", - dependencies: ["\(project.name)"]), + name: "\(project.name)LibraryTests", + dependencies: ["\(project.name)Library"]), ] ) """ @@ -135,8 +139,15 @@ extension Templates { project: Project, _ terminal: InteractiveWriter ) async throws { + // FIXME: We now create an intermediate library target to work around + // an issue that prevents us from testing executable targets on Wasm. + // See https://github.com/swiftwasm/swift/issues/5375 try fileSystem.changeCurrentWorkingDirectory(to: project.path) - try await createPackage(type: .executable, fileSystem: fileSystem, project: project, terminal) + try await createPackage( + type: .library, fileSystem: fileSystem, + project: Project(name: project.name + "Library", path: project.path, inPlace: true), + terminal + ) try createManifest( fileSystem: fileSystem, project: project, @@ -151,6 +162,17 @@ extension Templates { ], terminal ) + let sources = project.path.appending(component: "Sources") + let executableTarget = sources.appending(component: project.name) + // Create the executable target + try fileSystem.createDirectory(executableTarget) + try fileSystem.writeFileContents(executableTarget.appending(component: "main.swift")) { + """ + import \(project.name.spm_mangledToC99ExtendedIdentifier())Library + print("Hello, world!") + """ + .write(to: $0) + } } } } @@ -166,9 +188,9 @@ extension Templates { ) async throws { try fileSystem.changeCurrentWorkingDirectory(to: project.path) try await createPackage( - type: .executable, + type: .library, fileSystem: fileSystem, - project: project, + project: Project(name: project.name + "Library", path: project.path, inPlace: true), terminal) try createManifest( fileSystem: fileSystem, @@ -186,18 +208,13 @@ extension Templates { terminal ) - let sources = project.path.appending( - components: "Sources", - project.name - ) - - for source in try fileSystem.getDirectoryContents(sources) { - try fileSystem.removeFileTree(sources.appending(components: source)) - } + let sources = project.path.appending(component: "Sources") + let executableTarget = sources.appending(component: project.name) - try fileSystem.writeFileContents(sources.appending(components: "App.swift")) { + try fileSystem.writeFileContents(executableTarget.appending(components: "App.swift")) { """ import TokamakDOM + import \(project.name.spm_mangledToC99ExtendedIdentifier())Library @main struct TokamakApp: App { diff --git a/Sources/SwiftToolchain/Toolchain.swift b/Sources/SwiftToolchain/Toolchain.swift index f83918ed..d017caa3 100644 --- a/Sources/SwiftToolchain/Toolchain.swift +++ b/Sources/SwiftToolchain/Toolchain.swift @@ -390,13 +390,11 @@ public final class Toolchain { } public func runPackageInit(name: String, type: PackageType, inPlace: Bool) async throws { - var initArgs = [ + let initArgs = [ swiftPath.pathString, "package", "init", "--type", type.rawValue, + "--name", name ] - if !inPlace { - initArgs.append(contentsOf: ["--name", name]) - } try await TSCBasic.Process.run(initArgs, terminal) } diff --git a/Tests/CartonCommandTests/InitCommandTests.swift b/Tests/CartonCommandTests/InitCommandTests.swift index 9a0546fc..8feea7ef 100644 --- a/Tests/CartonCommandTests/InitCommandTests.swift +++ b/Tests/CartonCommandTests/InitCommandTests.swift @@ -32,7 +32,6 @@ final class InitCommandTests: XCTestCase { // Confirm that the files are actually in the folder XCTAssertTrue(packageDirectory.ls().contains("Package.swift"), "Package.swift does not exist") - XCTAssertTrue(packageDirectory.ls().contains("README.md"), "README.md does not exist") XCTAssertTrue(packageDirectory.ls().contains(".gitignore"), ".gitignore does not exist") XCTAssertTrue(packageDirectory.ls().contains("Sources"), "Sources does not exist") XCTAssertTrue( @@ -40,17 +39,17 @@ final class InitCommandTests: XCTestCase { "Sources/\(package) does not exist" ) XCTAssertTrue( - packageDirectory.ls().contains("Sources/\(package)/\(package).swift"), - "Sources/\(package)/\(package).swift does not exist" + packageDirectory.ls().contains("Sources/\(package)/main.swift"), + "Sources/\(package)/main.swift does not exist" ) XCTAssertTrue(packageDirectory.ls().contains("Tests"), "Tests does not exist") XCTAssertTrue( - packageDirectory.ls().contains("Tests/\(package)Tests"), - "Tests/\(package)Tests does not exist" + packageDirectory.ls().contains("Tests/\(package)LibraryTests"), + "Tests/\(package)LibraryTests does not exist" ) XCTAssertTrue( - packageDirectory.ls().contains("Tests/\(package)Tests/\(package)Tests.swift"), - "Tests/\(package)Tests/\(package)Tests.swift does not exist" + packageDirectory.ls().contains("Tests/\(package)LibraryTests/\(package)LibraryTests.swift"), + "Tests/\(package)LibraryTests/\(package)LibraryTests.swift does not exist" ) } } @@ -67,7 +66,6 @@ final class InitCommandTests: XCTestCase { // Confirm that the files are actually in the folder XCTAssertTrue(packageDirectory.ls().contains("Package.swift"), "Package.swift does not exist") - XCTAssertTrue(packageDirectory.ls().contains("README.md"), "README.md does not exist") XCTAssertTrue(packageDirectory.ls().contains(".gitignore"), ".gitignore does not exist") XCTAssertTrue(packageDirectory.ls().contains("Sources"), "Sources does not exist") XCTAssertTrue( @@ -80,40 +78,13 @@ final class InitCommandTests: XCTestCase { ) XCTAssertTrue(packageDirectory.ls().contains("Tests"), "Tests does not exist") XCTAssertTrue( - packageDirectory.ls().contains("Tests/\(package)Tests"), - "Tests/\(package)Tests does not exist" + packageDirectory.ls().contains("Tests/\(package)LibraryTests"), + "Tests/\(package)LibraryTests does not exist" ) XCTAssertTrue( - packageDirectory.ls().contains("Tests/\(package)Tests/\(package)Tests.swift"), - "Tests/\(package)Tests/\(package)Tests.swift does not exist" + packageDirectory.ls().contains("Tests/\(package)LibraryTests/\(package)LibraryTests.swift"), + "Tests/\(package)LibraryTests/\(package)LibraryTests.swift does not exist" ) - - let actualTemplateSource = try String( - contentsOfFile: - packageDirectory - .appending(components: "Sources", package, "App.swift").pathString) - - XCTAssertEqual(expectedTemplateSource, actualTemplateSource, "Template Sources do not match") } } - - let expectedTemplateSource = - """ - import TokamakDOM - - @main - struct TokamakApp: App { - var body: some Scene { - WindowGroup("Tokamak App") { - ContentView() - } - } - } - - struct ContentView: View { - var body: some View { - Text("Hello, world!") - } - } - """ } From 7986b520298dddc9f40b14fcff04265e7148e679 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 8 May 2023 00:29:01 +0000 Subject: [PATCH 3/3] Use the latest stable toolchain --- Dockerfile | 2 +- Sources/CartonHelpers/DefaultToolchain.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 474975d7..b860f8b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && ap rm -r /var/lib/apt/lists/* ENV CARTON_ROOT=/root/.carton -ENV CARTON_DEFAULT_TOOLCHAIN=wasm-5.8-SNAPSHOT-2023-04-30-a +ENV CARTON_DEFAULT_TOOLCHAIN=wasm-5.8.0-RELEASE RUN mkdir -p $CARTON_ROOT/sdk && \ mkdir -p $CARTON_ROOT/sdk/$CARTON_DEFAULT_TOOLCHAIN && \ diff --git a/Sources/CartonHelpers/DefaultToolchain.swift b/Sources/CartonHelpers/DefaultToolchain.swift index b6f1560e..a05c8078 100644 --- a/Sources/CartonHelpers/DefaultToolchain.swift +++ b/Sources/CartonHelpers/DefaultToolchain.swift @@ -12,4 +12,4 @@ // See the License for the specific language governing permissions and // limitations under the License. -public let defaultToolchainVersion = "wasm-5.8-SNAPSHOT-2023-04-30-a" +public let defaultToolchainVersion = "wasm-5.8.0-RELEASE"