64 lines
2.1 KiB
Swift
64 lines
2.1 KiB
Swift
//
|
|
// CLIToolCore.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 13/12/2021.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public class Shell {
|
|
|
|
@discardableResult
|
|
public static func shell(_ args: String...) -> (terminationStatus: Int32, output: String?) {
|
|
let task = Process()
|
|
task.launchPath = "/usr/bin/env"
|
|
task.arguments = args
|
|
|
|
let pipe = Pipe()
|
|
task.standardOutput = pipe
|
|
task.launch()
|
|
task.waitUntilExit()
|
|
|
|
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
|
|
|
guard let output: String = String(data: data, encoding: .utf8) else {
|
|
return (terminationStatus: task.terminationStatus, output: nil)
|
|
}
|
|
|
|
return (terminationStatus: task.terminationStatus, output: output)
|
|
}
|
|
}
|
|
|
|
public class GeneratorChecker {
|
|
|
|
/// Return `true` if inputFile is newer than extensionFile, otherwise `false`
|
|
public static func shouldGenerate(force: Bool, inputFilePath: String, extensionFilePath: String) -> Bool {
|
|
guard force == false else {
|
|
return true
|
|
}
|
|
|
|
// If inputFile is newer that generated extension -> Regenerate
|
|
let extensionFileURL = URL(fileURLWithPath: extensionFilePath)
|
|
let inputFileURL = URL(fileURLWithPath: inputFilePath)
|
|
|
|
let extensionRessourceValues = try? extensionFileURL.resourceValues(forKeys: [URLResourceKey.contentModificationDateKey])
|
|
let inputFileRessourceValues = try? inputFileURL.resourceValues(forKeys: [URLResourceKey.contentModificationDateKey])
|
|
|
|
if let extensionModificationDate = extensionRessourceValues?.contentModificationDate,
|
|
let inputFileModificationDate = inputFileRessourceValues?.contentModificationDate {
|
|
if inputFileModificationDate >= extensionModificationDate {
|
|
print("Input file is newer that generated extension.")
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// ModificationDate not available for both file
|
|
print("⚠️ Could not compare file modication date. ⚠️")
|
|
return true
|
|
}
|
|
|
|
}
|