From 3ca09176d79076e247395d698fe1eea5fb4fc8ee Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 4 May 2023 14:50:24 +0000 Subject: [PATCH] Update init template to follow the 'package init' changes --- Sources/CartonKit/Model/Template.swift | 32 ++++++++++++++++--- Sources/SwiftToolchain/Toolchain.swift | 6 ++-- .../CartonCommandTests/InitCommandTests.swift | 14 ++++---- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Sources/CartonKit/Model/Template.swift b/Sources/CartonKit/Model/Template.swift index 0b099bf9..241cf4d5 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 @@ -110,12 +110,18 @@ extension Template { targets: [ .executableTarget( name: "\(project.name)", + dependencies: [ + "\(project.name)Library", + \(targetDepencencies.map(\.description).joined(separator: ",\n")) + ]), + .target( + name: "\(project.name)Library", dependencies: [ \(targetDepencencies.map(\.description).joined(separator: ",\n")) ]), .testTarget( name: "\(project.name)Tests", - dependencies: ["\(project.name)"]), + dependencies: ["\(project.name)Library"]), ] ) """ @@ -135,8 +141,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 +164,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)Library + print("Hello, world!") + """ + .write(to: $0) + } } } } @@ -166,7 +190,7 @@ extension Templates { ) async throws { try fileSystem.changeCurrentWorkingDirectory(to: project.path) try await createPackage( - type: .executable, + type: .library, fileSystem: fileSystem, project: project, terminal) 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..6287ca93 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(