Skip to content

Commit

Permalink
Merge branch 'release/0.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKahnert committed Jan 16, 2020
2 parents dab23eb + f80cb00 commit 8261e75
Show file tree
Hide file tree
Showing 30 changed files with 673 additions and 181 deletions.
10 changes: 10 additions & 0 deletions .chia.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
skippedProviders:
# - ReadmeCheck
# - LicenseCheck
# - SwiftLintCheck

projectRootAppendix: Null

swiftLintConfig:
lintingRulesPath: https://PATH/TO/CONFIG/.swiftlint.yml
# lintingRulesPath: /LOCAL/PATH/TO/CONFIG/.swiftlint.yml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/Packages
/*.xcodeproj
xcuserdata/
.swiftlint.yml
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
27 changes: 27 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@
"revision": "e1577acf2b6e90086d01a6d5e2b8efdaae033568",
"version": "2.3.0"
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
"version": "1.2.0"
}
},
{
"package": "swift-tools-support-core",
"repositoryURL": "https://github.com/apple/swift-tools-support-core.git",
"state": {
"branch": null,
"revision": "693aba4c4c9dcc4767cc853a0dd38bf90ad8c258",
"version": "0.0.1"
}
},
{
"package": "Yams",
"repositoryURL": "https://github.com/jpsim/Yams.git",
"state": {
"branch": null,
"revision": "c947a306d2e80ecb2c0859047b35c73b8e1ca27f",
"version": "2.0.0"
}
}
]
},
Expand Down
16 changes: 13 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@ import PackageDescription
let package = Package(
name: "chia",
platforms: [
.macOS(.v10_12),
.macOS(.v10_12)
],
products: [
.library(name: "chiaLib", targets: ["chiaLib"]),
.library(name: "TerminalLog", targets: ["TerminalLog"]),
.executable(name: "chia", targets: ["chia"])
],
dependencies: [
.package(url: "https://github.com/johnsundell/shellout.git", from: "2.0.0"),
.package(url: "https://github.com/JohnSundell/Files", from: "4.0.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0")
],
targets: [
.target(
name: "chiaLib",
dependencies: [
"ShellOut", "Files"
"ShellOut", "Files", "Yams", "Logging"
]
),
.target(
name: "TerminalLog",
dependencies: [
"Logging"
]
),
.target(
name: "chia",
dependencies: ["chiaLib"]
dependencies: ["chiaLib", "SwiftToolsSupport-auto", "TerminalLog"]
),
.testTarget(
name: "chiaTests",
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ chia

# run docker container with all dependencies and mount the current folder for analysis
docker run -it -v ${PWD}:/project worldiety/chia:latest

```


## Installation

Using [Mint](https://github.com/yonaskolb/mint):
```bash
mint install worldiety/chia
```

Compiling from source:
```bash
git clone https://github.com/worldiety/chia && cd chia
swift build --configuration release
Expand Down
25 changes: 25 additions & 0 deletions Sources/TerminalLog/ASCIIColor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// ASCIIColor.swift
//
//
// Created by Julian Kahnert on 16.01.20.
//
// Source: https://stackoverflow.com/a/56510700/10026834

enum ASCIIColor: String {
case black = "\u{001B}[0;30m"
case red = "\u{001B}[0;31m"
case green = "\u{001B}[0;32m"
case yellow = "\u{001B}[0;33m"
case blue = "\u{001B}[0;34m"
case magenta = "\u{001B}[0;35m"
case cyan = "\u{001B}[0;36m"
case white = "\u{001B}[0;37m"
case `default` = "\u{001B}[0;0m"
}

extension DefaultStringInterpolation {
mutating func appendInterpolation<T: CustomStringConvertible>(_ value: T, color: ASCIIColor) {
appendInterpolation("\(color.rawValue)\(value)\(ASCIIColor.default.rawValue)")
}
}
78 changes: 78 additions & 0 deletions Sources/TerminalLog/TerminalLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// TerminalLog.swift
//
//
// Created by Julian Kahnert on 16.01.20.
//
// Source: https://github.com/chrisaljoudi/swift-log-oslog

import Logging

public struct TerminalLog: LogHandler {

public var metadata = Logger.Metadata() {
didSet {
self.prettyMetadata = self.prettify(self.metadata)
}
}
public var logLevel: Logger.Level = .info

private var prettyMetadata: String?

public init(_: String) { }

public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {

var combinedPrettyMetadata = self.prettyMetadata
if let metadataOverride = metadata, !metadataOverride.isEmpty {
combinedPrettyMetadata = self.prettify(
self.metadata.merging(metadataOverride) {
return $1
}
)
}

var formedMessage = message.description
if combinedPrettyMetadata != nil {
formedMessage += " -- " + combinedPrettyMetadata!
}

print("\(formedMessage, color: getColor(for: level))\n")
}

/// Add, remove, or change the logging metadata.
/// - parameters:
/// - metadataKey: the key for the metadata item.
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
return self.metadata[metadataKey]
}
set {
self.metadata[metadataKey] = newValue
}
}

private func prettify(_ metadata: Logger.Metadata) -> String? {
if metadata.isEmpty {
return nil
}
return metadata.map {
"\($0)=\($1)"
}.joined(separator: " ")
}

private func getColor(for level: Logger.Level) -> ASCIIColor {
let color: ASCIIColor
switch level {
case .critical, .error:
color = .red
case .warning:
color = .yellow
case .info:
color = .green
default:
color = .default
}
return color
}
}
58 changes: 56 additions & 2 deletions Sources/chia/main.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
// swiftlint:disable line_length

import chiaLib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Logging
import TerminalLog
import TSCUtility
import Files

// bootstrap logging
LoggingSystem.bootstrap(TerminalLog.init)
let logger = Logger(label: "chia-cli")

// parse command line argument
let parser = ArgumentParser(commandName: "chia",
usage: "[--only-language-detection]",
overview: "Run several check like linting etc. in your CI process.")
let configPath: OptionArgument<String> = parser.add(option: "--config", shortName: "-c", kind: String.self, usage: "Path to the Config file (local or remote), e.g. 'https://PATH/TO/.chia.yml'", completion: .filename)
let onlyLanguageDetection: OptionArgument<Bool> = parser.add(option: "--only-language-detection", kind: Bool.self)

do {
try Chia.runChecks()
let result = try parser.parse(Array(CommandLine.arguments.dropFirst()))

// setup chia
var chia = Chia(logger: logger)

// try to get a config path from the CLI - use default config otherwise
if let configPath = result.get(configPath) {

guard let url = URL(localOrRemotePath: configPath) else {
logger.error("Could not find a config at:\n\(configPath)")
exit(1)
}
try chia.setConfig(from: url)
} else {

// no url is provied - use the default one
try chia.setConfig(from: nil)
}

if result.get(onlyLanguageDetection) ?? false {
if let detectedLanguage = chia.detectProjectLanguage() {
logger.info("Language: \(detectedLanguage)")
} else {
logger.warning("No language detected.")
}
} else {
try chia.runChecks()
}
} catch ArgumentParserError.expectedValue(let value) {
logger.error("Missing value for argument \(value).")
exit(1)
} catch ArgumentParserError.expectedArguments(_, let stringArray) {
logger.error("Missing arguments: \(stringArray.joined()).")
exit(1)
} catch {
print(error)
logger.error("\(error.localizedDescription)")
exit(1)
}
exit(0)
Loading

0 comments on commit 8261e75

Please sign in to comment.