add dump command

This commit is contained in:
yonaskolb 2019-11-08 00:18:52 +11:00
parent 5711c6be0d
commit 43cd0ec33e
6 changed files with 116 additions and 44 deletions

View File

@ -130,7 +130,7 @@ Options:
- **--use-cache**: Used to prevent unnecessarily generating the project. If this is set, then a cache file will be written to when a project is generated. If `xcodegen` is later run but the spec and all the files it contains are the same, the project won't be generated.
- **--cache-path**: A custom path to use for your cache file. This defaults to `~/.xcodegen/cache/{PROJECT_SPEC_PATH_HASH}`
Use `xcodegen help` to see more detailed usage information.
There are other commands as well. Use `xcodegen help` to see more detailed usage information.
## Editing
```shell

View File

@ -7,7 +7,7 @@ import Yams
public class SpecLoader {
var project: Project!
private var projectDictionary: [String: Any]?
public private(set) var projectDictionary: [String: Any]?
let version: Version
public init(version: Version) {

View File

@ -0,0 +1,50 @@
import Foundation
import SwiftCLI
import PathKit
import ProjectSpec
import Yams
class DumpCommand: ProjectCommand {
override var name: String { "dump" }
override var shortDescription: String { "Dumps the project spec to stdout" }
private let dumpType = Key<DumpType>("--type", "-t", description: """
The type of dump to output. Either "json", "yaml", "summary", "swift-dump", "parsed-json", or "parsed-yaml". Defaults to summary. The "parsed" types parse the project into swift and then back again and can be used for testing.
""")
override func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {
let type = dumpType.value ?? .summary
let output: String
switch type {
case .swiftDump:
var string = ""
dump(project, to: &string)
output = string
case .json:
let data = try JSONSerialization.data(withJSONObject: specLoader.projectDictionary!, options: .prettyPrinted)
output = String(data: data, encoding: .utf8)!
case .yaml:
output = try Yams.dump(object: specLoader.projectDictionary!)
case .parsedJSON:
let data = try JSONSerialization.data(withJSONObject: project.toJSONDictionary(), options: .prettyPrinted)
output = String(data: data, encoding: .utf8)!
case .parsedYaml:
output = try Yams.dump(object: project.toJSONDictionary())
case .summary:
output = project.debugDescription
}
stdout.print(output)
}
}
fileprivate enum DumpType: String, ConvertibleFromString {
case swiftDump = "swift-dump"
case json
case yaml
case parsedJSON = "parsed-json"
case parsedYaml = "parsed-yaml"
case summary
}

View File

@ -5,10 +5,10 @@ import SwiftCLI
import XcodeGenKit
import XcodeProj
class GenerateCommand: Command {
class GenerateCommand: ProjectCommand {
let name: String = "generate"
let shortDescription: String = "Generate an Xcode project from a spec"
override var name: String { "generate" }
override var shortDescription: String { "Generate an Xcode project from a spec" }
let quiet = Flag(
"-q",
@ -17,13 +17,6 @@ class GenerateCommand: Command {
defaultValue: false
)
let disableEnvExpansion = Flag(
"-n",
"--no-env",
description: "Disable environment variables expansions",
defaultValue: false
)
let useCache = Flag(
"-c",
"--use-cache",
@ -36,42 +29,12 @@ class GenerateCommand: Command {
description: "Where the cache file will be loaded from and save to. Defaults to ~/.xcodegen/cache/{SPEC_PATH_HASH}"
)
let spec = Key<Path>(
"-s",
"--spec",
description: "The path to the project spec file. Defaults to project.yml"
)
let projectDirectory = Key<Path>("-p", "--project", description: "The path to the directory where the project should be generated. Defaults to the directory the spec is in. The filename is defined in the project spec")
let version: Version
init(version: Version) {
self.version = version
}
func execute() throws {
let projectSpecPath = (spec.value ?? "project.yml").absolute()
override func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {
let projectDirectory = self.projectDirectory.value?.absolute() ?? projectSpecPath.parent()
if !projectSpecPath.exists {
throw GenerationError.missingProjectSpec(projectSpecPath)
}
let specLoader = SpecLoader(version: version)
let project: Project
let variables: [String: String] = disableEnvExpansion.value ? [:] : ProcessInfo.processInfo.environment
// load project spec
do {
project = try specLoader.loadProject(path: projectSpecPath, variables: variables)
} catch {
throw GenerationError.projectSpecParsingError(error)
}
// validate project dictionary
do {
try specLoader.validateProjectDictionaryWarnings()

View File

@ -0,0 +1,56 @@
import Foundation
import SwiftCLI
import ProjectSpec
import XcodeGenKit
import PathKit
import Core
class ProjectCommand: Command {
let version: Version
var name: String { "ProjectCommand" }
var shortDescription: String { "" }
let spec = Key<Path>(
"-s",
"--spec",
description: "The path to the project spec file. Defaults to project.yml"
)
let disableEnvExpansion = Flag(
"-n",
"--no-env",
description: "Disable environment variable expansions",
defaultValue: false
)
init(version: Version) {
self.version = version
}
func execute() throws {
let projectSpecPath = (spec.value ?? "project.yml").absolute()
if !projectSpecPath.exists {
throw GenerationError.missingProjectSpec(projectSpecPath)
}
let specLoader = SpecLoader(version: version)
let project: Project
let variables: [String: String] = disableEnvExpansion.value ? [:] : ProcessInfo.processInfo.environment
do {
project = try specLoader.loadProject(path: projectSpecPath, variables: variables)
} catch {
throw GenerationError.projectSpecParsingError(error)
}
try execute(specLoader: specLoader, projectSpecPath: projectSpecPath, project: project)
}
func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {
}
}

View File

@ -12,7 +12,10 @@ public class XcodeGenCLI {
name: "xcodegen",
version: version.string,
description: "Generates Xcode projects",
commands: [generateCommand]
commands: [
generateCommand,
DumpCommand(version: version)
]
)
cli.parser.routeBehavior = .searchWithFallback(generateCommand)
}