Skip to content

Commit

Permalink
compiler throws error that dependent class is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Schuehly committed Aug 29, 2023
1 parent d848a25 commit c45bc99
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.tschuehly.spring.viewcomponent.core.processor

import java.nio.file.Path

fun interface ViewComponentCompiler {
fun compile(rootDir: String, names: String): String
fun compile(rootDir: Path, names: String, classDirectory: Path): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.tschuehly.spring.viewcomponent.core.processor
import de.tschuehly.spring.viewcomponent.core.action.*
import java.io.File
import java.nio.file.FileSystems
import java.nio.file.Path
import javax.annotation.processing.Messager
import javax.tools.Diagnostic
import kotlin.io.path.createDirectories
Expand All @@ -20,30 +21,59 @@ class ViewComponentParser(
MAVEN, GRADLE
}

fun parseFile(
) {
fun parseFile() {
val parsedHtml = parseSrcHtmlFile()
val (rootDir, packagePath) = getRootDirAndPackagePath(srcFile, messager)
val resourceDirPath = getResourceDirPath(rootDir, packagePath)
val resourceHtmlFile = getResourceFile(resourceDirPath)
resourceHtmlFile.writeAll(parsedHtml)

try {
val clazz = Class.forName("de.tschuehly.spring.viewcomponent.jte.JteViewComponentCompiler")
val compiler = (clazz.getConstructor().newInstance() as ViewComponentCompiler)

compiler.compile("$rootDir${FileSystems.getDefault().separator}$packagePath", srcFile.name)
}catch (e: ClassNotFoundException){
messager?.printMessage(Diagnostic.Kind.NOTE,"If you don't use JTE this message can be ignored: JteViewComponentCompiler not found")
val classDir = getClassDirPath(rootDir)
compiler.compile(
rootDir = resourceDirPath.toAbsolutePath(),
names = srcFile.name,
classDirectory = classDir
)
} catch (e: ClassNotFoundException) {
messager?.printMessage(
Diagnostic.Kind.NOTE,
"If you don't use JTE this message can be ignored: JteViewComponentCompiler not found"
)
}
val resourceHtmlFile = getResourceFile(rootDir,packagePath)
writeFile(resourceHtmlFile)
}

private fun getResourceFile(rootDir: String, packagePath: String): File {
val resourceDir = if (buildType == BuildType.GRADLE) {
FileSystems.getDefault()
.getPath(rootDir, "build", "resources", "main", packagePath)
} else {
FileSystems.getDefault()
.getPath(rootDir, "target", "classes", packagePath)
private fun File.writeAll(
parsedHtml: List<String>
) {
val writer = this.printWriter()
parsedHtml.forEach {
writer.println(it)
}
writer.flush()
writer.close()
}

private fun getResourceDirPath(rootDir: String, packagePath: String): Path = if (buildType == BuildType.GRADLE) {
FileSystems.getDefault()
.getPath(rootDir, "build", "resources", "main", packagePath)
} else {
FileSystems.getDefault()
.getPath(rootDir, "target", "classes", packagePath)
}

private fun getClassDirPath(rootDir: String): Path = if (buildType == BuildType.GRADLE) {
FileSystems.getDefault()
.getPath(rootDir, "build", "generated")
} else {
FileSystems.getDefault()
.getPath(rootDir, "target", "classes")
}

private fun getResourceFile(resourceDir: Path): File {
resourceDir.createDirectories()
val resourceHtmlFile = resourceDir.resolve(srcFile.name).toFile()
if (resourceHtmlFile.exists()) {
Expand Down Expand Up @@ -103,13 +133,6 @@ class ViewComponentParser(
return "$beforeViewAction$htmxAttr\"$htmxAttrVal\" hx-target=\"#$viewComponentName\"$afterViewActionAttrVal"
}

fun writeFile(
resourceHtmlFile: File,
) {
resourceHtmlFile.printWriter().use {

}
}

private fun getHtmxAttribute(
viewComponentName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class JteViewComponentAutoConfiguration(
}

@Bean
fun jteTemplateEngine(): TemplateEngine? {
return TemplateEngine.createPrecompiled(ContentType.Html);
fun templateEngine(): TemplateEngine{
return TemplateEngine.createPrecompiled(ContentType.Html)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import de.tschuehly.spring.viewcomponent.core.processor.ViewComponentCompiler
import gg.jte.ContentType
import gg.jte.TemplateConfig
import gg.jte.compiler.TemplateCompiler
import gg.jte.resolve.ResourceCodeResolver
import gg.jte.resolve.DirectoryCodeResolver
import gg.jte.runtime.Constants
import java.nio.file.Path

class JteViewComponentCompiler() : ViewComponentCompiler {
override fun compile(rootDir: String, names: String): String {
override fun compile(rootDir: Path, names: String, classDirectory: Path): String {
val compiler = TemplateCompiler(
/* config = */ TemplateConfig(
ContentType.Html,
Constants.PACKAGE_NAME_PRECOMPILED
),
/* codeResolver = */ ResourceCodeResolver(rootDir),
/* classDirectory = */ null,
/* parentClassLoader = */ null
/* codeResolver = */ DirectoryCodeResolver(rootDir),
/* classDirectory = */ classDirectory,
/* parentClassLoader = */ this.javaClass.classLoader
)
return compiler.precompile(listOf(names)).first()
}
Expand Down
2 changes: 1 addition & 1 deletion jte/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@

gg.jte.usePrecompiledTemplates=false

0 comments on commit c45bc99

Please sign in to comment.