From 66c4a9e09354b920f7bc906e27c847015e0f6897 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:33:09 -0800 Subject: [PATCH] Remove execution time reference to Project --- .../pluginsapi/PluginDownloadService.kt | 3 +- .../pluginsapi/PluginDownloadServiceImpl.kt | 33 +++++------ .../runtask/service/DownloadsAPIService.kt | 3 +- .../service/DownloadsAPIServiceImpl.kt | 5 +- .../xyz/jpenilla/runtask/task/AbstractRun.kt | 6 +- .../jpenilla/runtask/task/RunWithPlugins.kt | 2 +- .../xyz/jpenilla/runtask/util/Downloader.kt | 17 +++--- .../runtask/util/ProgressLoggerUtil.kt | 56 ------------------- 8 files changed, 37 insertions(+), 88 deletions(-) delete mode 100644 plugin/src/main/kotlin/xyz/jpenilla/runtask/util/ProgressLoggerUtil.kt diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadService.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadService.kt index 83ecf62..acf268c 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadService.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadService.kt @@ -23,6 +23,7 @@ import org.gradle.api.provider.Property import org.gradle.api.provider.Provider import org.gradle.api.services.BuildService import org.gradle.api.services.BuildServiceParameters +import org.gradle.internal.logging.progress.ProgressLoggerFactory import org.gradle.kotlin.dsl.registerIfAbsent import xyz.jpenilla.runtask.paperapi.Projects import xyz.jpenilla.runtask.util.Constants @@ -53,7 +54,7 @@ public interface PluginDownloadService : BuildService): Provider { diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadServiceImpl.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadServiceImpl.kt index 1f1ecfc..cf4f6cc 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadServiceImpl.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/runtask/pluginsapi/PluginDownloadServiceImpl.kt @@ -23,6 +23,7 @@ import com.fasterxml.jackson.module.kotlin.kotlinModule import com.fasterxml.jackson.module.kotlin.readValue import org.gradle.api.Project import org.gradle.api.logging.Logging +import org.gradle.internal.logging.progress.ProgressLoggerFactory import xyz.jpenilla.runtask.util.Constants import xyz.jpenilla.runtask.util.Downloader import xyz.jpenilla.runtask.util.HashingAlgorithm @@ -85,14 +86,14 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { } @Synchronized - override fun resolvePlugin(project: Project, download: PluginApiDownload): Path { + override fun resolvePlugin(progressLoggerFactory: ProgressLoggerFactory, download: PluginApiDownload): Path { manifest = loadOrCreateManifest() return when (download) { - is HangarApiDownload -> resolveHangarPlugin(project, download) - is ModrinthApiDownload -> resolveModrinthPlugin(project, download) - is GitHubApiDownload -> resolveGitHubPlugin(project, download) - is UrlDownload -> resolveUrl(project, download) + is HangarApiDownload -> resolveHangarPlugin(progressLoggerFactory, download) + is ModrinthApiDownload -> resolveModrinthPlugin(progressLoggerFactory, download) + is GitHubApiDownload -> resolveGitHubPlugin(progressLoggerFactory, download) + is UrlDownload -> resolveUrl(progressLoggerFactory, download) } } @@ -101,18 +102,18 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { private val offlineMode: Boolean get() = parameters.offlineMode.get() - private fun resolveUrl(project: Project, download: UrlDownload): Path { + private fun resolveUrl(progressLoggerFactory: ProgressLoggerFactory, download: UrlDownload): Path { val cacheDir = parameters.cacheDirectory.get().asFile.toPath() val targetDir = cacheDir.resolve(Constants.URL_PLUGIN_DIR) val urlHash = download.urlHash() val version = manifest.urlProvider[urlHash] ?: PluginVersion(fileName = "$urlHash.jar", displayName = download.url.get()) val targetFile = targetDir.resolve(version.fileName) val setter: (PluginVersion) -> Unit = { manifest.urlProvider[urlHash] = it } - val ctx = DownloadCtx(project, "url", download.url.get(), targetDir, targetFile, version, setter) + val ctx = DownloadCtx(progressLoggerFactory, "url", download.url.get(), targetDir, targetFile, version, setter) return download(ctx) } - private fun resolveHangarPlugin(project: Project, download: HangarApiDownload): Path { + private fun resolveHangarPlugin(progressLoggerFactory: ProgressLoggerFactory, download: HangarApiDownload): Path { val platformType = parameters.platformType.get() val cacheDir = parameters.cacheDirectory.get().asFile.toPath() @@ -134,11 +135,11 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { val downloadUrl = "$apiUrl/api/v1/projects/$apiPlugin/versions/$apiVersion/$platformType/download" val setter: (PluginVersion) -> Unit = { platform[apiVersion] = it } - val ctx = DownloadCtx(project, apiUrl, downloadUrl, targetDir, targetFile, version, setter) + val ctx = DownloadCtx(progressLoggerFactory, apiUrl, downloadUrl, targetDir, targetFile, version, setter) return download(ctx) } - private fun resolveModrinthPlugin(project: Project, download: ModrinthApiDownload): Path { + private fun resolveModrinthPlugin(progressLoggerFactory: ProgressLoggerFactory, download: ModrinthApiDownload): Path { val cacheDir = parameters.cacheDirectory.get().asFile.toPath() val apiVersion = download.version.get() @@ -156,7 +157,7 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { val versionRequestUrl = "$apiUrl/v2/project/$apiPlugin/version/$apiVersion" val versionJsonPath = download( - DownloadCtx(project, apiUrl, versionRequestUrl, targetDir, jsonFile, jsonVersion, setter = { plugin[jsonVersionName] = it }, requireValidJar = false) + DownloadCtx(progressLoggerFactory, apiUrl, versionRequestUrl, targetDir, jsonFile, jsonVersion, setter = { plugin[jsonVersionName] = it }, requireValidJar = false) ) val versionInfo = mapper.readValue(versionJsonPath.toFile()) val primaryFile = versionInfo.files.find { it.primary } ?: error("Could not find primary file for $download in $versionInfo") @@ -169,11 +170,11 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { val targetFile = targetDir.resolve(version.fileName) return download( - DownloadCtx(project, apiUrl, primaryFile.url, targetDir, targetFile, version, setter = { plugin[apiVersion] = it }) + DownloadCtx(progressLoggerFactory, apiUrl, primaryFile.url, targetDir, targetFile, version, setter = { plugin[apiVersion] = it }) ) } - private fun resolveGitHubPlugin(project: Project, download: GitHubApiDownload): Path { + private fun resolveGitHubPlugin(progressLoggerFactory: ProgressLoggerFactory, download: GitHubApiDownload): Path { val cacheDir = parameters.cacheDirectory.get().asFile.toPath() val owner = download.owner.get() @@ -192,7 +193,7 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { val downloadUrl = "https://github.com/$owner/$repo/releases/download/$tag/$asset" val setter: (PluginVersion) -> Unit = { tagProvider[asset] = it } - val ctx = DownloadCtx(project, "github.com", downloadUrl, targetDir, targetFile, version, setter) + val ctx = DownloadCtx(progressLoggerFactory, "github.com", downloadUrl, targetDir, targetFile, version, setter) return download(ctx) } @@ -262,7 +263,7 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { val opName = "${ctx.baseUrl}:${ctx.version.fileName}" val start = Instant.now() LOGGER.lifecycle("Downloading {}...", displayName) - when (val res = Downloader(url, ctx.targetFile, displayName, opName).download(ctx.project, connection)) { + when (val res = Downloader(url, ctx.targetFile, displayName, opName).download(ctx.progressLoggerFactory, connection)) { is Downloader.Result.Success -> LOGGER.lifecycle("Done downloading {}, took {}.", displayName, Duration.between(start, Instant.now()).prettyPrint()) is Downloader.Result.Failure -> throw IllegalStateException("Failed to download $displayName.", res.throwable) } @@ -311,7 +312,7 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService { } private data class DownloadCtx( - val project: Project, + val progressLoggerFactory: ProgressLoggerFactory, val baseUrl: String, val downloadUrl: String, val targetDir: Path, diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIService.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIService.kt index f6a1055..2531879 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIService.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIService.kt @@ -20,6 +20,7 @@ import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.provider.Provider import org.gradle.api.provider.ProviderFactory +import org.gradle.internal.logging.progress.ProgressLoggerFactory import org.gradle.jvm.toolchain.JavaLauncher import org.gradle.kotlin.dsl.registerIfAbsent import org.gradle.process.ExecOperations @@ -46,10 +47,10 @@ public interface DownloadsAPIService { * @param build build to resolve */ public fun resolveBuild( - project: Project, providers: ProviderFactory, javaLauncher: JavaLauncher, execOperations: ExecOperations, + progressLoggerFactory: ProgressLoggerFactory, version: String, build: Build ): List diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIServiceImpl.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIServiceImpl.kt index 4fb2bfa..53148cf 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIServiceImpl.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/runtask/service/DownloadsAPIServiceImpl.kt @@ -29,6 +29,7 @@ import org.gradle.api.provider.Property import org.gradle.api.provider.ProviderFactory import org.gradle.api.services.BuildService import org.gradle.api.services.BuildServiceParameters +import org.gradle.internal.logging.progress.ProgressLoggerFactory import org.gradle.jvm.toolchain.JavaLauncher import org.gradle.process.ExecOperations import xyz.jpenilla.runtask.paperapi.DownloadsAPI @@ -130,10 +131,10 @@ internal abstract class DownloadsAPIServiceImpl : BuildService { @@ -218,7 +219,7 @@ internal abstract class DownloadsAPIServiceImpl : BuildService LOGGER.lifecycle("Done downloading {}, took {}.", displayName, Duration.ofMillis(System.currentTimeMillis() - start).prettyPrint()) is Downloader.Result.Failure -> throw IllegalStateException("Failed to download $displayName.", downloadResult.throwable) } diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/AbstractRun.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/AbstractRun.kt index 039ed0c..4adf8c7 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/AbstractRun.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/AbstractRun.kt @@ -28,6 +28,7 @@ import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal import org.gradle.api.tasks.JavaExec import org.gradle.api.tasks.Optional +import org.gradle.internal.logging.progress.ProgressLoggerFactory import org.gradle.process.ExecOperations import xyz.jpenilla.runtask.service.DownloadsAPIService import xyz.jpenilla.runtask.util.path @@ -96,6 +97,9 @@ public abstract class AbstractRun : JavaExec() { @get:Inject protected abstract val providers: ProviderFactory + @get:Inject + protected abstract val progressLoggerFactory: ProgressLoggerFactory + init { init0() } @@ -122,10 +126,10 @@ public abstract class AbstractRun : JavaExec() { } protected open fun resolveBuild(): List = downloadsApiService.get().resolveBuild( - project, providers, javaLauncher.get(), execOperations, + progressLoggerFactory, version.get(), build.get() ) diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/RunWithPlugins.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/RunWithPlugins.kt index 7ee263c..f83c609 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/RunWithPlugins.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/runtask/task/RunWithPlugins.kt @@ -87,7 +87,7 @@ public abstract class RunWithPlugins : AbstractRun() { val service = pluginDownloadService.get() for (download in downloadPlugins.downloads) { - ourPluginJars.from(service.resolvePlugin(project, download)) + ourPluginJars.from(service.resolvePlugin(progressLoggerFactory, download)) } } diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/util/Downloader.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/util/Downloader.kt index 0853135..edb85dd 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/util/Downloader.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/runtask/util/Downloader.kt @@ -16,9 +16,9 @@ */ package xyz.jpenilla.runtask.util -import org.gradle.api.Project import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging +import org.gradle.internal.logging.progress.ProgressLoggerFactory import java.io.IOException import java.net.URL import java.net.URLConnection @@ -53,18 +53,18 @@ internal class Downloader( @Volatile private var expectedSize = 0L - fun download(project: Project): Result { + fun download(progressLoggerFactory: ProgressLoggerFactory): Result { if (started) { error("Cannot start download a second time.") } started = true val connection = remote.openConnection() - return download(project, connection) + return download(progressLoggerFactory, connection) } - fun download(project: Project, connection: URLConnection): Result { - val listener = createDownloadListener(project) + fun download(progressLoggerFactory: ProgressLoggerFactory, connection: URLConnection): Result { + val listener = createDownloadListener(progressLoggerFactory) val downloadFuture = CompletableFuture.runAsync { val expected = connection.contentLengthLong @@ -108,11 +108,8 @@ internal class Downloader( return Result.Failure(failure) } - private fun createDownloadListener(project: Project): ProgressListener { - // ProgressLogger is internal Gradle API and can technically be changed, - // (although it hasn't since 3.x) so we access it using reflection, and - // fallback to using LOGGER if it fails - val progressLogger = ProgressLoggerUtil.createProgressLogger(project, operationName) + private fun createDownloadListener(progressLoggerFactory: ProgressLoggerFactory): ProgressListener { + val progressLogger = progressLoggerFactory.newOperation(operationName) return if (progressLogger != null) { LoggingDownloadListener( progressLogger, diff --git a/plugin/src/main/kotlin/xyz/jpenilla/runtask/util/ProgressLoggerUtil.kt b/plugin/src/main/kotlin/xyz/jpenilla/runtask/util/ProgressLoggerUtil.kt deleted file mode 100644 index dacc0bc..0000000 --- a/plugin/src/main/kotlin/xyz/jpenilla/runtask/util/ProgressLoggerUtil.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Run Task Gradle Plugins - * Copyright (c) 2024 Jason Penilla - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package xyz.jpenilla.runtask.util - -import org.gradle.api.Project -import java.lang.reflect.Method - -internal object ProgressLoggerUtil { - fun createProgressLogger(project: Project, operationName: String): ProgressLoggerWrapper? = try { - val serviceFactory = project.javaClass.getMethod("getServices").invoke(project) - val get = serviceFactory.javaClass.getMethod("get", Class::class.java) - val progressLoggerFactoryClass = Class.forName("org.gradle.internal.logging.progress.ProgressLoggerFactory") - val factory = get(serviceFactory, progressLoggerFactoryClass) - val newOperation = progressLoggerFactoryClass.getMethod("newOperation", String::class.java) - val progressLoggerClass = Class.forName("org.gradle.internal.logging.progress.ProgressLogger") - val start = progressLoggerClass.getMethod("start", String::class.java, String::class.java) - val progress = progressLoggerClass.getMethod("progress", String::class.java) - val completed = progressLoggerClass.getMethod("completed") - ProgressLoggerWrapper(newOperation(factory, operationName), start, progress, completed) - } catch (ex: ReflectiveOperationException) { - null - } - - class ProgressLoggerWrapper( - private val logger: Any, - private val start: Method, - private val progress: Method, - private val completed: Method - ) { - fun start(description: String, status: String) { - start(logger, description, status) - } - - fun progress(status: String) { - progress(logger, status) - } - - fun completed() { - completed(logger) - } - } -}