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

parse Xcode 11 projects with integrated SPM #114

Merged
merged 1 commit into from Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/LicensePlist/main.swift
Expand Up @@ -13,6 +13,7 @@ private func loadConfig(configPath: URL) -> Config {
let main = command(Option("cartfile-path", default: Consts.cartfileName),
Option("pods-path", default: Consts.podsDirectoryName),
Option("package-path", default: Consts.packageName),
Option("xcodeproj-path", default: "*.xcodeproj"),
Option("output-path", default: Consts.outputPath),
Option("github-token", default: ""),
Option("config-path", default: Consts.configPath),
Expand All @@ -21,7 +22,7 @@ let main = command(Option("cartfile-path", default: Consts.cartfileName),
Option("markdown-path", default: ""),
Flag("force"),
Flag("add-version-numbers"),
Flag("suppress-opening-directory")) { cartfile, podsPath, packagePath, output, gitHubToken, configPath, prefix, htmlPath, markdownPath, force, version, suppressOpen in
Flag("suppress-opening-directory")) { cartfile, podsPath, packagePath, xcodeprojPath, output, gitHubToken, configPath, prefix, htmlPath, markdownPath, force, version, suppressOpen in

Logger.configure()
var config = loadConfig(configPath: URL(fileURLWithPath: configPath))
Expand All @@ -32,6 +33,7 @@ let main = command(Option("cartfile-path", default: Consts.cartfileName),
cartfilePath: URL(fileURLWithPath: cartfile),
podsPath: URL(fileURLWithPath: podsPath),
packagePath: URL(fileURLWithPath: packagePath),
xcodeprojPath: URL(fileURLWithPath: xcodeprojPath),
prefix: prefix,
gitHubToken: gitHubToken.isEmpty ? ProcessInfo.processInfo.environment["LICENSE_PLIST_GITHUB_TOKEN"] : gitHubToken,
htmlPath: htmlPath.isEmpty ? nil : URL(fileURLWithPath: htmlPath),
Expand Down
1 change: 1 addition & 0 deletions Sources/LicensePlistCore/Consts.swift
Expand Up @@ -4,6 +4,7 @@ public struct Consts {
public static let cartfileName = "Cartfile"
public static let podsDirectoryName = "Pods"
public static let packageName = "Package.swift"
public static let xcodeprojExtension = "xcodeproj"
public static let prefix = "com.mono0926.LicensePlist"
public static let outputPath = "\(prefix).Output"
public static let configPath = "license_plist.yml"
Expand Down
4 changes: 4 additions & 0 deletions Sources/LicensePlistCore/Entity/Options.swift
Expand Up @@ -5,6 +5,7 @@ public struct Options {
public let cartfilePath: URL
public let podsPath: URL
public let packagePath: URL
public let xcodeprojPath: URL
public let prefix: String
public let gitHubToken: String?
public let htmlPath: URL?
Expand All @@ -15,6 +16,7 @@ public struct Options {
cartfilePath: URL(fileURLWithPath: ""),
podsPath: URL(fileURLWithPath: ""),
packagePath: URL(fileURLWithPath: ""),
xcodeprojPath: URL(fileURLWithPath: ""),
prefix: Consts.prefix,
gitHubToken: nil,
htmlPath: nil,
Expand All @@ -25,6 +27,7 @@ public struct Options {
cartfilePath: URL,
podsPath: URL,
packagePath: URL,
xcodeprojPath: URL,
prefix: String,
gitHubToken: String?,
htmlPath: URL?,
Expand All @@ -34,6 +37,7 @@ public struct Options {
self.cartfilePath = cartfilePath
self.podsPath = podsPath
self.packagePath = packagePath
self.xcodeprojPath = xcodeprojPath
self.prefix = prefix
self.gitHubToken = gitHubToken
self.htmlPath = htmlPath
Expand Down
27 changes: 25 additions & 2 deletions Sources/LicensePlistCore/LicensePlist.swift
Expand Up @@ -11,7 +11,7 @@ public final class LicensePlist {
var info = PlistInfo(options: options)
info.loadCocoaPodsLicense(acknowledgements: readPodsAcknowledgements(path: options.podsPath))
info.loadGitHubLibraries(cartfile: readCartfile(path: options.cartfilePath))
info.loadSwiftPackageLibraries(packageFile: readSwiftPackages(path: options.packagePath))
info.loadSwiftPackageLibraries(packageFile: readSwiftPackages(path: options.packagePath) ?? readXcodeProject(path: options.xcodeprojPath))
info.loadManualLibraries()
info.compareWithLatestSummary()
info.downloadGitHubLicenses()
Expand All @@ -37,7 +37,7 @@ private func readCartfile(path: URL) -> String? {
}

private func readSwiftPackages(path: URL) -> String? {
if path.lastPathComponent != Consts.packageName {
if path.lastPathComponent != Consts.packageName && path.lastPathComponent != "Package.resolved" {
fatalError("Invalid Package.swift name: \(path.lastPathComponent)")
}
if let content = path.deletingPathExtension().appendingPathExtension("resolved").lp.read() {
Expand All @@ -46,6 +46,29 @@ private func readSwiftPackages(path: URL) -> String? {
return path.lp.read()
}

private func readXcodeProject(path: URL) -> String? {

var projectPath: URL?
if path.lastPathComponent.contains("*") {
// find first "xcodeproj" in directory
projectPath = path.deletingLastPathComponent().lp.listDir().first { $0.pathExtension == Consts.xcodeprojExtension }
} else {
// use the specified path
projectPath = path
}
guard let validatedPath = projectPath else { return nil }

if validatedPath.pathExtension != Consts.xcodeprojExtension {
return nil
}
let packageResolvedPath = validatedPath
.appendingPathComponent("project.xcworkspace")
.appendingPathComponent("xcshareddata")
.appendingPathComponent("swiftpm")
.appendingPathComponent("Package.resolved")
return readSwiftPackages(path: packageResolvedPath)
}

private func readPodsAcknowledgements(path: URL) -> [String] {
if path.lastPathComponent != Consts.podsDirectoryName {
fatalError("Invalid Pods name: \(path.lastPathComponent)")
Expand Down
1 change: 1 addition & 0 deletions Tests/LicensePlistTests/Entity/PlistInfoTests.swift
Expand Up @@ -13,6 +13,7 @@ class PlistInfoTests: XCTestCase {
cartfilePath: URL(fileURLWithPath: "test_result_dir"),
podsPath: URL(fileURLWithPath: "test_result_dir"),
packagePath: URL(fileURLWithPath: "test_result_dir"),
xcodeprojPath: URL(fileURLWithPath: "test_result_dir"),
prefix: Consts.prefix,
gitHubToken: nil,
htmlPath: nil,
Expand Down