Change error handling to print message before exiting script (error description will appear in error tab)

This commit is contained in:
Thibaut Schmitt 2022-01-03 10:55:34 +01:00
parent 669f8875aa
commit 4973b245ad
8 changed files with 37 additions and 47 deletions

View File

@ -1,7 +1,7 @@
#/bin/bash #/bin/bash
# Font # Font
swift run -c release FontToolCore "./Fonts/sampleFontsAll.txt" \ swift run -c release FontToolCore "./Fonts/sampleFontsAl.txt" \
--extension-output-path "./Fonts/Generated" \ --extension-output-path "./Fonts/Generated" \
--extension-name "R2Font" \ --extension-name "R2Font" \
--extension-suffix "GenAllScript" --extension-suffix "GenAllScript"

View File

@ -17,37 +17,19 @@ enum ColorToolError: Error {
var description: String { var description: String {
switch self { switch self {
case .badFormat(let info): case .badFormat(let info):
return """ return "error:[ColorTool] Bad line format: \(info). Accepted format are: colorName=\"#RGB/#ARGB\"; colorName \"#RGB/#ARGB\"; colorName \"#RGB/#ARGB\" \"#RGB/#ARGB\""
[ColorTool]
Bad line format: \(info). Accepted format are:
- colorName="#RGB/#ARGB"
- colorName "#RGB/#ARGB"
- colorName "#RGB/#ARGB" "#RGB/#ARGB"
"""
case .writeAsset(let info): case .writeAsset(let info):
return """ return "error:[ColorTool] An error occured while writing color in Xcasset: \(info)"
[ColorTool]
An error occured while writing color in Xcasset: \(info)
"""
case .writeExtension(let filename, let info): case .writeExtension(let filename, let info):
return """ return "error:[ColorTool] An error occured while writing extension in \(filename): \(info)"
[ColorTool]
An error occured while writing extension in \(filename): \(info)
"""
case .fileNotExists(let filename): case .fileNotExists(let filename):
return """ return "error:[ColorTool] File \(filename) does not exists"
[ColorTool]
File \(filename) does not exists
"""
case .badColorDefinition(let lightColor, let darkColor): case .badColorDefinition(let lightColor, let darkColor):
return """ return "error:[ColorTool]One of these two colors has invalid synthax: -\(lightColor)- or -\(darkColor)-"
[ColorTool]
One of these two colors has invalid synthax: -\(lightColor)- or -\(darkColor)-
"""
} }
} }
} }

View File

@ -34,7 +34,9 @@ struct ColorXcassetHelper {
do { do {
try color.contentsJSON().write(to: contentsJsonPathURL, atomically: true, encoding: .utf8) try color.contentsJSON().write(to: contentsJsonPathURL, atomically: true, encoding: .utf8)
} catch (let error) { } catch (let error) {
ColorTool.exit(withError: ColorToolError.writeAsset(error.localizedDescription)) let error = ColorToolError.writeAsset(error.localizedDescription)
print(error.localizedDescription)
ColorTool.exit(withError: error)
} }
} }
} }

View File

@ -25,7 +25,9 @@ struct GenColor {
} }
guard allComponents.contains(true) == false else { guard allComponents.contains(true) == false else {
ColorTool.exit(withError: ColorToolError.badColorDefinition(light, dark)) let error = ColorToolError.badColorDefinition(light, dark)
print(error.localizedDescription)
ColorTool.exit(withError: error)
} }
return """ return """

View File

@ -52,10 +52,14 @@ struct ColorTool: ParsableCommand {
// Check requirements // Check requirements
let fileManager = FileManager() let fileManager = FileManager()
guard fileManager.fileExists(atPath: xcassetsPath) else { guard fileManager.fileExists(atPath: xcassetsPath) else {
ColorTool.exit(withError: ColorToolError.fileNotExists(xcassetsPath)) let error = ColorToolError.fileNotExists(xcassetsPath)
print(error.localizedDescription)
ColorTool.exit(withError: error)
} }
guard fileManager.fileExists(atPath: inputFile) else { guard fileManager.fileExists(atPath: inputFile) else {
ColorTool.exit(withError: ColorToolError.fileNotExists(xcassetsPath)) let error = ColorToolError.fileNotExists(inputFile)
print(error.localizedDescription)
ColorTool.exit(withError: error)
} }
// Check if needed to regenerate // Check if needed to regenerate
@ -103,7 +107,9 @@ struct ColorTool: ParsableCommand {
do { do {
try extensionContent.write(to: extensionFilePathURL, atomically: true, encoding: .utf8) try extensionContent.write(to: extensionFilePathURL, atomically: true, encoding: .utf8)
} catch (let error) { } catch (let error) {
ColorTool.exit(withError: ColorToolError.writeExtension(extensionFilePath, error.localizedDescription)) let error = ColorToolError.writeExtension(extensionFilePath, error.localizedDescription)
print(error.localizedDescription)
ColorTool.exit(withError: error)
} }
} }
@ -128,7 +134,9 @@ struct ColorTool: ParsableCommand {
let colorContent = colorLineCleanedUp.split(separator: " ") let colorContent = colorLineCleanedUp.split(separator: " ")
guard colorContent.count >= 2 else { guard colorContent.count >= 2 else {
ColorTool.exit(withError: ColorToolError.badFormat(colorLine)) let error = ColorToolError.badFormat(colorLine)
print(error.localizedDescription)
ColorTool.exit(withError: error)
} }
switch colorStyle { switch colorStyle {

View File

@ -13,26 +13,16 @@ enum FontToolError: Error {
case inputFolderNotFound(String) case inputFolderNotFound(String)
case fileNotExists(String) case fileNotExists(String)
var description: String { var localizedDescription: String {
switch self { switch self {
case .fcScan(let path, let code, let output): case .fcScan(let path, let code, let output):
return """ return "error:[FontTool] Error while getting fontName (fc-scan --format %{postscriptname} \(path). fc-scan exit with \(code) and output is: \(output ?? "no output")"
error: [FontTool]
Error while getting fontName (fc-scan --format %{postscriptname} \(path).
fc-scan exit with \(code) and output is: \(output ?? "no output")
"""
case .inputFolderNotFound(let inputFolder): case .inputFolderNotFound(let inputFolder):
return """ return " error:[FontTool] Input folder not found: \(inputFolder)"
error: [FontTool]
Input folder not found: \(inputFolder)
"""
case .fileNotExists(let filename): case .fileNotExists(let filename):
return """ return " error:[FontTool] File \(filename) does not exists "
error: [FontTool]
File \(filename) does not exists
"""
} }
} }
} }

View File

@ -13,7 +13,9 @@ class FontToolHelper {
// Get a enumerator for all files // Get a enumerator for all files
let fileManager = FileManager() let fileManager = FileManager()
guard fileManager.fileExists(atPath: inputFolder) else { guard fileManager.fileExists(atPath: inputFolder) else {
FontTool.exit(withError: FontToolError.inputFolderNotFound(inputFolder)) let error = FontToolError.inputFolderNotFound(inputFolder)
print(error.localizedDescription)
FontTool.exit(withError: error)
} }
let enumerator: FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: inputFolder)! let enumerator: FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: inputFolder)!
@ -41,7 +43,9 @@ class FontToolHelper {
let task = Shell.shell("fc-scan", "--format", "%{postscriptname}", path) let task = Shell.shell("fc-scan", "--format", "%{postscriptname}", path)
guard let fontName = task.output, task.terminationStatus == 0 else { guard let fontName = task.output, task.terminationStatus == 0 else {
FontTool.exit(withError: FontToolError.fcScan(path, task.terminationStatus, task.output)) let error = FontToolError.fcScan(path, task.terminationStatus, task.output)
print(error.localizedDescription)
FontTool.exit(withError: error)
} }
return fontName return fontName

View File

@ -41,7 +41,9 @@ struct FontTool: ParsableCommand {
// Check requirements // Check requirements
let fileManager = FileManager() let fileManager = FileManager()
guard fileManager.fileExists(atPath: inputFile) else { guard fileManager.fileExists(atPath: inputFile) else {
FontTool.exit(withError: FontToolError.fileNotExists(inputFile)) let error = FontToolError.fileNotExists(inputFile)
print(error.localizedDescription)
FontTool.exit(withError: error)
} }
// Check if needed to regenerate // Check if needed to regenerate