Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CartonFrontend dev's file-watching opt-in to easily use it as a standalone CLI #433

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions Sources/CartonCLI/Commands/Dev.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ import CartonKit
import Foundation

struct Dev: AsyncParsableCommand {
enum Error: Swift.Error & CustomStringConvertible {
case noBuildRequestOption
case noBuildResponseOption
case failedToOpenBuildRequestPipe
case failedToOpenBuildResponsePipe

var description: String {
switch self {
case .noBuildRequestOption: "--build-request option is necessary if you want to watch, but has not been specified."
case .noBuildResponseOption: "--build-response option is necessary if you want to watch, but has not been specified."
case .failedToOpenBuildRequestPipe: "failed to open build request pipe"
case .failedToOpenBuildResponsePipe: "failed to open build response pipe"
}
}
}

static let entrypoint = Entrypoint(fileName: "dev.js", content: StaticResource.dev)

@Option(help: "Specify name of an executable product in development.")
Expand Down Expand Up @@ -68,15 +84,15 @@ struct Dev: AsyncParsableCommand {
visibility: .private
)
)
var buildRequest: String
var buildRequest: String?

@Option(
help: ArgumentHelp(
"Internal: Path to the named pipe used to receive build responses from the SwiftPM Plugin process.",
visibility: .private
)
)
var buildResponse: String
var buildResponse: String?

@Option(
help: ArgumentHelp(
Expand All @@ -90,27 +106,46 @@ struct Dev: AsyncParsableCommand {
abstract: "Watch the current directory, host the app, rebuild on change."
)

func run() async throws {
let terminal = InteractiveWriter.stdout
private func makeBuilderIfNeed() throws -> SwiftPMPluginBuilder? {
guard !watchPaths.isEmpty else {
return nil
}

guard let buildRequest else {
throw Error.noBuildRequestOption
}
guard let buildResponse else {
throw Error.noBuildResponseOption
}

let paths = try watchPaths.map {
let pathsToWatch = try watchPaths.map {
try AbsolutePath(validating: $0, relativeTo: localFileSystem.currentWorkingDirectory!)
}

guard let buildRequest = FileHandle(forWritingAtPath: buildRequest) else {
throw Error.failedToOpenBuildRequestPipe
}
guard let buildResponse = FileHandle(forReadingAtPath: buildResponse) else {
throw Error.failedToOpenBuildResponsePipe
}

return SwiftPMPluginBuilder(
pathsToWatch: pathsToWatch,
buildRequest: buildRequest,
buildResponse: buildResponse
)
}

func run() async throws {
let terminal = InteractiveWriter.stdout

if !verbose {
terminal.revertCursorAndClear()
}
terminal.write("\nWatching these directories for changes:\n", inColor: .green)
paths.forEach { terminal.logLookup("", $0) }
terminal.write("\n")

let server = try await Server(
.init(
builder: SwiftPMPluginBuilder(
pathsToWatch: paths,
buildRequest: FileHandle(forWritingAtPath: buildRequest)!,
buildResponse: FileHandle(forReadingAtPath: buildResponse)!
),
builder: try makeBuilderIfNeed(),
mainWasmPath: AbsolutePath(
validating: mainWasmPath, relativeTo: localFileSystem.currentWorkingDirectory!),
verbose: verbose,
Expand Down
6 changes: 6 additions & 0 deletions Sources/CartonKit/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public actor Server {
}

if !builder.pathsToWatch.isEmpty {
let terminal = configuration.terminal

terminal.write("\nWatching these directories for changes:\n", inColor: .green)
builder.pathsToWatch.forEach { terminal.logLookup("", $0) }
terminal.write("\n")

watcher = FSWatch(paths: builder.pathsToWatch, latency: 0.1) { [weak self] changes in
guard let self = self, !changes.isEmpty else { return }
Task { try await self.onChange(changes, configuration) }
Expand Down
Loading