From f964c5bc0bfe6069256ccfa7098905ab7b968c26 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:23:33 -0700 Subject: [PATCH] feat(userdev): Enable shared caches by default (#235) * userdev: Enable shared caches by default * Ignore locks for non-existent processes in cleanup * 1.7.0-SNAPSHOT --- gradle.properties | 2 +- .../paperweight/userdev/PaperweightUser.kt | 31 ++++++------------- .../userdev/internal/setup/util/utils.kt | 18 ++++++++--- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/gradle.properties b/gradle.properties index 200275e..1f3bb5c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = io.papermc.paperweight -version = 1.6.4-SNAPSHOT +version = 1.7.0-SNAPSHOT org.gradle.caching = true org.gradle.parallel = true diff --git a/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/PaperweightUser.kt b/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/PaperweightUser.kt index aeff7ec..a0f393c 100644 --- a/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/PaperweightUser.kt +++ b/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/PaperweightUser.kt @@ -45,8 +45,6 @@ import org.gradle.api.attributes.Bundling import org.gradle.api.attributes.Category import org.gradle.api.attributes.LibraryElements import org.gradle.api.attributes.Usage -import org.gradle.api.logging.Logger -import org.gradle.api.logging.Logging import org.gradle.api.plugins.JavaPlugin import org.gradle.api.provider.Provider import org.gradle.api.tasks.Delete @@ -57,9 +55,6 @@ import org.gradle.util.internal.NameMatcher import org.gradle.workers.WorkerExecutor abstract class PaperweightUser : Plugin { - companion object { - private val logger: Logger = Logging.getLogger(PaperweightUser::class.java) - } @get:Inject abstract val workerExecutor: WorkerExecutor @@ -70,32 +65,26 @@ abstract class PaperweightUser : Plugin { override fun apply(target: Project) { checkJavaVersion() - val sharedCacheRootRoot = target.gradle.gradleUserHomeDir.toPath().resolve("caches/paperweight-userdev") - val sharedCacheRoot = if (target.sharedCaches) { - logger.lifecycle("paperweight-userdev experimental shared caches are enabled.") - sharedCacheRootRoot.resolve(paperweightHash) - } else { - null - } + val sharedCacheRoot = target.gradle.gradleUserHomeDir.toPath().resolve("caches/paperweight-userdev") target.gradle.sharedServices.registerIfAbsent(DOWNLOAD_SERVICE_NAME, DownloadService::class) {} val cleanAll = target.tasks.register("cleanAllPaperweightUserdevCaches") { group = "paperweight" - description = "Delete the project & all shared paperweight-userdev setup cache." + description = "Delete the project-local & all shared paperweight-userdev setup caches." delete(target.layout.cache) - delete(sharedCacheRootRoot) + delete(sharedCacheRoot) } val cleanCache by target.tasks.registering { group = "paperweight" - description = "Delete the project paperweight-userdev setup cache." + description = "Delete the project-local paperweight-userdev setup cache." delete(target.layout.cache) } target.configurations.register(DEV_BUNDLE_CONFIG) // must not be initialized until afterEvaluate, as it resolves the dev bundle - val userdevSetup by lazy { createSetup(target, sharedCacheRoot) } + val userdevSetup by lazy { createSetup(target, sharedCacheRoot.resolve(paperweightHash)) } val userdev = target.extensions.create( PAPERWEIGHT_EXTENSION, @@ -176,7 +165,7 @@ abstract class PaperweightUser : Plugin { configureRepositories(userdevSetup) - cleanSharedCaches(this, sharedCacheRootRoot) + cleanSharedCaches(this, sharedCacheRoot) } } @@ -307,17 +296,15 @@ abstract class PaperweightUser : Plugin { private fun createContext(project: Project): SetupHandler.Context = SetupHandler.Context(project, workerExecutor, javaToolchainService) - private fun createSetup(target: Project, sharedCacheRoot: Path?): UserdevSetup { + private fun createSetup(target: Project, sharedCacheRoot: Path): UserdevSetup { val bundleConfig = target.configurations.named(DEV_BUNDLE_CONFIG) val devBundleZip = bundleConfig.map { it.singleFile }.convertToPath() val bundleHash = devBundleZip.sha256asHex() - val cacheDir = if (sharedCacheRoot == null) { + val cacheDir = if (!target.sharedCaches) { target.layout.cache } else { when (bundleConfig.get().dependencies.single()) { - is ProjectDependency -> { - throw PaperweightException("Shared caches does not support the dev bundle being a ProjectDependency.") - } + is ProjectDependency -> target.layout.cache is ModuleDependency -> { val resolved = diff --git a/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/internal/setup/util/utils.kt b/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/internal/setup/util/utils.kt index 54d97fd..7069e87 100644 --- a/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/internal/setup/util/utils.kt +++ b/paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/internal/setup/util/utils.kt @@ -172,6 +172,8 @@ val Project.ci: Provider .map { it.toBoolean() } .orElse(false) +private fun stableProp(name: String) = "paperweight.$name" + private fun experimentalProp(name: String) = "paperweight.experimental.$name" val Project.genSources: Boolean @@ -182,9 +184,12 @@ val Project.genSources: Boolean } val Project.sharedCaches: Boolean - get() = providers.gradleProperty(experimentalProp("sharedCaches")).orNull.toBoolean() + get() = providers.gradleProperty(stableProp("sharedCaches")) + .map { it.toBoolean() } + .orElse(true) + .get() -private fun deleteUnusedAfter(target: Project): Long = target.providers.gradleProperty(experimentalProp("sharedCaches.deleteUnusedAfter")) +private fun deleteUnusedAfter(target: Project): Long = target.providers.gradleProperty(stableProp("sharedCaches.deleteUnusedAfter")) .map { value -> parseDuration(value) } .orElse(Duration.ofDays(7)) .map { duration -> duration.toMillis() } @@ -200,11 +205,16 @@ fun cleanSharedCaches(target: Project, root: Path) { stream.asSequence() .filter { it.name == "last-used.txt" } .mapNotNull { - val dir = it.parent.parent // paperweight dir + val pwDir = it.parent.parent // paperweight dir + val cacheDir = pwDir.parent // cache dir + val lock = cacheDir.resolve(USERDEV_SETUP_LOCK) + if (lock.exists() && ProcessHandle.of(lock.readText().toLong()).isPresent) { + return@mapNotNull null + } val lastUsed = it.readText().toLong() val since = System.currentTimeMillis() - lastUsed val cutoff = deleteUnusedAfter(target) - if (since > cutoff) dir else null + if (since > cutoff) pwDir else null } .toList() }