diff --git a/build.gradle.kts b/build.gradle.kts index aa55166..a24a8c8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,7 +20,7 @@ plugins { kotlin("jvm") version "1.9.22" } -group = "com.bobek" +group = "de.philipp-bobek" version = "1.0-SNAPSHOT" repositories { diff --git a/src/main/kotlin/OssLicensesParser.kt b/src/main/kotlin/OssLicensesParser.kt index d41c0f7..b3b5624 100644 --- a/src/main/kotlin/OssLicensesParser.kt +++ b/src/main/kotlin/OssLicensesParser.kt @@ -16,14 +16,34 @@ * along with this program. If not, see . */ -package com.bobek.oss.licenses.parser +package de.philipp_bobek.oss_licenses_parser +import java.io.EOFException import java.io.InputStream +/** + * Parser for the text files generated by the OSS Licenses Gradle Plugin. + * @see + * OSS Licenses Gradle Plugin + */ object OssLicensesParser { private val licenseMetadataLineRegex = """^(?\d+):(?\d+) (?.+)$""".toRegex() + /** + * Parses all licenses contained in the third_party_licenses_metadata and third_party_licenses files. + * + * Generally it is advised to use parseMetadata instead of this method if you only want to display a list of all + * libraries. + * Later on the parseLicense method can be used to display the license content of a specific library. + * This approach can help saving resources. + * + * @param thirdPartyLicensesMetadataFile Content of the third_party_licenses_metadata file. + * @param thirdPartyLicensesFile Content of the third_party_licenses file. + * @return List of all licenses. + * @throws java.io.IOException If an I/O error occurs. + * @throws IllegalArgumentException If content is invalid. + */ @JvmStatic fun parseAllLicenses( thirdPartyLicensesMetadataFile: InputStream, @@ -38,6 +58,16 @@ object OssLicensesParser { } } + /** + * Parses the licenses metadata contained in the third_party_licenses_metadata file. + * + * Later on the parseLicense method can be used to display the license content of a specific library. + * + * @param thirdPartyLicensesMetadataFile Content of the third_party_licenses_metadata file. + * @return List of license metadata. + * @throws java.io.IOException If an I/O error occurs. + * @throws IllegalArgumentException If content is invalid. + */ @JvmStatic fun parseMetadata(thirdPartyLicensesMetadataFile: InputStream): List = thirdPartyLicensesMetadataFile.reader() @@ -64,6 +94,14 @@ object OssLicensesParser { return ThirdPartyLicenseMetadata(libraryName, offset, length) } + /** + * Parses a license contained in the third_party_licenses file. + * + * @param metadata License metadata obtained from the parseMetadata method. + * @param thirdPartyLicensesFile Content of the third_party_licenses file. + * @return The license. + * @throws java.io.IOException If an I/O error occurs. + */ @JvmStatic fun parseLicense(metadata: ThirdPartyLicenseMetadata, thirdPartyLicensesFile: InputStream): ThirdPartyLicense { thirdPartyLicensesFile.skipNBytes(metadata.offset) diff --git a/src/main/kotlin/ThirdPartyLicense.kt b/src/main/kotlin/ThirdPartyLicense.kt index a90b4d2..0467185 100644 --- a/src/main/kotlin/ThirdPartyLicense.kt +++ b/src/main/kotlin/ThirdPartyLicense.kt @@ -16,6 +16,12 @@ * along with this program. If not, see . */ -package com.bobek.oss.licenses.parser +package de.philipp_bobek.oss_licenses_parser +/** + * Holds information of a license obtained from the third_party_licenses file. + * + * @param libraryName The name of the library. + * @param licenseContent The content of the license. + */ data class ThirdPartyLicense(val libraryName: String, val licenseContent: String) diff --git a/src/main/kotlin/ThirdPartyLicenseMetadata.kt b/src/main/kotlin/ThirdPartyLicenseMetadata.kt index 2ab32b7..61da720 100644 --- a/src/main/kotlin/ThirdPartyLicenseMetadata.kt +++ b/src/main/kotlin/ThirdPartyLicenseMetadata.kt @@ -1,5 +1,5 @@ /* - * This file is part of Compass. + * This file is part of OssLicensesParser. * Copyright (C) 2024 Philipp Bobek * * This program is free software: you can redistribute it and/or modify @@ -16,6 +16,14 @@ * along with this program. If not, see . */ -package com.bobek.oss.licenses.parser +package de.philipp_bobek.oss_licenses_parser +/** + * Holds license metadata information obtained from the third_party_licenses_metadata file. + * This information is needed to extract the license content from the third_party_licenses file. + * + * @param libraryName The name of the library. + * @param offset The offset of bytes at which the license content starts. + * @param length The length of bytes that the license content has. + */ data class ThirdPartyLicenseMetadata(val libraryName: String, val offset: Long, val length: Int) diff --git a/src/test/kotlin/OssLicensesParserTest.kt b/src/test/kotlin/OssLicensesParserTest.kt index f30329f..494618e 100644 --- a/src/test/kotlin/OssLicensesParserTest.kt +++ b/src/test/kotlin/OssLicensesParserTest.kt @@ -1,4 +1,22 @@ -package com.bobek.oss.licenses.parser +/* + * This file is part of OssLicensesParser. + * Copyright (C) 2024 Philipp Bobek + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Compass is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.philipp_bobek.oss_licenses_parser import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertIterableEquals