Skip to content

Commit

Permalink
feat: project, modules and source sets
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet committed Jun 23, 2024
1 parent ebb191d commit f5239b2
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package software.guimauve.kode.usecases

import software.guimauve.kode.dsl.KodeGenerator
import software.guimauve.kode.kotlin.KotlinVisitor
import software.guimauve.kode.visitors.KotlinVisitor

class GenerateKotlinUseCase : IGenerateCodeUseCase {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package software.guimauve.kode.kotlin
package software.guimauve.kode.visitors

import software.guimauve.kode.dsl.*

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package software.guimauve.kode.visitors

import software.guimauve.kode.dsl.project
import software.guimauve.kode.dsl.type
import software.guimauve.kode.dsl.unaryPlus
import software.guimauve.kode.usecases.GenerateFolderUseCase
import software.guimauve.kode.usecases.GenerateKotlinUseCase
import kotlin.test.Test

class KotlinVisitorTest {

@Test
fun testModule() {
val basePackage = "com.example"

val proj = project {
module("shared") {
sourceSet("commonMain") {
directorySet("kotlin") {
`package`(basePackage) {
file("Application.kt") {
import("$basePackage.plugins")
import("io.ktor.server.application.*")
import("io.ktor.server.netty.*")

function("main") {
argument("args", type("Array") {
parameter(type("String"))
})
call("EngineMain.main") {
parameters(+"args")
}
}

function("Application.module") {
call("configureI18n")
// ...
}
}
`package`("plugins") {
file("I18n.kt") {
import("dev.kaccelero.plugins.I18n")
import("io.ktor.server.application.*")
import("java.util.*")

function("Application.configureI18n") {
call("install") {
parameter(+"I18n")
}
}
}
}
}
}
}
}
}
val folder = GenerateFolderUseCase(GenerateKotlinUseCase()).invoke(proj)
println(folder)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ fun String.shift(spaces: Int) =

operator fun String.unaryPlus() = Text(this)

fun project(init: Project.() -> Unit = {}) =
Project().apply(init)

fun type(name: String, init: Type.() -> Unit = {}) =
Type(name).apply(init)

fun `package`(name: String, init: Package.() -> Unit = {}) =
Package(name).apply(init)

fun file(init: File.() -> Unit) =
File().apply(init)
11 changes: 8 additions & 3 deletions kode/src/commonMain/kotlin/software/guimauve/kode/dsl/File.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package software.guimauve.kode.dsl

@KodeDsl
class File internal constructor() : KodeGenerator, DeclarationContainer {
class File internal constructor() : KodeGenerator, DeclarationContainer, FileOrDirectory {

var `package` = ""
val imports = mutableListOf<String>()
Expand All @@ -11,8 +11,13 @@ class File internal constructor() : KodeGenerator, DeclarationContainer {
`package` = name
}

fun import(import: String) = imports.add(import)
fun imports(vararg imports: String) = this.imports.addAll(imports)
fun import(import: String) {
imports.add(import)
}

fun imports(vararg imports: String) {
this.imports.addAll(imports)
}

override fun declaration(declaration: Declaration) {
declarations.add(declaration)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package software.guimauve.kode.dsl

sealed interface FileOrDirectory
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package software.guimauve.kode.dsl

data class Folder(
val content: Map<String, FolderContent>,
) : FolderContent
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package software.guimauve.kode.dsl

sealed interface FolderContent
14 changes: 14 additions & 0 deletions kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Module.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package software.guimauve.kode.dsl

@KodeDsl
class Module internal constructor(
val name: String,
) : FileOrDirectory {

val sourceSets = mutableListOf<SourceSet>()

fun sourceSet(name: String, block: SourceSet.() -> Unit = {}) {
sourceSets.add(SourceSet(name).apply(block))
}

}
15 changes: 11 additions & 4 deletions kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Package.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ package software.guimauve.kode.dsl
@KodeDsl
class Package internal constructor(
val name: String,
) {
) : FileOrDirectory {

val packages = mutableListOf<Package>()
val files = mutableMapOf<String, File>()
val textFiles = mutableMapOf<String, TextFile>()

fun `package`(name: String, init: Package.() -> Unit = {}) =
fun `package`(name: String, init: Package.() -> Unit = {}) {
packages.add(Package("${this.name}.$name").apply(init))
}

fun file(name: String, init: File.() -> Unit = {}) =
files.put(name, File().apply(init).apply { `package`(this@Package.name) })
fun file(name: String, init: File.() -> Unit = {}) {
files[name] = File().apply(init).apply { `package`(this@Package.name) }
}

fun textFile(name: String, content: String) {
textFiles[name] = TextFile(content)
}

}
17 changes: 17 additions & 0 deletions kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Project.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package software.guimauve.kode.dsl

@KodeDsl
class Project : FileOrDirectory {

val modules = mutableListOf<Module>()
val textFiles = mutableMapOf<String, TextFile>()

fun module(name: String, block: Module.() -> Unit = {}) {
modules.add(Module(name).apply(block))
}

fun textFile(name: String, content: String) {
textFiles[name] = TextFile(content)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package software.guimauve.kode.dsl

@KodeDsl
class SourceDirectorySet internal constructor(
val name: String,
) : FileOrDirectory {

val packages = mutableListOf<Package>()
val files = mutableMapOf<String, File>()
val textFiles = mutableMapOf<String, TextFile>()

fun `package`(name: String, init: Package.() -> Unit = {}) {
packages.add(Package(name).apply(init))
}

fun file(name: String, init: File.() -> Unit = {}) {
files[name] = File().apply(init)
}

fun textFile(name: String, content: String) {
textFiles[name] = TextFile(content)
}

}
14 changes: 14 additions & 0 deletions kode/src/commonMain/kotlin/software/guimauve/kode/dsl/SourceSet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package software.guimauve.kode.dsl

@KodeDsl
class SourceSet internal constructor(
val name: String,
) : FileOrDirectory {

val directorySets = mutableListOf<SourceDirectorySet>()

fun directorySet(name: String, block: SourceDirectorySet.() -> Unit = {}) {
directorySets.add(SourceDirectorySet(name).apply(block))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package software.guimauve.kode.dsl

@KodeDsl
data class TextFile internal constructor(
val content: String,
) : FolderContent, FileOrDirectory
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package software.guimauve.kode.usecases

import software.guimauve.kode.dsl.*

class GenerateFolderUseCase(
private val generateCodeUseCase: IGenerateCodeUseCase,
) : IGenerateFolderUseCase {

override fun invoke(input: FileOrDirectory): FolderContent = when (input) {
is Project -> Folder(input.modules.associate { it.name to invoke(it) } +
input.textFiles.map { it.key to invoke(it.value) }.associate { it })

is Module -> Folder(input.sourceSets.associate { it.name to invoke(it) })
is SourceSet -> Folder(input.directorySets.associate { it.name to invoke(it) })
is SourceDirectorySet -> folderFor(input.packages, input.files, input.textFiles)
is Package -> folderFor(input.packages, input.files, input.textFiles)
is File -> TextFile(generateCodeUseCase(input))
is TextFile -> input
}

// TODO: Fix recursive package names
private fun folderFor(packages: List<Package>, files: Map<String, File>, textFiles: Map<String, TextFile>) = Folder(
packages.associate { it.name to invoke(it) }
+ files.map { it.key to invoke(it.value) }.associate { it }
+ textFiles.map { it.key to it.value }.associate { it }
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package software.guimauve.kode.usecases

import dev.kaccelero.usecases.IUseCase
import software.guimauve.kode.dsl.FileOrDirectory
import software.guimauve.kode.dsl.FolderContent

interface IGenerateFolderUseCase : IUseCase<FileOrDirectory, FolderContent>

0 comments on commit f5239b2

Please sign in to comment.