import Foundation import CryptoKit import AppKit final class FileSystem { private static let tempFileName = "temp.bin" private let input: URL private let output: URL private let source = "FileSystem" private let images: ImageGenerator private var tempFile: URL { input.appendingPathComponent(FileSystem.tempFileName) } /** All files which should be copied to the output folder */ private var requiredFiles: Set = [] /** The files marked as external in element metadata. Files included here are not generated, since they are assumed to be added separately. */ private var externalFiles: Set = [] /** The files marked as expected, i.e. they exist after the generation is completed. The key of the dictionary is the file path, the value is the file providing the link */ private var expectedFiles: [String : String] = [:] /** All pages without content which have been created */ private var emptyPages: Set = [] /** All pages which have `status` set to ``PageState.draft`` */ private var draftPages: Set = [] /** All paths to page element folders, indexed by their unique id. This relation is used to generate relative links to pages using the ``Element.id` */ private var pagePaths: [String: String] = [:] /** The image creation tasks. The key is the destination path. */ private var imageTasks: [String : ImageOutput] = [:] /** The paths to all pages which were changed */ private var generatedPages: Set = [] init(in input: URL, to output: URL) { self.input = input self.output = output self.images = .init(input: input, output: output) } func urlInOutputFolder(_ path: String) -> URL { output.appendingPathComponent(path) } func urlInContentFolder(_ path: String) -> URL { input.appendingPathComponent(path) } private func exists(_ url: URL) -> Bool { FileManager.default.fileExists(atPath: url.path) } func dataOfRequiredFile(atPath path: String, source: String) -> Data? { let url = input.appendingPathComponent(path) guard exists(url) else { log.failedToOpen(path, requiredBy: source, error: nil) return nil } do { return try Data(contentsOf: url) } catch { log.failedToOpen(path, requiredBy: source, error: error) return nil } } func dataOfOptionalFile(atPath path: String, source: String) -> Data? { let url = input.appendingPathComponent(path) guard exists(url) else { return nil } do { return try Data(contentsOf: url) } catch { log.failedToOpen(path, requiredBy: source, error: error) return nil } } func contentOfOptionalFile(atPath path: String, source: String, createEmptyFileIfMissing: Bool = false) -> String? { let url = input.appendingPathComponent(path) guard exists(url) else { if createEmptyFileIfMissing { try? Data().write(to: url) } return nil } do { return try String(contentsOf: url) } catch { log.failedToOpen(path, requiredBy: source, error: error) return nil } } func writeDetectedFileChangesToDisk() { images.writeDetectedFileChangesToDisk() } // MARK: Images @discardableResult func requireImage(source: String, destination: String, requiredBy path: String, width: Int, desiredHeight: Int? = nil) -> NSSize { images.requireImage(at: destination, generatedFrom: source, requiredBy: path, width: width, height: desiredHeight) } func createImages() { images.createImages() } // MARK: File copying /** 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) } } /** Mark a file as explicitly missing. This is done for the `externalFiles` entries in metadata, to indicate that these files will be copied to the output folder manually. */ func exclude(file: String) { externalFiles.insert(file) } /** Mark a file as expected to be present in the output folder after generation. This is done for all links between pages, which only exist after the pages have been generated. */ func expect(file: String, source: String) { expectedFiles[file] = source } func copyRequiredFiles() { var copiedFiles = Set() for file in requiredFiles { let cleanPath = cleanRelativeURL(file) let sourceUrl = input.appendingPathComponent(cleanPath) let destinationUrl = output.appendingPathComponent(cleanPath) guard sourceUrl.exists else { if !isExternal(file: file) { log.add(error: "Missing required file", source: cleanPath) } continue } if copyFileIfChanged(from: sourceUrl, to: destinationUrl) { copiedFiles.insert(file) } } try? tempFile.delete() for (file, source) in expectedFiles { guard !isExternal(file: file) else { continue } let cleanPath = cleanRelativeURL(file) let destinationUrl = output.appendingPathComponent(cleanPath) if !destinationUrl.exists { log.add(error: "Missing \(cleanPath)", source: source) } } guard !copiedFiles.isEmpty else { print("No required files copied") return } print("\(copiedFiles.count) required files copied:") for file in copiedFiles.sorted() { print(" " + file) } } 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 { return raw } var result: [String] = [] for component in raw.components(separatedBy: "/") { if component == ".." { _ = result.popLast() } else { result.append(component) } } 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) { emptyPages.insert(page) } func printEmptyPages() { guard !emptyPages.isEmpty else { return } print("\(emptyPages.count) empty pages:") for page in emptyPages.sorted() { print(" " + page) } } func isDraft(path: String) { draftPages.insert(path) } func printDraftPages() { guard !draftPages.isEmpty else { return } print("\(draftPages.count) drafts:") for page in draftPages.sorted() { print(" " + page) } } func add(page: String, id: String) { if let existing = pagePaths[id] { log.add(error: "Conflicting id with \(existing)", source: page) } pagePaths[id] = page } func getPage(for id: String) -> String? { pagePaths[id] } func generated(page: String) { generatedPages.insert(page) } func printGeneratedPages() { guard !generatedPages.isEmpty else { print("No pages modified") return } print("\(generatedPages.count) pages modified") for page in generatedPages.sorted() { print(" " + page) } } // MARK: Writing files @discardableResult func writeIfChanged(_ data: Data, to url: URL) -> Bool { // Only write changed files if url.exists, let oldContent = try? Data(contentsOf: url), data == oldContent { return false } do { try data.createFolderAndWrite(to: url) return true } catch { log.add(error: "Failed to write file", source: url.path, error: error) return false } } @discardableResult func write(_ string: String, to url: URL) -> Bool { 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 } }