From 90229ff834e1a21cabadb93df1c2612120eb506b Mon Sep 17 00:00:00 2001 From: Jordan Baird Date: Sun, 15 May 2022 00:28:07 -0600 Subject: [PATCH] Refactoring --- Sources/Create.swift | 20 ++++---- Sources/CreationError.swift | 4 +- Sources/IconSet.swift | 47 +++++++------------ createicns.xcodeproj/project.pbxproj | 30 ++++++------ .../xcshareddata/swiftpm/Package.resolved | 8 ++-- 5 files changed, 47 insertions(+), 62 deletions(-) diff --git a/Sources/Create.swift b/Sources/Create.swift index 1b59ab1..fc704e1 100644 --- a/Sources/Create.swift +++ b/Sources/Create.swift @@ -19,24 +19,22 @@ struct Create: ParsableCommand { }() @Argument(help: """ - An image file from which to create an icon. The image's width and height \ - must be equal. - + An image file from which to create an icon. The image's width and \ + height must be equal.\n """) var input: String @Argument(help: """ - The output path of the icon. The path must have the 'icns' file extension. \ - If no output is provided, the icon will be saved in the same parent directory \ - as the input. - + The output path of the icon. The path must have the 'icns' file \ + extension. If no output is provided, the icon will be saved in the \ + same parent directory as the input.\n """) var output: String? @Flag(name: [.customShort("s"), .customLong("iconset")], help: """ - Convert the input into an iconset file instead of icns. If this option is \ - provided, the output path must have the 'iconset' extension instead of 'icns'. - + Convert the input into an iconset file instead of icns. If this option \ + is provided, the output path must have the 'iconset' extension instead \ + of 'icns'.\n """) var convertToIconSet = false @@ -72,7 +70,7 @@ struct Create: ParsableCommand { successMessage = "Icon successfully created." } - print(successMessage.ansiGreen) + print(successMessage.foregroundColor(.green)) } } diff --git a/Sources/CreationError.swift b/Sources/CreationError.swift index b1162b1..69e132c 100644 --- a/Sources/CreationError.swift +++ b/Sources/CreationError.swift @@ -12,7 +12,9 @@ import Prism struct CreationError: LocalizedError { let message: String - var errorDescription: String? { message.ansiRed } + var errorDescription: String? { + message.foregroundColor(.red) + } init(_ message: String) { self.message = message diff --git a/Sources/IconSet.swift b/Sources/IconSet.swift index f5e6da6..1fff0ea 100644 --- a/Sources/IconSet.swift +++ b/Sources/IconSet.swift @@ -10,11 +10,9 @@ import Cocoa /// Represents an 'iconset' file. struct IconSet { - typealias IconDimensions = (width: CGFloat, height: CGFloat, stringRepresentation: String) - typealias Icon = (data: Data, name: String) - private let dimensions: [IconDimensions] = [ + private let dimensions: [(CGFloat, CGFloat, String)] = [ (16, 16, "16x16"), (32, 32, "16x16@2x"), (32, 32, "32x32"), @@ -27,31 +25,21 @@ struct IconSet { (1024, 1024, "512x512@2x") ] - var icons: [Icon] { _icons } - - private var _icons = [Icon]() - - let image: NSImage + private(set) var icons = [Icon]() init(image: NSImage) throws { - self.image = image - _icons = try dimensions.map { - try createImageFile( - from: image, - size: .init(width: $0.width, height: $0.height), - name: $0.stringRepresentation) + icons = try dimensions.map { + try createIcon(from: image, width: $0.0, height: $0.1, name: $0.2) } } - private func createImageFile( + private func createIcon( from image: NSImage, - size: NSSize, + width: CGFloat, + height: CGFloat, name: String ) throws -> Icon { - let size = NSSize( - width: size.width / 2, - height: size.height / 2) - + let size = CGSize(width: width / 2, height: height / 2) let newImage = NSImage(size: size) newImage.lockFocus() @@ -62,29 +50,26 @@ struct IconSet { fraction: 1) newImage.unlockFocus() - let rep = NSBitmapImageRep(data: newImage.tiffRepresentation!) - guard let pngData = rep?.representation( - using: .png, - properties: [:]) + guard + let tiffRep = newImage.tiffRepresentation, + let bitmapRep = NSBitmapImageRep(data: tiffRep), + let pngData = bitmapRep.representation(using: .png, properties: [:]) else { - throw CreationError("Could not create png data for iconset.") + throw CreationError("Could not create data for iconset.") } + return Icon(data: pngData, name: name) } func write(to url: URL) throws { do { - try FileManager.default.createDirectory( - at: url, - withIntermediateDirectories: true) + try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) } catch { throw CreationError(error.localizedDescription) } for icon in icons { do { - try icon.data.write( - to: url.appendingPathComponent( - "icon_" + icon.name + ".png")) + try icon.data.write(to: url.appendingPathComponent("icon_" + icon.name + ".png")) } catch { throw CreationError(error.localizedDescription) } diff --git a/createicns.xcodeproj/project.pbxproj b/createicns.xcodeproj/project.pbxproj index 0ba6719..85abbeb 100644 --- a/createicns.xcodeproj/project.pbxproj +++ b/createicns.xcodeproj/project.pbxproj @@ -7,8 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 718F2F6E2830B73C00347BDA /* Prism in Frameworks */ = {isa = PBXBuildFile; productRef = 718F2F6D2830B73C00347BDA /* Prism */; }; 71CEA02027DDA18D00E747B8 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 71CEA01F27DDA18D00E747B8 /* ArgumentParser */; }; - 71CEA02327DDA1BB00E747B8 /* Prism in Frameworks */ = {isa = PBXBuildFile; productRef = 71CEA02227DDA1BB00E747B8 /* Prism */; }; 71CEA02527DDA1CA00E747B8 /* Create.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71CEA02427DDA1CA00E747B8 /* Create.swift */; }; 71CEA02727DDA1CF00E747B8 /* IconSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71CEA02627DDA1CF00E747B8 /* IconSet.swift */; }; 71CEA02927DDA1D300E747B8 /* IconUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71CEA02827DDA1D300E747B8 /* IconUtil.swift */; }; @@ -40,7 +40,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 71CEA02327DDA1BB00E747B8 /* Prism in Frameworks */, + 718F2F6E2830B73C00347BDA /* Prism in Frameworks */, 71CEA02027DDA18D00E747B8 /* ArgumentParser in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -93,7 +93,7 @@ name = createicns; packageProductDependencies = ( 71CEA01F27DDA18D00E747B8 /* ArgumentParser */, - 71CEA02227DDA1BB00E747B8 /* Prism */, + 718F2F6D2830B73C00347BDA /* Prism */, ); productName = createicns; productReference = 71CEA01427DDA16600E747B8 /* createicns */; @@ -125,7 +125,7 @@ mainGroup = 71CEA00B27DDA16600E747B8; packageReferences = ( 71CEA01E27DDA18D00E747B8 /* XCRemoteSwiftPackageReference "swift-argument-parser" */, - 71CEA02127DDA1BB00E747B8 /* XCRemoteSwiftPackageReference "Prism" */, + 718F2F6C2830B73C00347BDA /* XCRemoteSwiftPackageReference "Prism" */, ); productRefGroup = 71CEA01527DDA16600E747B8 /* Products */; projectDirPath = ""; @@ -312,35 +312,35 @@ /* End XCConfigurationList section */ /* Begin XCRemoteSwiftPackageReference section */ - 71CEA01E27DDA18D00E747B8 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = { + 718F2F6C2830B73C00347BDA /* XCRemoteSwiftPackageReference "Prism" */ = { isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/apple/swift-argument-parser.git"; + repositoryURL = "https://github.com/jordanbaird/Prism"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 1.0.0; + minimumVersion = 0.0.1; }; }; - 71CEA02127DDA1BB00E747B8 /* XCRemoteSwiftPackageReference "Prism" */ = { + 71CEA01E27DDA18D00E747B8 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = { isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/jordanbaird/Prism"; + repositoryURL = "https://github.com/apple/swift-argument-parser.git"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 0.1.0; + minimumVersion = 1.0.0; }; }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ + 718F2F6D2830B73C00347BDA /* Prism */ = { + isa = XCSwiftPackageProductDependency; + package = 718F2F6C2830B73C00347BDA /* XCRemoteSwiftPackageReference "Prism" */; + productName = Prism; + }; 71CEA01F27DDA18D00E747B8 /* ArgumentParser */ = { isa = XCSwiftPackageProductDependency; package = 71CEA01E27DDA18D00E747B8 /* XCRemoteSwiftPackageReference "swift-argument-parser" */; productName = ArgumentParser; }; - 71CEA02227DDA1BB00E747B8 /* Prism */ = { - isa = XCSwiftPackageProductDependency; - package = 71CEA02127DDA1BB00E747B8 /* XCRemoteSwiftPackageReference "Prism" */; - productName = Prism; - }; /* End XCSwiftPackageProductDependency section */ }; rootObject = 71CEA00C27DDA16600E747B8 /* Project object */; diff --git a/createicns.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/createicns.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index dcfbeb1..d580c3c 100644 --- a/createicns.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/createicns.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/jordanbaird/Prism", "state" : { - "revision" : "480b3ddb41aad3981d6258e4c6f50f6494cc9504", - "version" : "0.1.0" + "revision" : "dbd328ce41e63e35ac13989c3856c7aac86f38e8", + "version" : "0.0.1" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-argument-parser.git", "state" : { - "revision" : "e394bf350e38cb100b6bc4172834770ede1b7232", - "version" : "1.0.3" + "revision" : "f3c9084a71ef4376f2fabbdf1d3d90a49f1fabdb", + "version" : "1.1.2" } } ],