Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tschuehly committed Apr 6, 2024
1 parent f29dc7f commit f9508b5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@ class ViewComponentAutoConfiguration {
@Configuration
@ConditionalOnProperty("spring.view-component.local-development")
class LocalDevConfig(
private val classPathRestartStrategy: ClassPathRestartStrategy,
private val eventPublisher: ApplicationEventPublisher
private val classPathRestartStrategy: ClassPathRestartStrategy
) {
val gradleKotlinBuildDir = "build/classes/kotlin/main/"
val javaMavenBuildDir = "target/classes/"
private val logger = LoggerFactory.getLogger(LocalDevConfig::class.java)

@Bean
fun viewComponentFileSystemWatcher(
applicationContext: ApplicationContext,
fileSystemWatcherFactory: FileSystemWatcherFactory,
restartStrategy: ClassPathRestartStrategy,
applicationEventPublisher: ApplicationEventPublisher
): ClassPathFileSystemWatcher{
applicationEventPublisher: ApplicationEventPublisher
): ClassPathFileSystemWatcher {
val urls = Restarter.getInstance().initialUrls ?: arrayOf()
val watcher = ViewComponentFileSystemWatcher(
applicationContext,
Expand All @@ -47,43 +42,5 @@ class ViewComponentAutoConfiguration {
watcher.setStopWatcherOnRestart(true)
return watcher
}

// @Bean
// fun viewComponentFileSystemWatcher(applicationContext: ApplicationContext): FileSystemWatcher {
// val fileSystemWatcher = FileSystemWatcher(true, Duration.ofMillis(300), Duration.ofMillis(200))
// val classPath =
// applicationContext.getBeansWithAnnotation(SpringBootApplication::class.java)
// .values.first().javaClass.protectionDomain.codeSource.location.path
// val (srcDir, buildType) = getSrcDir(classPath)
// logger.info("Registering fileSystemWatcher: ${srcDir.path}")
// fileSystemWatcher.addSourceDirectory(srcDir)
// fileSystemWatcher.addListener(
// ViewComponentChangeListener(
// applicationContext,
// buildType
// )
// )
// fileSystemWatcher.start()
// return fileSystemWatcher
// }

private fun getSrcDir(classPath: String): Pair<File, BuildType> {
if (classPath.endsWith(gradleKotlinBuildDir)) {
val srcDir = classPath.split(gradleKotlinBuildDir)[0] + "/src/main/kotlin"
val file = File(srcDir)
if (file.exists()) {
return file to BuildType.GRADLE
}
}
if (classPath.endsWith(javaMavenBuildDir)) {
val srcDir = classPath.split(javaMavenBuildDir)[0] + "src/main/java"
val file = File(srcDir)
if (file.exists()) {
return file to BuildType.MAVEN
}
}
throw ViewComponentProcessingException("No srcDir found", null)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class ViewComponentParser(
val classDir = getGeneratedSourcesDir(rootDir,language)
val file = compiler.generate(
rootDir = resourceDirPath.toAbsolutePath(),
names = srcFile.name,
classDirectory = listOf(
classDir.toAbsolutePath().toString()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,31 @@ import kotlin.io.path.exists
@SupportedAnnotationTypes("de.tschuehly.spring.viewcomponent.core.component.ViewComponent")
@SupportedSourceVersion(SourceVersion.RELEASE_17)
class ViewComponentProcessor : AbstractProcessor() {

private var rootDir: String? = null
private var buildType: BuildType? = null
override fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean {

for (annotation in annotations) {
for (element in roundEnv.getElementsAnnotatedWith(annotation)) {
val messager = processingEnv.messager
val filer = processingEnv.filer
val (rootDir, buildType) = processingEnv.getRootDirAndBuildType()

if(rootDir == null || buildType == null){
val rootDirAndBuildType = processingEnv.getRootDirAndBuildType()
rootDir = rootDirAndBuildType.first
buildType = rootDirAndBuildType.second
assert(rootDir != null || buildType != null)
}
val separator = FileSystems.getDefault().separator
val packagePath = "${element.enclosingElement}".replace(".", separator)
val srcDir = getSrcDir(rootDir, messager)
val srcDir = getSrcDir(rootDir!!, messager)
val viewComponentDir = FileSystems.getDefault().getPath(srcDir.toString(), packagePath)
val srcHtmlFile = getSrcHtmlFile(viewComponentDir, element.simpleName, messager)
val methodList = getViewActionMethods(element)

val viewComponentName = element.simpleName.toString().lowercase()
verifyRenderMethodExists(element, messager)

val viewComponentParser = ViewComponentParser(
srcHtmlFile,
buildType = buildType,
buildType = buildType!!,
methodList = methodList,
viewComponentName = viewComponentName,
messager = messager,
Expand Down Expand Up @@ -84,18 +88,12 @@ class ViewComponentProcessor : AbstractProcessor() {

private fun ProcessingEnvironment.getJavaRootDir(): String {
try {
val fileName = "gen_${Date().toInstant().toEpochMilli()}"
val fileName = "rootDirFind_${Date().toInstant().toEpochMilli()}"
val sourceFile = this.filer.createSourceFile(fileName)

sourceFile.openWriter().use {
it.close()
}


return Paths.get(sourceFile.toUri()).let {
// it.deleteExisting() TODO: Cannot delete
it.toString()
}
return Paths.get(sourceFile.toUri()).toString()
} catch (e: IOException) {
processingEnv.messager.printMessage(Diagnostic.Kind.WARNING, "Unable to determine source file path!")
}
Expand Down Expand Up @@ -123,14 +121,6 @@ class ViewComponentProcessor : AbstractProcessor() {

}

private fun verifyRenderMethodExists(element: Element, messager: Messager) {
element.enclosedElements
.filter { it.kind == ElementKind.METHOD }
// .filter { (it.asType() as Type.MethodType).restype != null }
.map { it.asType().toString().replace("()","") }

}

private fun getSrcHtmlFile(srcDirPath: Path, viewComponentName: Name, messager: Messager): File {
val fileEndings = listOf(".html", ".jte", ".kte", ".th")
val testedFiles = mutableListOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.nio.file.Path


class JteViewComponentCompiler() {
fun generate(rootDir: Path, names: String, classDirectory: List<String>,packageName: String): String {
fun generate(rootDir: Path, classDirectory: List<String>,packageName: String): String {
val config = TemplateConfig(
ContentType.Html,
Constants.PACKAGE_NAME_PRECOMPILED + packageName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ class ThymeleafViewComponentTagProcessor(dialectPrefix: String) :
val appCtx = SpringContextUtils.getApplicationContext(webContext)
val engine = appCtx.getBean(SpringTemplateEngine::class.java)

val context = Context()
context.setVariable(
viewContext.javaClass.simpleName.replaceFirstChar { it.lowercase() },
viewContext
)
var sp = SpringContextUtils.getRequestContext(webContext)
val context2 = SpringContextUtils.getRequestContext(webContext)
context2.model[viewContext.javaClass.simpleName.replaceFirstChar { it.lowercase() }] = viewContext

webContext.setVariable(viewContext.javaClass.simpleName.replaceFirstChar { it.lowercase() }, viewContext)

// TODO: Cannot process attribute '{th:field,data-th-field}': no associated BindStatus could be found for the intended form binding operations. This can be due to the lack of a proper management of the Spring RequestContext, which is usually done through the ThymeleafView or ThymeleafReactiveView (template:
Expand Down

0 comments on commit f9508b5

Please sign in to comment.