diff --git a/build.gradle.kts b/build.gradle.kts index 43da64a..7f3a351 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,12 +1,12 @@ -import ru.mipt.npm.gradle.KScienceVersions +import space.kscience.gradle.KScienceVersions import space.kscience.snark.plugin.JSch import space.kscience.snark.plugin.execute import space.kscience.snark.plugin.uploadDirectory import space.kscience.snark.plugin.useSession plugins { - id("ru.mipt.npm.gradle.project") - id("ru.mipt.npm.gradle.jvm") + id("space.kscience.gradle.project") + id("space.kscience.gradle.jvm") id("space.kscience.snark") application } @@ -42,18 +42,6 @@ apiValidation{ validationDisabled = true } -tasks.withType { - kotlinOptions { - freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers" - } -} - -sourceSets { - main { - resources.srcDir(project.rootDir.resolve("data")) - } -} - /* Upload with JSch */ val host = System.getenv("SPC_HOST") diff --git a/gradle.properties b/gradle.properties index 26b15c8..42ef16a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ kotlin.code.style=official -toolsVersion=0.11.7-kotlin-1.7.0 +toolsVersion=0.12.1-kotlin-1.7.20-Beta snarkVersion=0.1.0-dev-1 \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index ea2d6ee..cc3252a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -15,10 +15,10 @@ pluginManagement { } plugins { - id("ru.mipt.npm.gradle.project") version toolsVersion - id("ru.mipt.npm.gradle.mpp") version toolsVersion - id("ru.mipt.npm.gradle.jvm") version toolsVersion - id("ru.mipt.npm.gradle.js") version toolsVersion + id("space.kscience.gradle.project") version toolsVersion + id("space.kscience.gradle.mpp") version toolsVersion + id("space.kscience.gradle.jvm") version toolsVersion + id("space.kscience.gradle.js") version toolsVersion id("space.kscience.snark") version snarkVersion } } @@ -34,7 +34,7 @@ dependencyResolutionManagement { versionCatalogs { create("npmlibs") { - from("ru.mipt.npm:version-catalog:$toolsVersion") + from("space.kscience:version-catalog:$toolsVersion") } } } diff --git a/src/main/kotlin/ru/mipt/spc/Application.kt b/src/main/kotlin/ru/mipt/spc/Application.kt index 61e33eb..8277062 100644 --- a/src/main/kotlin/ru/mipt/spc/Application.kt +++ b/src/main/kotlin/ru/mipt/spc/Application.kt @@ -1,47 +1,12 @@ package ru.mipt.spc import io.ktor.server.application.Application -import io.ktor.server.application.log -import kotlinx.css.CssBuilder -import kotlinx.html.CommonAttributeGroupFacade -import kotlinx.html.style import space.kscience.snark.SnarkEnvironment +import space.kscience.snark.ktor.extractResources +import space.kscience.snark.ktor.prepareSnarkDataCacheDirectory import space.kscience.snark.ktor.site -import java.net.URI -import java.nio.file.FileSystems -import java.nio.file.Files import java.nio.file.Path -import java.time.LocalDateTime -import kotlin.io.path.* - -fun CommonAttributeGroupFacade.css(block: CssBuilder.() -> Unit) { - style = CssBuilder().block().toString() -} - -private fun Application.resolveData(uri: URI, targetPath: Path): Path { - if (Files.isDirectory(targetPath)) { - log.info("Using existing data directory at $targetPath.") - } else { - log.info("Copying data from $uri into $targetPath.") - targetPath.createDirectories() - //Copy everything into a temporary directory - FileSystems.newFileSystem(uri, emptyMap()).use { fs -> - val rootPath: Path = fs.provider().getPath(uri) - Files.walk(rootPath).forEach { source: Path -> - if (source.isRegularFile()) { - val relative = source.relativeTo(rootPath).toString() - val destination: Path = targetPath.resolve(relative) - destination.parent.createDirectories() - Files.copy(source, destination) - } - } - } - } - return targetPath -} - -const val DEPLOY_DATE_FILE = "deployDate" -const val BUILD_DATE_FILE = "/buildDate" +import kotlin.io.path.div @Suppress("unused") fun Application.spcModule() { @@ -49,54 +14,24 @@ fun Application.spcModule() { val dataPath = Path.of("data") - // Clear data directory if it is outdated - val deployDate = dataPath.resolve(DEPLOY_DATE_FILE).takeIf { it.exists() } - ?.readText()?.let { LocalDateTime.parse(it) } - val buildDate = javaClass.getResource(BUILD_DATE_FILE)?.readText()?.let { LocalDateTime.parse(it) } - - val inProduction: Boolean = environment.config.propertyOrNull("ktor.environment.production") != null - - if (inProduction) { - log.info("Production mode activated") - log.info("Build date: $buildDate") - log.info("Deploy date: $deployDate") - } - - if (deployDate != null && buildDate != null && buildDate.isAfter(deployDate)) { - log.info("Outdated data. Resetting data directory.") - - Files.walk(dataPath) - .sorted(Comparator.reverseOrder()) - .forEach { it.deleteIfExists() } - - //Writing deploy date file - dataPath.createDirectories() - dataPath.resolve(DEPLOY_DATE_FILE).writeText(LocalDateTime.now().toString()) - - } else if (inProduction && deployDate == null && buildDate != null) { - val date = LocalDateTime.now().toString() - log.info("Deploy date: $date") - //Writing deploy date in production mode if it does not exist - dataPath.createDirectories() - dataPath.resolve(DEPLOY_DATE_FILE).writeText(date) - } + prepareSnarkDataCacheDirectory(dataPath) SnarkEnvironment.default.site { - resolveData( - this@spcModule.javaClass.getResource("/common")!!.toURI(), + extractResources( + "/common", dataPath / "common" ) - val homeDataPath = resolveData( - this@spcModule.javaClass.getResource("/home")!!.toURI(), + val homeDataPath = extractResources( + "/home", dataPath / "home" ) spcHome(dataPath = homeDataPath) - val mastersDataPath = resolveData( - this@spcModule.javaClass.getResource("/magprog")!!.toURI(), + val mastersDataPath = extractResources( + "/magprog", dataPath / "magprog" ) diff --git a/src/main/kotlin/ru/mipt/spc/spcCollection.kt b/src/main/kotlin/ru/mipt/spc/spcCollection.kt index aaf88b0..151da92 100644 --- a/src/main/kotlin/ru/mipt/spc/spcCollection.kt +++ b/src/main/kotlin/ru/mipt/spc/spcCollection.kt @@ -9,7 +9,6 @@ import space.kscience.dataforge.meta.string import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.parseAsName import space.kscience.dataforge.names.withIndex -import space.kscience.dataforge.values.string import space.kscience.snark.html.* import kotlin.collections.component1 import kotlin.collections.component2 diff --git a/src/main/kotlin/ru/mipt/spc/spcHome.kt b/src/main/kotlin/ru/mipt/spc/spcHome.kt index b9c1120..4e2550f 100644 --- a/src/main/kotlin/ru/mipt/spc/spcHome.kt +++ b/src/main/kotlin/ru/mipt/spc/spcHome.kt @@ -9,7 +9,6 @@ import space.kscience.dataforge.meta.string import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.startsWith -import space.kscience.dataforge.values.string import space.kscience.snark.html.* import java.nio.file.Path import kotlin.reflect.typeOf