From e2cb20c885ef0422fad3f47bcd65b387791235eb Mon Sep 17 00:00:00 2001 From: Sam Soffes Date: Tue, 21 Nov 2017 20:48:29 -0800 Subject: [PATCH 1/2] Add quiet option --- README.md | 5 ++-- Sources/XcodeGen/Logger.swift | 56 +++++++++++++++++++++++++++++++++++ Sources/XcodeGen/main.swift | 25 ++++++++-------- 3 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 Sources/XcodeGen/Logger.swift diff --git a/README.md b/README.md index e2d08157..ed460204 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/Sources/XcodeGen/Logger.swift b/Sources/XcodeGen/Logger.swift new file mode 100644 index 00000000..2c614eb0 --- /dev/null +++ b/Sources/XcodeGen/Logger.swift @@ -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) + } +} diff --git a/Sources/XcodeGen/main.swift b/Sources/XcodeGen/main.swift index 5c83db5d..3e829b37 100644 --- a/Sources/XcodeGen/main.swift +++ b/Sources/XcodeGen/main.swift @@ -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("spec", "project.yml", flag: "s", description: "The path to the spec file"), Option("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) From 198195cee95aa78ea382c5edc0b5d6f141b8342f Mon Sep 17 00:00:00 2001 From: Sam Soffes Date: Wed, 22 Nov 2017 15:04:30 -0800 Subject: [PATCH 2/2] Remove docstrings --- Sources/XcodeGen/Logger.swift | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Sources/XcodeGen/Logger.swift b/Sources/XcodeGen/Logger.swift index 2c614eb0..fd0fc579 100644 --- a/Sources/XcodeGen/Logger.swift +++ b/Sources/XcodeGen/Logger.swift @@ -1,23 +1,15 @@ 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 @@ -25,16 +17,10 @@ struct Logger { // 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 @@ -43,9 +29,6 @@ struct Logger { 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