Skip to content

Commit

Permalink
Fix broken carton init templates (#88)
Browse files Browse the repository at this point in the history
Resolves #77.

Also updates the Tokamak template to use the `App`/`Scene` lifecycle and to depend on the 0.3 version.

JavaScriptKit version check is also updated.
  • Loading branch information
MaxDesiatov committed Aug 20, 2020
1 parent a2e928a commit ef8bd94
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
28 changes: 22 additions & 6 deletions Sources/SwiftToolchain/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import CartonHelpers
import Foundation
import TSCBasic
import TSCUtility

private let compatibleJSKitRevision = "c90e82f"
public let compatibleJSKitVersion = Version(0, 5, 0)

enum ToolchainError: Error, CustomStringConvertible {
case directoryDoesNotExist(AbsolutePath)
Expand Down Expand Up @@ -55,6 +57,20 @@ enum ToolchainError: Error, CustomStringConvertible {
}
}

extension Package.Dependency.Requirement {
var isJavaScriptKitCompatible: Bool {
if let upperBound = range?.first?.upperBound, let version = Version(string: upperBound) {
return version >= compatibleJSKitVersion
}
return revision == [compatibleJSKitRevision] ||
exact?.compactMap { Version(string: $0) } == [compatibleJSKitVersion]
}

var version: String {
revision?.first ?? range?.first?.lowerBound ?? ""
}
}

public final class Toolchain {
private let fileSystem: FileSystem
private let terminal: TerminalController
Expand Down Expand Up @@ -177,17 +193,17 @@ public final class Toolchain {
else { throw ToolchainError.noExecutableProduct }

let package = try self.package.get()

if let jsKit = package.dependencies?.first(where: { $0.name == "JavaScriptKit" }),
jsKit.requirement.revision != ["c90e82f"] {
let version = jsKit.requirement.revision.flatMap { " (\($0[0]))" } ?? ""
!jsKit.requirement.isJavaScriptKitCompatible
{
let version = jsKit.requirement.version

terminal.write(
"""
This revision of JavaScriptKit\(version) is not known to be compatible with \
carton \(cartonVersion). Please specify a JavaScriptKit dependency to revision \
\(compatibleJSKitRevision) in your `Package.swift`.\n
This version of JavaScriptKit \(version) is not known to be compatible with \
carton \(cartonVersion). Please specify a JavaScriptKit dependency on version \
\(compatibleJSKitVersion) in your `Package.swift`.\n
""",
inColor: .red
Expand Down
65 changes: 36 additions & 29 deletions Sources/carton/Model/Template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ enum Templates: String, CaseIterable {

protocol Template {
static var description: String { get }
static func create(on fileSystem: FileSystem,
project: Project,
_ terminal: TerminalController) throws
static func create(
on fileSystem: FileSystem,
project: Project,
_ terminal: TerminalController
) throws
}

enum TemplateError: Error {
Expand Down Expand Up @@ -128,13 +130,22 @@ extension Templates {
_ terminal: TerminalController
) throws {
try fileSystem.changeCurrentWorkingDirectory(to: project.path)
try createPackage(type: .executable,
fileSystem: fileSystem,
project: project,
terminal)
try createManifest(fileSystem: fileSystem,
project: project,
terminal)
try createPackage(type: .executable, fileSystem: fileSystem, project: project, terminal)
try createManifest(
fileSystem: fileSystem,
project: project,
dependencies: [
.init(
name: "JavaScriptKit",
url: "https://github.com/swiftwasm/JavaScriptKit",
version: .from(compatibleJSKitVersion.description)
),
],
targetDepencencies: [
.init(name: "JavaScriptKit", package: "JavaScriptKit"),
],
terminal
)
}
}
}
Expand All @@ -160,11 +171,11 @@ extension Templates {
.init(
name: "Tokamak",
url: "https://github.com/swiftwasm/Tokamak",
version: .branch("main")
version: .from("0.3.0")
),
],
targetDepencencies: [
.init(name: "TokamakDOM", package: "Tokamak"),
.init(name: "TokamakShim", package: "Tokamak"),
],
terminal
)
Expand All @@ -174,30 +185,26 @@ extension Templates {
"main.swift"
)) {
"""
import TokamakDOM
import JavaScriptKit
import TokamakShim
let document = JSObjectRef.global.document.object!
let body = document.body.object!
body.style = "margin: 0;"
let div = document.createElement!("div").object!
let renderer = DOMRenderer(ContentView(), div)
_ = body.appendChild!(div)
"""
.write(to: $0)
}
try fileSystem.writeFileContents(
project.path.appending(components: "Sources", project.name, "ContentView.swift")
) {
"""
import TokamakDOM
struct TokamakApp: App {
var body: some Scene {
WindowGroup("Tokamak App") {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
Text("Hello, world!")
}
}
// @main attribute is not supported in SwiftPM apps.
// See https://bugs.swift.org/browse/SR-12683 for more details.
TokamakApp.main()
"""
.write(to: $0)
}
Expand Down

0 comments on commit ef8bd94

Please sign in to comment.