mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-03-25 09:05:47 +00:00
Stop writing temporary files when shelling out to less
This commit is contained in:
parent
447da5fdc1
commit
e53fda0851
2 changed files with 23 additions and 8 deletions
|
|
@ -1,10 +1,13 @@
|
||||||
// swift-tools-version:5.0
|
// swift-tools-version:5.1
|
||||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||||
|
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "SiteGenerator",
|
name: "SiteGenerator",
|
||||||
|
platforms: [
|
||||||
|
.macOS(.v10_15),
|
||||||
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/stencilproject/Stencil.git", from: "0.13.0"),
|
.package(url: "https://github.com/stencilproject/Stencil.git", from: "0.13.0"),
|
||||||
.package(url: "https://github.com/johnsundell/ink.git", from: "0.1.0"),
|
.package(url: "https://github.com/johnsundell/ink.git", from: "0.1.0"),
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,10 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public final class LessRenderer: Renderer {
|
public final class LessRenderer: Renderer {
|
||||||
|
enum Error: Swift.Error {
|
||||||
|
case invalidCSSData(Data)
|
||||||
|
}
|
||||||
|
|
||||||
public func canRenderFile(named filename: String, withExtension ext: String) -> Bool {
|
public func canRenderFile(named filename: String, withExtension ext: String) -> Bool {
|
||||||
ext == "less"
|
ext == "less"
|
||||||
}
|
}
|
||||||
|
|
@ -27,12 +31,17 @@ public final class LessRenderer: Renderer {
|
||||||
_ = try? FileManager.default.removeItem(at: tempDir)
|
_ = try? FileManager.default.removeItem(at: tempDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
let timestamp = Date().timeIntervalSince1970
|
let lessIn = Pipe()
|
||||||
let lessURL = tempDir.appendingPathComponent("LessParser-in-\(timestamp).less")
|
lessIn.fileHandleForWriting.write(Data(less.utf8))
|
||||||
let cssURL = tempDir.appendingPathComponent("LessParser-out-\(timestamp).css")
|
try lessIn.fileHandleForWriting.close()
|
||||||
try less.write(to: lessURL, atomically: true, encoding: .utf8)
|
let cssOut = Pipe()
|
||||||
shell(lesscPath, lessURL.path, cssURL.path)
|
shell(lesscPath, "-", stdin: lessIn, stdout: cssOut)
|
||||||
return try String(contentsOf: cssURL, encoding: .utf8)
|
let cssData = cssOut.fileHandleForReading.readDataToEndOfFile()
|
||||||
|
_ = try? cssOut.fileHandleForReading.close()
|
||||||
|
guard let css = String(data: cssData, encoding: .utf8) else {
|
||||||
|
throw Error.invalidCSSData(cssData)
|
||||||
|
}
|
||||||
|
return css
|
||||||
}
|
}
|
||||||
|
|
||||||
let lesscPath = URL(fileURLWithPath: #file)
|
let lesscPath = URL(fileURLWithPath: #file)
|
||||||
|
|
@ -45,10 +54,13 @@ public final class LessRenderer: Renderer {
|
||||||
.path
|
.path
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func shell(_ args: String...) -> Int32 {
|
func shell(_ args: String..., stdin: Pipe? = nil, stdout: Pipe? = nil, stderr: Pipe? = nil) -> Int32 {
|
||||||
let task = Process()
|
let task = Process()
|
||||||
task.launchPath = "/usr/bin/env"
|
task.launchPath = "/usr/bin/env"
|
||||||
task.arguments = args
|
task.arguments = args
|
||||||
|
task.standardInput = stdin
|
||||||
|
task.standardOutput = stdout
|
||||||
|
task.standardError = stderr
|
||||||
task.launch()
|
task.launch()
|
||||||
task.waitUntilExit()
|
task.waitUntilExit()
|
||||||
return task.terminationStatus
|
return task.terminationStatus
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue