mono0926 / LicensePlist

A license list generator of all your dependencies for iOS applications
https://www.slideshare.net/mono0926/licenseplist-a-license-list-generator-of-all-your-dependencies-for-ios-applications
MIT License
2.42k stars 145 forks source link

Wrong Package names with Package.resolved JSON version 2 #179

Closed AF-cgi closed 2 years ago

AF-cgi commented 2 years ago

With the new Package.resolved JSON version 2 (#174), which Apple introduce with Xcode 13.3 and Swift 5.6, I miss the correct package names. The possibility to improve the names is the rename-feature of your tool. Do you have any idea if there is a better way, to get the correct package names? An example:

// Version 1
{
  "package": "AppCenter",
  "repositoryURL": "https://github.com/microsoft/appcenter-sdk-apple.git",
  "state": {
    "branch": null,
    "revision": "b84bc8a39ff04bc8d5ae7b4d230c171c562891aa",
    "version": "4.4.1"
  }
}

// Version 2
{
  "identity" : "appcenter-sdk-apple",
  "kind" : "remoteSourceControl",
  "location" : "https://github.com/microsoft/appcenter-sdk-apple.git",
  "state" : {
    "revision" : "b84bc8a39ff04bc8d5ae7b4d230c171c562891aa",
    "version" : "4.4.1"
  }
}

Each package name is lowercase. Most package names ends with -sdk, -iOS-sdk or -apple.

BetweenTwoBits commented 2 years ago

@mono0926 are you already working on support for this?

mono0926 commented 2 years ago

@BetweenTwoBits

Not yet, and not planned, sorry. I'm welcome the pull request 🐶

ykws commented 2 years ago

We can use rename Configuration.

rename:
  appcenter-sdk-apple: AppCenter
AF-cgi commented 2 years ago

@ykws Yes, I know. That's the way what we currently do. But it's pretty hard to rename all your dependencies and find the correct naming for each of this dependencies.

ykws commented 2 years ago

@AF-cgi Good!

I think too, however it is impossible to rename automatically, because of the breaking change that the source of the origin package name was lost.

I think we can only manually renaming as mention above configuration.

ykws commented 2 years ago

Just an idea,

We may be able to pick up the name from Package(name: in Package.swift, we can access each location in Package.resolved as repository url.

bennokress commented 2 years ago

Just an idea,

We may be able to pick up the name from Package(name: in Package.swift, we can access each location in Package.resolved as repository url.

That's a good idea and I implemented it in my fork. However there are caveats to this: since Package.swift is not a JSON where we can rely on standardized parsing, we might encounter problems with some Packages. As an example from my own Swift Package, I don't like repeating myself in Swift and since the Package Definition is written in Swift, I defined my Package Name as a String variable. That's perfectly valid, but breaks any attempts I took when trying to parse the name in a globally valid way.

What I did in the end is a 3-step process to get the best name possible from the new version of Package.resolved:

  1. Make sure we parse a v2 Package, not a v1 and get the name from the Package.resolved as a last resort (current implementation here)
  2. Take the Repository Name from the Github Conversion Step that is already implemented in LicensePlist which is already an improvement on the result of Step 1, since it has uppercased characters in all the right places. We still have some issues when dealing with Packages containing spaces though (those have dashes instead, but replacing those would break Packages that really have dashes in their names).
  3. Parse the Package.swift from the Repository by looking for the first Package object, removing all nested code inside [...] to eliminate dependency and target names and finally getting the remaining value for name. If anything fails we return nil which lets us fall back to the result of Step 2.

Let me know if you see room for improvement since this is my first draft and all the parsing made me dizzy at times (trying a lot of Regex before landing on my solution). Edge cases I encountered are also added as Unit Tests, so you can play around in the parser with confidence.

stmitt commented 2 years ago

A more robust way of parsing the Package.swift would be to run swift package dump-package and parse its JSON output. With that approach, all future formats would be supported.