Add SwiftLint HARD rules
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
This commit is contained in:
@ -1,43 +1,42 @@
|
||||
//
|
||||
// GeneratorChecker.swift
|
||||
//
|
||||
//
|
||||
//
|
||||
// Created by Thibaut Schmitt on 22/12/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public class GeneratorChecker {
|
||||
|
||||
public enum 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
|
||||
}
|
||||
|
||||
|
||||
return Self.isFile(inputFilePath, moreRecenThan: extensionFilePath)
|
||||
}
|
||||
|
||||
|
||||
public static func isFile(_ fileOne: String, moreRecenThan fileTwo: String) -> Bool {
|
||||
let fileOneURL = URL(fileURLWithPath: fileOne)
|
||||
let fileTwoURL = URL(fileURLWithPath: fileTwo)
|
||||
|
||||
|
||||
let fileOneRessourceValues = try? fileOneURL.resourceValues(forKeys: [URLResourceKey.contentModificationDateKey])
|
||||
let fileTwoRessourceValues = try? fileTwoURL.resourceValues(forKeys: [URLResourceKey.contentModificationDateKey])
|
||||
|
||||
|
||||
guard let fileOneModificationDate = fileOneRessourceValues?.contentModificationDate,
|
||||
let fileTwoModificationDate = fileTwoRessourceValues?.contentModificationDate else {
|
||||
print("⚠️ Could not compare file modication date. ⚠️ (assume than file is newer)")
|
||||
// Date not available -> assume than fileOne is newer than fileTwo
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
if fileOneModificationDate >= fileTwoModificationDate {
|
||||
debugPrint("File one is more recent than file two.")
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,8 +7,9 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension Sequence where Iterator.Element: Hashable {
|
||||
func unique() -> [Iterator.Element] {
|
||||
extension Sequence where Iterator.Element: Hashable {
|
||||
|
||||
public func unique() -> [Iterator.Element] {
|
||||
var seen: [Iterator.Element: Bool] = [:]
|
||||
return self.filter { seen.updateValue(true, forKey: $0) == nil }
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
//
|
||||
// Shell.swift
|
||||
//
|
||||
//
|
||||
//
|
||||
// Created by Thibaut Schmitt on 22/12/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public class Shell {
|
||||
|
||||
public enum Shell {
|
||||
|
||||
public static var environment: [String: String] {
|
||||
ProcessInfo.processInfo.environment
|
||||
}
|
||||
@ -18,31 +18,31 @@ public class Shell {
|
||||
launchPath: String = "/usr/bin/env",
|
||||
_ args: [String]
|
||||
) -> (terminationStatus: Int32, output: String?) {
|
||||
#if os(macOS)
|
||||
#if os(macOS)
|
||||
let task = Process()
|
||||
task.launchPath = launchPath
|
||||
task.arguments = args
|
||||
|
||||
|
||||
var currentEnv = ProcessInfo.processInfo.environment
|
||||
for (key, value) in environment {
|
||||
currentEnv[key] = value
|
||||
}
|
||||
task.environment = currentEnv
|
||||
|
||||
|
||||
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 {
|
||||
|
||||
guard let output = String(data: data, encoding: .utf8) else {
|
||||
return (terminationStatus: task.terminationStatus, output: nil)
|
||||
}
|
||||
|
||||
|
||||
return (terminationStatus: task.terminationStatus, output: output)
|
||||
#else
|
||||
#else
|
||||
fatalError("Shell is only available on Mac")
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -1,87 +1,88 @@
|
||||
//
|
||||
// Extensions.swift
|
||||
//
|
||||
//
|
||||
//
|
||||
// Created by Thibaut Schmitt on 13/12/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension String {
|
||||
func removeCharacters(from forbiddenChars: CharacterSet) -> String {
|
||||
extension String {
|
||||
|
||||
public func removeCharacters(from forbiddenChars: CharacterSet) -> String {
|
||||
let passed = self.unicodeScalars.filter { !forbiddenChars.contains($0) }
|
||||
return String(String.UnicodeScalarView(passed))
|
||||
}
|
||||
|
||||
func removeCharacters(from: String) -> String {
|
||||
return removeCharacters(from: CharacterSet(charactersIn: from))
|
||||
|
||||
public func removeCharacters(from: String) -> String {
|
||||
removeCharacters(from: CharacterSet(charactersIn: from))
|
||||
}
|
||||
|
||||
func replacingOccurrences(of: [String], with: String) -> Self {
|
||||
|
||||
public func replacingOccurrences(of: [String], with: String) -> Self {
|
||||
var tmp = self
|
||||
for e in of {
|
||||
tmp = tmp.replacingOccurrences(of: e, with: with)
|
||||
for ofValue in of {
|
||||
tmp = tmp.replacingOccurrences(of: ofValue, with: with)
|
||||
}
|
||||
return tmp
|
||||
}
|
||||
|
||||
func removeTrailingWhitespace() -> Self {
|
||||
|
||||
public func removeTrailingWhitespace() -> Self {
|
||||
var newString = self
|
||||
|
||||
|
||||
while newString.last?.isWhitespace == true {
|
||||
newString = String(newString.dropLast())
|
||||
}
|
||||
|
||||
|
||||
return newString
|
||||
}
|
||||
|
||||
func removeLeadingWhitespace() -> Self {
|
||||
|
||||
public func removeLeadingWhitespace() -> Self {
|
||||
var newString = self
|
||||
|
||||
|
||||
while newString.first?.isWhitespace == true {
|
||||
newString = String(newString.dropFirst())
|
||||
}
|
||||
|
||||
|
||||
return newString
|
||||
}
|
||||
|
||||
func removeLeadingTrailingWhitespace() -> Self {
|
||||
|
||||
public func removeLeadingTrailingWhitespace() -> Self {
|
||||
var newString = self
|
||||
|
||||
|
||||
newString = newString.removeLeadingWhitespace()
|
||||
newString = newString.removeTrailingWhitespace()
|
||||
|
||||
|
||||
return newString
|
||||
}
|
||||
|
||||
func escapeDoubleQuote() -> Self {
|
||||
|
||||
public func escapeDoubleQuote() -> Self {
|
||||
replacingOccurrences(of: "\"", with: "\\\"")
|
||||
}
|
||||
|
||||
func replaceTiltWithHomeDirectoryPath() -> Self {
|
||||
|
||||
public func replaceTiltWithHomeDirectoryPath() -> Self {
|
||||
// See NSString.expandingTildeInPath
|
||||
#if os(macOS)
|
||||
#if os(macOS)
|
||||
replacingOccurrences(of: "~", with: "\(FileManager.default.homeDirectoryForCurrentUser.relativePath)")
|
||||
#else
|
||||
#else
|
||||
fatalError("This command should run on Mac only")
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
func colorComponent() -> (alpha: String, red: String, green: String, blue: String) {
|
||||
|
||||
public func colorComponent() -> (alpha: String, red: String, green: String, blue: String) { // swiftlint:disable:this large_tuple
|
||||
var alpha: String = "FF"
|
||||
var red: String
|
||||
var green: String
|
||||
var blue: String
|
||||
|
||||
var colorClean = self
|
||||
.replacingOccurrences(of: "#", with: "")
|
||||
.replacingOccurrences(of: "0x", with: "")
|
||||
.replacingOccurrences(of: "#", with: "")
|
||||
.replacingOccurrences(of: "0x", with: "")
|
||||
|
||||
if colorClean.count == 8 {
|
||||
alpha = String(colorClean.prefix(2))
|
||||
colorClean = String(colorClean.dropFirst(2))
|
||||
}
|
||||
|
||||
|
||||
red = String(colorClean.prefix(2))
|
||||
colorClean = String(colorClean.dropFirst(2))
|
||||
green = String(colorClean.prefix(2))
|
||||
@ -89,12 +90,12 @@ public extension String {
|
||||
blue = String(colorClean.prefix(2))
|
||||
return (alpha: alpha, red: red, green: green, blue: blue)
|
||||
}
|
||||
|
||||
func uppercasedFirst() -> String {
|
||||
|
||||
public func uppercasedFirst() -> String {
|
||||
prefix(1).uppercased() + dropFirst()
|
||||
}
|
||||
|
||||
func replacingFirstOccurrence(of: String, with: String) -> Self {
|
||||
public func replacingFirstOccurrence(of: String, with: String) -> Self {
|
||||
if let range = self.range(of: of) {
|
||||
let tmp = self.replacingOccurrences(
|
||||
of: of,
|
||||
|
@ -7,4 +7,6 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
// swiftlint:disable prefixed_toplevel_constant identifier_name
|
||||
|
||||
public let ResgenSwiftVersion = "2.1.0"
|
||||
|
Reference in New Issue
Block a user