Skip to content

Commit

Permalink
Remove execution time reference to Project
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 10, 2025
1 parent 67713a2 commit 66c4a9e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +54,7 @@ public interface PluginDownloadService : BuildService<PluginDownloadService.Para
* @param project project to use for context
* @param download plugin download
*/
public fun resolvePlugin(project: Project, download: PluginApiDownload): Path
public fun resolvePlugin(progressLoggerFactory: ProgressLoggerFactory, download: PluginApiDownload): Path

public companion object {
public fun registerIfAbsent(namePrefix: String, project: Project, config: Action<in Parameters>): Provider<out PluginDownloadService> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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()

Expand All @@ -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()
Expand All @@ -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<ModrinthVersionResponse>(versionJsonPath.toFile())
val primaryFile = versionInfo.files.find { it.primary } ?: error("Could not find primary file for $download in $versionInfo")
Expand All @@ -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()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Path>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -130,10 +131,10 @@ internal abstract class DownloadsAPIServiceImpl : BuildService<DownloadsAPIServi

@Synchronized
override fun resolveBuild(
project: Project,
providers: ProviderFactory,
javaLauncher: JavaLauncher,
execOperations: ExecOperations,
progressLoggerFactory: ProgressLoggerFactory,
version: String,
build: DownloadsAPIService.Build
): List<Path> {
Expand Down Expand Up @@ -218,7 +219,7 @@ internal abstract class DownloadsAPIServiceImpl : BuildService<DownloadsAPIServi
val start = System.currentTimeMillis()
val opName = "${parameters.downloadsEndpoint}:${parameters.downloadProject}"

when (val downloadResult = Downloader(downloadURL, tempFile, displayName, opName).download(project)) {
when (val downloadResult = Downloader(downloadURL, tempFile, displayName, opName).download(progressLoggerFactory)) {
is Downloader.Result.Success -> LOGGER.lifecycle("Done downloading {}, took {}.", displayName, Duration.ofMillis(System.currentTimeMillis() - start).prettyPrint())
is Downloader.Result.Failure -> throw IllegalStateException("Failed to download $displayName.", downloadResult.throwable)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -122,10 +126,10 @@ public abstract class AbstractRun : JavaExec() {
}

protected open fun resolveBuild(): List<Path> = downloadsApiService.get().resolveBuild(
project,
providers,
javaLauncher.get(),
execOperations,
progressLoggerFactory,
version.get(),
build.get()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
17 changes: 7 additions & 10 deletions plugin/src/main/kotlin/xyz/jpenilla/runtask/util/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

This file was deleted.

0 comments on commit 66c4a9e

Please sign in to comment.