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

Add '--swift-package-sources-path' option support #211

Merged
merged 6 commits into from
Mar 9, 2023

Conversation

vladvlasov256
Copy link
Contributor

Description

Xcode and xcodebuild resolve and check out Swift packages sources before building a project every time it's necessary. Sources include license files as well. Thus LicensePlist may rely not only on GitHub API but directly on the sources.

Motivation

In the sandbox mode, when the tool is used by the Swift package build tool plugin there is no way to use network requests. Using cloned sources is a workaround in this case.

Another example is a CI environment that builds apps with xcodebuild. The CI may specify a folder for cloned Swift package sources and use this folder for acknowledgments generation.

xcodebuild -clonedSourcePackagesDirPath ./SourcePackages ...
license-plist --swift-package-sources-path ./SourcePackages

By default Xcode clones package sources in a folder inside derived data. So it's not that simple to retrieve the path in case of regular Xcode usage.

Implementation

Reading licenses

Packages hold licenses in "LICENSE", "License", "LICENSE.md", or "LICENSE.txt" files. To read them the tool needs two pieces of data:

  • path to cloned sources,
  • a way to determine the license file name.

As a path to cloned sources, the tool uses --package-sources-path (or --swift-package-sources-path) parameter.

All possible license file names are also defined as a parameter whose name is --license-file-names. To simplify different path extensions let's support asterisks in names, ex. LICENSE.*.

Then if the path is specified PlistInfo reads licenses from disk instead of using GitHub API.

Name specified

One another feature that accesses GitHub is resolving nameSpecified. As suggested above, if the tool knows cloned sources path let's not fetch Package.resolved from GitHub but read it from disk.

Restriction

Unlike CocoaPods, Swift packages don't provide reach metadata. There are a couple of discussions on the swift forum about that but no proposals yet. Thus there is no simple way to determine the license type.

I was thinking about:

  • matching license file headers,
  • parsing CocoaPods spec if there is one,
  • or even using a tool like licensee.

All these approaches involve too much overhead, in my opinion.

AFAIK, license type is required to filter out licenses using the exclude feature. So I added a warning that this feature is not supported in combination with --package-sources-path.

Fixes

  • added two optional parameters --package-sources-path and --license-file-names,
  • added parameters duplications in the config,
  • added readCheckedOutLicenses() method to PlistInfo.swift,
  • added readFromDisk() method to GitHubLicense.swift,
  • added unit tests and example checked out files,
  • updated Readme.

@mono0926
Copy link
Owner

mono0926 commented Mar 9, 2023

@vladvlasov256

Thanks for the PR 🙏

Could you fix the failed test?

@vladvlasov256
Copy link
Contributor Author

Hi @mono0926

I believe the test is ok but it's referenced to the actual main branch. So it should be fine after the merge.

If I change the URL:

-        let url = try XCTUnwrap(URL(string: "https://raw.githubusercontent.com/mono0926/LicensePlist/master/Tests/LicensePlistTests/Resources/license_plist.yml"))
+        let url = try XCTUnwrap(URL(string: "https://raw.githubusercontent.com/vladvlasov256/LicensePlist/feature/checkout_path/Tests/LicensePlistTests/Resources/license_plist.yml"))

the test becomes green:

Green `testInit_sample()`

The same situation was in #207

@mono0926 mono0926 merged commit 1137b62 into mono0926:main Mar 9, 2023
@mono0926
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants