Skip to content

Commit

Permalink
Update init template to follow the 'package init' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed May 4, 2023
1 parent bc8025c commit 3ca0917
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
32 changes: 28 additions & 4 deletions Sources/CartonKit/Model/Template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]),
]
)
"""
Expand All @@ -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,
Expand All @@ -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)
}
}
}
}
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions Sources/SwiftToolchain/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 6 additions & 8 deletions Tests/CartonCommandTests/InitCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,24 @@ 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(
packageDirectory.ls().contains("Sources/\(package)"),
"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"
)
}
}
Expand All @@ -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(
Expand Down

0 comments on commit 3ca0917

Please sign in to comment.