Skip to content

Commit

Permalink
Merge pull request #218 from RyuNen344/feature/support-multi-new-line
Browse files Browse the repository at this point in the history
feature(yaml): replace yaml parser library to support multi new line
  • Loading branch information
mono0926 authored Jun 16, 2023
2 parents 69aab78 + 27673b3 commit d6c152c
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 116 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@
}
},
{
"identity" : "yamlswift",
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/behrang/YamlSwift.git",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "287f5cab7da0d92eb947b5fd8151b203ae04a9a3",
"version" : "3.4.4"
"revision" : "f47ba4838c30dbd59998a4e4c87ab620ff959e8a",
"version" : "5.0.5"
}
}
],
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ let package = Package(
from: "5.3.0"),
.package(url: "https://github.com/Kitura/HeliumLogger.git",
from: "2.0.0"),
.package(url: "https://github.com/behrang/YamlSwift.git",
from: "3.4.4"),
.package(url: "https://github.com/Kitura/swift-html-entities.git",
from: "4.0.1"),
.package(url: "https://github.com/YusukeHosonuma/SwiftParamTest",
.upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/tomlokhorst/XcodeEdit.git",
from: "2.9.0")
from: "2.9.0"),
.package(url: "https://github.com/jpsim/Yams.git",
from: "5.0.5")
],
targets: [
.executableTarget(
Expand All @@ -43,7 +43,7 @@ let package = Package(
"APIKit",
"HeliumLogger",
.product(name: "HTMLEntities", package: "swift-html-entities"),
.product(name: "Yaml", package: "YamlSwift")
.product(name: "Yams", package: "Yams")
]
),
.testTarget(
Expand Down
41 changes: 18 additions & 23 deletions Sources/LicensePlistCore/Entity/Config.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import LoggerAPI
import Yaml
import Yams

public struct Config {
let githubs: [GitHub]
Expand All @@ -19,38 +19,33 @@ public struct Config {
public static let empty = Config(githubs: [], manuals: [], excludes: [Exclude](), renames: [:], options: .empty)

public init(yaml: String, configBasePath: URL) {
let value = try! Yaml.load(yaml)
let excludes = value["exclude"].array?.compactMap({ Exclude(from: $0) }) ?? []
let renames = value["rename"].dictionary?.reduce([String: String]()) { sum, e in
let value = try! Yams.compose(yaml: yaml)?.mapping ?? Node.Mapping([])
let excludes = value["exclude"]?.sequence?.compactMap({ Exclude(from: $0) }) ?? []
let renames = value["rename"]?.mapping?.reduce([String: String]()) { sum, e in
guard let from = e.key.string, let to = e.value.string else { return sum }
var sum = sum
sum[from] = to
return sum
} ?? [:]
let manuals = value["manual"].array ?? []
let manualList = Manual.load(manuals, renames: renames, configBasePath: configBasePath)
let githubs = value["github"].array?.compactMap { $0.string }.compactMap { $0 } ?? []
let gitHubList = githubs.map { GitHub.load(.licensePlist(content: $0), renames: renames) }.flatMap { $0 }
gitHubList.forEach {
} ?? [:]
let manuals = Manual.load(value["manual"]?.sequence ?? Node.Sequence(), renames: renames, configBasePath: configBasePath)
let nonVersion = (value["github"]?.sequence ?? Node.Sequence())
.compactMap { $0.string }
.flatMap { GitHub.load(.licensePlist(content: $0), renames: renames)}
nonVersion.forEach {
Log.warning("\($0.name) is specified by the depricated format. It will be removed at Version 2." +
"See: https://github.com/mono0926/LicensePlist/blob/master/Tests/LicensePlistTests/Resources/license_plist.yml .")
}
let githubsVersion: [GitHub] = value["github"].array?.map {
guard let dictionary = $0.dictionary else {
return nil
let versioned: [GitHub] = (value["github"]?.sequence ?? Node.Sequence())
.compactMap {
guard let map = $0.mapping, let owner = map["owner"]?.string, let name = map["name"]?.string else {
return nil
}
return GitHub(name: name, nameSpecified: renames[name], owner: owner, version: map["version"]?.string)
}
guard let owner = dictionary["owner"]?.string, let name = dictionary["name"]?.string else {
return nil
}
return GitHub(name: name,
nameSpecified: renames[name],
owner: owner,
version: dictionary["version"]?.string)
}.compactMap { $0 } ?? []
let options: GeneralOptions = value["options"].dictionary.map {
let options: GeneralOptions = (value["options"]?.mapping ?? Node.Mapping()).map {
GeneralOptions.load($0, configBasePath: configBasePath)
} ?? .empty
self = Config(githubs: githubsVersion + gitHubList, manuals: manualList, excludes: excludes, renames: renames, options: options)
self = Config(githubs: versioned + nonVersion, manuals: manuals, excludes: excludes, renames: renames, options: options)
}

public init(githubs: [GitHub], manuals: [Manual], excludes: [String], renames: [String: String], options: GeneralOptions) {
Expand Down
7 changes: 3 additions & 4 deletions Sources/LicensePlistCore/Entity/Exclude.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import Yaml
import Yams
import LoggerAPI

public struct Exclude {
Expand All @@ -15,13 +15,12 @@ public struct Exclude {
self.licenseType = licenseType
}

public init?(from yaml: Yaml) {
public init?(from yaml: Node) {
if let name = yaml.string {
self.init(name: name, owner: nil, source: nil, licenseType: nil)
return
}

guard let dictionary = yaml.dictionary else {
guard let dictionary = yaml.mapping else {
Log.warning("Attempt to load exclude failed. Supported YAML types are String and Dictionary.")
return nil
}
Expand Down
45 changes: 23 additions & 22 deletions Sources/LicensePlistCore/Entity/GeneralOptions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import LoggerAPI
import Yaml
import Yams

public struct GeneralOptions {
public let outputPath: URL?
Expand Down Expand Up @@ -114,26 +114,27 @@ extension GeneralOptions {
}

extension GeneralOptions {
public static func load(_ raw: [Yaml: Yaml], configBasePath: URL) -> GeneralOptions {
return GeneralOptions(outputPath: raw["outputPath"]?.string.asPathURL(in: configBasePath),
cartfilePath: raw["cartfilePath"]?.string.asPathURL(in: configBasePath),
mintfilePath: raw["mintfilePath"]?.string.asPathURL(in: configBasePath),
podsPath: raw["podsPath"]?.string.asPathURL(in: configBasePath),
packagePaths: raw["packagePaths"]?.array?.compactMap { $0.string.asPathURL(in: configBasePath) },
packageSourcesPath: raw["packageSourcesPath"]?.string.asPathURL(in: configBasePath, isDirectory: true),
xcworkspacePath: raw["xcworkspacePath"]?.string.asPathURL(in: configBasePath),
xcodeprojPath: raw["xcodeprojPath"]?.string.asPathURL(in: configBasePath),
prefix: raw["prefix"]?.string,
gitHubToken: raw["gitHubToken"]?.string,
htmlPath: raw["htmlPath"]?.string.asPathURL(in: configBasePath),
markdownPath: raw["markdownPath"]?.string.asPathURL(in: configBasePath),
licenseFileNames: raw["licenseFileNames"]?.array?.compactMap(\.string),
force: raw["force"]?.bool,
addVersionNumbers: raw["addVersionNumbers"]?.bool,
suppressOpeningDirectory: raw["suppressOpeningDirectory"]?.bool,
singlePage: raw["singlePage"]?.bool,
failIfMissingLicense: raw["failIfMissingLicense"]?.bool,
addSources: raw["addSources"]?.bool,
sandboxMode: raw["sandboxMode"]?.bool)
public static func load(_ raw: Node.Mapping, configBasePath: URL) -> GeneralOptions {
return GeneralOptions(
outputPath: raw["outputPath"]?.string.asPathURL(in: configBasePath),
cartfilePath: raw["cartfilePath"]?.string.asPathURL(in: configBasePath),
mintfilePath: raw["mintfilePath"]?.string.asPathURL(in: configBasePath),
podsPath: raw["podsPath"]?.string.asPathURL(in: configBasePath),
packagePaths: raw["packagePaths"]?.sequence?.compactMap { $0.string.asPathURL(in: configBasePath) },
packageSourcesPath: raw["packageSourcesPath"]?.string.asPathURL(in: configBasePath, isDirectory: true),
xcworkspacePath: raw["xcworkspacePath"]?.string.asPathURL(in: configBasePath),
xcodeprojPath: raw["xcodeprojPath"]?.string.asPathURL(in: configBasePath),
prefix: raw["prefix"]?.string,
gitHubToken: raw["gitHubToken"]?.string,
htmlPath: raw["htmlPath"]?.string.asPathURL(in: configBasePath),
markdownPath: raw["markdownPath"]?.string.asPathURL(in: configBasePath),
licenseFileNames: raw["licenseFileNames"]?.sequence?.compactMap { $0.string },
force: raw["force"]?.bool,
addVersionNumbers: raw["addVersionNumbers"]?.bool,
suppressOpeningDirectory: raw["suppressOpeningDirectory"]?.bool,
singlePage: raw["singlePage"]?.bool,
failIfMissingLicense: raw["failIfMissingLicense"]?.bool,
addSources: raw["addSources"]?.bool,
sandboxMode: raw["sandboxMode"]?.bool)
}
}
41 changes: 17 additions & 24 deletions Sources/LicensePlistCore/Entity/Manual.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import APIKit
import LoggerAPI
import Yaml
import Yams

public class Manual: Library {
public let name: String
Expand Down Expand Up @@ -36,33 +36,26 @@ extension Manual: CustomStringConvertible {
}

extension Manual {
public static func load(_ raw: [Yaml],
renames: [String: String], configBasePath: URL) -> [Manual] {
return raw.map { (manualEntry) -> Manual in
var name = ""
public static func load(_ raw: Node.Sequence, renames: [String: String], configBasePath: URL) -> [Manual] {
return raw.compactMap({
let mapping = $0.mapping ?? Node.Mapping()
let name = mapping["name"]?.string ?? ""
let version = mapping["version"]?.string
let source = mapping["source"]?.string

var body: String?
var source: String?
var version: String?
for valuePair in manualEntry.dictionary ?? [:] {
switch valuePair.key.string ?? "" {
case "source":
source = valuePair.value.string
case "name":
name = valuePair.value.string ?? ""
case "version":
version = valuePair.value.string
case "body":
body = valuePair.value.string
case "file":
let url = configBasePath.appendingPathComponent(valuePair.value.string!)
body = try! String(contentsOf: url)
default:
Log.warning("Tried to parse an unknown YAML key")
}
if let raw = mapping["body"]?.string {
body = raw
}

if let file = mapping["file"]?.string {
let url = configBasePath.appendingPathComponent(file)
body = try! String(contentsOf: url)
}

let manual = Manual(name: name, source: source, nameSpecified: renames[name], version: version)
manual.body = body // This is so that we do not have to store a body at all ( for testing purposes mostly )
return manual
}
})
}
}
21 changes: 21 additions & 0 deletions Tests/LicensePlistTests/Entity/ConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class ConfigTests: XCTestCase {
source: "https://webrtc.googlesource.com/src",
nameSpecified: "Web RTC",
version: "M61"),
Manual(name: "Firebase",
source: "https://github.com/firebase/firebase-ios-sdk",
nameSpecified: nil,
version: "4.0.0"),
Manual(name: "Dummy License File", source: nil, nameSpecified: nil, version: nil)],
excludes: ["RxSwift", "ios-license-generator", "/^Core.*$/"],
renames: ["LicensePlist": "License Plist", "WebRTC": "Web RTC"],
Expand Down Expand Up @@ -198,4 +202,21 @@ class ConfigTests: XCTestCase {
XCTAssertEqual(result, [manual1, shouldBeIncluded])
}

func test_mulitnewlineYaml() throws {
let yaml = """
manual:
- body: |2
New Line
new line
endline.
name: name
source: source
version: 2.0.0
"""
XCTAssertEqual(
"\n New Line\n new line\n endline.\n",
Config(yaml: yaml, configBasePath: URL(string: "/LicensePlist")!).manuals[0].body
)
}
}
47 changes: 21 additions & 26 deletions Tests/LicensePlistTests/Entity/ExcludeTests.swift
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
import Foundation
import XCTest
import Yaml
import Yams
@testable import LicensePlistCore

class ExcludeTests: XCTestCase {

func testInit_yaml_dictionary() {
let testDict: [Yaml: Yaml] = [
let node: Node = [
"name": "LicensePlist",
"owner": "mono0926",
"source": "https://github.com/mono0926/LicensePlist",
"licenseType": "MIT"
]
let yaml = Yaml.dictionary(testDict)
let exclude = Exclude(from: yaml)
let exclude = Exclude(from: node)
XCTAssertNotNil(exclude)
XCTAssertEqual(exclude!.name, yaml["name"].string)
XCTAssertEqual(exclude!.owner, yaml["owner"].string)
XCTAssertEqual(exclude!.source, yaml["source"].string)
XCTAssertEqual(exclude!.licenseType, yaml["licenseType"].string)
XCTAssertEqual(exclude!.name, node.mapping!["name"]!.string)
XCTAssertEqual(exclude!.owner, node.mapping!["owner"]!.string)
XCTAssertEqual(exclude!.source, node.mapping!["source"]!.string)
XCTAssertEqual(exclude!.licenseType, node.mapping!["licenseType"]!.string)
}

func testInit_yaml_dictionary_missing_some_properties() {
let testDict: [Yaml: Yaml] = [
let node: Node = [
"name": "LicensePlist",
"owner": "mono0926"
]
let yaml = Yaml.dictionary(testDict)
let exclude = Exclude(from: yaml)
let exclude = Exclude(from: node)
XCTAssertNotNil(exclude)
XCTAssertEqual(exclude!.name, yaml["name"].string)
XCTAssertEqual(exclude!.owner, yaml["owner"].string)
XCTAssertEqual(exclude!.name, node.mapping!["name"]!.string)
XCTAssertEqual(exclude!.owner, node.mapping!["owner"]!.string)
XCTAssertNil(exclude!.source)
XCTAssertNil(exclude!.licenseType)
}

func testInit_yaml_no_properties() {
let testDict: [Yaml: Yaml] = [:]
let yaml = Yaml.dictionary(testDict)
let exclude = Exclude(from: yaml)
let node: Node = [:]
let exclude = Exclude(from: node)
XCTAssertNil(exclude)
}

func testInit_yaml_invalid_properties() {
let testDict: [Yaml: Yaml] = ["invalid": "invalid"]
let yaml = Yaml.dictionary(testDict)
let exclude = Exclude(from: yaml)
let node: Node = ["invalid": "invalid"]
let exclude = Exclude(from: node)
XCTAssertNil(exclude)
}

func testInit_yaml_string() {
let testString = "LicensePlist"
let yaml = Yaml.string(testString)
let exclude = Exclude(from: yaml)
let node = Node(testString)
let exclude = Exclude(from: node)
XCTAssertNotNil(exclude)
XCTAssertEqual(exclude!.name, testString)
XCTAssertNil(exclude!.owner)
Expand All @@ -61,17 +57,16 @@ class ExcludeTests: XCTestCase {
}

func testInit_yaml_invalid_yaml_type() {
let yaml = Yaml.bool(true)
let exclude = Exclude(from: yaml)
let node = Node([Node]())
let exclude = Exclude(from: node)
XCTAssertNil(exclude)
}

func testInit_yaml_invalid_license_type() {
let testDict: [Yaml: Yaml] = [
let node: Node = [
"licenseType": "invalid"
]
let yaml = Yaml.dictionary(testDict)
let exclude = Exclude(from: yaml)
let exclude = Exclude(from: node)
XCTAssertNotNil(exclude)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ class SwiftPackageFileReaderTests: XCTestCase {
}
},
{
"identity" : "yamlswift",
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/behrang/YamlSwift.git",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "287f5cab7da0d92eb947b5fd8151b203ae04a9a3",
"version" : "3.4.4"
"revision" : "f47ba4838c30dbd59998a4e4c87ab620ff959e8a",
"version" : "5.0.5"
}
}
],
Expand Down
Loading

0 comments on commit d6c152c

Please sign in to comment.