LicenseList
repository·main·Indexed 42 days ago
https://github.com/cybozu/licenselistA Swift library that automatically generates and displays a list of licenses for all Swift Package Manager dependencies. It includes ready-to-use UI components for SwiftUI and UIKit, a BuildToolPlugin called PrepareLicenseList to automate file generation, and the spp command-line tool for parsing SourcePackages directories. Requires Xcode 26.2+, Swift 6.2, and supports iOS 16.0+, macOS 13.0+, and tvOS 17.0+.
What's inside LicenseList
- LicenseList is a Swift Package designed to automatically collect and display a list of licenses for all libraries your app depends on. It provides ready-to-use, refined UI components for both SwiftUI and UIKit, while also offering access to the collected license data so you can build your own custom views.
How LicenseList Works
mainLicenseList uses a
BuildToolPluginto automatically extract license information from your project's dependencies.- Dependency Fetching: Swift Package Manager fetches dependent libraries into a
SourcePackagesdirectory (typically insideDerivedData). - Extraction: The
BuildToolPluginscans these cloned repositories forLICENSEfiles. - Display: The extracted information is used to populate the license list view in your application.
- Dependency Fetching: Swift Package Manager fetches dependent libraries into a
Configure custom SourcePackages path for CI
mainLicenseList relies on the standard
SourcePackagesdirectory location. If you change the location ofSourcePackagesusing the-clonedSourcePackagesDirPathflag (common in CI environments to cache libraries), LicenseList may fail to find the licenses.To resolve this, use one of the following two methods. Method 1 is recommended as it is less prone to error.
### Method 1: Use the PLL_SOURCE_PACKAGES_PATH environment variable (Recommended) Set an absolute path to the `PLL_SOURCE_PACKAGES_PATH` environment variable before running your build command. ```sh export PLL_SOURCE_PACKAGES_PATH="$PWD/SourcePackages" xcodebuild clean build \ -project Examples/Examples.xcodeproj \ -scheme ExamplesForSwiftUI \ -destination "platform=iOS Simulator,name=iPhone 16,OS=18.5" \ -clonedSourcePackagesDirPath $PLL_SOURCE_PACKAGES_PATHMethod 2: Use both -derivedDataPath and -clonedSourcePackagesDirPath
Specify both paths such that
SourcePackagesis located inside theDerivedDatadirectory.xcodebuild clean build \ -project Examples/Examples.xcodeproj \ -scheme ExamplesForSwiftUI \ -destination "platform=iOS Simulator,name=iPhone 16,OS=18.5" \ -derivedDataPath ./DerivedData \ -clonedSourcePackagesDirPath ./DerivedData/SourcePackagesImportant Caveat for Method 2: The
BuildToolPluginsearches forSourcePackagesspecifically within theDerivedDatadirectory. If you specify aSourcePackagespath that is independent of or outside of theDerivedDatapath, the LicenseList build will fail.Install LicenseList via Swift Package Manager
mainLicenseList can be installed using Xcode or by manually configuring a
Package.swiftfile for CLI/Server-side Swift environments.Xcode Installation
- Go to File > Add Package Dependencies…
- Search for
https://github.com/cybozu/LicenseList.git. - Add the package and ensure
LicenseListis linked to your application target.
CLI / Package.swift Installation
Add the dependency to your
Package.swiftfile and runswift package resolve.// swift-tools-version: 6.2 import PackageDescription let package = Package( name: "SomeProduct", products: [ .library(name: "SomeProduct", targets: ["SomeProduct"]) ], dependencies: [ .package(url: "https://github.com/cybozu/LicenseList.git", exact: "x.y.z") ], targets: [ .target( name: "SomeProduct", dependencies: [ .product(name: "LicenseList", package: "LicenseList") ] ) ] )Use the PrepareLicenseList Build Tool Plugin
mainThe
PrepareLicenseListplugin is a SwiftBuildToolPlugindesigned to automate the generation of aLicenseList.swiftfile. It locates theSourcePackagesdirectory and executes thespptool to produce the output file.Plugin Behavior
- Locating SourcePackages: The plugin searches for a
SourcePackagesdirectory by traversing up the directory tree from thepluginWorkDirectory. - Environment Override: You can manually specify the location of the
SourcePackagesdirectory by setting thePLL_SOURCE_PACKAGES_PATHenvironment variable. - Command Execution: The plugin generates a build command that runs the
sppexecutable, passing the desired output path and the source packages path as arguments.
Generated Output
The plugin produces a file named
LicenseList.swiftlocated in the plugin's work directory.// The plugin is typically integrated via Package.swift // It will automatically generate 'LicenseList.swift' in the plugin work directory.- Locating SourcePackages: The plugin searches for a
Configure PLL_SOURCE_PACKAGES_PATH for CI environments
mainIf you are running in a CI environment where theSourcePackagesdirectory is at a non-default path, you must set thePLL_SOURCE_PACKAGES_PATHenvironment variable to the absolute path of that directory. This prevents thePrepareLicenseListplugin from failing during the build process.Use LicenseList in UIKit
mainTo display the license list in a UIKit application, instantiate
LicenseListViewController. You can customize the view style to include repository anchor links.import LicenseList // in ViewController let vc = LicenseListViewController() vc.title = "LICENSE" // If you want to anchor link of the repository vc.licenseViewStyle = .withRepositoryAnchorLink navigationController?.pushViewController(vc, animated: true)Use LicenseList in SwiftUI
mainTo display the license list in a SwiftUI application, use the
LicenseListViewview. You can use the.licenseViewStyle()modifier to enable repository anchor links.import LicenseList struct ContentView: View { var body: some View { NavigationView { LicenseListView() // If you want to anchor link of the repository .licenseViewStyle(.withRepositoryAnchorLink) .navigationTitle("LICENSE") } } }Configure the SourcePackages path via PLL_SOURCE_PACKAGES_PATH
mainIf the plugin cannot automatically locate the
SourcePackagesdirectory through directory traversal, you can provide an explicit path using thePLL_SOURCE_PACKAGES_PATHenvironment variable. This variable should point to the directory containing the source packages.# Example of setting the environment variable for the build process export PLL_SOURCE_PACKAGES_PATH="/path/to/your/SourcePackages"LicenseList Requirements
mainEnsure your environment meets the following criteria:
- Xcode: 26.2+
- Swift: 6.2
- Platform Compatibility:
- iOS 16.0+
- macOS 13.0+
- tvOS 17.0+
Reference: PrepareLicenseList Build Command Arguments
mainWhen the
PrepareLicenseListplugin executes thespptool, it passes the following arguments in order:outputURL.absoluteURL.path(): The absolute path where theLicenseList.swiftfile should be written.sourcePackagesURL.absoluteURL.path(): The absolute path to theSourcePackagesdirectory.
Use the SourcePackagesParser CLI
mainThe
sppcommand-line tool parses aSourcePackagesdirectory and exports aLicenseList.swiftfile to a specified output directory. It requires exactly two positional arguments: the destination path for the output and the path to theSourcePackagesdirectory.Arguments:
[output directory path]: The directory whereLicenseList.swiftwill be generated.[SourcePackages directory path]: The path to the directory containing the Swift Package Manager'sSourcePackagesdata.
Exit Codes & Errors:
- If arguments are missing, the tool prints usage information and exits with code
1. - If a file cannot be read, it prints:
Error: Could not read [fileName]. - If the export fails, it prints:
Error: Could not export LicenseList.swift.
swift run spp [output directory path] [SourcePackages directory path]