Skip to content

Commit

Permalink
Add kotlinx.datetime & fix the Java Instant bug
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Oct 14, 2024
1 parent 4539c00 commit 80ef13c
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 44 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ kotlin {
// AtomicFU
implementation("org.jetbrains.kotlinx:atomicfu:0.24.0")

// Datetime
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")

// URL Encoding
implementation("net.thauvin.erik.urlencoder:urlencoder-lib:1.4.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.michaelbull.result.Result
import com.github.michaelbull.result.get
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.datetime.Instant
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.data.ConfigFile
import teksturepako.pakku.api.platforms.GitHub
Expand All @@ -14,7 +15,6 @@ import teksturepako.pakku.api.projects.Project
import teksturepako.pakku.api.projects.UpdateStrategy
import teksturepako.pakku.api.projects.combineWith
import teksturepako.pakku.api.projects.inheritPropertiesFrom
import java.time.Instant

/**
* Requests new data for provided [projects] from all platforms and updates them based on platform-specific slugs,
Expand Down Expand Up @@ -51,18 +51,27 @@ suspend fun updateMultipleProjectsWithFiles(
acc.find { accProject ->
accProject.slug[platform.serialName] == newProject.slug[platform.serialName]
}?.also { accProject ->
// Combine projects
val accFiles = combinedProjectsToOldFiles[accProject]
?: return@coroutineScope Err(ActionError("Failed to combine project ${accProject.pakkuId} when updating. Report it to Pakku's developer."))
val accFiles = combinedProjectsToOldFiles[accProject] ?: return@coroutineScope Err(
ActionError("Failed to combine project ${accProject.pakkuId} when updating." +
" Report it to Pakku's developer.")
)

val accPublished = accFiles.find { it.type == platform.serialName }?.datePublished
if (accPublished != null && accPublished != Instant.MIN)

if (accPublished != null && accPublished != Instant.DISTANT_FUTURE)
{
newProject.files.removeIf { it.type == platform.serialName && it.datePublished < accPublished }
}

// Combine projects
(accProject + newProject).get()
?.copy(files = (newProject.files.take(numberOfFiles) + accProject.files).toMutableSet())
?.let x@{ combinedProject ->
if (combinedProject.hasNoFiles()) return@x // Do not update project if files are missing

combinedProjectsToOldFiles[combinedProject] = accFiles
combinedProjectsToOldFiles -= accProject

acc -= accProject
acc += combinedProject
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package teksturepako.pakku.api.platforms

import kotlinx.datetime.Instant
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import net.thauvin.erik.urlencoder.UrlEncoderUtil.decode
Expand All @@ -13,7 +14,6 @@ import teksturepako.pakku.debug
import teksturepako.pakku.debugIfEmpty
import teksturepako.pakku.io.getEnvOrNull
import teksturepako.pakku.io.toMurmur2
import java.time.Instant

@Suppress("MemberVisibilityCanBePrivate")
object CurseForge : Platform(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package teksturepako.pakku.api.platforms

import kotlinx.datetime.Instant
import net.thauvin.erik.urlencoder.UrlEncoderUtil
import teksturepako.pakku.api.data.json
import teksturepako.pakku.api.http.Http
Expand All @@ -8,7 +9,6 @@ import teksturepako.pakku.api.models.gh.GhRepoModel
import teksturepako.pakku.api.projects.Project
import teksturepako.pakku.api.projects.ProjectFile
import teksturepako.pakku.api.projects.ProjectType
import java.time.Instant

object GitHub : Http(), Provider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.datetime.Instant
import kotlinx.serialization.json.JsonObject
import net.thauvin.erik.urlencoder.UrlEncoderUtil
import net.thauvin.erik.urlencoder.UrlEncoderUtil.encode
Expand All @@ -14,9 +15,7 @@ import teksturepako.pakku.api.models.mr.GetVersionsFromHashesRequest
import teksturepako.pakku.api.models.mr.MrProjectModel
import teksturepako.pakku.api.models.mr.MrVersionModel
import teksturepako.pakku.api.projects.*
import teksturepako.pakku.debug
import teksturepako.pakku.debugIfEmpty
import java.time.Instant
import kotlin.system.exitProcess
import kotlin.time.Duration.Companion.seconds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ data class Project(
return this.files.filter { it.type in providers.map { provider -> provider.serialName } }
}

fun getLatestFile(providers: Collection<Provider>) = getFilesForProviders(providers).maxByOrNull { it.datePublished }
fun getLatestFile(providers: Collection<Provider>): ProjectFile? =
getFilesForProviders(providers).maxByOrNull { it.datePublished }

// -- DEPENDENCIES --

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package teksturepako.pakku.api.projects

import kotlinx.datetime.Instant
import kotlinx.serialization.Required
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import teksturepako.pakku.api.data.InstantIso8601Serializer
import teksturepako.pakku.api.actions.ActionError
import teksturepako.pakku.api.actions.ActionError.HashMismatch
import teksturepako.pakku.api.actions.ActionError.NoHashes
Expand All @@ -12,7 +12,6 @@ import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.data.workingPath
import teksturepako.pakku.io.createHash
import java.nio.file.Path
import java.time.Instant
import kotlin.io.path.Path

@Serializable
Expand All @@ -29,7 +28,7 @@ data class ProjectFile(
val hashes: MutableMap<String, String>? = null,
@SerialName("required_dependencies") val requiredDependencies: MutableSet<String>? = null,
val size: Int = 0,
@SerialName("date_published") val datePublished: @Serializable(with = InstantIso8601Serializer::class) Instant = Instant.MIN,
@SerialName("date_published") val datePublished: Instant = Instant.DISTANT_PAST,
)
{
// -- PARENT --
Expand Down

0 comments on commit 80ef13c

Please sign in to comment.