Skip to content

Commit fd62e53

Browse files
author
Oleksandr Dzhychko
authored
Merge pull request #820 from drik98/feature/add-optional-typescript-barrels
feat(model-api-gen): allow generating typescript barrels
2 parents 6955d18 + 5d8da9e commit fd62e53

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

docs/global/modules/core/pages/reference/component-model-api-gen-gradle.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ Inside of the `metamodel` block the following settings can be configured.
101101
|File
102102
|Target TypeScript directory of the generator
103103

104+
|`includeTypescriptBarrels`
105+
|Boolean
106+
|Add barrelling to the generated index file for convenience.
107+
Only enable when you are completely sure there are no naming conflicts among the concepts of all generated languages.
108+
104109
|`registrationHelperName`
105110
|String
106111
|Fully qualified name of the generated language registration helper

model-api-gen-gradle/src/main/kotlin/org/modelix/metamodel/gradle/GenerateMetaModelSources.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ abstract class GenerateMetaModelSources @Inject constructor(of: ObjectFactory) :
3939
@Optional
4040
val typescriptOutputDir: DirectoryProperty = of.directoryProperty()
4141

42+
@get:Input
43+
val includeTypescriptBarrels: Property<Boolean> = of.property(Boolean::class.java)
44+
4245
@get:Input
4346
val includedNamespaces: ListProperty<String> = of.listProperty(String::class.java)
4447

@@ -114,7 +117,7 @@ abstract class GenerateMetaModelSources @Inject constructor(of: ObjectFactory) :
114117

115118
val typescriptOutputDir = this.typescriptOutputDir.orNull?.asFile
116119
if (typescriptOutputDir != null) {
117-
val tsGenerator = TypescriptMMGenerator(typescriptOutputDir.toPath(), nameConfig.get())
120+
val tsGenerator = TypescriptMMGenerator(typescriptOutputDir.toPath(), nameConfig.get(), includeTypescriptBarrels.get())
118121
tsGenerator.generate(processedLanguages)
119122
}
120123
}

model-api-gen-gradle/src/main/kotlin/org/modelix/metamodel/gradle/MetaModelGradlePlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class MetaModelGradlePlugin @Inject constructor(val project: Project) : Plugin<P
7676
settings.kotlinDir?.let { task.kotlinOutputDir.set(it) }
7777
settings.modelqlKotlinDir?.let { task.modelqlKotlinOutputDir.set(it) }
7878
settings.typescriptDir?.let { task.typescriptOutputDir.set(it) }
79+
task.includeTypescriptBarrels.set(settings.includeTypescriptBarrels)
7980
task.includedNamespaces.addAll(settings.includedLanguageNamespaces)
8081
task.includedLanguages.addAll(settings.includedLanguages)
8182
task.includedConcepts.addAll(settings.includedConcepts)

model-api-gen-gradle/src/main/kotlin/org/modelix/metamodel/gradle/MetaModelGradleSettings.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ open class MetaModelGradleSettings {
2929
field = value
3030
}
3131
var typescriptDir: File? = null
32+
var includeTypescriptBarrels: Boolean = false
3233
var registrationHelperName: String? = null
3334
var conceptPropertiesInterfaceName: String? = null
3435
val taskDependencies: MutableList<Any> = ArrayList()

model-api-gen/src/main/kotlin/org/modelix/metamodel/generator/TypescriptMMGenerator.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.nio.file.Files
88
import java.nio.file.Path
99
import kotlin.io.path.writeText
1010

11-
class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = NameConfig()) {
11+
class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = NameConfig(), private val includeTsBarrels: Boolean = false) {
1212

1313
private fun LanguageData.packageDir(): Path {
1414
val packageName = name
@@ -33,7 +33,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
3333
.resolve(language.generatedClassName().simpleName + ".ts")
3434
.fixFormatAndWriteText(generateLanguage(language))
3535

36-
generateRegistry(languages)
36+
generateIndexTs(languages)
3737
}
3838
}
3939

@@ -54,7 +54,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
5454

5555
private fun Path.fixFormatAndWriteText(text: String) = writeText(fixFormat(text))
5656

57-
private fun generateRegistry(languages: ProcessedLanguageSet) {
57+
private fun generateIndexTs(languages: ProcessedLanguageSet) {
5858
outputDir.resolve("index.ts").fixFormatAndWriteText(
5959
"""
6060
import { LanguageRegistry } from "@modelix/ts-model-api";
@@ -68,6 +68,13 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
6868
"""
6969
}}
7070
}
71+
${if (!includeTsBarrels) {
72+
""
73+
} else {
74+
languages.getLanguages().joinToString("\n") {
75+
"""export * from "./${it.simpleClassName()}";"""
76+
}
77+
}}
7178
""",
7279
)
7380
}

model-api-gen/src/test/kotlin/org/modelix/metamodel/generator/KotlinGeneratorTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlin.io.path.deleteRecursively
1010
import kotlin.io.path.readText
1111
import kotlin.test.Test
1212
import kotlin.test.assertContains
13+
import kotlin.test.assertFalse
1314

1415
class KotlinGeneratorTest {
1516

@@ -81,6 +82,50 @@ class KotlinGeneratorTest {
8182
// val outputDir = File(".").toPath().resolve("build").resolve("test-generator-output")
8283
val outputDir = File("build/test-generator-output").toPath()
8384
TypescriptMMGenerator(outputDir).generate(LanguageSet(listOf(language)).process())
85+
86+
val indexFileContents = outputDir.resolve("index.ts").readText()
87+
assertFalse(indexFileContents.contains("export * from"), "Does not include barrels when not explicitly opted in.")
88+
}
89+
90+
@Test
91+
fun test_ts_with_barrels() {
92+
val input = """
93+
name: org.modelix.entities
94+
concepts:
95+
- name: Entity
96+
properties:
97+
- name: name
98+
children:
99+
- name: properties
100+
type: Property
101+
multiple: true
102+
optional: true
103+
- name: Property
104+
children:
105+
- name: type
106+
type: Type
107+
optional: false
108+
- name: Type
109+
- name: EntityType
110+
extends:
111+
- Type
112+
references:
113+
- name: entity
114+
type: Entity
115+
optional: false
116+
enums: []
117+
""".trimIndent()
118+
119+
val language = Yaml.default.decodeFromString<LanguageData>(input)
120+
val outputDir = File("build/test-generator-output").toPath()
121+
TypescriptMMGenerator(outputDir, NameConfig(), true).generate(LanguageSet(listOf(language)).process())
122+
123+
val indexFileContents = outputDir.resolve("index.ts").readText()
124+
assertContains(
125+
indexFileContents,
126+
"""export * from "./L_org_modelix_entities";""",
127+
message = "Includes barrels when explicitly opted in.",
128+
)
84129
}
85130

86131
@OptIn(ExperimentalPathApi::class)

0 commit comments

Comments
 (0)