Skip to content

Commit

Permalink
had to enable the groovy plugin to be told what is the whitelist of c…
Browse files Browse the repository at this point in the history
…ustom tags so the compile will not fail
  • Loading branch information
deanhiller committed Aug 12, 2016
1 parent 442e312 commit 26292f5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.webpieces.gradle.compiler;

import java.util.Set;

import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

public class TemplateCompileOptions {
private String encoding = "UTF-8";
private Set<String> customTags;

@Optional @Input
public String getEncoding() {
Expand All @@ -14,4 +17,13 @@ public String getEncoding() {
public void setEncoding(String encoding) {
this.encoding = encoding;
}

public Set<String> getCustomTags() {
return customTags;
}

public void setCustomTags(Set<String> customTags) {
this.customTags = customTags;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public TemplateCompilerPlugin(SourceDirectorySetFactory sourceDirectorySetFactor

@Override
public void apply(ProjectInternal project) {
project.getExtensions().create("compileTemplateSetting", TemplateCompileOptions.class);
project.getPluginManager().apply(JavaBasePlugin.class);
JavaBasePlugin javaBasePlugin = project.getPlugins().getPlugin(JavaBasePlugin.class);
configureSourceSetDefaults(project, javaBasePlugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

import org.apache.commons.io.IOUtils;
import org.codehaus.groovy.tools.GroovyClass;
import org.gradle.api.Action;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.compile.AbstractCompile;
import org.webpieces.templating.api.CompileCallback;
Expand All @@ -26,37 +24,39 @@
import com.google.inject.Guice;
import com.google.inject.Injector;

import groovy.lang.Closure;

public class TemplateCompilerTask extends AbstractCompile {

private TemplateCompileOptions options = new TemplateCompileOptions();

@Nested
public TemplateCompileOptions getOptions() {
return options;
}

public void options(Action<TemplateCompileOptions> action) {
action.execute(getOptions());
}

public void options(Closure<?> closure) {
getProject().configure(getOptions(), closure);
}
// private TemplateCompileOptions options = new TemplateCompileOptions();
//
// @Nested
// public TemplateCompileOptions getOptions() {
// return options;
// }
//
// public void options(Action<TemplateCompileOptions> action) {
// action.execute(getOptions());
// }
//
// public void options(Closure<?> closure) {
// getProject().configure(getOptions(), closure);
// }

@TaskAction
public void compile() {
try {
compileImpl();
TemplateCompileOptions options = getProject().getExtensions().findByType(TemplateCompileOptions.class);
compileImpl(options);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void compileImpl() throws IOException {

public void compileImpl(TemplateCompileOptions options) throws IOException {
Charset encoding = Charset.forName(options.getEncoding());
TemplateCompileConfig config = new TemplateCompileConfig(encoding);
config.setPluginClient(true);
System.out.println("custom tags="+options.getCustomTags());
config.setCustomTagsFromPlugin(options.getCustomTags());
Injector injector = Guice.createInjector(new DevTemplateModule(config));
HtmlToJavaClassCompiler compiler = injector.getInstance(HtmlToJavaClassCompiler.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;

public class TemplateCompileConfig {

private Charset fileEncoding = StandardCharsets.UTF_8;
private Charset fileEncoding = StandardCharsets.UTF_8;
//These two fields are used by the template compiling plugin...
private boolean isPluginClient = false;
private Set<String> customTagsFromPlugin = new HashSet<>();

public TemplateCompileConfig() {
}
Expand All @@ -22,4 +27,20 @@ public void setFileEncoding(Charset fileEncoding) {
this.fileEncoding = fileEncoding;
}

public boolean isPluginClient() {
return isPluginClient;
}

public void setPluginClient(boolean isPluginClient) {
this.isPluginClient = isPluginClient;
}

public Set<String> getCustomTagsFromPlugin() {
return customTagsFromPlugin;
}

public void setCustomTagsFromPlugin(Set<String> customTagsFromPlugin) {
this.customTagsFromPlugin = customTagsFromPlugin;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.webpieces.templating.api.GroovyGen;
import org.webpieces.templating.api.HtmlTag;
import org.webpieces.templating.api.HtmlTagLookup;
import org.webpieces.templating.api.TemplateCompileConfig;
import org.webpieces.templating.api.TemplateConfig;
import org.webpieces.templating.impl.tags.TagGen;

public class ScriptWriter {
Expand All @@ -23,12 +25,14 @@ public class ScriptWriter {
private GenLookup generatorLookup;
private HtmlTagLookup htmlTagLookup;
private UniqueIdGenerator uniqueIdGen;
private TemplateCompileConfig config;

@Inject
public ScriptWriter(HtmlTagLookup htmlTagLookup, GenLookup lookup, UniqueIdGenerator generator) {
public ScriptWriter(HtmlTagLookup htmlTagLookup, GenLookup lookup, UniqueIdGenerator generator, TemplateCompileConfig config) {
this.htmlTagLookup = htmlTagLookup;
generatorLookup = lookup;
this.uniqueIdGen = generator;
this.config = config;
}

public void printHead(ScriptOutputImpl sourceCode, String packageStr, String className) {
Expand Down Expand Up @@ -155,9 +159,10 @@ public void printStartTag(TokenImpl token, TokenImpl previousToken, ScriptOutput
//Things like #{else}# tag are given chance to validate that it is only after an #{if}# tag
abstractTag.validatePreviousSibling(token, previousToken);
}
} else if(htmltag == null) {
throw new IllegalArgumentException("Unknown tag="+tagName+" location="+token.getSourceLocation(true));
} else {
if(htmltag == null && !config.getCustomTagsFromPlugin().contains(tagName))
throw new IllegalArgumentException("Unknown tag=#{"+tagName+"}# OR you didn't add '"
+tagName+"' to list of customTags in build.gradle file. "+token.getSourceLocation(true));
int id = uniqueIdGen.generateId();
generator = new TagGen(tagName, token, id, callbacks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sourceSets {
test {
resources {
srcDirs = ["src/main/java"]
includes = ["**/*.html"]
includes = ["**/*.html", "**/*.tag"]
}
}
}
Expand All @@ -48,3 +48,9 @@ dependencies {
}

mainClassName = "PACKAGE.CLASSNAMEServer"

compileTemplateSetting {
//Since the groovy plugin has no way of knowing about your custom tags, list them here or the compile will
//fail (This catches mispellings and such so you don't release a broken app to production)
customTags = [ "mytag", "anothertag" ]
}

0 comments on commit 26292f5

Please sign in to comment.