From 3bf49d3fae56de3011566138d721e6df6563a81e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 28 Jun 2019 10:19:58 +0300 Subject: [PATCH] Isolating build logic --- build.gradle.kts | 5 +- buildSrc/src/main/kotlin/Scientifik.kt | 9 ++++ .../src/main/kotlin/ScientifikMPPlugin.kt | 32 +++---------- .../main/kotlin/ScientifikPublishPlugin.kt | 46 ++++++++++++++++--- buildSrc/src/main/kotlin/Versions.kt | 9 ---- dataforge-context/build.gradle.kts | 2 +- dataforge-data/build.gradle.kts | 2 +- dataforge-io/build.gradle.kts | 4 +- .../hep/dataforge/io/BinaryMetaFormat.kt | 5 +- dataforge-meta/build.gradle.kts | 3 ++ ...{ConfigDelegates.kt => configDelegates.kt} | 21 +++++++-- .../meta/{Delegates.kt => metaDelegates.kt} | 11 +++-- .../kotlin/hep/dataforge/values/Value.kt | 18 +------- .../hep/dataforge/values/exoticValues.kt | 46 +++++++++++++++++++ 14 files changed, 140 insertions(+), 73 deletions(-) create mode 100644 buildSrc/src/main/kotlin/Scientifik.kt delete mode 100644 buildSrc/src/main/kotlin/Versions.kt rename dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/{ConfigDelegates.kt => configDelegates.kt} (86%) rename dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/{Delegates.kt => metaDelegates.kt} (98%) create mode 100644 dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt diff --git a/build.gradle.kts b/build.gradle.kts index 55a823d2..5723c33f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,7 @@ -val dataforgeVersion by extra("0.1.3-dev-6") +val dataforgeVersion by extra("0.1.3-dev-7") + +val bintrayRepo by extra("dataforge") +val vcs by extra("https://github.com/mipt-npm/dataforge-core") allprojects { repositories { diff --git a/buildSrc/src/main/kotlin/Scientifik.kt b/buildSrc/src/main/kotlin/Scientifik.kt new file mode 100644 index 00000000..617b2e67 --- /dev/null +++ b/buildSrc/src/main/kotlin/Scientifik.kt @@ -0,0 +1,9 @@ +/** + * Build constants + */ +object Scientifik { + val ioVersion = "0.1.10" + val coroutinesVersion = "1.2.2" + val atomicfuVersion = "0.12.9" + val serializationVersion = "0.11.1" +} diff --git a/buildSrc/src/main/kotlin/ScientifikMPPlugin.kt b/buildSrc/src/main/kotlin/ScientifikMPPlugin.kt index 0aef37be..e34c675b 100644 --- a/buildSrc/src/main/kotlin/ScientifikMPPlugin.kt +++ b/buildSrc/src/main/kotlin/ScientifikMPPlugin.kt @@ -1,15 +1,10 @@ -import com.jfrog.bintray.gradle.tasks.BintrayUploadTask import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.publish.PublishingExtension -import org.gradle.api.publish.maven.MavenPublication -import org.gradle.api.publish.maven.internal.artifact.FileBasedMavenArtifact import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.getValue import org.gradle.kotlin.dsl.getting import org.gradle.kotlin.dsl.invoke import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask open class ScientifikMPPlugin : Plugin { override fun apply(project: Project) { @@ -29,7 +24,7 @@ open class ScientifikMPPlugin : Plugin { kotlinOptions { sourceMap = true sourceMapEmbedSources = "always" - moduleKind = "commonjs" + moduleKind = "umd" } } } @@ -71,26 +66,11 @@ open class ScientifikMPPlugin : Plugin { targets.all { sourceSets.all { - languageSettings.progressiveMode = true - languageSettings.enableLanguageFeature("InlineClasses") - } - } - } - - - project.tasks.filter { it is ArtifactoryTask || it is BintrayUploadTask }.forEach { - it.doFirst { - project.configure { - publications - .filterIsInstance() - .forEach { publication -> - val moduleFile = project.buildDir.resolve("publications/${publication.name}/module.json") - if (moduleFile.exists()) { - publication.artifact(object : FileBasedMavenArtifact(moduleFile) { - override fun getDefaultExtension() = "module" - }) - } - } + languageSettings.apply{ + progressiveMode = true + enableLanguageFeature("InlineClasses") + useExperimentalAnnotation("ExperimentalUnsignedType") + } } } } diff --git a/buildSrc/src/main/kotlin/ScientifikPublishPlugin.kt b/buildSrc/src/main/kotlin/ScientifikPublishPlugin.kt index 81e56ad6..c94751a9 100644 --- a/buildSrc/src/main/kotlin/ScientifikPublishPlugin.kt +++ b/buildSrc/src/main/kotlin/ScientifikPublishPlugin.kt @@ -19,11 +19,22 @@ import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask open class ScientifikExtension { - var vcs = "https://github.com/altavir/dataforge-core" - var bintrayRepo = "dataforge" - var dokka = true + var vcs: String? = null + var bintrayRepo: String? = null + var kdoc: Boolean = true } +// recursively search up the project chain for configuration +private val Project.bintrayRepo: String? + get() = extensions.findByType()?.bintrayRepo + ?: parent?.bintrayRepo + ?: (findProperty("bintrayRepo") as? String) + +private val Project.vcs: String? + get() = extensions.findByType()?.vcs + ?: parent?.vcs + ?: (findProperty("vcs") as? String) + open class ScientifikPublishPlugin : Plugin { override fun apply(project: Project) { @@ -31,11 +42,16 @@ open class ScientifikPublishPlugin : Plugin { project.plugins.apply("maven-publish") val extension = project.extensions.create("scientifik") + val bintrayRepo = project.bintrayRepo + val vcs = project.vcs + if (bintrayRepo == null || vcs == null) { + project.logger.warn("[${project.name}] Missing deployment configuration. Skipping publish.") + } project.configure { repositories { - maven("https://bintray.com/mipt-npm/${extension.bintrayRepo}") + maven("https://bintray.com/mipt-npm/$bintrayRepo") } // Process each publication we have in this project @@ -45,7 +61,7 @@ open class ScientifikPublishPlugin : Plugin { publication.pom { name.set(project.name) description.set(project.description) - url.set(extension.vcs) + url.set(vcs) licenses { license { @@ -70,7 +86,7 @@ open class ScientifikPublishPlugin : Plugin { } } - if (extension.dokka) { + if (extension.kdoc) { project.plugins.apply("org.jetbrains.dokka") project.afterEvaluate { @@ -96,6 +112,7 @@ open class ScientifikPublishPlugin : Plugin { path = sourceSets["jvmMain"].kotlin.srcDirs.first().toString() platforms = listOf("JVM") } + } val kdocJar by tasks.registering(Jar::class) { @@ -113,6 +130,21 @@ open class ScientifikPublishPlugin : Plugin { // Patch publications with fake javadoc publication.artifact(kdocJar.get()) } + + tasks.filter { it is ArtifactoryTask || it is BintrayUploadTask }.forEach { + it.doFirst { + publications.filterIsInstance() + .forEach { publication -> + val moduleFile = + buildDir.resolve("publications/${publication.name}/module.json") + if (moduleFile.exists()) { + publication.artifact(object : FileBasedMavenArtifact(moduleFile) { + override fun getDefaultExtension() = "module" + }) + } + } + } + } } } @@ -152,7 +184,7 @@ open class ScientifikPublishPlugin : Plugin { // this is a problem of this plugin pkg.apply { userOrg = "mipt-npm" - repo = extension.bintrayRepo + repo = bintrayRepo name = project.name issueTrackerUrl = "${extension.vcs}/issues" setLicenses("Apache-2.0") diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt deleted file mode 100644 index c3fc6d6d..00000000 --- a/buildSrc/src/main/kotlin/Versions.kt +++ /dev/null @@ -1,9 +0,0 @@ -// Instead of defining runtime properties and use them dynamically -// define version in buildSrc and have autocompletion and compile-time check -// Also dependencies itself can be moved here -object Versions { - val ioVersion = "0.1.10" - val coroutinesVersion = "1.2.2" - val atomicfuVersion = "0.12.9" - val serializationVersion = "0.11.1" -} diff --git a/dataforge-context/build.gradle.kts b/dataforge-context/build.gradle.kts index a4f8faba..f550b58a 100644 --- a/dataforge-context/build.gradle.kts +++ b/dataforge-context/build.gradle.kts @@ -4,7 +4,7 @@ plugins { description = "Context and provider definitions" -val coroutinesVersion: String = Versions.coroutinesVersion +val coroutinesVersion: String = Scientifik.coroutinesVersion kotlin { sourceSets { diff --git a/dataforge-data/build.gradle.kts b/dataforge-data/build.gradle.kts index 7527bf6f..00052375 100644 --- a/dataforge-data/build.gradle.kts +++ b/dataforge-data/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("scientifik.mpp") } -val coroutinesVersion: String = Versions.coroutinesVersion +val coroutinesVersion: String = Scientifik.coroutinesVersion kotlin { jvm() diff --git a/dataforge-io/build.gradle.kts b/dataforge-io/build.gradle.kts index 4d7772a9..6ed06857 100644 --- a/dataforge-io/build.gradle.kts +++ b/dataforge-io/build.gradle.kts @@ -5,8 +5,8 @@ plugins { description = "IO for meta" -val ioVersion: String = Versions.ioVersion -val serializationVersion: String = Versions.serializationVersion +val ioVersion: String = Scientifik.ioVersion +val serializationVersion: String = Scientifik.serializationVersion kotlin { jvm() diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt index 843e5acb..941b4abc 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/BinaryMetaFormat.kt @@ -92,8 +92,7 @@ object BinaryMetaFormat : MetaFormat { @Suppress("UNCHECKED_CAST") private fun Input.readMetaItem(): MetaItem { - val keyChar = readByte().toChar() - return when (keyChar) { + return when (val keyChar = readByte().toChar()) { 'S' -> MetaItem.ValueItem(StringValue(readString())) 'N' -> MetaItem.ValueItem(Null) '+' -> MetaItem.ValueItem(True) @@ -120,6 +119,6 @@ object BinaryMetaFormat : MetaFormat { MetaItem.NodeItem(meta) } else -> error("Unknown serialization key character: $keyChar") - } as MetaItem + } } } \ No newline at end of file diff --git a/dataforge-meta/build.gradle.kts b/dataforge-meta/build.gradle.kts index 859c2ed6..3fd7b1e0 100644 --- a/dataforge-meta/build.gradle.kts +++ b/dataforge-meta/build.gradle.kts @@ -4,3 +4,6 @@ plugins { description = "Meta definition and basic operations on meta" +scientifik{ + +} \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/ConfigDelegates.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/configDelegates.kt similarity index 86% rename from dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/ConfigDelegates.kt rename to dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/configDelegates.kt index 71094888..6871b6e3 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/ConfigDelegates.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/configDelegates.kt @@ -1,5 +1,6 @@ package hep.dataforge.meta +import hep.dataforge.values.DoubleArrayValue import hep.dataforge.values.Null import hep.dataforge.values.Value import kotlin.jvm.JvmName @@ -13,8 +14,13 @@ import kotlin.jvm.JvmName fun Configurable.value(default: Any = Null, key: String? = null): MutableValueDelegate = MutableValueDelegate(config, key, Value.of(default)) -fun Configurable.value(default: T? = null, key: String? = null, transform: (Value?) -> T): ReadWriteDelegateWrapper = - MutableValueDelegate(config, key, default?.let { Value.of(it)}).transform(reader = transform) +fun Configurable.value( + default: T? = null, + key: String? = null, + writer: (T) -> Value = { Value.of(it) }, + reader: (Value?) -> T +): ReadWriteDelegateWrapper = + MutableValueDelegate(config, key, default?.let { Value.of(it) }).transform(reader = reader, writer = writer) fun Configurable.string(default: String? = null, key: String? = null): MutableStringDelegate = MutableStringDelegate(config, key, default) @@ -106,7 +112,6 @@ fun Configurable.spec(spec: Specification, key: String? = null fun Configurable.spec(builder: (Config) -> T, key: String? = null) = MutableMorphDelegate(config, key) { specification(builder).wrap(it) } - /* * Extra delegates for special cases */ @@ -117,7 +122,15 @@ fun Configurable.stringList(key: String? = null): ReadWriteDelegateWrapper> = value(emptyList(), key) { it?.list?.map { value -> value.number } ?: emptyList() } -fun Metoid.child(key: String? = null, converter: (Meta) -> T) = ChildDelegate(meta, key, converter) +/** + * A special delegate for double arrays + */ +fun Configurable.doubleArray(key: String? = null): ReadWriteDelegateWrapper = + value(doubleArrayOf(), key) { + (it as? DoubleArrayValue)?.value + ?: it?.list?.map { value -> value.number.toDouble() }?.toDoubleArray() + ?: doubleArrayOf() + } fun Configurable.child(key: String? = null, converter: (Meta) -> T) = MutableMorphDelegate(config, key, converter) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/metaDelegates.kt similarity index 98% rename from dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt rename to dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/metaDelegates.kt index bcd60cc8..9d606b6f 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Delegates.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/metaDelegates.kt @@ -113,8 +113,11 @@ class SafeEnumDelegate>( //Child node delegate -class ChildDelegate(val meta: Meta, private val key: String? = null, private val converter: (Meta) -> T) : - ReadOnlyProperty { +class ChildDelegate( + val meta: Meta, + private val key: String? = null, + private val converter: (Meta) -> T +) : ReadOnlyProperty { override fun getValue(thisRef: Any?, property: KProperty<*>): T? { return meta[key ?: property.name]?.node?.let { converter(it) } } @@ -164,6 +167,8 @@ inline fun > Meta.enum(default: E, key: String? = null) = SafeEnumDelegate(this, key, default) { enumValueOf(it) } +fun Metoid.child(key: String? = null, converter: (Meta) -> T) = ChildDelegate(meta, key, converter) + /* Read-write delegates */ class MutableValueDelegate>( @@ -418,4 +423,4 @@ fun > M.number(key: String? = null, default: () -> Number) = inline fun , reified E : Enum> M.enum(default: E, key: String? = null) = - MutableSafeEnumvDelegate(this, key, default) { enumValueOf(it) } + MutableSafeEnumvDelegate(this, key, default) { enumValueOf(it) } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt index 5753512b..ac651c20 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -110,6 +110,8 @@ object False : Value { override val type: ValueType get() = ValueType.BOOLEAN override val number: Number get() = -1.0 override val string: String get() = "false" + + override fun toString(): String = True.value.toString() } val Value.boolean get() = this == True || this.list.firstOrNull() == True || (type == ValueType.STRING && string.toBoolean()) @@ -206,9 +208,6 @@ fun String.asValue(): Value = StringValue(this) fun Iterable.asValue(): Value = ListValue(this.toList()) -//TODO maybe optimized storage performance -fun DoubleArray.asValue(): Value = ListValue(map{NumberValue(it)}) - fun IntArray.asValue(): Value = ListValue(map{NumberValue(it)}) fun LongArray.asValue(): Value = ListValue(map{NumberValue(it)}) @@ -253,17 +252,4 @@ fun String.parseValue(): Value { //Give up and return a StringValue return StringValue(this) -} - -class LazyParsedValue(override val string: String) : Value { - private val parsedValue by lazy { string.parseValue() } - - override val value: Any? - get() = parsedValue.value - override val type: ValueType - get() = parsedValue.type - override val number: Number - get() = parsedValue.number - - override fun toString(): String = value.toString() } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt new file mode 100644 index 00000000..9f13d73a --- /dev/null +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt @@ -0,0 +1,46 @@ +package hep.dataforge.values + + +/** + * A value built from string which content and type are parsed on-demand + */ +class LazyParsedValue(override val string: String) : Value { + private val parsedValue by lazy { string.parseValue() } + + override val value: Any? get() = parsedValue.value + override val type: ValueType get() = parsedValue.type + override val number: Number get() = parsedValue.number + + override fun toString(): String = string +} + +fun String.lazyParseValue(): LazyParsedValue = LazyParsedValue(this) + +/** + * A performance optimized version of list value for doubles + */ +class DoubleArrayValue(override val value: DoubleArray) : Value { + override val type: ValueType get() = ValueType.NUMBER + override val number: Double get() = value.first() + override val string: String get() = value.first().toString() + override val list: List get() = value.map { NumberValue(it) } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is Value) return false + + return if (other is DoubleArrayValue) { + value.contentEquals(other.value) + } else { + list == other.list + } + } + + override fun hashCode(): Int { + return value.contentHashCode() + } + + override fun toString(): String = value.toString() +} + +fun DoubleArray.asValue(): DoubleArrayValue = DoubleArrayValue(this)