Skip to content

Commit

Permalink
#1 #2 applied formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo committed Jan 1, 2024
1 parent c91978a commit 5feb5f3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 77 deletions.

This file was deleted.

46 changes: 23 additions & 23 deletions StringCatalogEnum/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
),
],
Expand All @@ -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(
Expand All @@ -71,7 +71,7 @@ let package = Package(
),
.testTarget(
name: "StringCatalogEnumTests",
dependencies: ["StringCatalogEnumLibrary","StringCatalogEnum", "Quick", "Nimble"]
dependencies: ["StringCatalogEnumLibrary", "StringCatalogEnum", "Quick", "Nimble"]
),
]
)
34 changes: 16 additions & 18 deletions StringCatalogEnum/Sources/StringCatalogEnum/main.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import StringCatalogEnumLibrary
import ArgumentParser
import Foundation

import StringCatalogEnumLibrary

struct StringCatalogEnum: ParsableCommand {
enum Error: Swift.Error {
Expand Down Expand Up @@ -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)`
Expand All @@ -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()
12 changes: 6 additions & 6 deletions StringCatalogEnum/Sources/StringCatalogEnum/model.swift
Original file line number Diff line number Diff line change
@@ -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]
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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"
}
Expand All @@ -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
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Nimble
import Quick
import StringCatalogEnumLibrary


final class StringCatalogEnumSpec: QuickSpec {
override class func spec() {
context("StringCatalogEnum") {
Expand All @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import XCTest

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
[
testCase(StringCatalogEnumTests.allTests),
]
}
Expand Down

0 comments on commit 5feb5f3

Please sign in to comment.