Skip to content

Commit

Permalink
#33 Align code format with swift-format (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: Henrik Panhans <[email protected]>
  • Loading branch information
henrik-dmg authored Oct 30, 2024
1 parent 902c8c4 commit 2c51d6a
Show file tree
Hide file tree
Showing 54 changed files with 509 additions and 226 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Linting

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: macos-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install swift-format
run: brew install swift-format
- name: Lint code
run: Scripts/lint-swift-code
16 changes: 10 additions & 6 deletions .github/workflows/swift.yml → .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
name: Swift
name: Tests

on: [push]
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: macos-14
runs-on: macos-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Configure Xcode version
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "15.2"
- name: Build
run: swift build -v
xcode-version: "16.0"
- name: Run tests
run: swift test -v
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.0")
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.0"),
],
targets: [
.executableTarget(
name: "SwiftFrame",
dependencies: [
"SwiftFrameCore",
.product(name: "ArgumentParser", package: "swift-argument-parser")
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
.target(name: "SwiftFrameCore", dependencies: ["Yams"]),
.testTarget(name: "SwiftFrameTests", dependencies: ["SwiftFrameCore"])
.testTarget(name: "SwiftFrameTests", dependencies: ["SwiftFrameCore"]),
]
)
11 changes: 11 additions & 0 deletions Scripts/configure-hooks
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

HOOKS=$(git rev-parse --git-path hooks)
ROOT=$(git rev-parse --show-toplevel)

# Symlink the hooks from scripts/hooks to the $HOOKS directory
for hook in "${ROOT}"/Scripts/hooks/*; do
HOOK_NAME=$(basename $hook)
echo "Symlinking $HOOK_NAME to $HOOKS/$HOOK_NAME"
ln -sf $hook $HOOKS/$HOOK_NAME
done
12 changes: 12 additions & 0 deletions Scripts/format-swift-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

GIT_ROOT=$(git rev-parse --show-toplevel)

swift-format format \
--recursive \
--parallel \
--in-place \
--configuration $GIT_ROOT/swift-format.json \
$GIT_ROOT/Sources/ \
$GIT_ROOT/Tests/ \
$GIT_ROOT/Package.swift
14 changes: 14 additions & 0 deletions Scripts/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash


GIT_ROOT=$(git rev-parse --show-toplevel)

SWIFT_FORMAT_OUTPUT=$($GIT_ROOT/Scripts/lint-swift-code)
RETURNED_ERROR=$?

if [ $RETURNED_ERROR -ne 0 ]; then
echo "Error: Swift code is not formatted correctly. Please run 'Scripts/format-swift-code' to fix most errors."
echo "$SWIFT_FORMAT_OUTPUT"
fi

exit $RETURNED_ERROR
12 changes: 12 additions & 0 deletions Scripts/lint-swift-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

GIT_ROOT=$(git rev-parse --show-toplevel)

swift-format lint \
--recursive \
--parallel \
--strict \
--configuration $GIT_ROOT/swift-format.json \
$GIT_ROOT/Sources/ \
$GIT_ROOT/Tests/ \
$GIT_ROOT/Package.swift
3 changes: 2 additions & 1 deletion Sources/SwiftFrame/Render.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct Render: ParsableCommand {

@Flag(
name: .long,
help: "Outputs the whole image canvas into the output directories before slicing it up into the correct screenshot sizes. Helpful for troubleshooting"
help:
"Outputs the whole image canvas into the output directories before slicing it up into the correct screenshot sizes. Helpful for troubleshooting"
)
var outputWholeImage = false

Expand Down
28 changes: 18 additions & 10 deletions Sources/SwiftFrame/Scaffold.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Scaffold: ParsableCommand, VerbosePrintable {

@Flag
var noHelperFiles = false

@Flag
var verbose = false

Expand All @@ -43,7 +43,7 @@ struct Scaffold: ParsableCommand, VerbosePrintable {
templatesDirectoryURL,
]

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

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

try Scaffold.defaultDevices.forEach { deviceName in
for deviceName in Scaffold.defaultDevices {
printVerbose("Creating screenshot directory for locale \(locale) and device \(deviceName)")
numberOfCreatedDirectories += 1

let localeScreenshotDirectoryURL = screenshotsDirectoryURL.appendingPathComponent(deviceName).appendingPathComponent(locale)
try FileManager.default.createDirectory(at: localeScreenshotDirectoryURL, withIntermediateDirectories: true, attributes: nil)

try FileManager.default.createDirectory(
at: localeScreenshotDirectoryURL,
withIntermediateDirectories: true,
attributes: nil
)

if shouldCreateHelperFiles {
let markdownContents = "# \(deviceName) - \(locale)\n\nPlace your screenshots for \(deviceName) (\(locale)) in this folder\n" +
"Please make sure that all screenshots that represent the same screen have the same name for every locale."
try FileManager.default.ky_writeToFile(markdownContents, destination: localeScreenshotDirectoryURL.appendingPathComponent("README.md"))
let markdownContents =
"# \(deviceName) - \(locale)\n\nPlace your screenshots for \(deviceName) (\(locale)) in this folder\n"
+ "Please make sure that all screenshots that represent the same screen have the same name for every locale."
try FileManager.default.ky_writeToFile(
markdownContents,
destination: localeScreenshotDirectoryURL.appendingPathComponent("README.md")
)
numberOfCreatedFiles += 1
}
}
Expand Down
55 changes: 33 additions & 22 deletions Sources/SwiftFrameCore/Config/ConfigData.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Foundation
import AppKit
import Foundation

protocol ConfigValidateable {
func validate() throws
func printSummary(insetByTabs tabs: Int)
}

/// First key is locale, second is regular key in string file
/// First key is locale, second is regular key in string file.
typealias LocalizedStringFiles = [String: [String: String]]

struct ConfigData: Decodable, ConfigValidateable {
Expand Down Expand Up @@ -51,8 +51,8 @@ struct ConfigData: Decodable, ConfigValidateable {
textColorSource: ColorSource,
outputFormat: FileFormat,
deviceData: [DeviceData],
localesRegex: String? = nil)
{
localesRegex: String? = nil
) {
self.textGroups = textGroups
self.stringsPath = stringsPath
self.maxFontSize = maxFontSize
Expand All @@ -67,15 +67,18 @@ struct ConfigData: Decodable, ConfigValidateable {
// MARK: - Processing

mutating func process() throws {
let regex: Regex<AnyRegexOutput>? = if let localesRegex, !localesRegex.isEmpty {
try Regex(localesRegex)
} else {
nil
}
let regex: Regex<AnyRegexOutput>? =
if let localesRegex, !localesRegex.isEmpty {
try Regex(localesRegex)
} else {
nil
}

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

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

guard !outputPaths.isEmpty else {
throw NSError(
description: "No output paths were specified",
expectation: "Please specify at least one output directory")
expectation: "Please specify at least one output directory"
)
}

try deviceData.forEach { try $0.validate() }
for device in deviceData {
try device.validate()
}
}

func printSummary(insetByTabs tabs: Int) {
Expand All @@ -106,21 +113,24 @@ struct ConfigData: Decodable, ConfigValidateable {
CommandLineFormatter.printKeyValue(
"String Files",
value: titles.isEmpty ? "none" : titles.keys.joined(separator: ", "),
insetBy: tabs)
insetBy: tabs
)

ky_print("Output paths:", insetByTabs: tabs)
outputPaths.forEach { ky_print($0.path.formattedGreen(), insetByTabs: tabs + 1) }
for path in outputPaths {
ky_print(path.path.formattedGreen(), insetByTabs: tabs + 1)
}

ky_print("Device data:", insetByTabs: tabs)
deviceData.forEach {
$0.printSummary(insetByTabs: tabs + 1)
for device in deviceData {
device.printSummary(insetByTabs: tabs + 1)
print()
}

if !textGroups.isEmpty {
print("Text groups:")
textGroups.forEach {
$0.printSummary(insetByTabs: tabs + 1)
for textGroup in textGroups {
textGroup.printSummary(insetByTabs: tabs + 1)
print()
}
} else {
Expand All @@ -133,7 +143,7 @@ struct ConfigData: Decodable, ConfigValidateable {
// MARK: - Screenshot Factory

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

func makeSharedFontSizes(for associatedStrings: [AssociatedString]) throws -> [String: CGFloat] {
return try textGroups.reduce(into: [String: CGFloat]()) { dictionary, group in
try textGroups.reduce(into: [String: CGFloat]()) { dictionary, group in
let strings = associatedStrings.filter({ $0.data.groupIdentifier == group.identifier })
dictionary[group.identifier] = try group.sharedFontSize(
with: strings,
globalFont: try fontSource.font(),
globalMaxSize: maxFontSize)
globalMaxSize: maxFontSize
)
}
}

Expand Down
Loading

0 comments on commit 2c51d6a

Please sign in to comment.