Add quiet option

This commit is contained in:
Sam Soffes
2017-11-22 10:00:13 -08:00
parent 54e24bc52e
commit e2cb20c885
3 changed files with 72 additions and 14 deletions
+3 -2
View File
@@ -113,8 +113,9 @@ This will look for a project spec in the current directory called `project.yml`
Use `xcodegen --help` to see the list of options:
- **--spec**: An optional path to a `.yml` or `.json` project spec
- **--project**: An optional path to a directory where the project will be generated. By default this is the current directory
- **--spec**: An optional path to a `.yml` or `.json` project spec.
- **--project**: An optional path to a directory where the project will be generated. By default this is the current directory.
- **--quiet**: Suppress informational and success messages. By default this is disabled.
## Editing
```
+56
View File
@@ -0,0 +1,56 @@
import Foundation
import Rainbow
/// Mechanism for logging informational, success, and fatal messages.
struct Logger {
// MARK: - Properties
/// Should informational and success messages be suppressed
let isQuiet: Bool
/// Should the output be colored
let isColored: Bool
// MARK: - Initializers
/// Initialize a logger
///
/// - parameter isQuiet: should informational and success messages be suppressed
/// - parameter isColored: should the output be colored
init(isQuiet: Bool = false, isColored: Bool = true) {
self.isQuiet = isQuiet
self.isColored = isColored
}
// MARK: - Logging
/// Log a fatal message and exit with status code 1
///
/// - parameter message: the message to log
func fatal(_ message: String) {
print(isColored ? message.red : message)
}
/// Log an informational message
///
/// - parameter message: the message to log
func info(_ message: String) {
if isQuiet {
return
}
print(message)
}
/// Log a success message and exit with status code 0
///
/// - parameter message: the message to log
func success(_ message: String) {
if isQuiet {
return
}
print(isColored ? message.green : message)
}
}
+13 -12
View File
@@ -5,45 +5,45 @@ import XcodeGenKit
import xcproj
import ProjectSpec
import JSONUtilities
import Rainbow
let version = "1.4.0"
func generate(spec: String, project: String) {
func generate(spec: String, project: String, isQuiet: Bool) {
let logger = Logger(isQuiet: isQuiet)
let specPath = Path(spec).normalize()
let projectPath = Path(project).normalize()
if !specPath.exists {
print("No project spec found at \(specPath.absolute())".red)
logger.fatal("No project spec found at \(specPath.absolute())")
exit(1)
}
let spec: ProjectSpec
do {
spec = try ProjectSpec(path: specPath)
print("📋 Loaded spec:\n \(spec.debugDescription.replacingOccurrences(of: "\n", with: "\n "))")
logger.info("📋 Loaded spec:\n \(spec.debugDescription.replacingOccurrences(of: "\n", with: "\n "))")
} catch let error as JSONUtilities.DecodingError {
print("Parsing spec failed: \(error.description)".red)
logger.fatal("Parsing spec failed: \(error.description)")
exit(1)
} catch {
print("Parsing spec failed: \(error.localizedDescription)".red)
logger.fatal("Parsing spec failed: \(error.localizedDescription)")
exit(1)
}
do {
let projectGenerator = ProjectGenerator(spec: spec)
let project = try projectGenerator.generateProject()
print("⚙️ Generated project")
logger.info("⚙️ Generated project")
let projectFile = projectPath + "\(spec.name).xcodeproj"
try project.write(path: projectFile, override: true)
print("💾 Saved project to \(projectFile.string)".green)
logger.success("💾 Saved project to \(projectFile.string)")
} catch let error as SpecValidationError {
print(error.description.red)
logger.fatal(error.description)
exit(1)
} catch {
print("Generation failed: \(error.localizedDescription)".red)
logger.fatal("Generation failed: \(error.localizedDescription)")
exit(1)
}
}
@@ -51,5 +51,6 @@ func generate(spec: String, project: String) {
command(
Option<String>("spec", "project.yml", flag: "s", description: "The path to the spec file"),
Option<String>("project", "", flag: "p", description: "The path to the folder where the project should be generated"),
generate)
.run(version)
Flag("quiet", flag: "q", description: "Suppress printing of informational and success messages", default: false),
generate
).run(version)