Rework target into interface; add overwrite mode and validity/identity tokens

This commit is contained in:
comp500 2022-03-06 21:28:27 +00:00
parent 89bdfd9c98
commit 07af6046c1
3 changed files with 63 additions and 21 deletions

View File

@ -0,0 +1,13 @@
package link.infra.packwiz.installer.target
enum class OverwriteMode {
/**
* Overwrite the destination with the source file, if the source file has changed.
*/
IF_SRC_CHANGED,
/**
* Never overwrite the destination; if it exists, it should not be written to.
*/
NEVER
}

View File

@ -1,32 +1,45 @@
package link.infra.packwiz.installer.target
import link.infra.packwiz.installer.metadata.SpaceSafeURI
import link.infra.packwiz.installer.metadata.hash.Hash
import java.nio.file.Path
import link.infra.packwiz.installer.target.path.PackwizPath
// TODO: rename to avoid conflicting with @Target
interface Target {
val src: PackwizPath
val dest: PackwizPath
val validityToken: ValidityToken
data class Target(
/**
* The name that uniquely identifies this target.
* Often equal to the name of the metadata file for this target, and can be displayed to the user in progress UI.
* Token interface for types used to compare Target identity. Implementations should use equals to indicate that
* these tokens represent the same file; used to preserve optional target choices between file renames.
*/
val name: String,
interface IdentityToken
/**
* An optional user-friendly name.
* Default implementation of IdentityToken that assumes files are not renamed; so optional choices do not need to
* be preserved across renames.
*/
val userFriendlyName: String?,
val src: SpaceSafeURI,
val dest: Path,
val hash: Hash,
val side: Side,
val optional: Boolean,
val optionalDefaultValue: Boolean,
val optionalDescription: String,
@JvmInline
value class PathIdentityToken(val path: String): IdentityToken
val ident: IdentityToken
get() = PathIdentityToken(dest.path)
/**
* If this is true, don't update a target when the file already exists.
* A user-friendly name; defaults to the destination path of the file.
*/
val noOverwrite: Boolean
) {
fun Iterable<Target>.filterForSide(side: Side) = this.filter {
it.side.hasSide(side)
val name: String
get() = dest.path
val side: Side
get() = Side.BOTH
val overwriteMode: OverwriteMode
get() = OverwriteMode.IF_SRC_CHANGED
interface Optional: Target {
val optional: Boolean
val optionalDefaultValue: Boolean
}
interface OptionalDescribed: Optional {
val optionalDescription: String
}
}

View File

@ -0,0 +1,16 @@
package link.infra.packwiz.installer.target
import link.infra.packwiz.installer.metadata.hash.Hash
/**
* A token used to determine if the source or destination file is changed, and ensure that the destination file is valid.
*/
interface ValidityToken {
// TODO: functions to allow validating this from file metadata, from file, or during the download process
/**
* Default implementation of ValidityToken based on a single hash.
*/
@JvmInline
value class HashValidityToken(val hash: Hash): ValidityToken
}