-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3bf956b
Showing
12 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
/.webber/dev | ||
/.webber/release | ||
/.webber/entrypoint/dev/.ssl | ||
/.webber/node_modules | ||
/.webber/package* | ||
/.swiftpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// swift-tools-version:5.3 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "spa-template", | ||
platforms: [ | ||
.macOS(.v10_15) | ||
], | ||
products: [ | ||
.executable(name: "App", targets: ["App"]) | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/swifweb/web", from: "1.0.0-beta.1.0.0") | ||
], | ||
targets: [ | ||
.target(name: "App", dependencies: [ | ||
.product(name: "Web", package: "web") | ||
], resources: [ | ||
.copy("images/favicon.ico"), | ||
.copy("images") | ||
]), | ||
.testTarget(name: "AppTests", dependencies: ["App"]) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Web | ||
|
||
public class App: WebApp { | ||
@State var color = Color.cyan | ||
|
||
enum Theme { | ||
case happy, sad | ||
} | ||
|
||
@State var theme: Theme = .happy | ||
|
||
@AppBuilder public override var body: AppBuilder.Content { | ||
Lifecycle.didFinishLaunching { | ||
print("Lifecycle.didFinishLaunching") | ||
}.willTerminate { | ||
print("Lifecycle.willTerminate") | ||
}.willResignActive { | ||
print("Lifecycle.willResignActive") | ||
}.didBecomeActive { | ||
print("Lifecycle.didBecomeActive") | ||
}.didEnterBackground { | ||
print("Lifecycle.didEnterBackground") | ||
}.willEnterForeground { | ||
print("Lifecycle.willEnterForeground") | ||
} | ||
Routes { | ||
Page { WelcomeViewController() } | ||
Page("hello") { HelloViewController() } | ||
Page("**") { NotFoundViewController() } | ||
} | ||
HappyStyle().id(.happyStyle).disabled($theme.map { $0 != .happy }) | ||
SadStyle().id("sadStyle").disabled($theme.map { $0 != .sad }) | ||
} | ||
} | ||
|
||
class HappyStyle: Stylesheet { | ||
@Rules | ||
override var rules: Rules.Content { | ||
Rule(H1.pointer).color(App.current.$color) | ||
Rule(Pointer.any) | ||
.margin(all: 0) | ||
.padding(all: 0) | ||
Rule(H1.class(.hello).after, H2.class(.hello).after) { | ||
AlignContent(.baseline) | ||
Color(.red) | ||
} | ||
.property(.alignContent, .auto) | ||
.alignContent(.auto) | ||
.color(.red) | ||
} | ||
} | ||
|
||
class SadStyle: Stylesheet { | ||
@Rules | ||
override var rules: Rules.Content { | ||
Rule(H1.pointer).color(.deepPink) | ||
} | ||
} | ||
|
||
extension Id { | ||
static var happyStyle: Id { "happyStyle" } | ||
} | ||
|
||
extension Class { | ||
static var hello: Class { "hello" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Web | ||
|
||
extension FontFamilyType { | ||
static var sanFrancisco: Self { .init("San Francisco") } | ||
|
||
static var roboto: Self { .init("Roboto") } | ||
|
||
static var segoeUI: Self { .init("Segoe UI") } | ||
|
||
static var helveticaNeue: Self { .init("Helvetica Neue") } | ||
|
||
static var lucidaGrande: Self { .init("Lucida Grande") } | ||
|
||
static var app: Self { .combined(.system, .appleSystem, .sanFrancisco, .roboto, .segoeUI, .helveticaNeue, .lucidaGrande, .sansSerif) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Web | ||
|
||
class HelloViewController: ViewController { | ||
@DOM override var body: DOM.Content { | ||
P("Hello page") | ||
.textAlign(.center) | ||
.body { | ||
Button("go back").display(.block) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Web | ||
|
||
class NotFoundViewController: ViewController { | ||
@DOM override var body: DOM.Content { | ||
P("this is catchall aka 404 NOT FOUND page") | ||
.textAlign(.center) | ||
.body { | ||
Button("go back").display(.block) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Web | ||
|
||
class WelcomeViewController: ViewController { | ||
@State var clickCount = 0 | ||
@State var clicked = false | ||
|
||
@DOM override var body: DOM.Content { | ||
Header { | ||
Div { | ||
H1(self.$clickCount.map { "Hello world SPA app, clicks: \($0)" }) | ||
.color(self.$clicked.map { $0 ? .red : .white }) | ||
.textAlign(.center) | ||
.fontFamily(.app) | ||
Br() | ||
H4("Now you can run the world using Swift") | ||
.color(.white) | ||
.textAlign(.center) | ||
.fontFamily(.system, .appleSystem, .sanFrancisco, .roboto, .segoeUI, .helveticaNeue, .lucidaGrande, .sansSerif) | ||
Button("click").onClick { | ||
self.clicked = !self.clicked | ||
self.clickCount += 1 | ||
} | ||
Button("go to hello").onClick { | ||
History.pushState(path: "hello") | ||
} | ||
Button("toggle style").onClick { | ||
App.current.theme = App.current.theme == .happy ? .sad : .happy | ||
} | ||
} | ||
.position(.absolute) | ||
.display(.block) | ||
.top(50.percent) | ||
.left(50.percent) | ||
.transform(.translate(-50.percent, -50.percent)) | ||
.whiteSpace(.nowrap) | ||
.overflow(.hidden) | ||
} | ||
.position(.fixed) | ||
.width(100.percent) | ||
.height(100.percent) | ||
.background(.linearGradient(180, .black/70, .gray)) | ||
} | ||
|
||
override func buildUI() { | ||
super.buildUI() | ||
title = "SPA Hello world" | ||
metaDescription = "An awesome Swift in heart of your website" | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let app = App.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import XCTest | ||
import class Foundation.Bundle | ||
|
||
final class SwiftWasmAppTests: XCTestCase { | ||
func testExample() throws { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct | ||
// results. | ||
|
||
// Some of the APIs that we use below are available in macOS 10.13 and above. | ||
guard #available(macOS 10.13, *) else { | ||
return | ||
} | ||
|
||
let fooBinary = productsDirectory.appendingPathComponent("SwiftWasmApp") | ||
|
||
let process = Process() | ||
process.executableURL = fooBinary | ||
|
||
let pipe = Pipe() | ||
process.standardOutput = pipe | ||
|
||
try process.run() | ||
process.waitUntilExit() | ||
|
||
let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
let output = String(data: data, encoding: .utf8) | ||
|
||
XCTAssertEqual(output, "Hello, world!\n") | ||
} | ||
|
||
/// Returns path to the built products directory. | ||
var productsDirectory: URL { | ||
#if os(macOS) | ||
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { | ||
return bundle.bundleURL.deletingLastPathComponent() | ||
} | ||
fatalError("couldn't find the products directory") | ||
#else | ||
return Bundle.main.bundleURL | ||
#endif | ||
} | ||
|
||
static var allTests = [ | ||
("testExample", testExample), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import XCTest | ||
|
||
#if !canImport(ObjectiveC) | ||
public func allTests() -> [XCTestCaseEntry] { | ||
return [ | ||
testCase(SwiftWasmAppTests.allTests), | ||
] | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import XCTest | ||
|
||
import SwiftWasmAppTests | ||
|
||
var tests = [XCTestCaseEntry]() | ||
tests += SwiftWasmAppTests.allTests() | ||
XCTMain(tests) |