Skip to content

Commit

Permalink
apply ktlint 12.1.0 (#418)
Browse files Browse the repository at this point in the history
* apply ktlint 12.1.0
  • Loading branch information
jaredsburrows authored Jan 30, 2024
1 parent f2456d3 commit 6fbc7bd
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 175 deletions.
2 changes: 1 addition & 1 deletion gradle-license-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {
compileOnly gradleApi()
compileOnly libs.android.plugin

implementation(platform(libs.kotlin.bom))
implementation platform(libs.kotlin.bom)
implementation libs.kotlin.stdlib
implementation libs.kotlinx.html
implementation libs.moshi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jaredsburrows.license

/** Configuration options for the gradle license plugin. */
open class LicenseReportExtension { // extensions can't be final
open class LicenseReportExtension {
/** Whether the Csv report should be generated. */
var generateCsvReport = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,18 @@ internal open class LicenseReportTask : DefaultTask() { // tasks can't be final

// Store the information that we need
val module = artifact.moduleVersion.id
val project = Model().apply {
this.groupId = module.group.orEmpty().trim()
this.artifactId = module.name.orEmpty().trim()
this.version = model.pomVersion(mavenReader, pomFile, configurations, dependencies)
this.name = model.pomName()
this.description = model.pomDescription()
this.url = model.pomUrl()
this.inceptionYear = model.pomInceptionYear()
this.licenses = licenses
this.developers = model.pomDevelopers()
}
val project =
Model().apply {
this.groupId = module.group.orEmpty().trim()
this.artifactId = module.name.orEmpty().trim()
this.version = model.pomVersion(mavenReader, pomFile, configurations, dependencies)
this.name = model.pomName()
this.description = model.pomDescription()
this.url = model.pomUrl()
this.inceptionYear = model.pomInceptionYear()
this.licenses = licenses
this.developers = model.pomDevelopers()
}

projects += project
}
Expand Down Expand Up @@ -272,10 +273,12 @@ internal open class LicenseReportTask : DefaultTask() { // tasks can't be final
* to match a specific variant of the library project. Instead, we skip the
* library project itself and enumerate its dependencies.
*/
"unspecified" -> resolvedArtifacts += getResolvedArtifactsFromResolvedDependencies(
resolvedDependency.children,
skipSet,
)
"unspecified" ->
resolvedArtifacts +=
getResolvedArtifactsFromResolvedDependencies(
resolvedDependency.children,
skipSet,
)

else -> resolvedArtifacts += resolvedDependency.allModuleArtifacts
}
Expand Down Expand Up @@ -306,21 +309,25 @@ internal open class LicenseReportTask : DefaultTask() { // tasks can't be final
.getByName(tempPomConfiguration)
.dependencies += dependencies.add(tempPomConfiguration, dependency)

val pomFile = configurations
.getByName(tempPomConfiguration)
.resolvedConfiguration
.lenientConfiguration
.artifacts
.firstOrNull { it.type == "pom" }
?.file
val pomFile =
configurations
.getByName(tempPomConfiguration)
.resolvedConfiguration
.lenientConfiguration
.artifacts
.firstOrNull { it.type == "pom" }
?.file

// Reset dependencies in temporary configuration
configurations.remove(configurations.getByName(tempPomConfiguration))

return pomFile
}

private fun <T : Report> createReport(file: File, report: () -> T) {
private fun <T : Report> createReport(
file: File,
report: () -> T,
) {
val newReport = report()

file.apply {
Expand All @@ -338,7 +345,10 @@ internal open class LicenseReportTask : DefaultTask() { // tasks can't be final
)
}

private fun <T : Report> copyReport(file: File, report: () -> T) {
private fun <T : Report> copyReport(
file: File,
report: () -> T,
) {
val newReport = report()

// Iterate through all asset directories
Expand Down Expand Up @@ -429,10 +439,11 @@ internal open class LicenseReportTask : DefaultTask() { // tasks can't be final
val licenseName = license.name.orEmpty().trim()
val licenseUrl = license.url.orEmpty().trim()
if (licenseUrl.isUrlValid()) {
licenses += License().apply {
this.name = licenseName
url = licenseUrl
}
licenses +=
License().apply {
this.name = licenseName
url = licenseUrl
}
}
}
return licenses
Expand Down Expand Up @@ -491,18 +502,24 @@ internal open class LicenseReportTask : DefaultTask() { // tasks can't be final
private fun Model.pomDevelopers(): List<Developer> {
val developers = mutableListOf<Developer>()
this.developers.orEmpty().forEach { developer ->
developers += Developer().apply {
id = developer.name.orEmpty().trim()
}
developers +=
Developer().apply {
id = developer.name.orEmpty().trim()
}
}
return developers
}

private fun File?.isNullOrEmpty(): Boolean = this == null || this.length() == 0L

private fun Exception.shortMessage(): String = with(message ?: "<no message>") {
if (length > MAX_EXCEPTION_MESSAGE_LENGTH) substring(0, MAX_EXCEPTION_MESSAGE_LENGTH) + "... (see --debug for complete message)" else this
}
private fun Exception.shortMessage(): String =
with(message ?: "<no message>") {
if (length > MAX_EXCEPTION_MESSAGE_LENGTH) {
substring(0, MAX_EXCEPTION_MESSAGE_LENGTH) + "... (see --debug for complete message)"
} else {
this
}
}

private companion object {
private const val ANDROID_SUPPORT_GROUP_ID = "com.android.support"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,28 @@ private fun Project.configureVariant(
) {
// Configure tasks for all variants
variants?.all { variant ->
val name = variant.name.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase(Locale.getDefault())
} else {
it.toString()
val name =
variant.name.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase(Locale.getDefault())
} else {
it.toString()
}
}
}

tasks.register("license${name}Report", LicenseReportTask::class.java) {
// Apply common task configuration first
configureCommon(it)

// Custom for Android tasks
val sourceSetName = if (it.useVariantSpecificAssetDirs) variant.name else "main"
it.assetDirs = baseExtension
.sourceSets
.getByName(sourceSetName)
.assets
.srcDirs
.toList()
it.assetDirs =
baseExtension
.sourceSets
.getByName(sourceSetName)
.assets
.srcDirs
.toList()
it.variantName = variant.name
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import java.net.URISyntaxException
* Taken from: https://github.com/gradle/gradle/blob/f3828bbb3350292dcbea7f505464eb5d30cb9d44/platforms/core-runtime/logging/src/main/java/org/gradle/internal/logging/ConsoleRenderer.java
*/
class ConsoleRenderer {

/** Renders a path name as a file URL that is likely recognized by consoles. */
fun asClickableFileUrl(path: File): String {
// File.toURI().toString() leads to an URL like this on Mac: file:/reports/index.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ package com.jaredsburrows.license.internal
* License text from: https://github.com/github/choosealicense.com/blob/gh-pages/_licenses.
*/
object LicenseHelper {
val licenseMap: Map<String, String>

init {
licenseMap = linkedMapOf(
val licenseMap: Map<String, String> =
linkedMapOf(
// Apache License 2.0
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/apache-2.0.txt
"Apache 2.0" to "apache-2.0.txt",
Expand All @@ -27,7 +25,6 @@ object LicenseHelper {
"https://www.apache.org/licenses/LICENSE-2.0.txt" to "apache-2.0.txt",
"http://opensource.org/licenses/Apache-2.0" to "apache-2.0.txt",
"https://opensource.org/licenses/Apache-2.0" to "apache-2.0.txt",

// BSD 2-Clause "Simplified" License
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/bsd-2-clause.txt
"BSD 2-Clause \"Simplified\" License" to "bsd-2-clause.txt",
Expand All @@ -36,62 +33,54 @@ object LicenseHelper {
"https://opensource.org/licenses/BSD-2-Clause" to "bsd-2-clause.txt",
"http://www.opensource.org/licenses/bsd-license.php" to "bsd-2-clause.txt",
"https://www.opensource.org/licenses/bsd-license.php" to "bsd-2-clause.txt",

// BSD 3-Clause "New" or "Revised" License
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/bsd-3-clause.txt
"BSD 3-Clause \"New\" or \"Revised\" License" to "bsd-3-clause.txt",
"BSD-3-Clause" to "bsd-3-clause.txt",
"http://opensource.org/licenses/BSD-3-Clause" to "bsd-3-clause.txt",
"https://opensource.org/licenses/BSD-3-Clause" to "bsd-3-clause.txt",

// Eclipse Public License 2.0
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/epl-2.0.txt
"Eclipse Public License 2.0" to "epl-2.0.txt",
"http://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt" to "epl-2.0.txt",
"https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt" to "epl-2.0.txt",
"http://opensource.org/licenses/EPL-2.0" to "epl-2.0.txt",
"https://opensource.org/licenses/EPL-2.0" to "epl-2.0.txt",

// GNU General Public License v2.0
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/gpl-2.0.txt
"GNU General Public License v2.0" to "gpl-2.0.txt",
"http://www.gnu.org/licenses/gpl-2.0.txt" to "gpl-2.0.txt",
"https://www.gnu.org/licenses/gpl-2.0.txt" to "gpl-2.0.txt",
"http://opensource.org/licenses/GPL-2.0" to "gpl-2.0.txt",
"https://opensource.org/licenses/GPL-2.0" to "gpl-2.0.txt",

// GNU General Public License v3.0
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/gpl-3.0.txt
"GNU General Public License v3.0" to "gpl-3.0.txt",
"https//www.gnu.org/licenses/gpl-3.0.txt" to "gpl-3.0.txt",
"https://www.gnu.org/licenses/gpl-3.0.txt" to "gpl-3.0.txt",
"http://opensource.org/licenses/GPL-3.0" to "gpl-3.0.txt",
"https://opensource.org/licenses/GPL-3.0" to "gpl-3.0.txt",

// GNU Lesser General Public License v2.1
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/lgpl-2.1.txt
"GNU Lesser General Public License v2.1" to "lgpl-2.1.txt",
"http://www.gnu.org/licenses/lgpl-2.1.txt" to "lgpl-2.1.txt",
"https://www.gnu.org/licenses/lgpl-2.1.txt" to "lgpl-2.1.txt",
"http://opensource.org/licenses/LGPL-2.1" to "lgpl-2.1.txt",
"https://opensource.org/licenses/LGPL-2.1" to "lgpl-2.1.txt",

// GNU Lesser General Public License v3.0
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/lgpl-3.0.txt
"GNU Lesser General Public License v3.0" to "lgpl-3.0.txt",
"http://www.gnu.org/licenses/lgpl-3.0.txt" to "lgpl-3.0.txt",
"https://www.gnu.org/licenses/lgpl-3.0.txt" to "lgpl-3.0.txt",
"http://opensource.org/licenses/LGPL-3.0" to "lgpl-3.0.txt",
"https://opensource.org/licenses/LGPL-3.0" to "lgpl-3.0.txt",

// MIT License
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/mit.txt
"MIT License" to "mit.txt",
"The MIT License" to "mit.txt",
"http://opensource.org/licenses/MIT" to "mit.txt",
"https://opensource.org/licenses/MIT" to "mit.txt",
"http://www.opensource.org/licenses/mit-license.php" to "mit.txt",

// Mozilla Public License 2.0
// https://github.com/github/choosealicense.com/blob/gh-pages/_licenses/mpl-2.0.txt
"Mozilla Public License 2.0" to "mpl-2.0.txt",
Expand All @@ -100,5 +89,4 @@ object LicenseHelper {
"http://opensource.org/licenses/MPL-2.0" to "mpl-2.0.txt",
"https://opensource.org/licenses/MPL-2.0" to "mpl-2.0.txt",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.apache.maven.model.Model
* @property projects list of [Model]s for thr CSV report.
*/
class CsvReport(private val projects: List<Model>) : Report {

override fun toString(): String = report()

override fun name(): String = NAME
Expand All @@ -22,34 +21,35 @@ class CsvReport(private val projects: List<Model>) : Report {
projectInfoList += COLUMNS

projects.map { project ->
val projectInfo = mutableListOf<String?>().apply {
// Project Name
addCsvString(project.name)
val projectInfo =
mutableListOf<String?>().apply {
// Project Name
addCsvString(project.name)

// Project Description
addCsvString(project.description)
// Project Description
addCsvString(project.description)

// Project Version
addCsvString(project.version)
// Project Version
addCsvString(project.version)

// Project Developers
addCsvList(project.developers) { it.id }
// Project Developers
addCsvList(project.developers) { it.id }

// Project Url
addCsvString(project.url)
// Project Url
addCsvString(project.url)

// Project Year
addCsvString(project.inceptionYear)
// Project Year
addCsvString(project.inceptionYear)

// Project License Names
addCsvList(project.licenses) { it.name }
// Project License Names
addCsvList(project.licenses) { it.name }

// Project License Url
addCsvList(project.licenses) { it.url }
// Project License Url
addCsvList(project.licenses) { it.url }

// Project Dependency
addCsvString("${project.groupId}:${project.artifactId}:${project.version}")
}
// Project Dependency
addCsvString("${project.groupId}:${project.artifactId}:${project.version}")
}

// Add each row to the list
projectInfoList += projectInfo.toCsv()
Expand All @@ -66,20 +66,21 @@ class CsvReport(private val projects: List<Model>) : Report {

/** Add elements to Csv. */
private fun MutableList<String?>.addCsvString(element: String): Boolean {
val escaped = element.valueOrNull()
?.replace("\"", "\"\"")
?.let { el ->
when {
el.contains(",") ||
el.contains("\n") ||
el.contains("'") ||
el.contains("\\") ||
el.contains("\"")
-> "\"$el\""

else -> el
val escaped =
element.valueOrNull()
?.replace("\"", "\"\"")
?.let { el ->
when {
el.contains(",") ||
el.contains("\n") ||
el.contains("'") ||
el.contains("\\") ||
el.contains("\"")
-> "\"$el\""

else -> el
}
}
}
return this.add(escaped)
}

Expand Down
Loading

0 comments on commit 6fbc7bd

Please sign in to comment.