Skip to content

Commit 604b02a

Browse files
authored
Reformat code and resolve manual linter complaints (#38)
Signed-off-by: Henrik Panhans <[email protected]>
1 parent 5fc94d8 commit 604b02a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+363
-221
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ let package = Package(
1313
],
1414
dependencies: [
1515
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
16-
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.0")
16+
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.0"),
1717
],
1818
targets: [
1919
.executableTarget(
2020
name: "SwiftFrame",
2121
dependencies: [
2222
"SwiftFrameCore",
23-
.product(name: "ArgumentParser", package: "swift-argument-parser")
23+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
2424
]
2525
),
2626
.target(name: "SwiftFrameCore", dependencies: ["Yams"]),
27-
.testTarget(name: "SwiftFrameTests", dependencies: ["SwiftFrameCore"])
27+
.testTarget(name: "SwiftFrameTests", dependencies: ["SwiftFrameCore"]),
2828
]
2929
)

Sources/SwiftFrame/Render.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ struct Render: ParsableCommand {
2828

2929
@Flag(
3030
name: .long,
31-
help: "Outputs the whole image canvas into the output directories before slicing it up into the correct screenshot sizes. Helpful for troubleshooting"
31+
help:
32+
"Outputs the whole image canvas into the output directories before slicing it up into the correct screenshot sizes. Helpful for troubleshooting"
3233
)
3334
var outputWholeImage = false
3435

Sources/SwiftFrame/Scaffold.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct Scaffold: ParsableCommand, VerbosePrintable {
1717

1818
@Flag
1919
var noHelperFiles = false
20-
20+
2121
@Flag
2222
var verbose = false
2323

@@ -43,7 +43,7 @@ struct Scaffold: ParsableCommand, VerbosePrintable {
4343
templatesDirectoryURL,
4444
]
4545

46-
try directoriesToCreate.forEach { directory in
46+
for directory in directoriesToCreate {
4747
printVerbose("Creating directory \(directory.path)")
4848
numberOfCreatedDirectories += 1
4949
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: nil)
@@ -56,7 +56,7 @@ struct Scaffold: ParsableCommand, VerbosePrintable {
5656
numberOfCreatedFiles += 1
5757
}
5858

59-
try locales.forEach { locale in
59+
for locale in locales {
6060
if shouldCreateHelperFiles {
6161
let localeStringsFileContents = "\"some string key\" = \"the corresponding translation\";"
6262
let localeStringsFileURL = stringsDirectoryURL.appendingPathComponent("\(locale).strings")
@@ -65,17 +65,25 @@ struct Scaffold: ParsableCommand, VerbosePrintable {
6565
numberOfCreatedFiles += 1
6666
}
6767

68-
try Scaffold.defaultDevices.forEach { deviceName in
68+
for deviceName in Scaffold.defaultDevices {
6969
printVerbose("Creating screenshot directory for locale \(locale) and device \(deviceName)")
7070
numberOfCreatedDirectories += 1
71-
71+
7272
let localeScreenshotDirectoryURL = screenshotsDirectoryURL.appendingPathComponent(deviceName).appendingPathComponent(locale)
73-
try FileManager.default.createDirectory(at: localeScreenshotDirectoryURL, withIntermediateDirectories: true, attributes: nil)
74-
73+
try FileManager.default.createDirectory(
74+
at: localeScreenshotDirectoryURL,
75+
withIntermediateDirectories: true,
76+
attributes: nil
77+
)
78+
7579
if shouldCreateHelperFiles {
76-
let markdownContents = "# \(deviceName) - \(locale)\n\nPlace your screenshots for \(deviceName) (\(locale)) in this folder\n" +
77-
"Please make sure that all screenshots that represent the same screen have the same name for every locale."
78-
try FileManager.default.ky_writeToFile(markdownContents, destination: localeScreenshotDirectoryURL.appendingPathComponent("README.md"))
80+
let markdownContents =
81+
"# \(deviceName) - \(locale)\n\nPlace your screenshots for \(deviceName) (\(locale)) in this folder\n"
82+
+ "Please make sure that all screenshots that represent the same screen have the same name for every locale."
83+
try FileManager.default.ky_writeToFile(
84+
markdownContents,
85+
destination: localeScreenshotDirectoryURL.appendingPathComponent("README.md")
86+
)
7987
numberOfCreatedFiles += 1
8088
}
8189
}

Sources/SwiftFrameCore/Config/ConfigData.swift

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import Foundation
21
import AppKit
2+
import Foundation
33

44
protocol ConfigValidateable {
55
func validate() throws
66
func printSummary(insetByTabs tabs: Int)
77
}
88

9-
/// First key is locale, second is regular key in string file
9+
/// First key is locale, second is regular key in string file.
1010
typealias LocalizedStringFiles = [String: [String: String]]
1111

1212
struct ConfigData: Decodable, ConfigValidateable {
@@ -51,8 +51,8 @@ struct ConfigData: Decodable, ConfigValidateable {
5151
textColorSource: ColorSource,
5252
outputFormat: FileFormat,
5353
deviceData: [DeviceData],
54-
localesRegex: String? = nil)
55-
{
54+
localesRegex: String? = nil
55+
) {
5656
self.textGroups = textGroups
5757
self.stringsPath = stringsPath
5858
self.maxFontSize = maxFontSize
@@ -67,15 +67,18 @@ struct ConfigData: Decodable, ConfigValidateable {
6767
// MARK: - Processing
6868

6969
mutating func process() throws {
70-
let regex: Regex<AnyRegexOutput>? = if let localesRegex, !localesRegex.isEmpty {
71-
try Regex(localesRegex)
72-
} else {
73-
nil
74-
}
70+
let regex: Regex<AnyRegexOutput>? =
71+
if let localesRegex, !localesRegex.isEmpty {
72+
try Regex(localesRegex)
73+
} else {
74+
nil
75+
}
7576

7677
deviceData = try deviceData.map { try $0.makeProcessedData(localesRegex: regex) }
7778

78-
let textFiles = try FileManager.default.ky_filesAtPath(stringsPath.absoluteURL, with: "strings").filterByFileOrFoldername(regex: regex)
79+
let textFiles = try FileManager.default.ky_filesAtPath(stringsPath.absoluteURL, with: "strings").filterByFileOrFoldername(
80+
regex: regex
81+
)
7982
let strings = textFiles.compactMap { NSDictionary(contentsOf: $0) as? [String: String] }
8083
titles = Dictionary(uniqueKeysWithValues: zip(textFiles.map({ $0.fileName }), strings))
8184
}
@@ -86,16 +89,20 @@ struct ConfigData: Decodable, ConfigValidateable {
8689
guard !deviceData.isEmpty else {
8790
throw NSError(
8891
description: "No screenshot data was supplied",
89-
expectation: "Please supply at least one screenshot along with metadata")
92+
expectation: "Please supply at least one screenshot along with metadata"
93+
)
9094
}
9195

9296
guard !outputPaths.isEmpty else {
9397
throw NSError(
9498
description: "No output paths were specified",
95-
expectation: "Please specify at least one output directory")
99+
expectation: "Please specify at least one output directory"
100+
)
96101
}
97102

98-
try deviceData.forEach { try $0.validate() }
103+
for device in deviceData {
104+
try device.validate()
105+
}
99106
}
100107

101108
func printSummary(insetByTabs tabs: Int) {
@@ -106,21 +113,24 @@ struct ConfigData: Decodable, ConfigValidateable {
106113
CommandLineFormatter.printKeyValue(
107114
"String Files",
108115
value: titles.isEmpty ? "none" : titles.keys.joined(separator: ", "),
109-
insetBy: tabs)
116+
insetBy: tabs
117+
)
110118

111119
ky_print("Output paths:", insetByTabs: tabs)
112-
outputPaths.forEach { ky_print($0.path.formattedGreen(), insetByTabs: tabs + 1) }
120+
for path in outputPaths {
121+
ky_print(path.path.formattedGreen(), insetByTabs: tabs + 1)
122+
}
113123

114124
ky_print("Device data:", insetByTabs: tabs)
115-
deviceData.forEach {
116-
$0.printSummary(insetByTabs: tabs + 1)
125+
for device in deviceData {
126+
device.printSummary(insetByTabs: tabs + 1)
117127
print()
118128
}
119129

120130
if !textGroups.isEmpty {
121131
print("Text groups:")
122-
textGroups.forEach {
123-
$0.printSummary(insetByTabs: tabs + 1)
132+
for textGroup in textGroups {
133+
textGroup.printSummary(insetByTabs: tabs + 1)
124134
print()
125135
}
126136
} else {
@@ -133,7 +143,7 @@ struct ConfigData: Decodable, ConfigValidateable {
133143
// MARK: - Screenshot Factory
134144

135145
func makeAssociatedStrings(for device: DeviceData, locale: String) throws -> [AssociatedString] {
136-
return try device.textData.map {
146+
try device.textData.map {
137147
guard let title = titles[locale]?[$0.titleIdentifier] else {
138148
throw NSError(description: "Title with key \"\($0.titleIdentifier)\" not found in string file \"\(locale)\"")
139149
}
@@ -142,12 +152,13 @@ struct ConfigData: Decodable, ConfigValidateable {
142152
}
143153

144154
func makeSharedFontSizes(for associatedStrings: [AssociatedString]) throws -> [String: CGFloat] {
145-
return try textGroups.reduce(into: [String: CGFloat]()) { dictionary, group in
155+
try textGroups.reduce(into: [String: CGFloat]()) { dictionary, group in
146156
let strings = associatedStrings.filter({ $0.data.groupIdentifier == group.identifier })
147157
dictionary[group.identifier] = try group.sharedFontSize(
148158
with: strings,
149159
globalFont: try fontSource.font(),
150-
globalMaxSize: maxFontSize)
160+
globalMaxSize: maxFontSize
161+
)
151162
}
152163
}
153164

Sources/SwiftFrameCore/Config/DeviceData.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ struct DeviceData: Decodable, ConfigValidateable {
1616

1717
private(set) var screenshotsGroupedByLocale: [String: [String: URL]]!
1818
private(set) var templateImage: NSBitmapImageRep?
19-
private(set) var screenshotData = [ScreenshotData]()
20-
private(set) var textData = [TextData]()
19+
private(set) var screenshotData: [ScreenshotData] = []
20+
private(set) var textData: [TextData] = []
2121

2222
// MARK: - Coding Keys
2323

@@ -40,10 +40,10 @@ struct DeviceData: Decodable, ConfigValidateable {
4040
numberOfSlices: Int,
4141
screenshotsGroupedByLocale: [String: [String: URL]]? = nil,
4242
templateImage: NSBitmapImageRep? = nil,
43-
screenshotData: [ScreenshotData] = [ScreenshotData](),
44-
textData: [TextData] = [TextData](),
45-
gapWidth: Int = 0)
46-
{
43+
screenshotData: [ScreenshotData] = [],
44+
textData: [TextData] = [],
45+
gapWidth: Int = 0
46+
) {
4747
self.outputSuffixes = outputSuffixes
4848
self.templateImagePath = templateImagePath
4949
self.screenshotsPath = screenshotsPath
@@ -57,24 +57,27 @@ struct DeviceData: Decodable, ConfigValidateable {
5757

5858
// MARK: - Methods
5959

60+
// swift-format:
6061
func makeProcessedData(localesRegex: Regex<AnyRegexOutput>?) throws -> DeviceData {
6162
guard let templateImage = ImageLoader.loadRepresentation(at: templateImagePath.absoluteURL) else {
6263
throw NSError(description: "Error while loading template image at path \(templateImagePath.absoluteString)")
6364
}
6465

65-
var parsedScreenshots = [String: [String: URL]]()
66-
try screenshotsPath.absoluteURL.subDirectories.filterByFileOrFoldername(regex: localesRegex).forEach { folder in
67-
var dictionary = [String: URL]()
66+
var parsedScreenshots: [String: [String: URL]] = [:]
67+
for folder in try screenshotsPath.absoluteURL.subDirectories.filterByFileOrFoldername(regex: localesRegex) {
68+
var dictionary: [String: URL] = [:]
69+
// swift-format-ignore: ReplaceForEachWithForLoop
6870
try FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles])
6971
.filter { url in
7072
kScreenshotExtensions.contains(url.pathExtension.lowercased())
71-
&& screenshotData.contains(where: { $0.screenshotName == url.lastPathComponent })
73+
&& screenshotData.contains(where: { $0.screenshotName == url.lastPathComponent })
7274
}.forEach { dictionary[$0.lastPathComponent] = $0 }
7375
parsedScreenshots[folder.lastPathComponent] = dictionary
7476
}
7577

7678
let processedTextData = try textData.map { try $0.makeProcessedData(size: templateImage.size) }
77-
let processedScreenshotData = screenshotData
79+
let processedScreenshotData =
80+
screenshotData
7881
.map { $0.makeProcessedData(size: templateImage.size) }
7982
.sorted { $0.zIndex < $1.zIndex }
8083

@@ -122,15 +125,13 @@ struct DeviceData: Decodable, ConfigValidateable {
122125
)
123126
}
124127

125-
try screenshotData.forEach { try $0.validate() }
126-
try textData.forEach { try $0.validate() }
128+
for screenshot in screenshotData { try screenshot.validate() }
129+
for text in textData { try text.validate() }
127130

128131
let screenshotNames = screenshotData.map { $0.screenshotName }
129-
try screenshotsGroupedByLocale.forEach { localeDict in
130-
try screenshotNames.forEach { name in
131-
if localeDict.value[name] == nil {
132-
throw NSError(description: "Screenshot folder \(localeDict.key) does not contain a screenshot named \"\(name)\"")
133-
}
132+
for localeDict in screenshotsGroupedByLocale {
133+
for name in screenshotNames where localeDict.value[name] == nil {
134+
throw NSError(description: "Screenshot folder \(localeDict.key) does not contain a screenshot named \"\(name)\"")
134135
}
135136
}
136137
}
@@ -156,8 +157,8 @@ struct DeviceData: Decodable, ConfigValidateable {
156157
insetBy: tabs
157158
)
158159

159-
screenshotData.forEach { $0.printSummary(insetByTabs: tabs) }
160-
textData.forEach { $0.printSummary(insetByTabs: tabs) }
160+
for screenshot in screenshotData { screenshot.printSummary(insetByTabs: tabs) }
161+
for text in textData { text.printSummary(insetByTabs: tabs) }
161162
}
162163

163164
}

Sources/SwiftFrameCore/Config/ScreenshotData.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ struct ScreenshotData: Decodable, ConfigValidateable, Equatable {
3232
bottomRight: bottomRight.convertingToBottomLeftOrigin(withSize: size),
3333
topLeft: topLeft.convertingToBottomLeftOrigin(withSize: size),
3434
topRight: topRight.convertingToBottomLeftOrigin(withSize: size),
35-
zIndex: zIndex)
35+
zIndex: zIndex
36+
)
3637
}
3738

3839
// MARK: - ConfigValidateable

Sources/SwiftFrameCore/Config/TextData.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ struct TextData: Decodable, ConfigValidateable {
4848
groupIdentifier: String?,
4949
topLeft: Point,
5050
bottomRight: Point,
51-
textColorOverride: NSColor? = nil)
52-
{
51+
textColorOverride: NSColor? = nil
52+
) {
5353
self.titleIdentifier = titleIdentifier
5454
self.textAlignment = textAlignment
5555
self.maxFontSizeOverride = maxFontSizeOverride
@@ -77,7 +77,8 @@ struct TextData: Decodable, ConfigValidateable {
7777
groupIdentifier: groupIdentifier,
7878
topLeft: processedTopLeft,
7979
bottomRight: processedBottomRight,
80-
textColorOverride: colorOverride)
80+
textColorOverride: colorOverride
81+
)
8182
}
8283

8384
// MARK: - ConfigValidateable

Sources/SwiftFrameCore/Config/TextGroup.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ struct TextGroup: Decodable, ConfigValidateable, Hashable {
2929

3030
func sharedFontSize(with strings: [AssociatedString], globalFont: NSFont, globalMaxSize: CGFloat) throws -> CGFloat {
3131
let maxFontSizes: [CGFloat] = try strings.compactMap {
32-
return try TextRenderer.maximumFontSizeThatFits(
32+
try TextRenderer.maximumFontSizeThatFits(
3333
string: $0.string,
3434
font: $0.data.fontOverride?.font() ?? globalFont,
3535
alignment: $0.data.textAlignment,
3636
maxSize: $0.data.maxFontSizeOverride ?? globalMaxSize,
37-
size: $0.data.rect.size)
37+
size: $0.data.rect.size
38+
)
3839
}
3940
// Can force-unwrap since array will never be empty
41+
// swift-format-ignore: NeverForceUnwrap
4042
return ([globalMaxSize, maxFontSize] + maxFontSizes).min()!
4143
}
4244
}

Sources/SwiftFrameCore/Extensions/FileManager+Extensions.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ extension FileManager {
1515
}
1616

1717
func ky_clearDirectories(_ urls: [FileURL], localeFolders: [String]) throws {
18-
try urls.forEach { url in
18+
for url in urls {
1919
let mappedURLs: [URL] = localeFolders.compactMap {
2020
let mappedURL = url.absoluteURL.appendingPathComponent($0)
2121
return fileExists(atPath: mappedURL.path) ? mappedURL : nil
2222
}
23-
try mappedURLs.forEach {
24-
try removeItem(at: $0)
23+
for url in mappedURLs {
24+
try removeItem(at: url)
2525
}
2626
}
2727
}
@@ -33,11 +33,11 @@ extension FileManager {
3333
try ky_createFile(atURL: destination, contents: data, attributes: nil)
3434
}
3535

36-
public func ky_createFile(atURL url: URL, contents: Data?, attributes: [FileAttributeKey : Any]? = nil) throws {
36+
public func ky_createFile(atURL url: URL, contents: Data?, attributes: [FileAttributeKey: Any]? = nil) throws {
3737
try ky_createFile(atPath: url.path, contents: contents, attributes: attributes)
3838
}
3939

40-
public func ky_createFile(atPath path: String, contents: Data?, attributes: [FileAttributeKey : Any]? = nil) throws {
40+
public func ky_createFile(atPath path: String, contents: Data?, attributes: [FileAttributeKey: Any]? = nil) throws {
4141
if !createFile(atPath: path, contents: contents, attributes: attributes) {
4242
throw NSError(description: "Could not create file at path \(path)")
4343
}

0 commit comments

Comments
 (0)