-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use swift-argument-parser instead of swift-tools-support-core's vendo…
…red parser (#19) * Use swift-argument-parser instead of swift-tools-support-core's vendored parser * Require Xcode 11.3 & Swift 5.1
- Loading branch information
1 parent
8395628
commit 3c82718
Showing
7 changed files
with
69 additions
and
83 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
os: osx | ||
osx_image: xcode11.2 | ||
osx_image: xcode11.3 | ||
language: swift | ||
sudo: required | ||
script: | ||
|
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
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,67 +1,58 @@ | ||
import Foundation | ||
import TSCUtility | ||
import ArgumentParser | ||
|
||
enum Parser { | ||
struct Result { | ||
let projectPath: String | ||
let outputURL: Foundation.URL | ||
let topLevelEnumName: String | ||
let targetName: String? | ||
let locale: String | ||
} | ||
|
||
static func parse() throws -> Result { | ||
let exampleUsageString = "$PROJECT_FILE_PATH $PROJECT_DIR/$PROJECT_NAME" | ||
let parser = ArgumentParser(usage: exampleUsageString, overview: "Shark") | ||
let pathArgument = parser.add(positional: ".xcodeproj path", kind: String.self, usage: "The path to the .xcodeproj file") | ||
let outputArgument = parser.add(positional: "output path", kind: String.self, usage: "Path for the output file. Creates a Shark.swift file when this value is a folder") | ||
let nameArgument = parser.add(option: "--name", kind: String.self, usage: #"Top level enum name under which the cases are defined. Defaults to "Shark""#, completion: nil) | ||
let targetArgument = parser.add(option: "--target", kind: String.self, usage: "Target name of the application, useful in case there are multiple application targets", completion: nil) | ||
let localeArgument = parser.add(option: "--locale",kind: String.self,usage: | ||
#"Localization code to use when selecting the Localizable.strings. i.e "en", "de", "es" The "en" locale is used unless specified"#) | ||
|
||
let parseResults: ArgumentParser.Result | ||
do { | ||
parseResults = try parser.parse(Array(CommandLine.arguments.dropFirst())) | ||
} catch { | ||
switch error { | ||
case ArgumentParserError.expectedArguments(_, let missingArguments): | ||
print("Missing arguments: \(missingArguments.joined(separator: ", "))") | ||
print("Example usage: \(exampleUsageString)") | ||
case ArgumentParserError.unknownOption(let option): | ||
print("Unknown option: \(option)") | ||
default: | ||
print(error.localizedDescription) | ||
} | ||
exit(EXIT_FAILURE) | ||
} | ||
|
||
guard let projectPath = parseResults.get(pathArgument)?.expandingTildeInPath, let outputPath = parseResults.get(outputArgument)?.expandingTildeInPath else { | ||
print("xcodeproj file path and output path parameters are required") | ||
exit(EXIT_FAILURE) | ||
} | ||
|
||
guard projectPath.pathExtension == "xcodeproj" else { | ||
print("\(projectPath) should point to a .xcodeproj file") | ||
exit(EXIT_FAILURE) | ||
struct Options: ParsableArguments { | ||
@Argument(help: "The .xcodeproj file path") | ||
fileprivate(set) var projectPath: String | ||
|
||
@Argument(help: "The output file path") | ||
fileprivate(set) var outputPath: String | ||
|
||
@Option(name: .customLong("name"), | ||
default: "Shark", | ||
help: "Top level enum name under which the cases are defined.") | ||
private(set) var topLevelEnumName: String | ||
|
||
@Option(name: .customLong("target"), | ||
help: "Target name of the application, useful in case there are multiple application targets") | ||
private(set) var targetName: String? | ||
|
||
@Option(name: .long, | ||
default: "en", | ||
help: "Localization code to use when selecting the Localizable.strings. i.e en, de, es.") | ||
private(set) var locale: String | ||
} | ||
|
||
struct Shark: ParsableCommand { | ||
static var configuration: CommandConfiguration = .init(abstract:#""" | ||
Paste the following line in a Xcode run phase script that runs after the "Compile Sources" run phase: | ||
$PROJECT_FILE_PATH $PROJECT_DIR/$PROJECT_NAME | ||
"""#) | ||
|
||
@OptionGroup() | ||
private var options: Options | ||
|
||
mutating func validate() throws { | ||
guard options.projectPath.pathExtension == "xcodeproj" else { | ||
throw ValidationError("\(options.projectPath) should point to a .xcodeproj file") | ||
} | ||
|
||
var isDirectory: ObjCBool = false | ||
|
||
let outputURL: Foundation.URL | ||
if FileManager.default.fileExists(atPath: outputPath, isDirectory: &isDirectory), isDirectory.boolValue { | ||
outputURL = URL(fileURLWithPath: outputPath).appendingPathComponent("Shark.swift") | ||
} else if outputPath.pathExtension == "swift" { | ||
outputURL = URL(fileURLWithPath: outputPath) | ||
} else { | ||
print("The output path should either point to an existing folder or end with a .swift extension") | ||
exit(2) | ||
if FileManager.default.fileExists(atPath: options.outputPath, isDirectory: &isDirectory), isDirectory.boolValue { | ||
options.outputPath.append("Shark.swift") | ||
} else if options.outputPath.pathExtension != "swift" { | ||
throw ValidationError("The output path should either point to an existing folder or end with a .swift extension") | ||
} | ||
|
||
return Result(projectPath: projectPath, | ||
outputURL: outputURL, | ||
topLevelEnumName: parseResults.get(nameArgument) ?? "Shark", | ||
targetName: parseResults.get(targetArgument), | ||
locale: parseResults.get(localeArgument) ?? "en") | ||
|
||
options.projectPath = options.projectPath.expandingTildeInPath | ||
options.outputPath = options.outputPath.expandingTildeInPath | ||
} | ||
|
||
func run() throws { | ||
let enumString = try SharkEnumBuilder.sharkEnumString(forOptions: options) | ||
|
||
try FileBuilder | ||
.fileContents(with: enumString, filename: options.outputPath.lastPathComponent) | ||
.write(to: URL(fileURLWithPath: options.outputPath), atomically: true, 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
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 |
---|---|---|
@@ -1,6 +1 @@ | ||
let parseResult = try Parser.parse() | ||
let enumString = try SharkEnumBuilder.sharkEnumString(forParseResult: parseResult) | ||
|
||
try FileBuilder | ||
.fileContents(with: enumString, filename: parseResult.outputURL.lastPathComponent) | ||
.write(to: parseResult.outputURL, atomically: true, encoding: .utf8) | ||
Shark.main() |