Remove Shell invocation as many as possible (high cost in term of speed of execution)
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:
@ -52,10 +52,9 @@ class XcassetsGenerator {
|
||||
Images.exit(withError: error)
|
||||
}()
|
||||
|
||||
// Create imageset folder
|
||||
// Create imageset folder name
|
||||
let imagesetName = "\(parsedImage.name).imageset"
|
||||
let imagesetPath = "\(xcassetsPath)/\(imagesetName)"
|
||||
Shell.shell(["mkdir", "-p", imagesetPath])
|
||||
|
||||
// Store managed images path
|
||||
generatedAssetsPaths.append(imagesetName)
|
||||
@ -66,11 +65,23 @@ class XcassetsGenerator {
|
||||
let output3x = "\(imagesetPath)/\(parsedImage.name)@3x.\(XcassetsGenerator.outputImageExtension)"
|
||||
|
||||
// Check if we need to convert image
|
||||
if self.shouldBypassGeneration(for: parsedImage, xcassetImagePath: output1x) {
|
||||
print("\(parsedImage.name) -> Not regenerating")
|
||||
guard self.shouldGenerate(inputImagePath: imageData.path, xcassetImagePath: output1x) else {
|
||||
//print("\(parsedImage.name) -> Not regenerating")
|
||||
return
|
||||
}
|
||||
|
||||
// Create imageset folder
|
||||
if fileManager.fileExists(atPath: imagesetPath) == false {
|
||||
do {
|
||||
try fileManager.createDirectory(atPath: imagesetPath,
|
||||
withIntermediateDirectories: true)
|
||||
} catch {
|
||||
let error = ImagesError.createAssetFolder(imagesetPath)
|
||||
print(error.localizedDescription)
|
||||
Images.exit(withError: error)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert image
|
||||
let convertArguments = parsedImage.convertArguments
|
||||
if imageData.ext == "svg" {
|
||||
@ -110,12 +121,9 @@ class XcassetsGenerator {
|
||||
// Write Content.json
|
||||
let imagesetContentJson = parsedImage.contentJson
|
||||
let contentJsonFilePath = "\(imagesetPath)/Contents.json"
|
||||
if fileManager.fileExists(atPath: contentJsonFilePath) == false {
|
||||
Shell.shell(["touch", "\(contentJsonFilePath)"])
|
||||
}
|
||||
|
||||
let contentJsonFilePathURL = URL(fileURLWithPath: contentJsonFilePath)
|
||||
try! imagesetContentJson.write(to: contentJsonFilePathURL, atomically: true, encoding: .utf8)
|
||||
try! imagesetContentJson.write(to: contentJsonFilePathURL, atomically: false, encoding: .utf8)
|
||||
|
||||
print("\(parsedImage.name) -> Generated")
|
||||
}
|
||||
@ -153,43 +161,11 @@ class XcassetsGenerator {
|
||||
|
||||
// MARK: - Helpers: bypass generation
|
||||
|
||||
private func shouldBypassGeneration(for image: ParsedImage, xcassetImagePath: String) -> Bool {
|
||||
private func shouldGenerate(inputImagePath: String, xcassetImagePath: String) -> Bool {
|
||||
if forceGeneration {
|
||||
return false
|
||||
}
|
||||
|
||||
let fileManager = FileManager()
|
||||
|
||||
// File not exists -> do not bypass
|
||||
guard fileManager.fileExists(atPath: xcassetImagePath) else {
|
||||
return false
|
||||
}
|
||||
|
||||
// Info unavailable -> do not bypass
|
||||
let taskWidth = Shell.shell(["identify", "-format", "%w", xcassetImagePath])
|
||||
let taskHeight = Shell.shell(["identify", "-format", "%h", xcassetImagePath])
|
||||
guard taskWidth.terminationStatus == 0,
|
||||
taskHeight.terminationStatus == 0 else {
|
||||
return false
|
||||
}
|
||||
|
||||
let currentWidth = Int(taskWidth.output ?? "-1") ?? -1
|
||||
let currentheight = Int(taskHeight.output ?? "-1") ?? -1
|
||||
|
||||
// Info unavailable -> do not bypass
|
||||
guard currentWidth > 0 && currentheight > 0 else {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check width and height
|
||||
if image.width != -1 && currentWidth == image.width {
|
||||
return true
|
||||
}
|
||||
if image.height != -1 && currentheight == image.height {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
return GeneratorChecker.isFile(inputImagePath, moreRecenThan: xcassetImagePath)
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user