Search in sources :

Example 1 with TemplateCompileConfig

use of org.webpieces.templatingdev.api.TemplateCompileConfig in project webpieces by deanhiller.

the class TemplateCompilerTask method compileImpl.

public void compileImpl(TemplateCompileOptions options) throws IOException {
    File buildDir = getProject().getBuildDir();
    //need to make customizable...
    File groovySrcGen = new File(buildDir, "groovysrc");
    System.out.println("groovy src directory=" + groovySrcGen);
    Charset encoding = Charset.forName(options.getEncoding());
    TemplateCompileConfig config = new TemplateCompileConfig(false);
    config.setFileEncoding(encoding);
    config.setPluginClient(true);
    config.setGroovySrcWriteDirectory(groovySrcGen);
    System.out.println("custom tags=" + options.getCustomTags());
    config.setCustomTagsFromPlugin(options.getCustomTags());
    LogLevel logLevel = getProject().getGradle().getStartParameter().getLogLevel();
    File destinationDir = getDestinationDir();
    System.out.println("destDir=" + destinationDir);
    File routeIdFile = new File(destinationDir, ProdTemplateModule.ROUTE_META_FILE);
    if (routeIdFile.exists())
        routeIdFile.delete();
    routeIdFile.createNewFile();
    System.out.println("routeId.txt file=" + routeIdFile.getAbsolutePath());
    FileCollection srcCollection = getSource();
    Set<File> files = srcCollection.getFiles();
    File firstFile = files.iterator().next();
    File baseDir = findBase(firstFile);
    try (FileOutputStream routeOut = new FileOutputStream(routeIdFile);
        OutputStreamWriter write = new OutputStreamWriter(routeOut, encoding.name());
        BufferedWriter bufWrite = new BufferedWriter(write)) {
        Injector injector = Guice.createInjector(new StubModule(), new DevTemplateModule(config, new PluginCompileCallback(destinationDir, bufWrite)));
        HtmlToJavaClassCompiler compiler = injector.getInstance(HtmlToJavaClassCompiler.class);
        GroovyClassLoader cl = new GroovyClassLoader();
        for (File f : files) {
            System.out.println("file=" + f);
            String fullName = findFullName(baseDir, f);
            System.out.println("name=" + fullName);
            String source = readSource(f);
            compiler.compile(cl, fullName, source);
        }
    }
    setDidWork(true);
}
Also used : Charset(java.nio.charset.Charset) HtmlToJavaClassCompiler(org.webpieces.templatingdev.impl.HtmlToJavaClassCompiler) FileCollection(org.gradle.api.file.FileCollection) LogLevel(org.gradle.api.logging.LogLevel) BufferedWriter(java.io.BufferedWriter) GroovyClassLoader(groovy.lang.GroovyClassLoader) StubModule(org.webpieces.templatingdev.api.StubModule) DevTemplateModule(org.webpieces.templatingdev.api.DevTemplateModule) Injector(com.google.inject.Injector) FileOutputStream(java.io.FileOutputStream) TemplateCompileConfig(org.webpieces.templatingdev.api.TemplateCompileConfig) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 2 with TemplateCompileConfig

use of org.webpieces.templatingdev.api.TemplateCompileConfig in project webpieces by deanhiller.

the class TestDevRefreshPageWithNoRestarting method setUp.

@Before
public void setUp() throws ClassNotFoundException, IOException, InterruptedException, ExecutionException, TimeoutException {
    Asserts.assertWasCompiledWithParamNames("test");
    userDir = System.getProperty("user.dir");
    log.info("running from dir=" + userDir);
    existingCodeLoc = FileFactory.newBaseFile("src/test/java/org/webpieces/webserver/dev/app");
    // developers tend to exit their test leaving the code in a bad state so if they run it again, restore the original
    // version for them(if we change the original version, we have to copy it to this directory as well though :(
    File original = FileFactory.newBaseFile("src/test/devServerTest/devServerOriginal");
    FileUtils.copyDirectory(original, existingCodeLoc, null);
    // cache existing code for use by teardown...
    stashedExistingCodeDir = FileFactory.newCacheLocation("webpieces/" + getClass().getSimpleName() + "/javaFiles");
    FileUtils.copyDirectory(existingCodeLoc, stashedExistingCodeDir);
    // list all source paths here as you add them(or just create for loop)
    // These are the list of directories that we detect java file changes under
    List<VirtualFile> srcPaths = new ArrayList<>();
    srcPaths.add(VirtualFileFactory.newBaseFile("src/test/java"));
    VirtualFile metaFile = VirtualFileFactory.newBaseFile("src/test/resources/devMeta.txt");
    log.info("LOADING from meta file=" + metaFile.getCanonicalPath());
    // html and json template file encoding...
    TemplateCompileConfig templateConfig = new TemplateCompileConfig(srcPaths);
    VirtualFile cacheLocation = new VirtualFileImpl(FileFactory.newCacheLocation("webpieces/" + getClass().getSimpleName() + "/bytecode"));
    // java source files encoding...
    CompileConfig devConfig = new CompileConfig(srcPaths, cacheLocation);
    SimpleMeterRegistry metrics = new SimpleMeterRegistry();
    Module platformOverrides = Modules.combine(new DevRouterModule(devConfig), new OverridesForEmbeddedSvrWithParsing(mgr, time, mockTimer, templateConfig, metrics));
    PrivateWebserverForTest webserver = new PrivateWebserverForTest(platformOverrides, null, false, metaFile);
    webserver.start();
    http11Socket = connectHttp(false, webserver.getUnderlyingHttpChannel().getLocalAddress());
}
Also used : VirtualFile(org.webpieces.util.file.VirtualFile) DevRouterModule(org.webpieces.devrouter.api.DevRouterModule) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) ArrayList(java.util.ArrayList) TemplateCompileConfig(org.webpieces.templatingdev.api.TemplateCompileConfig) CompileConfig(org.webpieces.compiler.api.CompileConfig) TemplateCompileConfig(org.webpieces.templatingdev.api.TemplateCompileConfig) VirtualFileImpl(org.webpieces.util.file.VirtualFileImpl) Module(com.google.inject.Module) DevRouterModule(org.webpieces.devrouter.api.DevRouterModule) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile) OverridesForEmbeddedSvrWithParsing(org.webpieces.webserver.test.OverridesForEmbeddedSvrWithParsing) Before(org.junit.Before)

Example 3 with TemplateCompileConfig

use of org.webpieces.templatingdev.api.TemplateCompileConfig in project webpieces by deanhiller.

the class TestDevSynchronousErrors method setUp.

@Before
public void setUp() throws InterruptedException, ClassNotFoundException, ExecutionException, TimeoutException {
    Asserts.assertWasCompiledWithParamNames("test");
    String filePath1 = System.getProperty("user.dir");
    log.info("running from dir=" + filePath1);
    List<VirtualFile> srcPaths = new ArrayList<>();
    srcPaths.add(VirtualFileFactory.newBaseFile("src/test/java"));
    TemplateCompileConfig templateConfig = new TemplateCompileConfig(false);
    SimpleMeterRegistry metrics = new SimpleMeterRegistry();
    Module platformOverrides = Modules.combine(new OverridesForEmbeddedSvrWithParsing(mgr, time, mockTimer, templateConfig, metrics), new ForTestingStaticDevelopmentModeModule());
    // you may want to create this server ONCE in a static method BUT if you do, also remember to clear out all your
    // mocks after every test AND you can no longer run single threaded(tradeoffs, tradeoffs)
    // This is however pretty fast to do in many systems...
    PrivateWebserverForTest webserver = new PrivateWebserverForTest(platformOverrides, new AppOverridesModule(), false, null);
    webserver.start();
    http11Socket = connectHttp(false, webserver.getUnderlyingHttpChannel().getLocalAddress());
}
Also used : VirtualFile(org.webpieces.util.file.VirtualFile) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) ArrayList(java.util.ArrayList) TemplateCompileConfig(org.webpieces.templatingdev.api.TemplateCompileConfig) Module(com.google.inject.Module) OverridesForEmbeddedSvrWithParsing(org.webpieces.webserver.test.OverridesForEmbeddedSvrWithParsing) Before(org.junit.Before)

Example 4 with TemplateCompileConfig

use of org.webpieces.templatingdev.api.TemplateCompileConfig in project webpieces by deanhiller.

the class TestSimpleTemplate method setup.

@Before
public void setup() {
    Injector injector = Guice.createInjector(new DevTemplateModule(new TemplateCompileConfig(false)), new StubModule());
    svc = injector.getInstance(DevTemplateService.class);
}
Also used : DevTemplateService(org.webpieces.templatingdev.impl.DevTemplateService) DevTemplateModule(org.webpieces.templatingdev.api.DevTemplateModule) StubModule(org.webpieces.templatingdev.api.StubModule) Injector(com.google.inject.Injector) TemplateCompileConfig(org.webpieces.templatingdev.api.TemplateCompileConfig) Before(org.junit.Before)

Example 5 with TemplateCompileConfig

use of org.webpieces.templatingdev.api.TemplateCompileConfig in project webpieces by deanhiller.

the class TemplateCompile method compileImpl.

public void compileImpl(TemplateCompileOptions options) throws IOException {
    File buildDir = getProject().getBuildDir();
    // need to make customizable...
    File groovySrcGen = new File(buildDir, "groovysrc");
    log.log(LogLevel.LIFECYCLE, "putting groovy scripts in: " + groovySrcGen);
    Charset encoding = Charset.forName(options.getEncoding());
    TemplateCompileConfig config = new TemplateCompileConfig(false);
    config.setFileEncoding(encoding);
    config.setPluginClient(true);
    config.setGroovySrcWriteDirectory(groovySrcGen);
    log.log(LogLevel.LIFECYCLE, "Custom tags: " + options.getCustomTags());
    config.setCustomTagsFromPlugin(options.getCustomTags());
    LogLevel logLevel = getProject().getGradle().getStartParameter().getLogLevel();
    File destinationDir = getDestinationDirectory().getAsFile().get();
    log.log(LogLevel.LIFECYCLE, "Writing class files to destDir: " + destinationDir);
    File routeIdFile = new File(destinationDir, ProdConstants.ROUTE_META_FILE);
    if (routeIdFile.exists())
        routeIdFile.delete();
    routeIdFile.createNewFile();
    log.log(LogLevel.LIFECYCLE, "routeId file: " + routeIdFile);
    FileCollection srcCollection = getSource();
    Set<File> files = srcCollection.getFiles();
    File firstFile = files.iterator().next();
    File baseDir = findBase(firstFile);
    try (FileOutputStream routeOut = new FileOutputStream(routeIdFile);
        OutputStreamWriter write = new OutputStreamWriter(routeOut, encoding.name());
        BufferedWriter bufWrite = new BufferedWriter(write)) {
        Injector injector = Guice.createInjector(new StubModule(), new DevTemplateModule(config, new PluginCompileCallback(destinationDir, bufWrite)));
        HtmlToJavaClassCompiler compiler = injector.getInstance(HtmlToJavaClassCompiler.class);
        GroovyClassLoader cl = new GroovyClassLoader();
        for (File f : files) {
            String fullName = findFullName(baseDir, f);
            log.log(LogLevel.INFO, "file compile name={}, file={}", fullName, f);
            String source = readSource(f);
            compiler.compile(cl, fullName, source);
        }
    }
    setDidWork(true);
}
Also used : Charset(java.nio.charset.Charset) HtmlToJavaClassCompiler(org.webpieces.templatingdev.impl.HtmlToJavaClassCompiler) FileCollection(org.gradle.api.file.FileCollection) LogLevel(org.gradle.api.logging.LogLevel) BufferedWriter(java.io.BufferedWriter) GroovyClassLoader(groovy.lang.GroovyClassLoader) StubModule(org.webpieces.templatingdev.api.StubModule) DevTemplateModule(org.webpieces.templatingdev.api.DevTemplateModule) Injector(com.google.inject.Injector) FileOutputStream(java.io.FileOutputStream) TemplateCompileConfig(org.webpieces.templatingdev.api.TemplateCompileConfig) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Aggregations

TemplateCompileConfig (org.webpieces.templatingdev.api.TemplateCompileConfig)5 Injector (com.google.inject.Injector)3 File (java.io.File)3 Before (org.junit.Before)3 DevTemplateModule (org.webpieces.templatingdev.api.DevTemplateModule)3 StubModule (org.webpieces.templatingdev.api.StubModule)3 Module (com.google.inject.Module)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 BufferedWriter (java.io.BufferedWriter)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2 FileCollection (org.gradle.api.file.FileCollection)2 LogLevel (org.gradle.api.logging.LogLevel)2 HtmlToJavaClassCompiler (org.webpieces.templatingdev.impl.HtmlToJavaClassCompiler)2 VirtualFile (org.webpieces.util.file.VirtualFile)2 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)2 OverridesForEmbeddedSvrWithParsing (org.webpieces.webserver.test.OverridesForEmbeddedSvrWithParsing)2