Files
divkit/client/ios/DivKitSnapshotTests/FilesPreprocessing/FilesPreprocessing.swift
T
pkurchatov 3670a7c879 Applied swiftormat
d22237ac3335f62b16f37a172b1eb42a7558a5b5
2024-04-23 18:37:14 +03:00

52 lines
1.3 KiB
Swift

import Foundation
let snapshotsPath = CommandLine.arguments[1]
let arrayName = CommandLine.arguments[2]
let jsonFiles = loadJsonFiles().filter(isSupported(file:))
print("""
let \(arrayName) = [
\(jsonFiles.map(\.description).joined(separator: ",\n"))
]
""")
private func loadJsonFiles() -> [JsonFile] {
guard let paths = try? FileManager.default.subpathsOfDirectory(atPath: snapshotsPath) else {
return []
}
return paths.compactMap { path -> JsonFile? in
JsonFile(
absolutePath: snapshotsPath + "/" + path,
relativePath: path
)
}.filter { $0.absolutePath.contains(".json") }
}
private func isSupported(file: JsonFile) -> Bool {
guard let jsonDict = jsonDict(fileName: file.absolutePath) else { return false }
guard let platforms = jsonDict["platforms"] as? [String] else {
return true
}
return platforms.contains { $0 == "ios" }
}
private func jsonDict(fileName: String) -> [String: Any]? {
guard let data = FileManager.default.contents(atPath: fileName) else {
return nil
}
return (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
}
struct JsonFile: CustomStringConvertible {
let absolutePath: String
let relativePath: String
var description: String {
"""
JsonFile(absolutePath: "\(absolutePath)", relativePath: "\(relativePath)")
"""
}
}