LicenseList

repository·main·Indexed 42 days ago

https://github.com/cybozu/licenselist

A 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+.

Tokens
2.5K
Snippets
7
Records
12
Agent score
41%

What's inside LicenseList

  1. How LicenseList Works

    main

    LicenseList uses a BuildToolPlugin to automatically extract license information from your project's dependencies.

    1. Dependency Fetching: Swift Package Manager fetches dependent libraries into a SourcePackages directory (typically inside DerivedData).
    2. Extraction: The BuildToolPlugin scans these cloned repositories for LICENSE files.
    3. Display: The extracted information is used to populate the license list view in your application.
  2. Configure custom SourcePackages path for CI

    main

    LicenseList relies on the standard SourcePackages directory location. If you change the location of SourcePackages using the -clonedSourcePackagesDirPath flag (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_PATH

    Method 2: Use both -derivedDataPath and -clonedSourcePackagesDirPath

    Specify both paths such that SourcePackages is located inside the DerivedData directory.

    xcodebuild clean build \
      -project Examples/Examples.xcodeproj \
      -scheme ExamplesForSwiftUI \
      -destination "platform=iOS Simulator,name=iPhone 16,OS=18.5" \
      -derivedDataPath ./DerivedData \
      -clonedSourcePackagesDirPath ./DerivedData/SourcePackages

    Important Caveat for Method 2: The BuildToolPlugin searches for SourcePackages specifically within the DerivedData directory. If you specify a SourcePackages path that is independent of or outside of the DerivedData path, the LicenseList build will fail.

  3. Install LicenseList via Swift Package Manager

    main

    LicenseList can be installed using Xcode or by manually configuring a Package.swift file for CLI/Server-side Swift environments.

    Xcode Installation

    1. Go to File > Add Package Dependencies…
    2. Search for https://github.com/cybozu/LicenseList.git.
    3. Add the package and ensure LicenseList is linked to your application target.

    CLI / Package.swift Installation

    Add the dependency to your Package.swift file and run swift 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")
                ]
            )
        ]
    )
  4. Use the PrepareLicenseList Build Tool Plugin

    main

    The PrepareLicenseList plugin is a Swift BuildToolPlugin designed to automate the generation of a LicenseList.swift file. It locates the SourcePackages directory and executes the spp tool to produce the output file.

    Plugin Behavior

    1. Locating SourcePackages: The plugin searches for a SourcePackages directory by traversing up the directory tree from the pluginWorkDirectory.
    2. Environment Override: You can manually specify the location of the SourcePackages directory by setting the PLL_SOURCE_PACKAGES_PATH environment variable.
    3. Command Execution: The plugin generates a build command that runs the spp executable, passing the desired output path and the source packages path as arguments.

    Generated Output

    The plugin produces a file named LicenseList.swift located 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.
  5. Configure PLL_SOURCE_PACKAGES_PATH for CI environments

    main
    If you are running in a CI environment where the SourcePackages directory is at a non-default path, you must set the PLL_SOURCE_PACKAGES_PATH environment variable to the absolute path of that directory. This prevents the PrepareLicenseList plugin from failing during the build process.
  6. Use LicenseList in UIKit

    main

    To 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)
  7. Use LicenseList in SwiftUI

    main

    To display the license list in a SwiftUI application, use the LicenseListView view. 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")
            }
        }
    }
  8. Configure the SourcePackages path via PLL_SOURCE_PACKAGES_PATH

    main

    If the plugin cannot automatically locate the SourcePackages directory through directory traversal, you can provide an explicit path using the PLL_SOURCE_PACKAGES_PATH environment 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"
  9. Reference: PrepareLicenseList Build Command Arguments

    main

    When the PrepareLicenseList plugin executes the spp tool, it passes the following arguments in order:

    1. outputURL.absoluteURL.path(): The absolute path where the LicenseList.swift file should be written.
    2. sourcePackagesURL.absoluteURL.path(): The absolute path to the SourcePackages directory.
  10. Use the SourcePackagesParser CLI

    main

    The spp command-line tool parses a SourcePackages directory and exports a LicenseList.swift file to a specified output directory. It requires exactly two positional arguments: the destination path for the output and the path to the SourcePackages directory.

    Arguments:

    1. [output directory path]: The directory where LicenseList.swift will be generated.
    2. [SourcePackages directory path]: The path to the directory containing the Swift Package Manager's SourcePackages data.

    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]