Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed May 15, 2022
1 parent 21fe34a commit 90229ff
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 62 deletions.
20 changes: 9 additions & 11 deletions Sources/Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -72,7 +70,7 @@ struct Create: ParsableCommand {
successMessage = "Icon successfully created."
}

print(successMessage.ansiGreen)
print(successMessage.foregroundColor(.green))
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/CreationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 16 additions & 31 deletions Sources/IconSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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()
Expand All @@ -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)
}
Expand Down
30 changes: 15 additions & 15 deletions createicns.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -40,7 +40,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
71CEA02327DDA1BB00E747B8 /* Prism in Frameworks */,
718F2F6E2830B73C00347BDA /* Prism in Frameworks */,
71CEA02027DDA18D00E747B8 /* ArgumentParser in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -93,7 +93,7 @@
name = createicns;
packageProductDependencies = (
71CEA01F27DDA18D00E747B8 /* ArgumentParser */,
71CEA02227DDA1BB00E747B8 /* Prism */,
718F2F6D2830B73C00347BDA /* Prism */,
);
productName = createicns;
productReference = 71CEA01427DDA16600E747B8 /* createicns */;
Expand Down Expand Up @@ -125,7 +125,7 @@
mainGroup = 71CEA00B27DDA16600E747B8;
packageReferences = (
71CEA01E27DDA18D00E747B8 /* XCRemoteSwiftPackageReference "swift-argument-parser" */,
71CEA02127DDA1BB00E747B8 /* XCRemoteSwiftPackageReference "Prism" */,
718F2F6C2830B73C00347BDA /* XCRemoteSwiftPackageReference "Prism" */,
);
productRefGroup = 71CEA01527DDA16600E747B8 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -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 */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/jordanbaird/Prism",
"state" : {
"revision" : "480b3ddb41aad3981d6258e4c6f50f6494cc9504",
"version" : "0.1.0"
"revision" : "dbd328ce41e63e35ac13989c3856c7aac86f38e8",
"version" : "0.0.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "e394bf350e38cb100b6bc4172834770ede1b7232",
"version" : "1.0.3"
"revision" : "f3c9084a71ef4376f2fabbdf1d3d90a49f1fabdb",
"version" : "1.1.2"
}
}
],
Expand Down

0 comments on commit 90229ff

Please sign in to comment.