Skip to content

Commit

Permalink
Merge pull request #106 from cashapp/jw/spotless-and-ktlint/2022-07-12
Browse files Browse the repository at this point in the history
Upgrade Spotless and Ktlint
  • Loading branch information
JakeWharton authored Jul 12, 2022
2 parents c11d475 + 9bf58f7 commit e8d5ca8
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 64 deletions.
4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ trim_trailing_whitespace = true
insert_final_newline = true

[*.{kt,kts}]
kotlin_imports_layout = ascii
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_imports_layout = *
9 changes: 6 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
dependencies {
classpath deps.kotlinGradlePlugin
classpath "org.jetbrains.kotlin:kotlin-serialization:${versions.kotlin}"
classpath 'com.diffplug.spotless:spotless-plugin-gradle:5.1.0'
classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.8.0'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.14.2'
classpath 'org.jetbrains.dokka:dokka-gradle-plugin:1.7.0'
classpath 'de.undercouch:gradle-download-task:5.1.0'
Expand Down Expand Up @@ -132,10 +132,13 @@ spotless {
target('**/*.kt')
licenseHeaderFile(rootProject.file('gradle/license-header.txt'))
// Spotless doesn't read .editorconfig yet: https://github.com/diffplug/spotless/issues/142
ktlint('0.41.0').userData([
'insert_final_newline': 'true',
ktlint('0.46.1').editorConfigOverride([
'disabled_rules': 'filename',
'end_of_line': 'lf',
'indent_size': '2',
'ij_kotlin_allow_trailing_comma': 'true',
'ij_kotlin_allow_trailing_comma_on_call_site': 'true',
'ij_kotlin_imports_layout': '*',
])
}
}
19 changes: 13 additions & 6 deletions src/main/kotlin/app/cash/licensee/dependencyGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package app.cash.licensee

import java.io.File
import java.io.Serializable
import javax.xml.parsers.DocumentBuilderFactory
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.LenientConfiguration
Expand All @@ -28,9 +31,6 @@ import org.gradle.api.attributes.Attribute
import org.gradle.api.logging.Logger
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import java.io.File
import java.io.Serializable
import javax.xml.parsers.DocumentBuilderFactory

internal data class DependencyConfig(
val ignoredGroupIds: Map<String, IgnoredData>,
Expand Down Expand Up @@ -139,7 +139,7 @@ private fun loadDependencyCoordinates(
if (ignoreSuffix != null) {
append(ignoreSuffix)
}
}
},
)
}

Expand All @@ -149,7 +149,14 @@ private fun loadDependencyCoordinates(
val selected = dependency.selected
if (seen.add(selected.id)) {
loadDependencyCoordinates(
logger, selected, config, unusedGroupIds, unusedCoordinates, destination, seen, depth + 1
logger,
selected,
config,
unusedGroupIds,
unusedCoordinates,
destination,
seen,
depth + 1,
)
}
}
Expand Down Expand Up @@ -299,7 +306,7 @@ internal fun loadPomInfo(
logger = logger,
id = DependencyCoordinates(group, artifact, version),
variants = variants,
depth = depth + 1
depth = depth + 1,
)
if (licenses.isEmpty()) {
licenses += parentPomInfo.licenses
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/app/cash/licensee/licenseValidation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ internal fun validateArtifacts(
if (spdxLicense.identifier in validationConfig.allowedIdentifiers) {
unusedAllowedIdentifiers -= spdxLicense.identifier
artifactResults += ValidationResult.Info(
"SPDX identifier '${spdxLicense.identifier}' allowed"
"SPDX identifier '${spdxLicense.identifier}' allowed",
)
validated = true
break
}
if (spdxLicense.url in validationConfig.allowedUrls) {
unusedAllowedUrls -= spdxLicense.url
artifactResults += ValidationResult.Warning(
"License URL '${spdxLicense.url}' was allowed but could use SPDX identifier '${spdxLicense.identifier}'"
"License URL '${spdxLicense.url}' was allowed but could use SPDX identifier '${spdxLicense.identifier}'",
)
validated = true
break
Expand All @@ -83,7 +83,7 @@ internal fun validateArtifacts(
if (!validated) {
for (spdxLicense in artifact.spdxLicenses) {
artifactResults += ValidationResult.Error(
"SPDX identifier '${spdxLicense.identifier}' is NOT allowed"
"SPDX identifier '${spdxLicense.identifier}' is NOT allowed",
)
}
for (unknownLicense in artifact.unknownLicenses) {
Expand All @@ -92,7 +92,7 @@ internal fun validateArtifacts(
"Unknown license name '${unknownLicense.name}' with no URL is NOT allowed"
} else {
"Unknown license URL '${unknownLicense.url}' is NOT allowed"
}
},
)
}
if (artifact.spdxLicenses.isEmpty() && artifact.unknownLicenses.isEmpty()) {
Expand All @@ -113,7 +113,7 @@ internal fun validateArtifacts(
append(" because ")
append(reason)
}
}
},
)

// Downgrade errors to info.
Expand All @@ -127,7 +127,7 @@ internal fun validateArtifacts(
.firstOrNull { it.group == artifact.groupId && it.artifact == artifact.artifactId }
if (candidate != null) {
artifactResults += ValidationResult.Warning(
"Coordinates match an allowed dependency but version does not match (${candidate.version} != ${artifact.version})"
"Coordinates match an allowed dependency but version does not match (${candidate.version} != ${artifact.version})",
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/cash/licensee/licenses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal fun normalizeLicenseInfo(
pomInfo.name,
spdxLicenses,
unknownLicenses,
pomInfo.scm?.url?.let(::ArtifactScm)
pomInfo.scm?.url?.let(::ArtifactScm),
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/cash/licensee/licensesSpdx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package app.cash.licensee

import kotlin.LazyThreadSafetyMode.PUBLICATION
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.LazyThreadSafetyMode.PUBLICATION

internal class SpdxLicenses(
private val identifierToLicense: Map<String, SpdxLicense>,
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/app/cash/licensee/plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package app.cash.licensee
import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.internal.publishing.AndroidArtifacts.ArtifactType.CLASSES
import java.io.File
import java.util.Locale.ROOT
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
Expand All @@ -28,8 +30,6 @@ import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.androidJvm
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.common
import java.io.File
import java.util.Locale.ROOT

private const val baseTaskName = "licensee"
private const val reportFolder = "licensee"
Expand Down Expand Up @@ -75,7 +75,7 @@ class LicenseePlugin : Plugin<Project> {
"project ${project.path}"
}
throw IllegalStateException(
"'app.cash.licensee' requires compatible language/platform plugin to be applied ($name)"
"'app.cash.licensee' requires compatible language/platform plugin to be applied ($name)",
)
}

Expand Down
36 changes: 20 additions & 16 deletions src/main/kotlin/app/cash/licensee/pluginExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ internal class MutableLicenseeExtension : LicenseeExtension {
options: Action<AllowDependencyOptions>,
) {
var setReason: String? = null
options.execute(object : AllowDependencyOptions {
override fun because(reason: String) {
setReason = reason
}
})
options.execute(
object : AllowDependencyOptions {
override fun because(reason: String) {
setReason = reason
}
},
)
allowedDependencies[DependencyCoordinates(groupId, artifactId, version)] = setReason
}

Expand All @@ -260,17 +262,19 @@ internal class MutableLicenseeExtension : LicenseeExtension {
) {
var setReason: String? = null
var setTransitive = false
options.execute(object : IgnoreDependencyOptions {
override fun because(reason: String) {
setReason = reason
}

override var transitive: Boolean
get() = setTransitive
set(value) {
setTransitive = value
options.execute(
object : IgnoreDependencyOptions {
override fun because(reason: String) {
setReason = reason
}
})

override var transitive: Boolean
get() = setTransitive
set(value) {
setTransitive = value
}
},
)

if (setTransitive && setReason == null) {
throw RuntimeException(
Expand All @@ -282,7 +286,7 @@ internal class MutableLicenseeExtension : LicenseeExtension {
append(artifactId)
}
append("' is dangerous and requires a reason string")
}
},
)
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/app/cash/licensee/task.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package app.cash.licensee

import app.cash.licensee.ViolationAction.FAIL
import app.cash.licensee.ViolationAction.IGNORE
import java.io.File
import kotlin.properties.Delegates
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import org.gradle.api.DefaultTask
Expand All @@ -35,8 +37,6 @@ import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.io.File
import kotlin.properties.Delegates

internal open class LicenseeTask : DefaultTask() {
@get:Input
Expand Down Expand Up @@ -96,7 +96,7 @@ internal open class LicenseeTask : DefaultTask() {
append(" because ")
append(ignoreData.reason)
}
}
},
)
}
for ((groupId, artifactIds) in dependencyConfig.ignoredCoordinates) {
Expand All @@ -114,7 +114,7 @@ internal open class LicenseeTask : DefaultTask() {
append(" because ")
append(ignoreData.reason)
}
}
},
)
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ internal open class LicenseeTask : DefaultTask() {
append(artifactDetail.spdxLicenses)
append(' ')
append(artifactDetail.unknownLicenses)
}
},
)
}
}
Expand All @@ -176,7 +176,7 @@ internal open class LicenseeTask : DefaultTask() {
logger.info("")
logger.info("Allowed identifiers:")
logger.info(
validationConfig.allowedIdentifiers.ifEmpty { listOf("None") }.joinToString(prefix = " ")
validationConfig.allowedIdentifiers.ifEmpty { listOf("None") }.joinToString(prefix = " "),
)
logger.info("Allowed URLs:")
if (validationConfig.allowedUrls.isEmpty()) {
Expand All @@ -203,7 +203,7 @@ internal open class LicenseeTask : DefaultTask() {
append(" because ")
append(reason)
}
}
},
)
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/test/kotlin/app/cash/licensee/LicenseePluginFixtureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package app.cash.licensee
import com.google.common.truth.Truth.assertThat
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import java.io.File
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File

@RunWith(TestParameterInjector::class)
class LicenseePluginFixtureTest {
Expand Down Expand Up @@ -145,7 +145,7 @@ class LicenseePluginFixtureTest {
val fixtureDir = File(fixturesDir, fixtureName)
val result = createRunner(fixtureDir).buildAndFail()
assertThat(result.output).containsMatch(
"Transitive dependency ignore on 'com\\.example(:example)?' is dangerous and requires a reason string"
"Transitive dependency ignore on 'com\\.example(:example)?' is dangerous and requires a reason string",
)
}

Expand All @@ -155,7 +155,7 @@ class LicenseePluginFixtureTest {
val fixtureDir = File(fixturesDir, fixtureName)
val result = createRunner(fixtureDir).buildAndFail()
assertThat(result.output).contains(
"'app.cash.licensee' requires compatible language/platform plugin to be applied (root project)"
"'app.cash.licensee' requires compatible language/platform plugin to be applied (root project)",
)
}

Expand All @@ -165,7 +165,7 @@ class LicenseePluginFixtureTest {
val fixtureDir = File(fixturesDir, fixtureName)
val result = createRunner(fixtureDir).buildAndFail()
assertThat(result.output).contains(
"'app.cash.licensee' requires compatible language/platform plugin to be applied (project :some:thing)"
"'app.cash.licensee' requires compatible language/platform plugin to be applied (project :some:thing)",
)
}

Expand All @@ -182,7 +182,8 @@ class LicenseePluginFixtureTest {
"""
|com.example:example:1.0.0
| - ERROR: SPDX identifier 'Apache-2.0' is NOT allowed
|""".trimMargin()
|
""".trimMargin(),
)
}

Expand All @@ -199,7 +200,8 @@ class LicenseePluginFixtureTest {
"""
|com.example:example:1.0.0
| - ERROR: SPDX identifier 'Apache-2.0' is NOT allowed
|""".trimMargin()
|
""".trimMargin(),
)
}

Expand Down
Loading

0 comments on commit e8d5ca8

Please sign in to comment.