Skip to content

Commit

Permalink
Add ArgumentParser
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlokhorst committed Sep 3, 2017
1 parent 1d50dc9 commit a5c3d86
Showing 1 changed file with 74 additions and 68 deletions.
142 changes: 74 additions & 68 deletions Sources/rswift/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,91 +78,97 @@ struct CommanderArguments {
static let outputDir = Argument<String>("outputDir", description: "Output directory for the 'R.generated.swift' file.")
}

// Temporary warning message during migration to R.swift 4
if CommandLine.arguments.dropFirst().first != "generate" {
var arguments = CommandLine.arguments
arguments.insert("generate", at: 1)
let command = arguments
.map { $0.contains(" ") ? "\"\($0)\"" : $0 }
.joined(separator: " ")

let message = "⚠️ R.swift 4 requires \"generate\" command as first argument to the executable.\n"
+ "Change your call to something similar to this:\n\n"
+ "\(command)"
+ "\n"
let generate = command(
CommanderFlags.edge,

if isatty(fileno(stderr)) != 0 {
fputs("\(message)\n", stderr)
} else {
fputs("\(message)\n", stderr)
}
exit(EXIT_FAILURE)
}
CommanderOptions.importModules,
CommanderOptions.accessLevel,
CommanderOptions.rswiftIgnore,

Group {
$0.command("generate",
CommanderFlags.version,
CommanderFlags.edge,
CommanderOptions.xcodeproj,
CommanderOptions.target,

CommanderOptions.importModules,
CommanderOptions.accessLevel,
CommanderOptions.rswiftIgnore,
CommanderOptions.bundleIdentifier,
CommanderOptions.productModuleName,
CommanderOptions.buildProductsDir,
CommanderOptions.developerDir,
CommanderOptions.sourceRoot,
CommanderOptions.sdkRoot,

CommanderOptions.xcodeproj,
CommanderOptions.target,
CommanderArguments.outputDir
) { edgeFlag, importModules, accessLevel, rswiftIgnore, xcodeproj, target, bundle, productModule, buildProductsDir, developerDir, sourceRoot, sdkRoot, outputDir in

CommanderOptions.bundleIdentifier,
CommanderOptions.productModuleName,
CommanderOptions.buildProductsDir,
CommanderOptions.developerDir,
CommanderOptions.sourceRoot,
CommanderOptions.sdkRoot,
let info = ProcessInfo()

CommanderArguments.outputDir
) { version, edgeFlag, importModules, accessLevel, rswiftIgnore, xcodeproj, target, bundle, productModule, buildProductsDir, developerDir, sourceRoot, sdkRoot, outputDir in
let xcodeprojPath = try info.value(from: xcodeproj, name: "xcodeproj", key: EnvironmentKeys.xcodeproj)
let targetName = try info.value(from: target, name: "target", key: EnvironmentKeys.target)
let bundleIdentifier = try info.value(from: bundle, name: "bundleIdentifier", key: EnvironmentKeys.bundleIdentifier)
let productModuleName = try info.value(from: productModule, name: "productModuleName", key: EnvironmentKeys.productModuleName)

let info = ProcessInfo()
let buildProductsDirPath = try info.value(from: buildProductsDir, name: "buildProductsDir", key: EnvironmentKeys.buildProductsDir)
let developerDirPath = try info.value(from: developerDir, name: "developerDir", key: EnvironmentKeys.developerDir)
let sourceRootPath = try info.value(from: sourceRoot, name: "sourceRoot", key: EnvironmentKeys.sourceRoot)
let sdkRootPath = try info.value(from: sdkRoot, name: "sdkRoot", key: EnvironmentKeys.sdkRoot)

let xcodeprojPath = try info.value(from: xcodeproj, name: "xcodeproj", key: EnvironmentKeys.xcodeproj)
let targetName = try info.value(from: target, name: "target", key: EnvironmentKeys.target)
let bundleIdentifier = try info.value(from: bundle, name: "bundleIdentifier", key: EnvironmentKeys.bundleIdentifier)
let productModuleName = try info.value(from: productModule, name: "productModuleName", key: EnvironmentKeys.productModuleName)

let buildProductsDirPath = try info.value(from: buildProductsDir, name: "buildProductsDir", key: EnvironmentKeys.buildProductsDir)
let developerDirPath = try info.value(from: developerDir, name: "developerDir", key: EnvironmentKeys.developerDir)
let sourceRootPath = try info.value(from: sourceRoot, name: "sourceRoot", key: EnvironmentKeys.sourceRoot)
let sdkRootPath = try info.value(from: sdkRoot, name: "sdkRoot", key: EnvironmentKeys.sdkRoot)
let outputURL = URL(fileURLWithPath: outputDir).appendingPathComponent(Rswift.resourceFileName, isDirectory: false)
let rswiftIgnoreURL = URL(fileURLWithPath: sourceRootPath).appendingPathComponent(rswiftIgnore, isDirectory: false)
let modules = importModules
.components(separatedBy: ",")
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
.filter { !$0.isEmpty }
.map { Module.custom(name: $0) }


let outputURL = URL(fileURLWithPath: outputDir).appendingPathComponent(Rswift.resourceFileName, isDirectory: false)
let rswiftIgnoreURL = URL(fileURLWithPath: sourceRootPath).appendingPathComponent(rswiftIgnore, isDirectory: false)
let modules = importModules
.components(separatedBy: ",")
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
.filter { !$0.isEmpty }
.map { Module.custom(name: $0) }
let callInformation = CallInformation(
outputURL: outputURL,
rswiftIgnoreURL: rswiftIgnoreURL,

edgeEnabled: edgeFlag,
accessLevel: accessLevel,
imports: Set(modules),

let callInformation = CallInformation(
outputURL: outputURL,
rswiftIgnoreURL: rswiftIgnoreURL,
xcodeprojURL: URL(fileURLWithPath: xcodeprojPath),
targetName: targetName,
bundleIdentifier: bundleIdentifier,
productModuleName: productModuleName,

edgeEnabled: edgeFlag,
accessLevel: accessLevel,
imports: Set(modules),
buildProductsDirURL: URL(fileURLWithPath: buildProductsDirPath),
developerDirURL: URL(fileURLWithPath: developerDirPath),
sourceRootURL: URL(fileURLWithPath: sourceRootPath),
sdkRootURL: URL(fileURLWithPath: sdkRootPath)
)

xcodeprojURL: URL(fileURLWithPath: xcodeprojPath),
targetName: targetName,
bundleIdentifier: bundleIdentifier,
productModuleName: productModuleName,
try RswiftCore.run(callInformation)

buildProductsDirURL: URL(fileURLWithPath: buildProductsDirPath),
developerDirURL: URL(fileURLWithPath: developerDirPath),
sourceRootURL: URL(fileURLWithPath: sourceRootPath),
sdkRootURL: URL(fileURLWithPath: sdkRootPath)
)
}

try RswiftCore.run(callInformation)
// Temporary warning message during migration to R.swift 4
let parser = ArgumentParser(arguments: CommandLine.arguments)
_ = parser.shift()
let exception = parser.hasOption("version") || parser.hasOption("help")

if !exception && parser.shift() != "generate" {
var arguments = CommandLine.arguments
arguments.insert("generate", at: 1)
let command = arguments
.map { $0.contains(" ") ? "\"\($0)\"" : $0 }
.joined(separator: " ")

let message = "⚠️ R.swift 4 requires \"generate\" command as first argument to the executable.\n"
+ "Change your call to something similar to this:\n\n"
+ "\(command)"
+ "\n"

if isatty(fileno(stderr)) != 0 {
fputs("\(message)\n", stderr)
} else {
fputs("\(message)\n", stderr)
}
}.run(Rswift.version)
exit(EXIT_FAILURE)
}

let group = Group()
group.addCommand("generate", "Generates R.generated.swift file", generate)
group.run(Rswift.version)

0 comments on commit a5c3d86

Please sign in to comment.