Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use githubLicenses for the latest_result.txt output #228

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/LicensePlistCore/Entity/GitHubLicense.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public struct GitHubLicense: License, Equatable {
}
}

extension GitHubLicense: CustomStringConvertible {
public var description: String { library.description }
}

extension GitHubLicense {

public enum DownloadError: Error {
Expand Down
31 changes: 23 additions & 8 deletions Sources/LicensePlistCore/Entity/PlistInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ struct PlistInfo {

let config = options.config

let contents = (cocoaPodsLicenses.map { String(describing: $0) } +
githubLibraries.map { String(describing: $0) } +
manualLicenses.map { String(describing: $0) } +
["add-version-numbers: \(options.config.addVersionNumbers)", "LicensePlist Version: \(Consts.version)"])
.joined(separator: "\n\n")
let potential = makeSummary(
licenseDescriptions: (
cocoaPodsLicenses.map { String(describing: $0) } +
githubLibraries.map { String(describing: $0) } +
manualLicenses.map { String(describing: $0) }
)
)
let savePath = options.outputPath.appendingPathComponent("\(options.prefix).latest_result.txt")
if let previous = savePath.lp.read(), previous == contents, !config.force {
if let previous = savePath.lp.read(), previous == potential, !config.force {
Log.warning("Completed because no diff. You can execute force by `--force` flag.")
exit(0)
}
summary = contents
summaryPath = savePath
}

Expand All @@ -100,13 +101,19 @@ struct PlistInfo {
let githubLicenses = githubLicenses,
let manualLicenses = manualLicenses else { preconditionFailure() }

licenses = ((cocoaPodsLicenses as [LicenseInfo]) + (githubLicenses as [LicenseInfo]) + (manualLicenses as [LicenseInfo]))
let licenseInfos: [LicenseInfo] = cocoaPodsLicenses + githubLicenses + manualLicenses

licenses = licenseInfos
.reduce([String: LicenseInfo]()) { sum, e in
var sum = sum
sum[e.name] = e
return sum
}.values
.sorted { $0.name.lowercased() < $1.name.lowercased() }

summary = makeSummary(
licenseDescriptions: licenseInfos.map { String(describing: $0) }
)
}

func outputPlist() {
Expand Down Expand Up @@ -200,4 +207,12 @@ struct PlistInfo {
fatalError("'--package-sources-path' must be specified when using '--sandbox-mode'")
}
}

private func makeSummary(licenseDescriptions: [String]) -> String {
let additionalInfos: [String] = [
"add-version-numbers: \(options.config.addVersionNumbers)",
"LicensePlist Version: \(Consts.version)"
]
return (licenseDescriptions + additionalInfos).joined(separator: "\n\n")
}
}
2 changes: 1 addition & 1 deletion Sources/LicensePlistCore/LicensePlist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class LicensePlist {
}
}

/// Gets the result of attempting to read the `Package.resolved` from ether a Xcode Workspace or Xcode project.
/// Gets the result of attempting to read the `Package.resolved` from either a Xcode Workspace or Xcode project.
/// - note: If an Xcode workspace is found it is preferred over a Xcode project.
private func xcodeFileReadResult(xcworkspacePath: URL, xcodeprojPath: URL) throws -> String? {

Expand Down
17 changes: 13 additions & 4 deletions Tests/LicensePlistTests/Entity/PlistInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ class PlistInfoTests: XCTestCase {
XCTAssertNil(target.summaryPath)
target.compareWithLatestSummary()

XCTAssertEqual(target.summary,
"add-version-numbers: false\n\nLicensePlist Version: 3.25.1")
XCTAssertNotNil(target.summaryPath)
}

Expand All @@ -111,26 +109,37 @@ class PlistInfoTests: XCTestCase {

func testCollectLicenseInfos() throws {
var target = PlistInfo(options: options)
let github = GitHub(name: "LicensePlist", nameSpecified: nil, owner: "mono0926", version: nil)
let github = GitHub(name: "LicensePlist", nameSpecified: "LicensePlist", owner: "mono0926", version: "0.0.1")
let githubLicense = GitHubLicense(library: github,
body: "body",
githubResponse: LicenseResponse(content: "",
encoding: "",
kind: LicenseKindResponse(name: "name",
spdxId: nil)))
target.cocoaPodsLicenses = []
let manual = Manual(name: "FooBar", source: "https://foo.bar", nameSpecified: nil, version: nil)
let manual = Manual(name: "FooBar", source: "https://foo.bar", nameSpecified: "FooBar", version: "0.0.1")
let manualLicense = ManualLicense(library: manual,
body: "body")
target.manualLicenses = [manualLicense]
target.githubLicenses = [githubLicense]
let expectedSummary = """
name: LicensePlist, nameSpecified: LicensePlist, owner: mono0926, version: 0.0.1, source: https://github.com/mono0926/LicensePlist

name: FooBar, nameSpecified: FooBar, version: 0.0.1
body: body…

add-version-numbers: false

LicensePlist Version: 3.25.1
"""

XCTAssertNil(target.licenses)
target.collectLicenseInfos()
let licenses = try XCTUnwrap(target.licenses)
XCTAssertEqual(licenses.count, 2)
let license = licenses.last
XCTAssertEqual(license?.name, "LicensePlist")
XCTAssertEqual(target.summary, expectedSummary)
}

// MEMO: No result assertions
Expand Down
Loading