Compare commits
2 Commits
1c13f4fc60
...
cfb68f5237
Author | SHA1 | Date | |
---|---|---|---|
|
cfb68f5237 | ||
|
a69da0fa35 |
@ -372,7 +372,10 @@ extension Element {
|
||||
}
|
||||
|
||||
static func rootPaths(for input: Set<String>?, path: String) -> Set<String> {
|
||||
input.unwrapped { Set($0.map { relativeToRoot(filePath: $0, folder: path) }) } ?? []
|
||||
guard let input = input else {
|
||||
return []
|
||||
}
|
||||
return Set(input.map { relativeToRoot(filePath: $0, folder: path) })
|
||||
}
|
||||
|
||||
func relativePathToFileWithPath(_ filePath: String) -> String {
|
||||
|
@ -14,7 +14,17 @@ extension URL {
|
||||
}
|
||||
|
||||
var isDirectory: Bool {
|
||||
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
|
||||
do {
|
||||
let resources = try resourceValues(forKeys: [.isDirectoryKey])
|
||||
guard let isDirectory = resources.isDirectory else {
|
||||
print("No isDirectory info for \(path)")
|
||||
return false
|
||||
}
|
||||
return isDirectory
|
||||
} catch {
|
||||
print("Failed to get directory information from \(path): \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var exists: Bool {
|
||||
|
@ -3,4 +3,6 @@ import Foundation
|
||||
struct Configuration {
|
||||
|
||||
let pageImageWidth: Int
|
||||
|
||||
let minifyCSSandJS: Bool
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ typealias SourceTextFile = (content: String, didChange: Bool)
|
||||
|
||||
final class FileSystem {
|
||||
|
||||
private static let tempFileName = "temp.bin"
|
||||
|
||||
private static let hashesFileName = "hashes.json"
|
||||
|
||||
private let input: URL
|
||||
@ -19,6 +21,10 @@ final class FileSystem {
|
||||
input.appendingPathComponent(FileSystem.hashesFileName)
|
||||
}
|
||||
|
||||
private var tempFile: URL {
|
||||
input.appendingPathComponent(FileSystem.tempFileName)
|
||||
}
|
||||
|
||||
/**
|
||||
The hashes of all accessed files from the previous run
|
||||
|
||||
@ -357,7 +363,21 @@ final class FileSystem {
|
||||
Add a file as required, so that it will be copied to the output directory.
|
||||
*/
|
||||
func require(file: String) {
|
||||
let url = input.appendingPathComponent(file)
|
||||
guard url.exists, url.isDirectory else {
|
||||
requiredFiles.insert(file)
|
||||
return
|
||||
}
|
||||
do {
|
||||
try FileManager.default
|
||||
.contentsOfDirectory(atPath: url.path)
|
||||
.forEach {
|
||||
// Recurse into subfolders
|
||||
require(file: file + "/" + $0)
|
||||
}
|
||||
} catch {
|
||||
log.add(error: "Failed to read folder \(file): \(error)", source: source)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -386,24 +406,18 @@ final class FileSystem {
|
||||
let sourceUrl = input.appendingPathComponent(cleanPath)
|
||||
let destinationUrl = output.appendingPathComponent(cleanPath)
|
||||
guard sourceUrl.exists else {
|
||||
if !externalFiles.contains(file) {
|
||||
if !isExternal(file: file) {
|
||||
log.add(error: "Missing required file", source: cleanPath)
|
||||
}
|
||||
continue
|
||||
}
|
||||
let data: Data
|
||||
do {
|
||||
data = try Data(contentsOf: sourceUrl)
|
||||
} catch {
|
||||
log.add(error: "Failed to read data at \(sourceUrl.path)", source: source, error: error)
|
||||
continue
|
||||
}
|
||||
if writeIfChanged(data, to: destinationUrl) {
|
||||
if copyFileIfChanged(from: sourceUrl, to: destinationUrl) {
|
||||
copiedFiles.insert(file)
|
||||
}
|
||||
}
|
||||
try? tempFile.delete()
|
||||
for (file, source) in expectedFiles {
|
||||
guard !externalFiles.contains(file) else {
|
||||
guard !isExternal(file: file) else {
|
||||
continue
|
||||
}
|
||||
let cleanPath = cleanRelativeURL(file)
|
||||
@ -422,6 +436,52 @@ final class FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
private func copyFileIfChanged(from sourceUrl: URL, to destinationUrl: URL) -> Bool {
|
||||
guard configuration.minifyCSSandJS else {
|
||||
return copyBinaryFileIfChanged(from: sourceUrl, to: destinationUrl)
|
||||
}
|
||||
switch sourceUrl.pathExtension.lowercased() {
|
||||
case "js":
|
||||
return minifyJS(at: sourceUrl, andWriteTo: destinationUrl)
|
||||
case "css":
|
||||
return minifyCSS(at: sourceUrl, andWriteTo: destinationUrl)
|
||||
default:
|
||||
return copyBinaryFileIfChanged(from: sourceUrl, to: destinationUrl)
|
||||
}
|
||||
}
|
||||
|
||||
private func copyBinaryFileIfChanged(from sourceUrl: URL, to destinationUrl: URL) -> Bool {
|
||||
do {
|
||||
let data = try Data(contentsOf: sourceUrl)
|
||||
return writeIfChanged(data, to: destinationUrl)
|
||||
} catch {
|
||||
log.add(error: "Failed to read data at \(sourceUrl.path)", source: source, error: error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func minifyJS(at sourceUrl: URL, andWriteTo destinationUrl: URL) -> Bool {
|
||||
let command = "uglifyjs \(sourceUrl.path) > \(tempFile.path)"
|
||||
do {
|
||||
_ = try safeShell(command)
|
||||
return copyBinaryFileIfChanged(from: tempFile, to: destinationUrl)
|
||||
} catch {
|
||||
log.add(error: "Failed to minify \(sourceUrl.path): \(error)", source: source)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func minifyCSS(at sourceUrl: URL, andWriteTo destinationUrl: URL) -> Bool {
|
||||
let command = "cleancss \(sourceUrl.path) -o \(tempFile.path)"
|
||||
do {
|
||||
_ = try safeShell(command)
|
||||
return copyBinaryFileIfChanged(from: tempFile, to: destinationUrl)
|
||||
} catch {
|
||||
log.add(error: "Failed to minify \(sourceUrl.path): \(error)", source: source)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func cleanRelativeURL(_ raw: String) -> String {
|
||||
let raw = raw.dropAfterLast("#") // Clean links to page content
|
||||
guard raw.contains("..") else {
|
||||
@ -438,6 +498,42 @@ final class FileSystem {
|
||||
return result.joined(separator: "/")
|
||||
}
|
||||
|
||||
/**
|
||||
Check if a file is marked as external.
|
||||
|
||||
Also checks for sub-paths of the file, e.g if the folder `docs` is marked as external,
|
||||
then files like `docs/index.html` are also found to be external.
|
||||
- Note: All paths are either relative to root (no leading slash) or absolute paths of the domain (leading slash)
|
||||
*/
|
||||
func isExternal(file: String) -> Bool {
|
||||
// Deconstruct file path
|
||||
var path = ""
|
||||
for part in file.components(separatedBy: "/") {
|
||||
guard part != "" else {
|
||||
continue
|
||||
}
|
||||
if path == "" {
|
||||
path = part
|
||||
} else {
|
||||
path += "/" + part
|
||||
}
|
||||
if externalFiles.contains(path) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func printExternalFiles() {
|
||||
guard !externalFiles.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(externalFiles.count) external resources needed:")
|
||||
for file in externalFiles.sorted() {
|
||||
print(" " + file)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Pages
|
||||
|
||||
func isEmpty(page: String) {
|
||||
@ -517,6 +613,27 @@ final class FileSystem {
|
||||
let data = string.data(using: .utf8)!
|
||||
return writeIfChanged(data, to: url)
|
||||
}
|
||||
|
||||
// MARK: Running other tasks
|
||||
|
||||
@discardableResult
|
||||
func safeShell(_ command: String) throws -> String {
|
||||
let task = Process()
|
||||
let pipe = Pipe()
|
||||
|
||||
task.standardOutput = pipe
|
||||
task.standardError = pipe
|
||||
task.arguments = ["-cl", command]
|
||||
task.executableURL = URL(fileURLWithPath: "/bin/zsh")
|
||||
task.standardInput = nil
|
||||
|
||||
try task.run()
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output = String(data: data, encoding: .utf8)!
|
||||
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
private extension Digest {
|
||||
|
@ -3,7 +3,9 @@ import Foundation
|
||||
private let contentDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace")
|
||||
private let outputDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace/Site")
|
||||
|
||||
let configuration = Configuration(pageImageWidth: 748)
|
||||
let configuration = Configuration(
|
||||
pageImageWidth: 748,
|
||||
minifyCSSandJS: true)
|
||||
|
||||
let log = ValidationLog()
|
||||
let files = FileSystem(in: contentDirectory, to: outputDirectory)
|
||||
|
Loading…
Reference in New Issue
Block a user