Skip to content

Commit dd50d2f

Browse files
committed
Fix: clean and composite builds (#87)
* tidy checksum generator * test running `clean` then `updateDevRepo` * GradleTestKitUtils: always add `gradle.properties` (makes it easier to open the projects to test them) * fix failure when discovering snapshot version if there are no files * formatting * tidying * GradleTestKitUtils: extract repos to separate property (which will be helpful later when creating the composite-build test) * tidy CreatePublicationChecksum, and trim whitespace when creating/loading checksums * update `toTreeString()` to just use NIO Path * add composite-build test * update checksums on `publishAllToDevRepo` tasks, so the checksums get created when a project is depended on by another project. * tidy * update inputs to use sorted files (Gradle seems to miss inputs otherwise?), and register `publicationStore` as an output without converting to files (otherwise Gradle seems to not track outputs properly?) * explicitly don't cache outputs for Maven Publish tasks (cherry picked from commit 008a662)
1 parent e6ea5dd commit dd50d2f

14 files changed

Lines changed: 400 additions & 123 deletions

‎src/main/kotlin/DevPublishPlugin.kt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ constructor(
163163
inputs
164164
// Must convert to FileTree, because the directory might not exist, and
165165
// Gradle won't accept directories that don't exist as inputs.
166-
.files(checksumsStore.asFileTree.sortedElements())
166+
.files(checksumsStore.sortedFiles())
167167
.withPropertyName("devPubChecksumsStoreFiles")
168168
.withPathSensitivity(RELATIVE)
169169

170170
outputs
171-
.files(publicationStore.map { it.asFileTree.sortedElements() })
171+
.dir(publicationStore)
172172
.withPropertyName("devPubPublicationStore")
173173

174174
val currentProjectDir = layout.projectDirectory
@@ -200,6 +200,10 @@ constructor(
200200
}
201201
}
202202

203+
outputs.cacheIf("do not cache - this task only performs simple file modifications") { _ ->
204+
false
205+
}
206+
203207
doFirst_("clear staging repo") {
204208
if (repoIsDevPub.get()) {
205209
// clear the staging repo so that we can only sync this publication's files in the doLast {} below

‎src/main/kotlin/internal/checksums/CreatePublicationChecksum.kt‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,27 @@ internal abstract class CreatePublicationChecksum : ValueSource<String, CreatePu
1414
}
1515

1616
override fun obtain(): String? {
17+
val identifier = parameters.identifier.get()
18+
val artifactsChecksums = artifactsChecksums()
19+
20+
return buildString {
21+
appendLine(identifier)
22+
appendLine("---")
23+
artifactsChecksums.forEach {
24+
appendLine(it)
25+
}
26+
}.trim()
27+
}
28+
29+
private fun artifactsChecksums(): List<String> {
1730
val projectDir = parameters.projectDir.get().asFile
1831

19-
val checksum = parameters.artifacts
32+
return parameters.artifacts
2033
.map { artifact ->
2134
val artifactPath = artifact.relativeTo(projectDir).invariantSeparatorsPath
2235
"${artifactPath}$FileChecksumSeparator${artifact.checksum()}"
2336
}
2437
.sorted()
25-
.joinToString("\n")
26-
27-
val identifier = parameters.identifier.get()
28-
29-
return /* language=TEXT */ """
30-
|$identifier
31-
|---
32-
|$checksum
33-
""".trimMargin()
3438
}
3539

3640
internal companion object {

‎src/main/kotlin/internal/checksums/LoadPublicationChecksum.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal abstract class LoadPublicationChecksum : ValueSource<String, LoadPublic
2121
.resolve(checksumFilename)
2222
.takeIf(File::exists)
2323
?.readText()
24+
?.trim()
2425
}
2526

2627
internal companion object {

‎src/main/kotlin/internal/checksums/checksums.kt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import java.io.InputStream
55
import java.io.OutputStream.nullOutputStream
66
import java.security.DigestOutputStream
77
import java.security.MessageDigest
8-
import java.util.*
8+
import kotlin.io.encoding.Base64
99

1010
internal fun File.checksum(): String =
1111
if (exists()) inputStream().checksum() else "missing"
1212

1313
private fun InputStream.checksum(): String {
14-
val messageDigester = MessageDigest.getInstance("SHA-256")
14+
val md = MessageDigest.getInstance("SHA-256")
1515

1616
buffered().use { input ->
17-
DigestOutputStream(nullOutputStream(), messageDigester).use { digestStream ->
17+
DigestOutputStream(nullOutputStream(), md).use { digestStream ->
1818
input.copyTo(digestStream)
1919
}
2020
}
2121

22-
return Base64.getEncoder().encodeToString(messageDigester.digest())
22+
return Base64.encode(md.digest())
2323
}

‎src/main/kotlin/tasks/DevPublishTasksContainer.kt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class DevPublishTasksContainer(
4040
// provider property, so checking via the task name will have to do:
4141
project.tasks.matching { it.name == "publishAllPublicationsTo${DEV_PUB__MAVEN_REPO_NAME}Repository" }
4242
)
43+
44+
// always auto-refresh stored checksums
45+
finalizedBy(generatePublicationChecksum)
4346
}
4447

4548
private fun TaskContainer.registerGeneratePublicationChecksumTask(): TaskProvider<GeneratePublicationDataChecksumTask> =

‎src/main/kotlin/tasks/GeneratePublicationDataChecksumTask.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dev.adamko.gradle.dev_publish.data.PublicationData
44
import dev.adamko.gradle.dev_publish.internal.DevPublishInternalApi
55
import dev.adamko.gradle.dev_publish.internal.checksums.CreatePublicationChecksum.Companion.createPublicationChecksum
66
import dev.adamko.gradle.dev_publish.utils.info
7+
import java.io.File
78
import javax.inject.Inject
89
import org.gradle.api.NamedDomainObjectContainer
910
import org.gradle.api.file.DirectoryProperty
@@ -40,7 +41,7 @@ constructor(
4041
abstract val tempDir: DirectoryProperty
4142

4243
/** Used for prettier log messages. */
43-
private val rootDir = project.rootDir
44+
private val rootDir: File = project.rootDir
4445

4546
@TaskAction
4647
@DevPublishInternalApi

‎src/main/kotlin/tasks/UpdateDevRepoTask.kt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package dev.adamko.gradle.dev_publish.tasks
22

33
import dev.adamko.gradle.dev_publish.internal.DevPublishInternalApi
44
import dev.adamko.gradle.dev_publish.utils.dropDirectory
5-
import dev.adamko.gradle.dev_publish.utils.sortedElements
5+
import dev.adamko.gradle.dev_publish.utils.sortedFiles
66
import java.io.File
77
import javax.inject.Inject
88
import org.gradle.api.file.ConfigurableFileCollection
99
import org.gradle.api.file.DirectoryProperty
10-
import org.gradle.api.file.FileSystemLocation
1110
import org.gradle.api.file.FileSystemOperations
1211
import org.gradle.api.provider.Provider
1312
import org.gradle.api.tasks.*
@@ -40,8 +39,8 @@ constructor(
4039
@get:InputFiles
4140
@get:PathSensitive(RELATIVE)
4241
@DevPublishInternalApi
43-
protected val publicationsStoreFiles: Provider<out Collection<FileSystemLocation>>
44-
get() = publicationsStore.asFileTree.sortedElements()
42+
protected val publicationsStoreFiles: Provider<out Set<File>>
43+
get() = publicationsStore.sortedFiles()
4544

4645
/**
4746
* Additional files to include in [devRepo].

‎src/main/kotlin/utils/gradleUtils.kt‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package dev.adamko.gradle.dev_publish.utils
22

3+
import java.io.File
34
import org.gradle.api.Action
45
import org.gradle.api.DefaultTask
56
import org.gradle.api.Project
67
import org.gradle.api.Task
78
import org.gradle.api.artifacts.Configuration
89
import org.gradle.api.attributes.Attribute
910
import org.gradle.api.attributes.AttributeContainer
10-
import org.gradle.api.file.FileSystemLocation
11-
import org.gradle.api.file.FileTree
11+
import org.gradle.api.file.Directory
1212
import org.gradle.api.file.RelativePath
1313
import org.gradle.api.provider.Provider
1414
import org.gradle.kotlin.dsl.dependencies
@@ -116,19 +116,28 @@ internal fun RelativePath.dropDirectory(): RelativePath =
116116

117117

118118
/** Instantiate a new [Attribute] of type [T] */
119-
internal inline fun <reified T: Any> Attribute(name: String): Attribute<T> =
119+
internal inline fun <reified T : Any> Attribute(name: String): Attribute<T> =
120120
Attribute.of(name, T::class.java)
121121

122122

123-
internal operator fun <T: Any> AttributeContainer.get(key: Attribute<T>): T? =
123+
internal operator fun <T : Any> AttributeContainer.get(key: Attribute<T>): T? =
124124
getAttribute(key)
125125

126-
127-
/** Sorted [FileTree] elements. */
128-
// Sorting is important because FileTrees have unstable sorting, even on the same machine
129-
internal fun FileTree.sortedElements(): Provider<out Collection<FileSystemLocation>> =
130-
elements.map { it.sortedBy(FileSystemLocation::getAsFile) }
131-
126+
/**
127+
* Get all files within the given directory, and in any subdirectories.
128+
* All files are sorted alphabetically.
129+
*
130+
* This is useful for registering task inputs,
131+
* since Gradle does not support input directories that do not yet exist.
132+
*/
133+
internal fun Provider<Directory>.sortedFiles(): Provider<out Set<File>> =
134+
map { dir ->
135+
dir.asFile
136+
.walk()
137+
.filter { it.isFile }
138+
.sorted()
139+
.toSet()
140+
}
132141

133142
/** Calls [DefaultTask.onlyIf], but smart-casts the task. */
134143
@Suppress("FunctionName")

‎src/test/kotlin/BuildCacheTest.kt‎

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ class BuildCacheTest : FunSpec({
4141
}
4242

4343
val initialBuildCacheSize =
44-
expectedBuildCacheDir.walk().filter { it.isRegularFile() }.map { it.fileSize() }.sum()
44+
expectedBuildCacheDir.walk().filter { it.isRegularFile() }.sumOf { it.fileSize() }
4545

4646
project.runner
47-
//.forwardOutput()
4847
.withArguments(
4948
":updateDevRepo",
5049
"--stacktrace",
@@ -75,7 +74,7 @@ class BuildCacheTest : FunSpec({
7574

7675
companion object {
7776
private fun Path.recursiveFileSize(): Long =
78-
walk().filter { it.isRegularFile() }.map { it.fileSize() }.sum()
77+
walk().filter { it.isRegularFile() }.sumOf { it.fileSize() }
7978

8079
private fun TestScope.project(): GradleProjectTest =
8180
gradleKtsProjectTest(
@@ -84,32 +83,32 @@ class BuildCacheTest : FunSpec({
8483
) {
8584

8685
buildGradleKts = """
87-
plugins {
88-
kotlin("jvm") version embeddedKotlinVersion
89-
id("dev.adamko.dev-publish") version "+"
90-
`maven-publish`
91-
}
92-
93-
group = "foo.project"
94-
version = "0.0.1"
95-
96-
publishing {
97-
publications {
98-
create<MavenPublication>("mavenJava") {
99-
from(components["java"])
100-
}
101-
}
102-
}
103-
""".trimIndent()
86+
|plugins {
87+
| kotlin("jvm") version embeddedKotlinVersion
88+
| id("dev.adamko.dev-publish") version "+"
89+
| `maven-publish`
90+
|}
91+
|
92+
|group = "foo.project"
93+
|version = "0.0.1"
94+
|
95+
|publishing {
96+
| publications {
97+
| create<MavenPublication>("mavenJava") {
98+
| from(components["java"])
99+
| }
100+
| }
101+
|}
102+
|""".trimMargin()
104103

105104
settingsGradleKts += """
106-
107-
buildCache {
108-
local {
109-
directory = file("local-cache").toURI()
110-
}
111-
}
112-
""".trimIndent()
105+
|
106+
|buildCache {
107+
| local {
108+
| directory = file("local-cache").toURI()
109+
| }
110+
|}
111+
|""".trimMargin()
113112

114113
createKotlinFile(
115114
"src/main/kotlin/FooClass.kt", """

0 commit comments

Comments
 (0)