Skip to content

Commit

Permalink
🌲 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Oct 21, 2022
1 parent a4d8ce7 commit 5a5855d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
65 changes: 20 additions & 45 deletions Sources/Grain/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,6 @@ import TSCUtility

let RUNTIME_NAME = "GrainDescriptor"

enum Log {

static func debug(
file: StaticString = #file,
line: UInt = #line,
_ log: OSLog,
_ object: @autoclosure () -> Any
) {
print(object())
}

static func error(
file: StaticString = #file,
line: UInt = #line,
_ log: OSLog,
_ object: @autoclosure () -> Any
) {
os_log(.info, log: log, "%{public}@\n%{public}@:%{public}@", "\(object())", "\(file)", "\(line.description)")
}

}

extension OSLog {

@inline(__always)
private static func makeOSLogInDebug(isEnabled: Bool = true, _ factory: () -> OSLog) -> OSLog {
#if DEBUG
return factory()
#else
return .disabled
#endif
}

static let generic: OSLog = makeOSLogInDebug { OSLog.init(subsystem: "app.muukii", category: "generic") }
}

struct CLIError: Swift.Error, LocalizedError, Equatable {

var errorDescription: String?
Expand All @@ -51,10 +15,21 @@ struct CLIError: Swift.Error, LocalizedError, Equatable {
}

struct CLI: AsyncParsableCommand {

static var configuration: CommandConfiguration {
.init(
commandName: "Grain",
abstract: "",
usage: "",
version: "---",
shouldDisplay: true,
subcommands: [Render.self],
defaultSubcommand: Render.self,
helpNames: .long
)
}

static var configuration: CommandConfiguration = .init(subcommands: [Gen.self])

struct Gen: AsyncParsableCommand {
struct Render: AsyncParsableCommand {

struct DomainError: Swift.Error, LocalizedError, Equatable {

Expand Down Expand Up @@ -107,10 +82,10 @@ struct CLI: AsyncParsableCommand {
}
}

// Log.debug(.generic, """
//applicationPath: \(applicationPath)
//runtimeFrameworksPath: \(runtimeFrameworksPath)
//""")
Log.debug("""
applicationPath: \(applicationPath)
runtimeFrameworksPath: \(runtimeFrameworksPath)
""")

guard localFileSystem.exists(libraryPath) else {
throw CLIError.runtimeNotFound
Expand Down Expand Up @@ -169,7 +144,7 @@ struct CLI: AsyncParsableCommand {
// Return now if there was an error.
if result.exitStatus != .terminated(code: 0) {
let output = try result.utf8stderrOutput()
Log.debug(.generic, "\(output)\n\(cmd.joined(separator: " "))")
Log.error("\(output)\n\(cmd.joined(separator: " "))")
throw DomainError.failedToCompile
}

Expand Down Expand Up @@ -198,7 +173,7 @@ struct CLI: AsyncParsableCommand {
if result.exitStatus != .terminated(code: 0) {

let output = try result.utf8stderrOutput()
Log.debug(.generic, "\(output)\n\(cmd.joined(separator: " "))")
Log.error("\(output)\n\(cmd.joined(separator: " "))")

throw DomainError.failureInMakingOutput
}
Expand Down
24 changes: 24 additions & 0 deletions Sources/Grain/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

enum Log {

static func debug(
file: StaticString = #file,
line: UInt = #line,
_ object: @autoclosure () -> Any
) {

if CommandLine.arguments.contains("--verbose") {
print(object())
}
}

static func error(
file: StaticString = #file,
line: UInt = #line,
_ object: @autoclosure () -> Any
) {
FileHandle.standardError.write(String(describing: object()).data(using: .utf8)!)
}

}

0 comments on commit 5a5855d

Please sign in to comment.