Fixes to groovy class loading mechanisms.
This commit is contained in:
parent
d32ac97caf
commit
31a6c79929
@ -15,7 +15,12 @@ import com.jessebrault.ssg.view.PageView
|
||||
import com.jessebrault.ssg.view.WvcPageView
|
||||
import groovy.transform.TupleConstructor
|
||||
import groowt.util.di.RegistryObjectFactory
|
||||
import groowt.view.component.compiler.DefaultComponentTemplateCompilerConfiguration
|
||||
import groowt.view.component.compiler.SimpleComponentTemplateClassFactory
|
||||
import groowt.view.component.compiler.source.ComponentTemplateSource
|
||||
import groowt.view.component.web.DefaultWebViewComponentContext
|
||||
import groowt.view.component.web.DefaultWebViewComponentScope
|
||||
import groowt.view.component.web.compiler.DefaultWebViewComponentTemplateCompileUnit
|
||||
import io.github.classgraph.ClassGraph
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
@ -142,7 +147,10 @@ class DefaultStaticSiteGenerator implements StaticSiteGenerator {
|
||||
pageSpec.name(),
|
||||
pageSpec.path(),
|
||||
pageSpec.fileExtension(),
|
||||
(Class<? extends PageView>) pageViewInfo.loadClass()
|
||||
(Class<? extends PageView>) pageViewInfo.loadClass(),
|
||||
!pageSpec.templateResource().empty
|
||||
? pageSpec.templateResource()
|
||||
: pageViewInfo.simpleName + 'Template.wvc'
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -172,6 +180,9 @@ class DefaultStaticSiteGenerator implements StaticSiteGenerator {
|
||||
}
|
||||
|
||||
def diagnostics = [] as Collection<Diagnostic>
|
||||
def wvcCompilerConfiguration = new DefaultComponentTemplateCompilerConfiguration()
|
||||
wvcCompilerConfiguration.groovyClassLoader = this.groovyClassLoader
|
||||
def componentTemplateClassFactory = new SimpleComponentTemplateClassFactory(this.groovyClassLoader)
|
||||
|
||||
pages.each {
|
||||
// instantiate PageView
|
||||
@ -191,7 +202,33 @@ class DefaultStaticSiteGenerator implements StaticSiteGenerator {
|
||||
pageView.pageTitle = it.name
|
||||
pageView.url = buildSpec.baseUrl.get() + it.path
|
||||
if (pageView instanceof WvcPageView) {
|
||||
pageView.context = new DefaultWebViewComponentContext()
|
||||
pageView.context = new DefaultWebViewComponentContext().tap {
|
||||
configureRootScope {
|
||||
// TODO: scan components in same package, add them to the scope with factories which
|
||||
// use the object factory to construct the component
|
||||
}
|
||||
}
|
||||
|
||||
if (pageView.componentTemplate == null) {
|
||||
def templateUrl = pageView.class.getResource(it.templateResource)
|
||||
if (templateUrl == null) {
|
||||
diagnostics.add(new Diagnostic(
|
||||
"Could not find templateResource: $it.templateResource"
|
||||
))
|
||||
return
|
||||
}
|
||||
def source = ComponentTemplateSource.of(templateUrl)
|
||||
def compileUnit = new DefaultWebViewComponentTemplateCompileUnit(
|
||||
source.descriptiveName,
|
||||
pageView.class,
|
||||
source,
|
||||
pageView.class.packageName
|
||||
)
|
||||
def compileResult = compileUnit.compile(wvcCompilerConfiguration)
|
||||
def templateClass = componentTemplateClassFactory.getTemplateClass(compileResult)
|
||||
def componentTemplate = templateClass.getConstructor().newInstance()
|
||||
pageView.componentTemplate = componentTemplate
|
||||
}
|
||||
}
|
||||
|
||||
// Render the page
|
||||
|
@ -14,10 +14,11 @@ class DefaultPage implements Page {
|
||||
final String path
|
||||
final String fileExtension
|
||||
final Class<? extends PageView> viewType
|
||||
final String templateResource
|
||||
|
||||
@Override
|
||||
String toString() {
|
||||
"SimplePage(name: $name, path: $path, fileExtension: $fileExtension)"
|
||||
"SimplePage(name: $name, path: $path, fileExtension: $fileExtension, templateResource: $templateResource)"
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,4 +7,5 @@ interface Page {
|
||||
String getPath()
|
||||
String getFileExtension()
|
||||
Class<? extends PageView> getViewType()
|
||||
String getTemplateResource()
|
||||
}
|
||||
|
@ -10,5 +10,6 @@ import java.lang.annotation.Target
|
||||
@interface PageSpec {
|
||||
String name()
|
||||
String path()
|
||||
String templateResource() default ''
|
||||
String fileExtension() default '.html'
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ abstract class WvcPageView extends BaseWebViewComponent implements PageView, Wit
|
||||
String pageTitle
|
||||
String url
|
||||
|
||||
WvcPageView() {}
|
||||
|
||||
WvcPageView(ComponentTemplate template) {
|
||||
super(template)
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.jessebrault.site
|
||||
|
||||
import groowt.view.component.web.BaseWebViewComponent
|
||||
|
||||
class Head extends BaseWebViewComponent {
|
||||
|
||||
final String title
|
||||
|
||||
Head(Map attr) {
|
||||
super(Head.getResource('HeadTemplate.wvc'))
|
||||
this.title = attr.title()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
---
|
||||
package com.jessebrault.site
|
||||
---
|
||||
<head>
|
||||
<title>$title</title>
|
||||
</head>
|
@ -4,6 +4,7 @@ import com.jessebrault.ssg.di.InjectText
|
||||
import com.jessebrault.ssg.page.PageSpec
|
||||
import com.jessebrault.ssg.text.Text
|
||||
import com.jessebrault.ssg.view.WvcPageView
|
||||
import groowt.view.component.web.WebViewComponentScope
|
||||
import jakarta.inject.Inject
|
||||
|
||||
@PageSpec(name = 'Biography', path = '/biography')
|
||||
@ -15,8 +16,14 @@ class Biography extends WvcPageView {
|
||||
|
||||
@Inject
|
||||
Biography(@InjectText('/Biography.md') Text biographyText) {
|
||||
super(Biography.getResource('BiographyTemplate.wvc'))
|
||||
this.biographyText = biographyText
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void beforeRender() {
|
||||
context.configureRootScope(WebViewComponentScope) {
|
||||
addWithAttr(Head)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,7 @@
|
||||
package com.jessebrault.site
|
||||
---
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>$pageTitle</title>
|
||||
</head>
|
||||
<Head title={pageTitle} />
|
||||
<body>
|
||||
<h1>$pageTitle</h1>
|
||||
<h2>$url</h2>
|
||||
|
Loading…
Reference in New Issue
Block a user