mirror of
https://github.com/GayPizzaSpecifications/dough.git
synced 2025-08-03 05:30:55 +00:00
dough-core and all of that
This commit is contained in:
10
dough-core/build.gradle.kts
Normal file
10
dough-core/build.gradle.kts
Normal file
@ -0,0 +1,10 @@
|
||||
plugins {
|
||||
dough_component
|
||||
}
|
||||
|
||||
kotlin {
|
||||
js(IR) {
|
||||
nodejs()
|
||||
browser()
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package gay.pizza.dough.core
|
||||
|
||||
import gay.pizza.dough.core.time.ClockProvider
|
||||
|
||||
expect val PlatformClock: ClockProvider
|
@ -0,0 +1,5 @@
|
||||
package gay.pizza.dough.core.time
|
||||
|
||||
interface ClockProvider {
|
||||
fun now(): UnixTime
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package gay.pizza.dough.core.time
|
||||
|
||||
import gay.pizza.dough.core.PlatformClock
|
||||
|
||||
class StaticClockProvider(private val value: UnixTime) : ClockProvider {
|
||||
override fun now(): UnixTime = value
|
||||
|
||||
companion object {
|
||||
fun snapshot(): StaticClockProvider = StaticClockProvider(PlatformClock.now())
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package gay.pizza.dough.core.time
|
||||
|
||||
import kotlin.jvm.JvmInline
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.DurationUnit
|
||||
import kotlin.time.toDuration
|
||||
|
||||
@JvmInline
|
||||
value class UnixTime(val millisecondsSinceEpoch: Long) : Comparable<UnixTime> {
|
||||
operator fun plus(time: UnixTime): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch + time.millisecondsSinceEpoch)
|
||||
|
||||
operator fun minus(time: UnixTime): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch - time.millisecondsSinceEpoch)
|
||||
|
||||
operator fun times(time: UnixTime): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch * time.millisecondsSinceEpoch)
|
||||
|
||||
operator fun div(duration: Duration): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch / duration.inWholeMilliseconds)
|
||||
|
||||
operator fun rem(time: UnixTime): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch % time.millisecondsSinceEpoch)
|
||||
|
||||
operator fun plus(duration: Duration): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch + duration.inWholeMilliseconds)
|
||||
|
||||
operator fun minus(duration: Duration): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch - duration.inWholeMilliseconds)
|
||||
|
||||
operator fun times(duration: Duration): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch * duration.inWholeMilliseconds)
|
||||
|
||||
operator fun div(time: UnixTime): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch / time.millisecondsSinceEpoch)
|
||||
|
||||
operator fun rem(duration: Duration): UnixTime =
|
||||
UnixTime(millisecondsSinceEpoch % duration.inWholeMilliseconds)
|
||||
|
||||
fun toDuration(): Duration = millisecondsSinceEpoch.toDuration(DurationUnit.MILLISECONDS)
|
||||
|
||||
override fun compareTo(other: UnixTime): Int =
|
||||
millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch)
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package gay.pizza.dough.core
|
||||
|
||||
enum class JsPlatformType {
|
||||
Nodejs,
|
||||
Browser,
|
||||
Unknown;
|
||||
|
||||
companion object {
|
||||
fun current(): JsPlatformType {
|
||||
val isWindowAvailable = js("typeof window") != undefined
|
||||
val isProcessAvailable = js("typeof process") != undefined
|
||||
|
||||
if (isProcessAvailable) {
|
||||
return Nodejs
|
||||
}
|
||||
|
||||
if (isWindowAvailable) {
|
||||
return Browser
|
||||
}
|
||||
return Unknown
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package gay.pizza.dough.core
|
||||
|
||||
import gay.pizza.dough.core.time.ClockProvider
|
||||
|
||||
actual val PlatformClock: ClockProvider = StandardClockProvider
|
@ -0,0 +1,9 @@
|
||||
package gay.pizza.dough.core
|
||||
|
||||
import gay.pizza.dough.core.time.ClockProvider
|
||||
import gay.pizza.dough.core.time.UnixTime
|
||||
|
||||
object StandardClockProvider : ClockProvider {
|
||||
override fun now(): UnixTime =
|
||||
UnixTime(js("(new Date()).getTime()") as Long)
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package gay.pizza.dough.core
|
||||
|
||||
import gay.pizza.dough.core.time.ClockProvider
|
||||
import gay.pizza.dough.core.time.UnixTime
|
||||
|
||||
object JavaClockProvider : ClockProvider {
|
||||
override fun now(): UnixTime = UnixTime(System.currentTimeMillis())
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package gay.pizza.dough.core
|
||||
|
||||
import gay.pizza.dough.core.time.ClockProvider
|
||||
|
||||
actual val PlatformClock: ClockProvider = JavaClockProvider
|
@ -0,0 +1,9 @@
|
||||
package gay.pizza.dough.core
|
||||
|
||||
import gay.pizza.dough.core.time.UnixTime
|
||||
import java.nio.file.attribute.FileTime
|
||||
import java.time.Instant
|
||||
|
||||
fun UnixTime.toInstant(): Instant = Instant.ofEpochMilli(millisecondsSinceEpoch)
|
||||
fun Instant.toUnixTime(): UnixTime = UnixTime(toEpochMilli())
|
||||
fun FileTime.toUnixTime(): UnixTime = toInstant().toUnixTime()
|
@ -1,3 +1,13 @@
|
||||
plugins {
|
||||
dough_component
|
||||
}
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api(project(":dough-core"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import gay.pizza.dough.core.time.UnixTime
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.SerializationStrategy
|
||||
|
||||
interface FsOperations {
|
||||
fun exists(path: FsPath): Boolean
|
||||
|
||||
fun isDirectory(path: FsPath): Boolean
|
||||
fun isRegularFile(path: FsPath): Boolean
|
||||
fun isSymbolicLink(path: FsPath): Boolean
|
||||
@ -12,6 +14,8 @@ interface FsOperations {
|
||||
fun isWritable(path: FsPath): Boolean
|
||||
fun isExecutable(path: FsPath): Boolean
|
||||
|
||||
fun lastModified(path: FsPath): UnixTime
|
||||
|
||||
fun list(path: FsPath): Sequence<FsPath>
|
||||
|
||||
fun walk(path: FsPath): Sequence<FsPath>
|
||||
|
@ -0,0 +1,8 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
interface FsProvider {
|
||||
val currentWorkingDirectory: FsPath
|
||||
val operations: FsOperations
|
||||
|
||||
fun resolve(path: String): FsPath
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
expect fun FsPath(path: String): FsPath
|
||||
expect val PlatformFsProvider: FsProvider
|
||||
|
@ -1,5 +1,7 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import gay.pizza.dough.core.time.UnixTime
|
||||
import gay.pizza.dough.core.toUnixTime
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.SerializationStrategy
|
||||
import kotlinx.serialization.json.Json
|
||||
@ -11,6 +13,7 @@ import kotlin.streams.asSequence
|
||||
|
||||
object JavaFsOperations : FsOperations {
|
||||
override fun exists(path: FsPath): Boolean = Files.exists(path.toJavaPath())
|
||||
|
||||
override fun isDirectory(path: FsPath): Boolean = Files.isDirectory(path.toJavaPath())
|
||||
override fun isRegularFile(path: FsPath): Boolean = Files.isRegularFile(path.toJavaPath())
|
||||
override fun isSymbolicLink(path: FsPath): Boolean = Files.isSymbolicLink(path.toJavaPath())
|
||||
@ -18,10 +21,12 @@ object JavaFsOperations : FsOperations {
|
||||
override fun isWritable(path: FsPath): Boolean = Files.isWritable(path.toJavaPath())
|
||||
override fun isExecutable(path: FsPath): Boolean = Files.isExecutable(path.toJavaPath())
|
||||
|
||||
override fun lastModified(path: FsPath): UnixTime = Files.getLastModifiedTime(path.toJavaPath()).toUnixTime()
|
||||
|
||||
override fun list(path: FsPath): Sequence<FsPath> = Files.list(path.toJavaPath()).asSequence().map { it.toFsPath() }
|
||||
|
||||
override fun walk(path: FsPath): Sequence<FsPath> = Files.walk(path.toJavaPath()).asSequence().map { it.toFsPath() }
|
||||
override fun visit(path: FsPath, visitor: FsPathVisitor) =
|
||||
override fun visit(path: FsPath, visitor: FsPathVisitor): Unit =
|
||||
Files.walkFileTree(path.toJavaPath(), JavaFsPathVisitorAdapter(visitor)).run {}
|
||||
|
||||
override fun readString(path: FsPath): String = Files.readString(path.toJavaPath())
|
||||
|
@ -0,0 +1,14 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import java.nio.file.FileSystem
|
||||
import kotlin.io.path.absolute
|
||||
|
||||
class JavaFsProvider(val fileSystem: FileSystem) : FsProvider {
|
||||
override val currentWorkingDirectory: FsPath
|
||||
get() = fileSystem.getPath(".").absolute().toFsPath()
|
||||
|
||||
override val operations: FsOperations = JavaFsOperations
|
||||
|
||||
override fun resolve(path: String): FsPath =
|
||||
fileSystem.getPath(path).toFsPath()
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import java.nio.file.Paths
|
||||
import java.nio.file.FileSystems
|
||||
|
||||
actual fun FsPath(path: String): FsPath {
|
||||
return JavaPath(Paths.get(path))
|
||||
}
|
||||
actual val PlatformFsProvider: FsProvider = JavaFsProvider(FileSystems.getDefault())
|
||||
|
@ -1,2 +1,6 @@
|
||||
rootProject.name = "dough"
|
||||
include("dough-fs")
|
||||
|
||||
include(
|
||||
"dough-core",
|
||||
"dough-fs"
|
||||
)
|
||||
|
Reference in New Issue
Block a user