-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overview of changes: - Converted the repo to a Swift package - dropped CocoaPods support, since SPM and Mint are now supported - updated installation instructions - Simplified Makefile to use swift test - Greatly improved argument parsing by using Apple's ArgumentParser package (also removed unit tests for parsing) - Writing .strings files now also works if the directory/file doesn't exist yet - Added Github Actions for running tests on push/PR
- Loading branch information
1 parent
2cc45ce
commit 96b2eb9
Showing
41 changed files
with
352 additions
and
1,006 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Swift | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: macos-latest | ||
|
||
steps: | ||
- uses: maxim-lobanov/setup-xcode@v1 | ||
with: | ||
xcode-version: latest-stable | ||
- uses: actions/checkout@v2 | ||
- name: Build | ||
run: swift build -v | ||
- name: Run tests | ||
run: swift test -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,4 @@ fastlane/test_output | |
*.xcarchive/ | ||
Products/ | ||
.DS_Store | ||
.swiftpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
4.1 | ||
5.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
test: | ||
xcodebuild test -project "SwiftGenStrings.xcodeproj" -scheme "SwiftGenStrings" -destination "platform=macOS" | ||
|
||
release: | ||
xcodebuild -scheme "SwiftGenStrings" -configuration "Release" -destination "generic/platform=macOS" clean archive -archivePath "build/" | ||
swift build -c release -v | ||
mkdir -p Products | ||
cp build.xcarchive/Products/usr/local/bin/SwiftGenStrings Products/ | ||
cp .build/release/SwiftGenStrings Products/SwiftGenStrings | ||
|
||
install: release | ||
install -v Products/SwiftGenStrings /usr/local/bin/SwiftGenStrings | ||
|
||
test: | ||
swift test -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "swift-argument-parser", | ||
"repositoryURL": "https://github.com/apple/swift-argument-parser", | ||
"state": { | ||
"branch": null, | ||
"revision": "92646c0cdbaca076c8d3d0207891785b3379cbff", | ||
"version": "0.3.1" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// swift-tools-version:5.3 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SwiftGenStrings", | ||
platforms: [ | ||
.macOS(.v10_12) | ||
], | ||
products: [ | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.executable( | ||
name: "SwiftGenStrings", | ||
targets: ["SwiftGenStrings"] | ||
), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.3.0") | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "SwiftGenStrings", | ||
dependencies: [ | ||
"SwiftGenStringsCore", | ||
.product(name: "ArgumentParser", package: "swift-argument-parser") | ||
] | ||
), | ||
.target( | ||
name: "SwiftGenStringsCore", | ||
dependencies: [] | ||
), | ||
.testTarget( | ||
name: "SwiftGenStringsTests", | ||
dependencies: ["SwiftGenStringsCore"], | ||
resources: [ | ||
.process("Resources") | ||
] | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import ArgumentParser | ||
import Foundation | ||
|
||
extension URL: ExpressibleByArgument { | ||
|
||
public init?(argument: String) { | ||
if argument.hasPrefix("~/") { | ||
let cleanedPath = String(argument.dropFirst()) | ||
self.init(fileURLWithPath: cleanedPath, relativeTo: FileManager.default.homeDirectoryForCurrentUser) | ||
} else { | ||
self.init(fileURLWithPath: argument) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import ArgumentParser | ||
import Foundation | ||
import SwiftGenStringsCore | ||
|
||
struct SwiftGenStrings: ParsableCommand { | ||
|
||
private static var abstract = """ | ||
SwiftGenStrings is a command line application that can be used as a drop-in replacement for the standard genstrings command for Swift sources. | ||
The latter only supports the short form of the NSLocalizedString function but breaks as soon as you use any parameters other than key and comment as in | ||
""" | ||
|
||
static let configuration = CommandConfiguration( | ||
commandName: "SwiftGenStrings", | ||
abstract: SwiftGenStrings.abstract, | ||
version: "0.0.2", | ||
helpNames: .shortAndLong | ||
) | ||
|
||
@Argument(help: "List of files, that are used as source of Localizable.strings generation.") | ||
var files: [URL] | ||
|
||
@Option(name: .short, help: "(Optional) Substitute for NSLocalizedString, useful when different macro is used.") | ||
var substitute: String? | ||
|
||
@Option(name: .short, help: "(Optional) Specifies what directory Localizable.strings table is created in. Not specifying output directory will print script output content to standard output (console).") | ||
var outputDirectory: URL? | ||
|
||
func run() throws { | ||
do { | ||
try ky_run() | ||
} catch let error as NSError { | ||
ErrorFormatter().writeFormattedError(error.localizedDescription) | ||
Darwin.exit(Int32(error.code)) | ||
} | ||
} | ||
|
||
private func ky_run() throws { | ||
let collectionErrorOutput = LocalizedStringCollectionStandardErrorOutput() | ||
let finalStrings = LocalizedStringCollection(strings: [], errorOutput: collectionErrorOutput) | ||
|
||
let tokenizer = SwiftTokenizer() | ||
|
||
var numberOfWrittenErrors = 0 | ||
for file in files { | ||
let contents = try String(contentsOf: file) | ||
let tokens = tokenizer.tokenizeSwiftString(contents) | ||
let errorOutput = LocalizedStringFinderStandardErrorOutput(fileURL: file) | ||
let finder = LocalizedStringFinder(routine: substitute ?? "NSLocalizedString", errorOutput: errorOutput) | ||
let strings = finder.findLocalizedStrings(tokens) | ||
let collection = LocalizedStringCollection(strings: strings, errorOutput: collectionErrorOutput) | ||
finalStrings.merge(with: collection) | ||
numberOfWrittenErrors += errorOutput.numberOfWrittenErrors + collectionErrorOutput.numberOfWrittenErrors | ||
} | ||
|
||
guard numberOfWrittenErrors == 0 else { | ||
let errorMessage = numberOfWrittenErrors == 1 ? "1 error was written" : "\(numberOfWrittenErrors) errors were written" | ||
throw NSError(description: errorMessage) | ||
} | ||
|
||
let output = finalStrings.formattedContent | ||
|
||
if let outputDirectory = outputDirectory { | ||
let outputFileURL = outputDirectory.appendingPathComponent("Localizable.strings") | ||
try output.ky_write(to: outputFileURL, atomically: false, encoding: .utf8) | ||
} else { | ||
print(output, terminator: "") // No newline at the end | ||
} | ||
} | ||
|
||
} | ||
|
||
SwiftGenStrings.main() |
4 changes: 2 additions & 2 deletions
4
SwiftGenStrings/CharacterIterator.swift → ...iftGenStringsCore/CharacterIterator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Sources/SwiftGenStringsCore/Extensions/NSError+Extensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
public extension NSError { | ||
|
||
convenience init(domain: String = "com.kayak.travel.SwiftGenStrings", code: Int = 1, description: String) { | ||
let userInfo = [NSLocalizedDescriptionKey: description] | ||
self.init(domain: domain, code: code, userInfo: userInfo) | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
Sources/SwiftGenStringsCore/Extensions/String+Extensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
public extension String { | ||
|
||
func ky_write(to url: URL, atomically: Bool, encoding: Encoding = .utf8) throws { | ||
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil) | ||
try write(to: url, atomically: atomically, encoding: .utf8) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.