cookpad / LicenseToolsPlugin

Gradle plugin to check library licenses and generate license pages for Android
Apache License 2.0
174 stars 29 forks source link

Sort libraries in YAML file in the task `gradlew updateLicenses` #103

Closed AlanChiou closed 3 years ago

AlanChiou commented 3 years ago

Current, the task gradlew updateLicenses appends missing libraries at the end of YAML file. In this case, it is hard to be reviewed and manage in source code control systems, like Git.

Could the task gradlew updateLicenses sort all libraries alphabetically by their artifact if it changes the YAML file?

AlanChiou commented 3 years ago

This is a python script to sort licenses YAML file by the artifact name.

# python
import re

with open('licenses.yml', 'r') as file:
    data = file.read()
    file.close()
    licensesSplit = data.split("- artifact:")
    # Remove the first empty string item
    del licensesSplit[0]

licenses = []
for license in licensesSplit:
    licenses.append("- artifact:" + license)

# Get the artifact name
def getArtifact(license):
    return re.search(r"- artifact:\s(\b.*\n)", license).group()

# Sort licenses by the artifact name
licenses.sort(key=getArtifact)

licensesString = ""
for license in licenses:
    licensesString = licensesString + license

with open('licenses.yml', 'w') as file:
    file.write(licensesString)
    file.close()
AlanChiou commented 3 years ago

Released in 1.2.8