Skip to content

Commit 8e1e388

Browse files
author
Oleksandr Dzhychko
authored
Merge pull request #266 from modelix/feature/js-model-client
Feature/js model client
2 parents 1c2622e + e14d15f commit 8e1e388

File tree

6 files changed

+548
-0
lines changed

6 files changed

+548
-0
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ modelix-mps-buildtools = { id = "org.modelix.mps.build-tools", version = "1.1.0"
1818
dokka = {id = "org.jetbrains.dokka", version = "1.9.10"}
1919
node = {id = "com.github.node-gradle.node", version = "7.0.1"}
2020
detekt = { id = "io.gitlab.arturbosch.detekt", version = "1.23.1" }
21+
npm-publish = { id = "dev.petuska.npm.publish", version = "3.4.1" }
2122

2223
[versions]
2324
kotlin = "1.9.10"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2023.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.kotlin.utils
18+
19+
/**
20+
* Marks an API as unstable.
21+
*
22+
* @param reason Describes why the feature is experimental.
23+
* @param intendedFinalization Describes when this API is intended to be finalized or removed.
24+
*/
25+
@RequiresOptIn(message = "This API is experimental. It may be changed in the future without notice.")
26+
@Retention(AnnotationRetention.BINARY)
27+
@Target(
28+
AnnotationTarget.CLASS,
29+
AnnotationTarget.FUNCTION,
30+
AnnotationTarget.PROPERTY,
31+
AnnotationTarget.ANNOTATION_CLASS,
32+
AnnotationTarget.CONSTRUCTOR,
33+
AnnotationTarget.PROPERTY_SETTER,
34+
AnnotationTarget.PROPERTY_GETTER,
35+
AnnotationTarget.TYPEALIAS,
36+
)
37+
annotation class UnstableModelixFeature(
38+
val reason: String,
39+
val intendedFinalization: String,
40+
)

model-client/build.gradle.kts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
id("com.diffplug.spotless")
55
`java-library`
66
jacoco
7+
alias(libs.plugins.npm.publish)
78
}
89

910
java {
@@ -34,6 +35,8 @@ kotlin {
3435
}
3536
}
3637
}
38+
binaries.library()
39+
generateTypeScriptDefinitions()
3740
useCommonJs()
3841
}
3942
@Suppress("UNUSED_VARIABLE", "KotlinRedundantDiagnosticSuppress")
@@ -86,6 +89,7 @@ kotlin {
8689
}
8790
}
8891
val jsMain by getting {
92+
languageSettings.optIn("kotlin.js.ExperimentalJsExport")
8993
dependencies {
9094
implementation(kotlin("stdlib-js"))
9195
implementation(npm("uuid", "^8.3.0"))
@@ -128,3 +132,82 @@ spotless {
128132
)
129133
}
130134
}
135+
136+
val productionLibraryByKotlinOutputDirectory = layout.buildDirectory.dir("compileSync/js/main/productionLibrary/kotlin")
137+
val preparedProductionLibraryOutputDirectory = layout.buildDirectory.dir("npmPublication")
138+
139+
val patchTypesScriptInProductionLibrary = tasks.register("patchTypesScriptInProductionLibrary") {
140+
dependsOn("compileProductionLibraryKotlinJs")
141+
inputs.dir(productionLibraryByKotlinOutputDirectory)
142+
outputs.dir(preparedProductionLibraryOutputDirectory)
143+
outputs.cacheIf { true }
144+
doLast {
145+
// Delete old data
146+
delete {
147+
delete(preparedProductionLibraryOutputDirectory)
148+
}
149+
150+
// Copy over library create by Kotlin
151+
copy {
152+
from(productionLibraryByKotlinOutputDirectory)
153+
into(preparedProductionLibraryOutputDirectory)
154+
}
155+
156+
// Add correct TypeScript imports and mark exports as experimental.
157+
val typescriptDeclaration =
158+
preparedProductionLibraryOutputDirectory.get().file("modelix.core-model-client.d.ts").asFile
159+
val originalTypescriptDeclarationContent = typescriptDeclaration.readLines()
160+
val experimentalDeclaration = """
161+
162+
/**
163+
* @experimental This feature is expected to be finalized with https://issues.modelix.org/issue/MODELIX-500.
164+
*/
165+
""".trimIndent()
166+
typescriptDeclaration.writer().use {
167+
it.appendLine("""import { INodeJS } from "@modelix/ts-model-api";""")
168+
.appendLine()
169+
for (line in originalTypescriptDeclarationContent) {
170+
// Only mark the parts of the client (`org.modelix.model.client2`) experimental.
171+
// Reported declarations from `org.modelix.model.api` should not be annotated as experimental.
172+
if (line.startsWith("export declare namespace org.modelix.model.client2")) {
173+
it.appendLine(experimentalDeclaration)
174+
}
175+
it.appendLine(line)
176+
}
177+
}
178+
}
179+
}
180+
181+
npmPublish {
182+
registries {
183+
register("itemis-npm-open") {
184+
uri.set("https://artifacts.itemis.cloud/repository/npm-open")
185+
System.getenv("NODE_AUTH_TOKEN").takeIf { !it.isNullOrBlank() }?.let {
186+
authToken.set(it)
187+
}
188+
}
189+
}
190+
packages {
191+
named("js") {
192+
files {
193+
// The files need to be set manually because we patch
194+
// the JS/TS produces by `compileProductionLibraryKotlinJs`
195+
// with the `patchTypesScriptInProductionLibrary` task
196+
setFrom(patchTypesScriptInProductionLibrary)
197+
}
198+
packageJson {
199+
name.set("@modelix/model-client")
200+
homepage.set("https://modelix.org/")
201+
repository {
202+
type.set("git")
203+
url.set("https://github.com/modelix/modelix.core.git")
204+
directory.set(project.name)
205+
}
206+
}
207+
dependencies {
208+
// The model client NPM package uses the types from this @modelix/ts-model-api
209+
normal("@modelix/ts-model-api", rootProject.version.toString())
210+
}
211+
}
212+
}
213+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2023.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.model.client2
18+
19+
import INodeJS
20+
import org.modelix.kotlin.utils.UnstableModelixFeature
21+
22+
@UnstableModelixFeature(
23+
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
24+
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
25+
)
26+
@JsExport
27+
sealed interface ChangeJS {
28+
val node: INodeJS
29+
}
30+
31+
@UnstableModelixFeature(
32+
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
33+
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
34+
)
35+
@JsExport
36+
data class PropertyChanged(override val node: INodeJS, val role: String) : ChangeJS
37+
38+
@UnstableModelixFeature(
39+
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
40+
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
41+
)
42+
@JsExport
43+
data class ChildrenChanged(override val node: INodeJS, val role: String?) : ChangeJS
44+
45+
@UnstableModelixFeature(
46+
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
47+
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
48+
)
49+
@JsExport
50+
data class ReferenceChanged(override val node: INodeJS, val role: String) : ChangeJS
51+
52+
@UnstableModelixFeature(
53+
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
54+
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
55+
)
56+
@JsExport
57+
data class ContainmentChanged(override val node: INodeJS) : ChangeJS

0 commit comments

Comments
 (0)