Skip to content

Instantly share code, notes, and snippets.

@TakayukiKando
Created April 21, 2016 18:40
Show Gist options
  • Save TakayukiKando/e6a49a4c5ea7e695a5eb03757ef2458d to your computer and use it in GitHub Desktop.
Save TakayukiKando/e6a49a4c5ea7e695a5eb03757ef2458d to your computer and use it in GitHub Desktop.
Velocity sample code for Java tools study session in Fukuoka.
package fukuoka.javatool;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Properties;
import java.util.stream.Collectors;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
public class AntFileGenerator {
private static final Properties VELOCITY_PROPS;
private static final String LOADER_NAME = "script_loader";
private static final String PACKAGE_PATH;
static {
Properties props = new Properties();
props.setProperty("runtime.log", "/velocity.log");
//props.setProperty("runtime.log.error.stacktrace", "true");
//props.setProperty("runtime.log.warn.stacktrace", "true");
//props.setProperty("runtime.log.info.stacktrace", "true");
props.setProperty("resource.loader", LOADER_NAME);
props.setProperty(LOADER_NAME+".resource.loader.class", ClasspathResourceLoader.class.getName());
props.setProperty(LOADER_NAME+".resource.loader.cache", "true");
props.setProperty(LOADER_NAME+".resource.loader.modificationCheckInterval", "10");
props.setProperty(LOADER_NAME+".resource.loader.description", "Script template loader.");
VELOCITY_PROPS = props;
PACKAGE_PATH = Arrays.stream(AntFileGenerator.class.getPackage().getName().split("\\.")).collect(Collectors.joining("/"));
}
private Template template;
public AntFileGenerator(String templateName) throws IOException {
VelocityEngine velocity = new VelocityEngine(VELOCITY_PROPS);
this.template = velocity.getTemplate(PACKAGE_PATH+"/"+templateName, "UTF-8");
}
protected void writeScript(Template template, VelocityContext context, Writer writer){
context.internalPut("templatesPath", PACKAGE_PATH);
synchronized(template){
//template.initDocument();
template.merge(context, writer);
}
}
public void writeScript(String vmGrade, Path buildScriptFile) throws IOException{
VelocityContext context = new VelocityContext();
context.internalPut("vmGrade", vmGrade);
try(FileWriter writer = new FileWriter(buildScriptFile.toFile())){
writeScript(this.template, context, writer);
}
}
}
<project default="main">
<property name="local_class_path" value="../../bin"/>
<property name="clang_base" value="/usr/local/clang-3.5.1" />
<property name="stdcxx_include_path" value="${clang_base}/include/c++/v1"/>
<property name="stdc_include_path" value="/usr/include"/>
<property name="clang_bin" value="${clang_base}/bin" />
<target name="main" depends="compile">
<echo>Complete.</echo>
</target>
<target name="compile" depends="">
<taskdef name="trexec" classname="org.ptaas.lib.ant.TRExec" />
<trexec toolset="Clang" executable="${clang_bin}/clang++" vmgrade="m3.large" newenvironment="false" >
<arg value="-v"/>
<arg value="-Wall"/>
<arg value="-I"/>
<arg value="${stdcxx_include_path}"/>
<arg value="-I"/>
<arg value="${stdc_include_path}"/>
<arg value="-c"/>
<arg value="-o"/>
<arg value="helloworld.o"/>
<arg value="helloworld.cc"/>
</trexec>
</target>
</project>
<project default="main">
<property name="local_class_path" value="../../bin"/>
<property name="clang_base" value="/usr/local/clang-3.5.1" />
<property name="stdcxx_include_path" value="${clang_base}/include/c++/v1"/>
<property name="stdc_include_path" value="/usr/include"/>
<property name="clang_bin" value="${clang_base}/bin" />
<target name="main" depends="compile">
<echo>Complete.</echo>
</target>
<target name="compile" depends="">
<taskdef name="trexec" classname="org.ptaas.lib.ant.TRExec" />
<trexec toolset="Clang" executable="${clang_bin}/clang++" vmgrade="${vmGrade}" newenvironment="false" >
<arg value="-v"/>
<arg value="-Wall"/>
<arg value="-I"/>
<arg value="${stdcxx_include_path}"/>
<arg value="-I"/>
<arg value="${stdc_include_path}"/>
<arg value="-c"/>
<arg value="-o"/>
<arg value="helloworld.o"/>
<arg value="helloworld.cc"/>
</trexec>
</target>
</project>
package fukuoka.javatool.test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import fukuoka.javatool.AntFileGenerator;
public class TestAntFileGenerator {
private static final String UDIR = System.getProperty("user.dir");
private static final Path TEST_DIR_PATH = Paths.get(UDIR, "test");
private static final String TEMPLATE = "remote_build.xml.vm";
private static final String GENERATED = "remote_build.xml";
private Path generated;
@Before
public void setup() throws IOException{
this.generated = TEST_DIR_PATH.resolve(GENERATED);
Files.deleteIfExists(generated);
}
@After
public void cleanup() throws IOException{
//Files.deleteIfExists(generated);
}
@Test
public void testGenerate() throws IOException{
AntFileGenerator generator = new AntFileGenerator(TEMPLATE);
String vmGrade = "m3.large";
generator.writeScript(vmGrade, generated);
assertThat(Files.exists(generated), is(true));
assertThat(Files.size(generated) > 0, is(true));
try(BufferedReader r = new BufferedReader(new FileReader(generated.toFile()))){
assertThat(r.lines().filter(l->l.contains(vmGrade)).findFirst().isPresent(), is(true));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment