88 lines
2.7 KiB
Swift
88 lines
2.7 KiB
Swift
//
|
|
// ImageExtensionGenerator.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 14/02/2022.
|
|
//
|
|
|
|
import CLIToolCore
|
|
import Foundation
|
|
|
|
class ImageExtensionGenerator {
|
|
|
|
// MARK: - Extension files
|
|
|
|
static func writeStringsFiles(images: [ImageToGen], staticVar: Bool, inputFilename: String, extensionName: String, extensionFilePath: String) {
|
|
// Get header/footer
|
|
let extensionHeader = Self.getHeader(inputFilename: inputFilename, extensionClassname: extensionName)
|
|
let extensionFooter = Self.getFooter()
|
|
|
|
// Create content
|
|
let extensionContent: String = {
|
|
var content = ""
|
|
images.forEach { img in
|
|
if staticVar {
|
|
content += "\n\(img.getStaticImageProperty())"
|
|
} else {
|
|
content += "\n\(img.getImageProperty())"
|
|
}
|
|
content += "\n "
|
|
}
|
|
return content
|
|
}()
|
|
|
|
// Create file if not exists
|
|
let fileManager = FileManager()
|
|
if fileManager.fileExists(atPath: extensionFilePath) == false {
|
|
Shell.shell("touch", "\(extensionFilePath)")
|
|
}
|
|
|
|
// Generate extension
|
|
Self.generateExtensionFile(extensionFilePath: extensionFilePath, extensionHeader, extensionContent, extensionFooter)
|
|
}
|
|
|
|
// MARK: - pragm
|
|
|
|
private static func generateExtensionFile(extensionFilePath: String, _ args: String...) {
|
|
// Create file if not exists
|
|
let fileManager = FileManager()
|
|
if fileManager.fileExists(atPath: extensionFilePath) == false {
|
|
Shell.shell("touch", "\(extensionFilePath)")
|
|
}
|
|
|
|
// Create extension content
|
|
let extensionContent = args.joined(separator: "\n")
|
|
|
|
// Write content
|
|
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
|
|
do {
|
|
try extensionContent.write(to: extensionFilePathURL, atomically: true, encoding: .utf8)
|
|
} catch (let error) {
|
|
let error = ImagiumError.writeFile(extensionFilePath, error.localizedDescription)
|
|
print(error.localizedDescription)
|
|
Imagium.exit(withError: error)
|
|
}
|
|
}
|
|
|
|
private static func getHeader(inputFilename: String, extensionClassname: String) -> String {
|
|
"""
|
|
// Generated from Imagium at \(Date())
|
|
// Images from \(inputFilename)
|
|
|
|
import UIKit
|
|
|
|
extension \(extensionClassname) {
|
|
"""
|
|
}
|
|
|
|
private static func getFooter() -> String {
|
|
"""
|
|
}
|
|
"""
|
|
}
|
|
}
|
|
|
|
//@objc var onboarding_foreground3: UIImage {
|
|
// return UIImage(named: "onboarding_foreground3")!
|
|
// }
|