Compare commits
No commits in common. "cfb68f5237c89dd2e047e47d7c385cbe6abf9557" and "1c13f4fc608d49cbba2106fe24ba14960034f76b" have entirely different histories.
cfb68f5237
...
1c13f4fc60
@ -372,10 +372,7 @@ extension Element {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static func rootPaths(for input: Set<String>?, path: String) -> Set<String> {
|
static func rootPaths(for input: Set<String>?, path: String) -> Set<String> {
|
||||||
guard let input = input else {
|
input.unwrapped { Set($0.map { relativeToRoot(filePath: $0, folder: path) }) } ?? []
|
||||||
return []
|
|
||||||
}
|
|
||||||
return Set(input.map { relativeToRoot(filePath: $0, folder: path) })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func relativePathToFileWithPath(_ filePath: String) -> String {
|
func relativePathToFileWithPath(_ filePath: String) -> String {
|
||||||
|
@ -14,17 +14,7 @@ extension URL {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var isDirectory: Bool {
|
var isDirectory: Bool {
|
||||||
do {
|
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
|
||||||
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 {
|
var exists: Bool {
|
||||||
|
@ -3,6 +3,4 @@ import Foundation
|
|||||||
struct Configuration {
|
struct Configuration {
|
||||||
|
|
||||||
let pageImageWidth: Int
|
let pageImageWidth: Int
|
||||||
|
|
||||||
let minifyCSSandJS: Bool
|
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,6 @@ typealias SourceTextFile = (content: String, didChange: Bool)
|
|||||||
|
|
||||||
final class FileSystem {
|
final class FileSystem {
|
||||||
|
|
||||||
private static let tempFileName = "temp.bin"
|
|
||||||
|
|
||||||
private static let hashesFileName = "hashes.json"
|
private static let hashesFileName = "hashes.json"
|
||||||
|
|
||||||
private let input: URL
|
private let input: URL
|
||||||
@ -21,10 +19,6 @@ final class FileSystem {
|
|||||||
input.appendingPathComponent(FileSystem.hashesFileName)
|
input.appendingPathComponent(FileSystem.hashesFileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var tempFile: URL {
|
|
||||||
input.appendingPathComponent(FileSystem.tempFileName)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
The hashes of all accessed files from the previous run
|
The hashes of all accessed files from the previous run
|
||||||
|
|
||||||
@ -363,21 +357,7 @@ final class FileSystem {
|
|||||||
Add a file as required, so that it will be copied to the output directory.
|
Add a file as required, so that it will be copied to the output directory.
|
||||||
*/
|
*/
|
||||||
func require(file: String) {
|
func require(file: String) {
|
||||||
let url = input.appendingPathComponent(file)
|
requiredFiles.insert(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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -406,18 +386,24 @@ final class FileSystem {
|
|||||||
let sourceUrl = input.appendingPathComponent(cleanPath)
|
let sourceUrl = input.appendingPathComponent(cleanPath)
|
||||||
let destinationUrl = output.appendingPathComponent(cleanPath)
|
let destinationUrl = output.appendingPathComponent(cleanPath)
|
||||||
guard sourceUrl.exists else {
|
guard sourceUrl.exists else {
|
||||||
if !isExternal(file: file) {
|
if !externalFiles.contains(file) {
|
||||||
log.add(error: "Missing required file", source: cleanPath)
|
log.add(error: "Missing required file", source: cleanPath)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if copyFileIfChanged(from: sourceUrl, to: destinationUrl) {
|
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) {
|
||||||
copiedFiles.insert(file)
|
copiedFiles.insert(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try? tempFile.delete()
|
|
||||||
for (file, source) in expectedFiles {
|
for (file, source) in expectedFiles {
|
||||||
guard !isExternal(file: file) else {
|
guard !externalFiles.contains(file) else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
let cleanPath = cleanRelativeURL(file)
|
let cleanPath = cleanRelativeURL(file)
|
||||||
@ -436,52 +422,6 @@ 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 {
|
private func cleanRelativeURL(_ raw: String) -> String {
|
||||||
let raw = raw.dropAfterLast("#") // Clean links to page content
|
let raw = raw.dropAfterLast("#") // Clean links to page content
|
||||||
guard raw.contains("..") else {
|
guard raw.contains("..") else {
|
||||||
@ -498,42 +438,6 @@ final class FileSystem {
|
|||||||
return result.joined(separator: "/")
|
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
|
// MARK: Pages
|
||||||
|
|
||||||
func isEmpty(page: String) {
|
func isEmpty(page: String) {
|
||||||
@ -613,27 +517,6 @@ final class FileSystem {
|
|||||||
let data = string.data(using: .utf8)!
|
let data = string.data(using: .utf8)!
|
||||||
return writeIfChanged(data, to: url)
|
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 {
|
private extension Digest {
|
||||||
|
@ -3,9 +3,7 @@ import Foundation
|
|||||||
private let contentDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace")
|
private let contentDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace")
|
||||||
private let outputDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace/Site")
|
private let outputDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace/Site")
|
||||||
|
|
||||||
let configuration = Configuration(
|
let configuration = Configuration(pageImageWidth: 748)
|
||||||
pageImageWidth: 748,
|
|
||||||
minifyCSSandJS: true)
|
|
||||||
|
|
||||||
let log = ValidationLog()
|
let log = ValidationLog()
|
||||||
let files = FileSystem(in: contentDirectory, to: outputDirectory)
|
let files = FileSystem(in: contentDirectory, to: outputDirectory)
|
||||||
|
Loading…
Reference in New Issue
Block a user