Skip to content

Commit

Permalink
copyToTmpAndMove
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Dec 19, 2023
1 parent 8bcd898 commit 6732cf4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
22 changes: 11 additions & 11 deletions main/src/dsl/FileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ import org.gradle.api.Task
import java.io.File
import java.io.InputStream
import java.net.URL
import java.nio.file.FileSystem
import java.nio.file.Files

/**
* Copy data from this input stream to [file].
* @receiver Caller has responsibility to close this stream
*/
internal
fun InputStream.copyTo(file: File): File {
fun InputStream.copyTo(file: File) {
file.outputStream().use {
this.copyTo(it)
}
return file
}

/**
* Copy data from this url to [file].
* It will create the parent folder if it doesn't exist.
*/
internal
fun URL.copyTo(file: File): File {
file.parentFile.mkdirs()
this.openStream().use {
it.copyTo(file)
fun InputStream.copyToTmpAndMove(file: File) {
val tmp = Files.createTempFile(file.name, null).toFile()
this.use {
it.copyTo(tmp)
}
tmp.renameTo(file)
// ignore the error when deleting the temp file
runCatching {
tmp.delete()
}
return file
}

internal
Expand Down
16 changes: 15 additions & 1 deletion main/src/run/model/Mods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.io.File
import java.io.Serializable
import java.net.URL
import java.security.MessageDigest
import kotlin.math.absoluteValue

/**
* An abstract mod file.
Expand Down Expand Up @@ -216,4 +217,17 @@ fun tryReadGitHubModInfo(infoFi: File, logger: Logger? = null): GihHubModDownloa
} else {
writeAndGetDefault()
}
}
}

fun IGitHubMod.isUpdateToDate(): Boolean {
val cacheFile = this.resolveCacheFile()
val infoFi = File("$cacheFile.$infoX")
if (!cacheFile.exists()) {
if (infoFi.exists()) infoFi.delete()
return false
}
val meta = tryReadGitHubModInfo(infoFi)
val curTime = System.currentTimeMillis()
// TODO: Configurable out-of-date time
return curTime - meta.lastUpdateTimestamp < R.outOfDataTime.absoluteValue
}
13 changes: 4 additions & 9 deletions main/src/task/ResolveGame.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.liplum.mindustry

import io.github.liplum.dsl.copyTo
import io.github.liplum.dsl.createSymbolicLinkOrCopyCache
import io.github.liplum.dsl.*
import io.github.liplum.dsl.fileProp
import io.github.liplum.dsl.prop
import org.gradle.api.DefaultTask
Expand Down Expand Up @@ -45,14 +44,10 @@ open class ResolveGame : DefaultTask() {
fun IDownloadableGameLoc.download(cacheFile: File) {
logger.lifecycle("Downloading $this -> $cacheFile...")
try {
this.resolveDownloadSrc().openStream().use {
it.copyTo(cacheFile)
}
logger.lifecycle("${this.fileName4Local} was downloaded.")
this.resolveDownloadSrc().openStream().copyToTmpAndMove(cacheFile)
logger.info("$cacheFile was downloaded.")
} catch (e: Exception) {
// now output is corrupted, delete it
cacheFile.delete()
throw e
logger.error("Failed to download $this", e)
}
}
}
58 changes: 23 additions & 35 deletions main/src/task/ResolveMods.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.github.liplum.mindustry

import io.github.liplum.dsl.*
import io.github.liplum.dsl.copyTo
import io.github.liplum.dsl.listProp
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.TaskAction
import java.io.File
import kotlin.math.absoluteValue

internal
const val infoX = "info.json"
Expand Down Expand Up @@ -47,43 +45,33 @@ open class ResolveMods : DefaultTask() {
if (!cacheFile.exists()) {
when (mod) {
is LocalMod -> if (!cacheFile.isFile) throw GradleException("Local mod $cacheFile not found.")
is IGitHubMod -> mod.downloadOrUpdate(cacheFile)
is UrlMod -> mod.download(cacheFile)
is IGitHubMod -> {
if (!mod.isUpdateToDate()) {
logger.lifecycle("Updating $this -> $cacheFile...")
try {
mod.resolveDownloadSrc().openStream().copyToTmpAndMove(cacheFile)
logger.info("$cacheFile was downloaded.")
} catch (e: Exception) {
logger.error("Failed to update $this", e)
}
}
}

is IDownloadableMod -> {
logger.lifecycle("Downloading $this -> $cacheFile...")
try {
mod.resolveDownloadSrc().openStream().copyToTmpAndMove(cacheFile)
logger.info("$cacheFile was downloaded.")
} catch (e: Exception) {
logger.error("Failed to download $this", e)
}
}

else -> {}
}
}
createSymbolicLinkOrCopyCache(link = mod.resolveOutputFile(), target = cacheFile)
}
}

fun IGitHubMod.downloadOrUpdate(cacheFile: File) {
if (!this.isUpdateToDate()) {
val temp = File.createTempFile(this.fileName4Local, null)
try {
this.resolveDownloadSrc().openStream().use {
it.copyTo(temp)
}
} catch (e: Exception) {
logger.warn("Failed to update $this", e)
}
temp.copyTo(cacheFile)
}
}

fun IGitHubMod.isUpdateToDate(): Boolean {
val cacheFile = this.resolveCacheFile()
val infoFi = File("$cacheFile.$infoX")
if (!cacheFile.exists()) {
if (infoFi.exists()) infoFi.delete()
return false
}
val meta = tryReadGitHubModInfo(infoFi)
val curTime = System.currentTimeMillis()
// TODO: Configurable out-of-date time
return curTime - meta.lastUpdateTimestamp < R.outOfDataTime.absoluteValue
}

fun UrlMod.download(cacheFile: File) {
url.copyTo(cacheFile)
}
}

0 comments on commit 6732cf4

Please sign in to comment.