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..fd0fc579 --- /dev/null +++ b/Sources/XcodeGen/Logger.swift @@ -0,0 +1,39 @@ +import Foundation +import Rainbow + +struct Logger { + + // MARK: - Properties + + let isQuiet: Bool + let isColored: Bool + + // MARK: - Initializers + + init(isQuiet: Bool = false, isColored: Bool = true) { + self.isQuiet = isQuiet + self.isColored = isColored + } + + // MARK: - Logging + + func fatal(_ message: String) { + print(isColored ? message.red : message) + } + + func info(_ message: String) { + if isQuiet { + return + } + + print(message) + } + + 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)