Skip to content

Commit 8d1736d

Browse files
committed
feat: Add cache support for saga dev
1 parent 58b45c8 commit 8d1736d

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

Sources/SagaCLI/DevCommand.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ArgumentParser
22
import Foundation
3+
import SagaPathKit
34

45
struct Dev: ParsableCommand {
56
static let configuration = CommandConfiguration(
@@ -19,8 +20,15 @@ struct Dev: ParsableCommand {
1920
var ignore: [String] = []
2021

2122
func run() throws {
23+
// Create a fresh cache directory for this dev session
24+
let cachePath = Path.current + ".build/saga-cache"
25+
if cachePath.exists {
26+
try cachePath.delete()
27+
}
28+
try cachePath.mkpath()
29+
2230
print("Building site...")
23-
let buildResult = runBuild()
31+
let buildResult = runBuild(cachePath: cachePath)
2432
if !buildResult {
2533
print("Initial build failed, starting server anyway...")
2634
}
@@ -74,7 +82,7 @@ struct Dev: ParsableCommand {
7482
rebuildLock.unlock()
7583

7684
print("Change detected, rebuilding...")
77-
let success = runBuild()
85+
let success = runBuild(cachePath: cachePath)
7886
if success {
7987
print("Rebuild complete.")
8088
server.sendReload()
@@ -107,14 +115,15 @@ struct Dev: ParsableCommand {
107115
}
108116
}
109117

110-
private func runBuild() -> Bool {
118+
private func runBuild(cachePath: Path) -> Bool {
111119
let process = Process()
112120
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
113121
process.arguments = ["swift", "run"]
114122
process.currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
115123

116124
var env = ProcessInfo.processInfo.environment
117125
env["SAGA_DEV"] = "1"
126+
env["SAGA_CACHE_DIR"] = cachePath.string
118127
process.environment = env
119128

120129
do {

Sources/SagaCLI/InitCommand.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct Init: ParsableCommand {
3131
(projectPath + "content" + "index.md", ProjectTemplate.indexMarkdown()),
3232
(projectPath + "content" + "articles" + "hello-world.md", ProjectTemplate.helloWorldMarkdown()),
3333
(projectPath + "content" + "static" + "style.css", ProjectTemplate.styleCss()),
34+
(projectPath + ".gitignore", ProjectTemplate.gitignore()),
3435
]
3536

3637
for (path, content) in files {

Sources/SagaCLI/ProjectTemplate.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ enum ProjectTemplate {
339339
"""
340340
}
341341

342+
static func gitignore() -> String {
343+
"""
344+
.DS_Store
345+
.build/
346+
.swiftpm/
347+
*.xcodeproj
348+
xcuserdata/
349+
"""
350+
}
351+
342352
private static func currentDateString() -> String {
343353
let formatter = DateFormatter()
344354
formatter.dateFormat = "yyyy-MM-dd"

0 commit comments

Comments
 (0)