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

Feature Request: add LicenseName field to the output #43

Open
mmaetzler opened this issue Oct 24, 2022 · 7 comments
Open

Feature Request: add LicenseName field to the output #43

mmaetzler opened this issue Oct 24, 2022 · 7 comments

Comments

@mmaetzler
Copy link

I want to start with saying: I love this software! Thanks for creating it!

I was wondering if there is an easy way to add the license name as a field to the output? The output files right now have a license file with the raw text of the license, but I was wondering if you would be open to add the licenseName, in the form of an SPDX license identifier to the output as well?

Background: In our software attributions list, we list all the included components, their name, version and license identifier. Then, when one clicks on a license, the full text appears, but we first want to start by showing just the license identifier.

In our use case we are using the json output of the package list.

@FelixHerrmann
Copy link
Owner

Hey @mmaetzler, I will investigate what's possible next weekend. AFAIK we don't have this information present out of the box.

@mmaetzler
Copy link
Author

Thanks for the reply! Yeah after posting the feature request I went through the contents of the DerivedData folder and it seems the license name is not part of the git repo local copy.

The example package I'm using is grpc-swift which contains the Apache-2.0 license file. When checking out the repo on github, the license name is right there, but it seems that github is parsing the LICENSE file and then guesses the license name, maybe using a tool like https://github.com/licensee/licensee

It looks like a bigger task 😢


image

@FelixHerrmann
Copy link
Owner

FelixHerrmann commented Oct 30, 2022

Hey, so I did some research...

We don't have any license identifier locally as mentioned before (which makes sense because it's not a git feature rather than GitHub). They have an API for that, but the big advantage of this tool is that everything is coming from local data and does not need any network request.

You are totally right, GitHub is using (and maintaining) the Ruby gem Licensee to detect license types in GitHub.

It would be possible to build something similar in Swift, we just need a mapping (with some tolerance) of the license text to a snapshot of the SPDX License List. However, I think this should not be part of this package itself because it's a different use case.

As you guessed it, it is definitely a bigger task! I don't have time right now and something like that should be done right. I can think of a simple String-contains workaround for your project for now, like the following:

enum LicenseType {
    case mit
    case apachev2
    case gplv2
    case gplv3
    
    var spdxIdentifier: String {
        switch self {
        case .mit: return "MIT"
        case .apachev2: return "Apache-2.0"
        case .gplv2: return "GPL-2.0"
        case .gplv3: return "GPL-3.0"
        }
    }
    
    var localizedName: String {
        let format = NSLocalizedString("LICENSE_NAME", value: "%@ license", comment: "The license name displayed to the user (%@ is the license type, e.g. Apache-2.0)")
        return .localizedStringWithFormat(format, spdxIdentifier)
    }
    
    init?(license: String) {
        if license.contains("MIT License") {
            self = .mit
        } else if license.contains("Apache License") && license.contains("Version 2.0") {
            self = .apachev2
        } else if license.contains("GNU GENERAL PUBLIC LICENSE") && license.contains("Version 2") {
            self = .gplv2
        } else if license.contains("GNU GENERAL PUBLIC LICENSE") && license.contains("Version 3") {
            self = .gplv3
        } else {
            return nil
        }
    }
}

extension Package {
    var licenseType: LicenseType? {
        guard let license else { return nil }
        return LicenseType(license: license)
    }
}

@mmaetzler
Copy link
Author

Thanks for looking into this so thoroughly, I appreciate it. I think for now I plan to run licensee locally against the identified LICENSES files and build a workaround that way.

@FelixHerrmann
Copy link
Owner

No problem! That's also a good workaround 🙂

@NikolaiMadlener
Copy link

Hey, so I did some research...

We don't have any license identifier locally as mentioned before (which makes sense because it's not a git feature rather than GitHub). They have an API for that, but the big advantage of this tool is that everything is coming from local data and does not need any network request.

You are totally right, GitHub is using (and maintaining) the Ruby gem Licensee to detect license types in GitHub.

It would be possible to build something similar in Swift, we just need a mapping (with some tolerance) of the license text to a snapshot of the SPDX License List. However, I think this should not be part of this package itself because it's a different use case.

As you guessed it, it is definitely a bigger task! I don't have time right now and something like that should be done right. I can think of a simple String-contains workaround for your project for now, like the following:

enum LicenseType {
    case mit
    case apachev2
    case gplv2
    case gplv3
    
    var spdxIdentifier: String {
        switch self {
        case .mit: return "MIT"
        case .apachev2: return "Apache-2.0"
        case .gplv2: return "GPL-2.0"
        case .gplv3: return "GPL-3.0"
        }
    }
    
    var localizedName: String {
        let format = NSLocalizedString("LICENSE_NAME", value: "%@ license", comment: "The license name displayed to the user (%@ is the license type, e.g. Apache-2.0)")
        return .localizedStringWithFormat(format, spdxIdentifier)
    }
    
    init?(license: String) {
        if license.contains("MIT License") {
            self = .mit
        } else if license.contains("Apache License") && license.contains("Version 2.0") {
            self = .apachev2
        } else if license.contains("GNU GENERAL PUBLIC LICENSE") && license.contains("Version 2") {
            self = .gplv2
        } else if license.contains("GNU GENERAL PUBLIC LICENSE") && license.contains("Version 3") {
            self = .gplv3
        }
        return nil
    }
}

extension Package {
    var licenseType: LicenseType? {
        guard let license else { return nil }
        return LicenseType(license: license)
    }
}

If anyone wants to use this nicely written extension by @FelixHerrmann just make sure to wrap the return nil in the initialiser in a final else clause. Otherwise the licenseType will always be nil...

Once again, thanks for your effort and work @FelixHerrmann ! Really enjoy using this.

@FelixHerrmann
Copy link
Owner

Oh you are totally right @NikolaiMadlener, this was not really tested. I have updated it in case someone just copies it. Thanks for correcting this!

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

No branches or pull requests

3 participants