diff --git a/Sources/LicensePlist/main.swift b/Sources/LicensePlist/main.swift index 0fa4207e..0ce3f15e 100644 --- a/Sources/LicensePlist/main.swift +++ b/Sources/LicensePlist/main.swift @@ -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), @@ -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)) @@ -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), diff --git a/Sources/LicensePlistCore/Consts.swift b/Sources/LicensePlistCore/Consts.swift index a2317f34..0c6e40bf 100644 --- a/Sources/LicensePlistCore/Consts.swift +++ b/Sources/LicensePlistCore/Consts.swift @@ -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" diff --git a/Sources/LicensePlistCore/Entity/Options.swift b/Sources/LicensePlistCore/Entity/Options.swift index fedd8450..85b2858b 100644 --- a/Sources/LicensePlistCore/Entity/Options.swift +++ b/Sources/LicensePlistCore/Entity/Options.swift @@ -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? @@ -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, @@ -25,6 +27,7 @@ public struct Options { cartfilePath: URL, podsPath: URL, packagePath: URL, + xcodeprojPath: URL, prefix: String, gitHubToken: String?, htmlPath: URL?, @@ -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 diff --git a/Sources/LicensePlistCore/LicensePlist.swift b/Sources/LicensePlistCore/LicensePlist.swift index 8d461403..c27e4d24 100644 --- a/Sources/LicensePlistCore/LicensePlist.swift +++ b/Sources/LicensePlistCore/LicensePlist.swift @@ -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() @@ -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() { @@ -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)") diff --git a/Tests/LicensePlistTests/Entity/PlistInfoTests.swift b/Tests/LicensePlistTests/Entity/PlistInfoTests.swift index dfa99cc2..ce6daa6d 100644 --- a/Tests/LicensePlistTests/Entity/PlistInfoTests.swift +++ b/Tests/LicensePlistTests/Entity/PlistInfoTests.swift @@ -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,