Update build tooks and DF

This commit is contained in:
Alexander Nozik 2022-09-03 11:54:34 +03:00
parent 0d75295782
commit e0bd2a792a
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
10 changed files with 126 additions and 34 deletions

View File

@ -1,24 +1,26 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { plugins {
id("ru.mipt.npm.gradle.project") id("space.kscience.gradle.project")
} }
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.1.0-dev-1" version = "0.1.0-dev-1"
if(name!="snark-gradle-plugin") { if (name != "snark-gradle-plugin") {
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { tasks.withType<KotlinCompile> {
kotlinOptions { kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers" freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers"
} }
} }
} }
} }
val dataforgeVersion by extra("0.6.0-dev-10") val dataforgeVersion by extra("0.6.0-dev-15")
ksciencePublish{ ksciencePublish {
github("snark") github("SciProgCentre", "snark")
space("https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven") space("https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven")
// sonatype() // sonatype()
} }

View File

@ -1,3 +1,3 @@
kotlin.code.style=official kotlin.code.style=official
toolsVersion=0.11.7-kotlin-1.7.0 toolsVersion=0.12.1-kotlin-1.7.20-Beta

View File

@ -14,10 +14,10 @@ pluginManagement {
} }
plugins { plugins {
id("ru.mipt.npm.gradle.project") version toolsVersion id("space.kscience.gradle.project") version toolsVersion
id("ru.mipt.npm.gradle.mpp") version toolsVersion id("space.kscience.gradle.mpp") version toolsVersion
id("ru.mipt.npm.gradle.jvm") version toolsVersion id("space.kscience.gradle.jvm") version toolsVersion
id("ru.mipt.npm.gradle.js") version toolsVersion id("space.kscience.gradle.js") version toolsVersion
} }
} }
@ -32,7 +32,7 @@ dependencyResolutionManagement {
versionCatalogs { versionCatalogs {
create("npmlibs") { create("npmlibs") {
from("ru.mipt.npm:version-catalog:$toolsVersion") from("space.kscience:version-catalog:$toolsVersion")
} }
} }
} }

View File

@ -1,5 +1,5 @@
plugins{ plugins{
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
`maven-publish` `maven-publish`
} }

View File

@ -1,5 +1,5 @@
plugins{ plugins{
id("ru.mipt.npm.gradle.jvm") id("space.kscience.gradle.jvm")
`kotlin-dsl` `kotlin-dsl`
`java-gradle-plugin` `java-gradle-plugin`
`maven-publish` `maven-publish`

View File

@ -2,8 +2,7 @@ package space.kscience.snark.plugin
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import java.io.File import java.io.File
import java.time.LocalDateTime import java.time.LocalDateTime
@ -41,13 +40,18 @@ public class SnarkGradlePlugin : Plugin<Project> {
tasks.getByName("processResources").dependsOn(writeBuildDate) tasks.getByName("processResources").dependsOn(writeBuildDate)
configure<KotlinJvmProjectExtension> { extensions.configure<org.gradle.api.tasks.SourceSetContainer>("sourceSets") {
sourceSets.apply { getByName("main") {
getByName("main") { logger.info("Adding ${snarkExtension.dataDirectory} to resources")
resources.srcDir(project.rootDir.resolve("data")) resources.srcDir(snarkExtension.dataDirectory)
}
} }
} }
} }
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers"
}
}
} }
} }

View File

@ -1,10 +1,10 @@
plugins { plugins {
id("ru.mipt.npm.gradle.jvm") id("space.kscience.gradle.jvm")
`maven-publish` `maven-publish`
} }
val dataforgeVersion: String by rootProject.extra val dataforgeVersion: String by rootProject.extra
val ktorVersion = ru.mipt.npm.gradle.KScienceVersions.ktorVersion val ktorVersion = space.kscience.gradle.KScienceVersions.ktorVersion
dependencies { dependencies {
api(projects.snarkCore) api(projects.snarkCore)
@ -19,7 +19,7 @@ dependencies {
} }
readme { readme {
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL maturity = space.kscience.gradle.Maturity.EXPERIMENTAL
feature("data") { "Data-based processing. Instead of traditional layout-based" } feature("data") { "Data-based processing. Instead of traditional layout-based" }
feature("layouts") { "Use custom layouts to represent a data tree" } feature("layouts") { "Use custom layouts to represent a data tree" }
feature("parsers") { "Add custom file formats and parsers using DataForge dependency injection" } feature("parsers") { "Add custom file formats and parsers using DataForge dependency injection" }

View File

@ -1,10 +1,10 @@
plugins { plugins {
id("ru.mipt.npm.gradle.jvm") id("space.kscience.gradle.jvm")
`maven-publish` `maven-publish`
} }
val dataforgeVersion: String by rootProject.extra val dataforgeVersion: String by rootProject.extra
val ktorVersion = ru.mipt.npm.gradle.KScienceVersions.ktorVersion val ktorVersion = space.kscience.gradle.KScienceVersions.ktorVersion
dependencies { dependencies {
api(projects.snarkHtml) api(projects.snarkHtml)

View File

@ -13,7 +13,10 @@ import io.ktor.server.routing.Route
import io.ktor.server.routing.createRouteFromPath import io.ktor.server.routing.createRouteFromPath
import io.ktor.server.routing.get import io.ktor.server.routing.get
import io.ktor.server.routing.routing import io.ktor.server.routing.routing
import kotlinx.css.CssBuilder
import kotlinx.html.CommonAttributeGroupFacade
import kotlinx.html.HTML import kotlinx.html.HTML
import kotlinx.html.style
import space.kscience.dataforge.data.DataTree import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.withDefault import space.kscience.dataforge.meta.withDefault
@ -27,8 +30,11 @@ import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
import kotlin.io.path.isDirectory import kotlin.io.path.isDirectory
@PublishedApi public fun CommonAttributeGroupFacade.css(block: CssBuilder.() -> Unit) {
internal class KtorSiteBuilder( style = CssBuilder().block().toString()
}
public class KtorSiteBuilder(
override val snark: SnarkHtmlPlugin, override val snark: SnarkHtmlPlugin,
override val data: DataTree<*>, override val data: DataTree<*>,
override val siteMeta: Meta, override val siteMeta: Meta,
@ -58,7 +64,7 @@ internal class KtorSiteBuilder(
} }
inner class KtorWebPage( private inner class KtorWebPage(
val pageBaseUrl: String, val pageBaseUrl: String,
override val pageMeta: Meta = this@KtorSiteBuilder.siteMeta, override val pageMeta: Meta = this@KtorSiteBuilder.siteMeta,
) : WebPage { ) : WebPage {
@ -117,8 +123,8 @@ internal class KtorSiteBuilder(
} }
} }
context(Route, SnarkEnvironment) public fun siteInRoute( context(Route, SnarkEnvironment) private fun siteInRoute(
block: SiteBuilder.() -> Unit, block: KtorSiteBuilder.() -> Unit,
) { ) {
contract { contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE)
@ -127,7 +133,7 @@ context(Route, SnarkEnvironment) public fun siteInRoute(
} }
context(Application) public fun SnarkEnvironment.site( context(Application) public fun SnarkEnvironment.site(
block: SiteBuilder.() -> Unit, block: KtorSiteBuilder.() -> Unit,
) { ) {
contract { contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE)
@ -135,4 +141,4 @@ context(Application) public fun SnarkEnvironment.site(
routing { routing {
siteInRoute(block) siteInRoute(block)
} }
} }

View File

@ -0,0 +1,80 @@
package space.kscience.snark.ktor
import io.ktor.server.application.Application
import io.ktor.server.application.log
import space.kscience.dataforge.context.info
import space.kscience.dataforge.context.logger
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.*
public fun KtorSiteBuilder.extractResources(uri: URI, targetPath: Path): Path {
if (Files.isDirectory(targetPath)) {
logger.info { "Using existing data directory at $targetPath." }
} else {
logger.info { "Copying data from $uri into $targetPath." }
targetPath.createDirectories()
//Copy everything into a temporary directory
FileSystems.newFileSystem(uri, emptyMap<String, Any>()).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
}
public fun KtorSiteBuilder.extractResources(resource: String, targetPath: Path): Path =
extractResources(javaClass.getResource(resource)!!.toURI(), targetPath)
private const val DEPLOY_DATE_FILE = "deployDate"
private const val BUILD_DATE_FILE = "/buildDate"
/**
* Prepare the data cache directory for snark. Clear data if it is outdated.
* TODO make internal
*/
fun Application.prepareSnarkDataCacheDirectory(dataPath: Path) {
// 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)
}
}