Bugs fixes, Lint fixes, Refactoring
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit

This commit is contained in:
2022-08-29 13:38:46 +02:00
parent e3f90e0d48
commit a54a264447
35 changed files with 652 additions and 401 deletions

View File

@ -12,7 +12,7 @@ class ImageExtensionGenerator {
// MARK: - Extension files
static func writeStringsFiles(images: [ImageToGen], staticVar: Bool, inputFilename: String, extensionName: String, extensionFilePath: String) {
static func writeStringsFiles(images: [ParsedImage], staticVar: Bool, inputFilename: String, extensionName: String, extensionFilePath: String) {
// Get header/footer
let extensionHeader = Self.getHeader(inputFilename: inputFilename, extensionClassname: extensionName)
let extensionFooter = Self.getFooter()

View File

@ -22,7 +22,7 @@ class XcassetsGenerator {
// MARK: - Assets generation
func generateXcassets(inputPath: String, imagesToGenerate: [ImageToGen], xcassetsPath: String) {
func generateXcassets(inputPath: String, imagesToGenerate: [ParsedImage], xcassetsPath: String) {
let fileManager = FileManager()
let svgConverter = Imagium.getSvgConverterPath()
let allSubFiles = fileManager.getAllRegularFileIn(directory: inputPath)
@ -30,30 +30,30 @@ class XcassetsGenerator {
var generatedAssetsPaths = [String]()
// Generate new assets
imagesToGenerate.forEach { imageToGen in
imagesToGenerate.forEach { parsedImage in
// Get image path
let imageData: (path: String, ext: String) = {
for subfile in allSubFiles {
if subfile.hasSuffix("/" + imageToGen.name + ".svg") {
if subfile.hasSuffix("/" + parsedImage.name + ".svg") {
return (subfile, "svg")
}
if subfile.hasSuffix("/" + imageToGen.name + ".png") {
if subfile.hasSuffix("/" + parsedImage.name + ".png") {
return (subfile, "png")
}
if subfile.hasSuffix("/" + imageToGen.name + ".jpg") {
if subfile.hasSuffix("/" + parsedImage.name + ".jpg") {
return (subfile, "jpg")
}
if subfile.hasSuffix("/" + imageToGen.name + ".jepg") {
if subfile.hasSuffix("/" + parsedImage.name + ".jepg") {
return (subfile, "jepg")
}
}
let error = ImagiumError.unknownImageExtension(imageToGen.name)
let error = ImagiumError.unknownImageExtension(parsedImage.name)
print(error.localizedDescription)
Imagium.exit(withError: error)
}()
// Create imageset folder
let imagesetName = "\(imageToGen.name).imageset"
let imagesetName = "\(parsedImage.name).imageset"
let imagesetPath = "\(xcassetsPath)/\(imagesetName)"
Shell.shell("mkdir", "-p", imagesetPath)
@ -61,18 +61,18 @@ class XcassetsGenerator {
generatedAssetsPaths.append(imagesetName)
// Generate output images path
let output1x = "\(imagesetPath)/\(imageToGen.name).\(XcassetsGenerator.outputImageExtension)"
let output2x = "\(imagesetPath)/\(imageToGen.name)@2x.\(XcassetsGenerator.outputImageExtension)"
let output3x = "\(imagesetPath)/\(imageToGen.name)@3x.\(XcassetsGenerator.outputImageExtension)"
let output1x = "\(imagesetPath)/\(parsedImage.name).\(XcassetsGenerator.outputImageExtension)"
let output2x = "\(imagesetPath)/\(parsedImage.name)@2x.\(XcassetsGenerator.outputImageExtension)"
let output3x = "\(imagesetPath)/\(parsedImage.name)@3x.\(XcassetsGenerator.outputImageExtension)"
// Check if we need to convert image
if self.shouldBypassGeneration(for: imageToGen, xcassetImagePath: output1x) {
print("\(imageToGen.name) -> Not regenerating")
if self.shouldBypassGeneration(for: parsedImage, xcassetImagePath: output1x) {
print("\(parsedImage.name) -> Not regenerating")
return
}
// Convert image
let convertArguments = imageToGen.convertArguments
let convertArguments = parsedImage.convertArguments
if imageData.ext == "svg" {
// /usr/local/bin/rsvg-convert path/to/image.png -w 200 -h 300 -o path/to/output.png
// /usr/local/bin/rsvg-convert path/to/image.png -w 200 -o path/to/output.png
@ -102,7 +102,7 @@ class XcassetsGenerator {
}
// Write Content.json
let imagesetContentJson = imageToGen.contentJson
let imagesetContentJson = parsedImage.contentJson
let contentJsonFilePath = "\(imagesetPath)/Contents.json"
if fileManager.fileExists(atPath: contentJsonFilePath) == false {
Shell.shell("touch", "\(contentJsonFilePath)")
@ -111,7 +111,7 @@ class XcassetsGenerator {
let contentJsonFilePathURL = URL(fileURLWithPath: contentJsonFilePath)
try! imagesetContentJson.write(to: contentJsonFilePathURL, atomically: true, encoding: .utf8)
print("\(imageToGen.name) -> Generated")
print("\(parsedImage.name) -> Generated")
}
// Success info
@ -147,7 +147,7 @@ class XcassetsGenerator {
// MARK: - Helpers: bypass generation
private func shouldBypassGeneration(for image: ImageToGen, xcassetImagePath: String) -> Bool {
private func shouldBypassGeneration(for image: ParsedImage, xcassetImagePath: String) -> Bool {
guard forceGeneration == false else {
return false
}

View File

@ -1,5 +1,5 @@
//
// File.swift
// ParsedImage.swift
//
//
// Created by Thibaut Schmitt on 24/01/2022.
@ -7,7 +7,7 @@
import Foundation
struct ImageToGen {
struct ParsedImage {
let name: String
let tags: String
let width: Int

View File

@ -0,0 +1,13 @@
//
// PlatormTag.swift
//
//
// Created by Thibaut Schmitt on 29/08/2022.
//
import Foundation
enum PlatormTag: String {
case droid = "d"
case ios = "i"
}

View File

@ -9,11 +9,11 @@ import Foundation
class ImageFileParser {
static func parse(_ inputFile: String, platform: PlatormTag) -> [ImageToGen] {
static func parse(_ inputFile: String, platform: PlatormTag) -> [ParsedImage] {
let inputFileContent = try! String(contentsOfFile: inputFile, encoding: .utf8)
let stringsByLines = inputFileContent.components(separatedBy: .newlines)
var imagesToGenerate = [ImageToGen]()
var imagesToGenerate = [ParsedImage]()
// Parse file
stringsByLines.forEach {
@ -36,7 +36,7 @@ class ImageFileParser {
return Int(splittedLine[3])!
}()
let image = ImageToGen(name: String(splittedLine[1]), tags: String(splittedLine[0]), width: width, height: height)
let image = ParsedImage(name: String(splittedLine[1]), tags: String(splittedLine[0]), width: width, height: height)
imagesToGenerate.append(image)
}

View File

@ -9,21 +9,22 @@ import Foundation
import ArgumentParser
import ToolCore
enum PlatormTag: String {
case droid = "d"
case ios = "i"
}
struct Imagium: ParsableCommand {
// MARK: - CommandConfiguration
static var configuration = CommandConfiguration(
abstract: "A utility for generate images.",
abstract: "A utility for generate images and an extension to access them easily.",
version: "0.1.0"
)
// MARK: - Static
static let toolName = "Imagium"
static let defaultExtensionName = "UIImage"
// MARK: - Properties
var extensionFileName: String { "\(options.extensionName)+\(options.extensionSuffix).swift" }
var extensionFilePath: String { "\(options.extensionOutputPath)/\(extensionFileName)" }
var inputFilenameWithoutExt: String {
@ -32,8 +33,12 @@ struct Imagium: ParsableCommand {
.lastPathComponent
}
// MARK: - Command Options
@OptionGroup var options: ImagiumOptions
// MARK: - Run
mutating func run() {
print("[\(Self.toolName)] Starting images generation")