Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Jul 5, 2023
1 parent 01263ca commit c8852f6
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 93 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ allprojects {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/"}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ fun jsonArray(values: Iterator<Any?>): JsonArray {
}

private fun Any?.toJsonElement(): JsonElement {
if (this == null)
if (this == null) {
return JsonNull.INSTANCE
}

return when (this) {
is JsonElement -> this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LionWebGradlePlugin : Plugin<Project> {
createGenLanguagesTask(project, configuration)
}

private fun createGenASTClassesTask(project: Project, configuration: LionWebGradleExtension) : Task {
private fun createGenASTClassesTask(project: Project, configuration: LionWebGradleExtension): Task {
return project.tasks.create(genASTClasses) {
it.group = tasksGroup
it.doLast {
Expand All @@ -45,9 +45,14 @@ class LionWebGradlePlugin : Plugin<Project> {
val jsonser = JsonSerialization.getStandardSerialization()
jsonser.nodeResolver.addTree(StarLasuLWLanguage)
val language = jsonser.unserializeToNodes(FileInputStream(languageFile)).first() as Language
val existingKotlinClasses = KotlinCodeProcessor().classesDeclaredInDir(project.file("src/main/kotlin"))

val ktFiles = ASTGenerator(configuration.importPackageNames.get()[language.name] ?: language.name, language)
val existingKotlinClasses = KotlinCodeProcessor().classesDeclaredInDir(
project.file("src/main/kotlin")
)

val ktFiles = ASTGenerator(
configuration.importPackageNames.get()[language.name] ?: language.name,
language
)
.generateClasses(existingKotlinClasses)
ktFiles.forEach { ktFile ->
val file = File(configuration.outdir.get(), ktFile.path)
Expand All @@ -57,14 +62,13 @@ class LionWebGradlePlugin : Plugin<Project> {
}
}
}

}
println("LIonWeb AST Classes generation task - completed")
}
}
}

private fun createGenLanguagesTask(project: Project, configuration: LionWebGradleExtension) : Task {
private fun createGenLanguagesTask(project: Project, configuration: LionWebGradleExtension): Task {
return project.tasks.create(genLanguages) { it ->
it.group = tasksGroup
it.dependsOn("compileKotlin")
Expand All @@ -73,7 +77,7 @@ class LionWebGradlePlugin : Plugin<Project> {
configuration.exportPackages.get().forEach { packageName ->
project.javaexec { jes ->
jes.classpath = project.sourceSets.getByName("main").runtimeClasspath
jes.mainClass.set("${packageName}.LanguageKt")
jes.mainClass.set("$packageName.LanguageKt")
jes.args = mutableListOf(lionwebLanguageFile(project, packageName).absolutePath)
}
}
Expand All @@ -84,7 +88,7 @@ class LionWebGradlePlugin : Plugin<Project> {
/**
* Prepare the plugin configuration setting in place default values.
*/
private fun prepareConfiguration(project: Project) : LionWebGradleExtension {
private fun prepareConfiguration(project: Project): LionWebGradleExtension {
val configuration = project.extensions.create("lionweb", LionWebGradleExtension::class.java)
configuration.outdir.convention(File(project.buildDir, "lionweb-gen"))
val srcMainLionweb = project.file("src${File.separator}main${File.separator}lionweb")
Expand All @@ -98,7 +102,7 @@ class LionWebGradlePlugin : Plugin<Project> {
return configuration
}

private fun lionwebLanguageFile(project: Project, packageName: String) : File {
private fun lionwebLanguageFile(project: Project, packageName: String): File {
return File(project.buildDir, "lionwebgen${File.separator}$packageName.json")
}

Expand Down Expand Up @@ -133,16 +137,20 @@ class LionWebGradlePlugin : Plugin<Project> {
}

project.tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).forEach {
it.source(File(project.buildDir, "lionweb-gen"),
File(project.rootDir, "src${File.separator}main${File.separator}kotlin"))
it.source(
File(project.buildDir, "lionweb-gen"),
File(project.rootDir, "src${File.separator}main${File.separator}kotlin")
)
it.dependsOn(genASTClasses)
}
}

private fun addDependencies(project: Project) {
fun addKolasuModule(moduleName: String) {
project.dependencies.add("implementation",
"com.strumenta.kolasu:kolasu-$moduleName:${project.kolasuVersion}")
project.dependencies.add(
"implementation",
"com.strumenta.kolasu:kolasu-$moduleName:${project.kolasuVersion}"
)
}

addKolasuModule("core")
Expand All @@ -153,7 +161,9 @@ class LionWebGradlePlugin : Plugin<Project> {
project.dependencies.add("implementation", "com.github.ajalt.clikt:clikt:3.5.0")

// We need to use this one to avoid an issue with Gson
project.dependencies.add("implementation", "io.lionweb.lioncore-java:lioncore-java-core-fat:${project.lionwebVersion}")
project.dependencies.add(
"implementation",
"io.lionweb.lioncore-java:lioncore-java-core-fat:${project.lionwebVersion}"
)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ val Project.kolasuVersion
get() = project.propertyValue("kolasuVersion", BuildConfig.PLUGIN_VERSION)

val Project.lionwebVersion
get() = project.propertyValue("lionwebVersion", "0.0.18")
get() = project.propertyValue("lionwebVersion", "0.0.18")
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.File
import java.nio.file.Files
import kotlin.test.Ignore


class LionWebGradlePluginTest {
var projectDir: File? = null
Expand Down Expand Up @@ -60,5 +58,4 @@ class LionWebGradlePluginTest {
runner.withProjectDir(projectDir)
val result = runner.build()
}

}
}
5 changes: 4 additions & 1 deletion lionweb-gen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlin_version")
}

def isReleaseVersion = !(version as String).endsWith("SNAPSHOT")


publishing {

repositories {
Expand All @@ -60,7 +63,7 @@ publishing {
from components.java
artifactId "kolasu-" + project.name
artifact sourcesJar
artifact javadocJar
artifact kdocJar
suppressPomMetadataWarningsFor('cliApiElements')
suppressPomMetadataWarningsFor('cliRuntimeElements')
pom {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class KotlinCodeProcessor {
val disposable = Disposer.newDisposable()
try {
val env = KotlinCoreEnvironment.createForProduction(
disposable, CompilerConfiguration(), EnvironmentConfigFiles.JVM_CONFIG_FILES
disposable,
CompilerConfiguration(),
EnvironmentConfigFiles.JVM_CONFIG_FILES
)
val file = LightVirtualFile("temp.kt", KotlinFileType.INSTANCE, code)
return PsiManager.getInstance(env.project).findFile(file) as KtFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public data class Property(
val existingClasses = KotlinCodeProcessor().classesDeclaredInFile(code)
assertEquals(
setOf(
"com.strumenta.props.PropertiesFile", "com.strumenta.props.SomeOtherClass",
"com.strumenta.props.PropertiesFile",
"com.strumenta.props.SomeOtherClass",
"com.strumenta.props.Property"
),
existingClasses
Expand Down
2 changes: 2 additions & 0 deletions lionweb-ksp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies {
implementation(project(":core"))
}

def isReleaseVersion = !(version as String).endsWith("SNAPSHOT")

publishing {

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ import java.lang.IllegalStateException

class KolasuSymbolProcessor(val environment: SymbolProcessorEnvironment) : SymbolProcessor {

var generated : Boolean = false
var generated: Boolean = false

class LanguageDef(val packageName: String) {
val classes = mutableListOf<KSClassDeclaration>()

fun dependencies() : Dependencies {
fun dependencies(): Dependencies {
val usedFiles = this.classes.mapNotNull { it.containingFile }.toSet().toTypedArray()
val dependencies = Dependencies(true, *usedFiles)
return dependencies
}

fun write(os: OutputStream) {
val buf = PrintWriter(os)
buf.println("""
buf.println(
"""
// ${classes.first().containingFile?.filePath}
// ${classes.first().containingFile?.origin}
package $packageName
Expand All @@ -50,7 +51,8 @@ class KolasuSymbolProcessor(val environment: SymbolProcessorEnvironment) : Symbo
fun main(args: Array<String>) {
LanguageGeneratorCommand(lwLanguage).main(args)
}
""".trimIndent())
""".trimIndent()
)
buf.flush()
}
}
Expand All @@ -61,7 +63,6 @@ class KolasuSymbolProcessor(val environment: SymbolProcessorEnvironment) : Symbo
}
val exportPackagesStr = kspFile.readText().trim().split("=")[1]


if (exportPackagesStr.isNullOrBlank()) {
// No packages to export, we are done here
return emptyList()
Expand All @@ -73,7 +74,9 @@ class KolasuSymbolProcessor(val environment: SymbolProcessorEnvironment) : Symbo
val packageName = ksFile.packageName.asString()
ksFile.declarations.forEach { ksDeclaration ->
if (ksDeclaration is KSClassDeclaration) {
val isNodeDecl = ksDeclaration.getAllSuperTypes().any { it.declaration.qualifiedName?.asString() == Node::class.qualifiedName}
val isNodeDecl = ksDeclaration.getAllSuperTypes().any {
it.declaration.qualifiedName?.asString() == Node::class.qualifiedName
}
if (isNodeDecl) {
languagesByPackage.computeIfAbsent(packageName) {
LanguageDef(packageName)
Expand All @@ -85,12 +88,18 @@ class KolasuSymbolProcessor(val environment: SymbolProcessorEnvironment) : Symbo

if (!generated) {
languagesByPackage.values.forEach { languageDef ->
languageDef.write(environment.codeGenerator.createNewFile(languageDef.dependencies(),
languageDef.packageName, "Language", "kt"))
languageDef.write(
environment.codeGenerator.createNewFile(
languageDef.dependencies(),
languageDef.packageName,
"Language",
"kt"
)
)
}

generated = true
}
return emptyList()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class KolasuSymbolProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return KolasuSymbolProcessor(environment)
}
}
}
Loading

0 comments on commit c8852f6

Please sign in to comment.