Skip to content

Commit

Permalink
updated enum case generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Liu committed Nov 7, 2023
1 parent ae5233d commit 29042ca
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
5 changes: 4 additions & 1 deletion SringCatalogEnum/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
NAME=xcstrings-enum-generate

all: debug
./$(NAME) --xcstrings-path ~/prj/ios/quible/Quible/Resources/Localizable.xcstrings
./$(NAME) \
--xcstrings-path ~/prj/ios/quible/Quible/Resources/Localizable.xcstrings \
--enum-name XcodeStrings \
--enum-type-alias X

debug:
swift build
Expand Down
65 changes: 61 additions & 4 deletions SringCatalogEnum/Sources/SringCatalogEnum/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ struct SringCatalogEnum: ParsableCommand {
// @Flag(help: "Include a counter with each repetition.")
// var includeCounter = false

@Option(name: .shortAndLong, help: "Full path and filename of the 'xcstrings' file.")
@Option(name: .long, help: "Full path and filename of the 'xcstrings' file.")
var xcstringsPath: String

@Option(name: .long, help: "Generated enum name.")
var enumName: String = "XcodeString"

@Option(name: .long, help: "A typealias of the generated enum name.")
var enumTypeAlias: String = "XCS"

func run() throws {
print("LOADING: \(xcstringsPath)")
let url = URL(fileURLWithPath: xcstringsPath)
Expand All @@ -42,15 +48,67 @@ struct SringCatalogEnum: ParsableCommand {
}
// print(strings)

var output = """
// This file is generated by XcodeStringEnum. Please do *NOT* update it manually.
import SwiftUI
/// Makes it a bit easier to type.
typealias \(enumTypeAlias) = \(enumName)
enum \(enumName): String, CaseIterable {
"""

var cases = [String]()
for (key, _) in strings {
guard let name = convertToVariableName(key) else {
print("SKIPPING: \(key)")
continue
}
print("\(name):\t\(key)")
guard key == name else {
continue
}

// print("\(name):\t\(key)")
// TODO: extract `localizations.en.stringUnit.value` and add in comments as inline documents
cases.append(" case \(name)\n")
}
cases.sort()
cases.forEach { string in
output += string
}

output += """
// MARK: - The following cases should be manually replaced in your codebase.
"""
cases.removeAll()
for (key, _) in strings {
guard let name = convertToVariableName(key) else {
print("SKIPPING: \(key)")
continue
}
guard key != name else {
continue
}

// print("\(name):\t\(key)")
// TODO: probably missing " handling?
cases.append(" case \(name) = \"\(key.replacingOccurrences(of: "\n", with: "")))\"\n")
}
cases.sort()
cases.forEach { string in
output += string
}

output += "}"
print(output)
}

// TODO: add to some StringUtility
private func convertToVariableName(_ str: String) -> String? {
// str.components(separatedBy: CharacterSet.alphanumerics.inverted).joined().lowercased()
var result = str.components(separatedBy: CharacterSet.letters.union(CharacterSet.alphanumerics).inverted).joined()
Expand Down Expand Up @@ -87,8 +145,7 @@ struct SringCatalogEnum: ParsableCommand {

return result


// Generated from:
// Generated from: (didn't really work)
// in swift, write a function to convert a string into a variable name, which means it starts with a lowercase letter, and contains only letters and numbers
/*
let regex = try! NSRegularExpression(pattern: "^[a-z][a-zA-Z0-9]*$", options: [])
Expand Down

0 comments on commit 29042ca

Please sign in to comment.