From 5feb5f328feb6e2ec4847afdde40498820c87f4d Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 1 Jan 2024 07:02:55 -0800 Subject: [PATCH] #1 #2 applied formatting --- .../contents.xcworkspacedata | 7 --- StringCatalogEnum/Package.swift | 46 +++++++++---------- .../Sources/StringCatalogEnum/main.swift | 34 +++++++------- .../Sources/StringCatalogEnum/model.swift | 12 ++--- .../StringEnumHelper.swift | 32 +++++-------- .../StringCatalogEnumTests.swift | 3 +- .../XCTestManifests.swift | 2 +- 7 files changed, 59 insertions(+), 77 deletions(-) delete mode 100644 StringCatalogEnum/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/StringCatalogEnum/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/StringCatalogEnum/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index 919434a..0000000 --- a/StringCatalogEnum/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/StringCatalogEnum/Package.swift b/StringCatalogEnum/Package.swift index b8f8051..6235dca 100644 --- a/StringCatalogEnum/Package.swift +++ b/StringCatalogEnum/Package.swift @@ -5,18 +5,18 @@ import PackageDescription // TODO: clean up this local CLI package dependency /* -let packageCLIGit = Package.Dependency.package( - name: "StringCatalogEnum", // <- Not sure why this is needed, help? - url: "https://github.com/superarts/swift-cli-core", - .revision("70d7df4e862be86799e9d514e5e55ca92585e7f8") -) -let packageCLILocal = Package.Dependency.package( - name: "StringCatalogEnum", // <- Not sure why this is needed, help? - path: "../../" -) -let packageCLI = packageCLILocal -//let packageCLI = packageCLIGit -*/ + let packageCLIGit = Package.Dependency.package( + name: "StringCatalogEnum", // <- Not sure why this is needed, help? + url: "https://github.com/superarts/swift-cli-core", + .revision("70d7df4e862be86799e9d514e5e55ca92585e7f8") + ) + let packageCLILocal = Package.Dependency.package( + name: "StringCatalogEnum", // <- Not sure why this is needed, help? + path: "../../" + ) + let packageCLI = packageCLILocal + //let packageCLI = packageCLIGit + */ let package = Package( name: "StringCatalogEnum", @@ -25,22 +25,22 @@ let package = Package( ], products: [ .executable(name: "xcstrings-enum-generate", targets: ["StringCatalogEnum"]), - .library(name:"StringCatalogEnumLibrary", targets: ["StringCatalogEnumLibrary"]), + .library(name: "StringCatalogEnumLibrary", targets: ["StringCatalogEnumLibrary"]), ], dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), .package( - url: "https://github.com/apple/swift-argument-parser", + url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "1.2.3") ), // packageCLI, .package( - url: "https://github.com/Quick/Quick.git", + url: "https://github.com/Quick/Quick.git", .upToNextMinor(from: "7.3.0") ), .package( - url: "https://github.com/Quick/Nimble.git", + url: "https://github.com/Quick/Nimble.git", .upToNextMinor(from: "13.0.0") ), ], @@ -50,17 +50,17 @@ let package = Package( .target( name: "StringCatalogEnum", dependencies: [ - "StringCatalogEnumLibrary", + "StringCatalogEnumLibrary", .product( name: "ArgumentParser", package: "swift-argument-parser" ), /* - .product( - name: "packageCLI", - package: "packageCLI" - ), - */ + .product( + name: "packageCLI", + package: "packageCLI" + ), + */ ] ), .target( @@ -71,7 +71,7 @@ let package = Package( ), .testTarget( name: "StringCatalogEnumTests", - dependencies: ["StringCatalogEnumLibrary","StringCatalogEnum", "Quick", "Nimble"] + dependencies: ["StringCatalogEnumLibrary", "StringCatalogEnum", "Quick", "Nimble"] ), ] ) diff --git a/StringCatalogEnum/Sources/StringCatalogEnum/main.swift b/StringCatalogEnum/Sources/StringCatalogEnum/main.swift index 9e04521..550c4ed 100644 --- a/StringCatalogEnum/Sources/StringCatalogEnum/main.swift +++ b/StringCatalogEnum/Sources/StringCatalogEnum/main.swift @@ -1,7 +1,6 @@ -import StringCatalogEnumLibrary import ArgumentParser import Foundation - +import StringCatalogEnumLibrary struct StringCatalogEnum: ParsableCommand { enum Error: Swift.Error { @@ -40,35 +39,35 @@ struct StringCatalogEnum: ParsableCommand { guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { throw Error.unexpectedJSON(message: "cannot parse first level object") } - + guard let strings = json["strings"] as? [String: Any] else { throw Error.unexpectedJSON(message: "cannot parse `strings`") } var output = """ - // This file is generated by XcodeStringEnum. Please do *NOT* update it manually. - // As a common practice, swiftLint is disabled for generated files. - // swiftlint:disable all + // This file is generated by XcodeStringEnum. Please do *NOT* update it manually. + // As a common practice, swiftLint is disabled for generated files. + // swiftlint:disable all - import SwiftUI + import SwiftUI - /// Makes it a bit easier to type. - typealias \(enumTypealias) = \(enumName) + /// Makes it a bit easier to type. + typealias \(enumTypealias) = \(enumName) - /// Generated by StringCatalogEnum, this enum contains all existing Strin Category keys. - enum \(enumName): String, CaseIterable { + /// Generated by StringCatalogEnum, this enum contains all existing Strin Category keys. + enum \(enumName): String, CaseIterable { - """ + """ let keywordRawValues = getKeywordRawValues() let firstCases = helper.createEnumKeys(with: strings, keyNameMatches: true, keywordEnum: keywordRawValues) let secondCases = helper.createEnumKeys(with: strings, keyNameMatches: false, keywordEnum: keywordRawValues) output += """ - + \(firstCases) - + // MARK: - The following cases should be manually replaced in your codebase. - + \(secondCases) /// Usage: `SwiftUI.Text(\(enumTypealias).yourStringCatalogKey.key)` @@ -86,11 +85,10 @@ struct StringCatalogEnum: ParsableCommand { try output.write(to: outputURL, atomically: true, encoding: .utf8) print("Written to: \(outputFilename)") } - + func getKeywordRawValues() -> [String] { - return Keyword.allCases.map { $0.rawValue } + Keyword.allCases.map(\.rawValue) } - } StringCatalogEnum.main() diff --git a/StringCatalogEnum/Sources/StringCatalogEnum/model.swift b/StringCatalogEnum/Sources/StringCatalogEnum/model.swift index 94f6e22..5f8a01d 100644 --- a/StringCatalogEnum/Sources/StringCatalogEnum/model.swift +++ b/StringCatalogEnum/Sources/StringCatalogEnum/model.swift @@ -1,9 +1,9 @@ // TODO: make XCStrings Decodable by listing all possible models // Example: let obj = try JSONDecoder().decode(XCStrings.self, from: data) /* -struct XCStrings: Decodable { - let sourceLanguage: String - let version: String - let strings: [Strsing: String] -} -*/ + struct XCStrings: Decodable { + let sourceLanguage: String + let version: String + let strings: [Strsing: String] + } + */ diff --git a/StringCatalogEnum/Sources/StringCatalogEnumLibrary/StringEnumHelper.swift b/StringCatalogEnum/Sources/StringCatalogEnumLibrary/StringEnumHelper.swift index a1c1508..91a5c8d 100644 --- a/StringCatalogEnum/Sources/StringCatalogEnumLibrary/StringEnumHelper.swift +++ b/StringCatalogEnum/Sources/StringCatalogEnumLibrary/StringEnumHelper.swift @@ -3,11 +3,8 @@ import Foundation /// Model that helps separate the logic used in StringCatalogEnum struct. public struct StringEnumHelper { - - public init() { - - } - + public init() {} + /// Creates enum cases depending on whether key == name /// - Parameters: /// - stringData: A dictionary containing string data. @@ -18,12 +15,11 @@ public struct StringEnumHelper { var cases = [String]() var knownCases = [String]() for (key, _) in stringData { - guard let name = convertToVariableName(key: key) else { print("SKIPPING: \(key)") continue } - + if keyNameMatches { guard key == name else { continue @@ -33,22 +29,21 @@ public struct StringEnumHelper { continue } } - + guard !knownCases.contains(name) else { cases.append(" // TODO: fix duplicated entry - case \(name)\n") continue } knownCases.append(name) - + // TODO: extract `localizations.en.stringUnit.value` and add in comments as inline documents - let caseString: String - if keywordEnum.contains(name) { - caseString = keyNameMatches + let caseString: String = if keywordEnum.contains(name) { + keyNameMatches ? " case `\(name)`\n" : " case `\(name)` = \"\(key.replacingOccurrences(of: "\n", with: ""))\"\n" } else { - caseString = keyNameMatches + keyNameMatches ? " case \(name)\n" : " case \(name) = \"\(key.replacingOccurrences(of: "\n", with: ""))\"\n" } @@ -63,8 +58,6 @@ public struct StringEnumHelper { return partialOutput } - - /// Convert a Strint Catalog key to a Swift variable name. public func convertToVariableName(key: String) -> String? { var result = key @@ -73,12 +66,11 @@ public struct StringEnumHelper { result = key.lowercased() } - // Uppercase remaining words, e.g. "an example" to "anExample"; - result = result.split(separator: " ").enumerated().map { (index, substring) in - index == 0 ? String(substring.prefix(1)).lowercased() + substring.dropFirst() : String(substring).capitalized - }.joined() - + result = result.split(separator: " ").enumerated().map { index, substring in + index == 0 ? String(substring.prefix(1)).lowercased() + substring.dropFirst() : String(substring).capitalized + }.joined() + // Leave only letters and numeric characters result = result.components(separatedBy: CharacterSet.letters.union(CharacterSet.alphanumerics).inverted).joined() diff --git a/StringCatalogEnum/Tests/StringCatalogEnumTests/StringCatalogEnumTests.swift b/StringCatalogEnum/Tests/StringCatalogEnumTests/StringCatalogEnumTests.swift index f9a1aec..2db024f 100644 --- a/StringCatalogEnum/Tests/StringCatalogEnumTests/StringCatalogEnumTests.swift +++ b/StringCatalogEnum/Tests/StringCatalogEnumTests/StringCatalogEnumTests.swift @@ -2,7 +2,6 @@ import Nimble import Quick import StringCatalogEnumLibrary - final class StringCatalogEnumSpec: QuickSpec { override class func spec() { context("StringCatalogEnum") { @@ -22,7 +21,7 @@ final class StringKeyModelSpec: QuickSpec { context("StringKeyModel") { describe("Convert To Variable Name Function") { - it("Basic cases should convert properly"){ + it("Basic cases should convert properly") { // Basic test cases expect(stringEnumHelper.convertToVariableName(key: "SomeKey")).to(equal("someKey")) expect(stringEnumHelper.convertToVariableName(key: "123Key")).to(equal("key")) diff --git a/StringCatalogEnum/Tests/StringCatalogEnumTests/XCTestManifests.swift b/StringCatalogEnum/Tests/StringCatalogEnumTests/XCTestManifests.swift index a740ab9..4beb3db 100644 --- a/StringCatalogEnum/Tests/StringCatalogEnumTests/XCTestManifests.swift +++ b/StringCatalogEnum/Tests/StringCatalogEnumTests/XCTestManifests.swift @@ -2,7 +2,7 @@ import XCTest #if !canImport(ObjectiveC) public func allTests() -> [XCTestCaseEntry] { - return [ + [ testCase(StringCatalogEnumTests.allTests), ] }