Permalink
Browse files

+ Make examples work.

+ Miscellaneous changes.


git-svn-id: http://closure-templates.googlecode.com/svn/trunk@4 ba6e359a-b4f9-11de-880b-11b4e8d67c75
  • Loading branch information...
kai.huang
kai.huang committed Oct 29, 2009
1 parent 8b2b0b1 commit 9693a80313c779d8f10ca35bb38b20adce29a954
View
221 build.xml
@@ -13,16 +13,20 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
Author: Kai Huang
Author: Robert Bowdidge
-->
<project name="Soy" basedir="." default="jar">
<!-- Define properties. -->
<property name="java.src.dir" value="${basedir}/java/src" />
<property name="java.lib.dir" value="${basedir}/java/lib" />
<property name="build.dir" value="${basedir}/build" />
<property name="soy.jar.file" value="${build.dir}/Soy.jar" />
<property name="build.classes.dir" value="${build.dir}/classes" />
<property name="build.genfiles.dir" value="${build.dir}/genfiles" />
<property name="java.src.dir" value="${basedir}/java/src" />
<property name="java.lib.dir" value="${basedir}/java/lib" />
<!-- Target to delete the generated files. -->
@@ -82,55 +86,210 @@ limitations under the License.
</target>
<!-- Macro for targets to create standalone executable jars. -->
<macrodef name="executable-jar">
<attribute name="executable.name" />
<sequential>
<jar destfile="${build.dir}/@{executable.name}.jar" update="true">
<manifest>
<attribute name="Main-Class" value="com.google.template.soy.@{executable.name}" />
</manifest>
<fileset dir="${build.classes.dir}" />
<zipfileset src="${java.lib.dir}/google-guava-snapshot.jar" />
<zipfileset src="${java.lib.dir}/guice-snapshot.jar" />
<zipfileset src="${java.lib.dir}/guice-multibindings-snapshot.jar" />
<zipfileset src="${java.lib.dir}/guice-assistedinject-snapshot.jar" />
<zipfileset src="${java.lib.dir}/aopalliance.jar" />
<zipfileset src="${java.lib.dir}/args4j-2.0.12.jar" />
</jar>
</sequential>
</macrodef>
<!-- Target to create a standalone executable SoyToJsSrcCompiler.jar. -->
<target name="SoyToJsSrcCompiler"
depends="compile"
depends="jar"
description="Creates a standalone executable SoyToJsSrcCompiler.jar.">
<executable-jar executable.name="SoyToJsSrcCompiler" />
<jar destfile="${build.dir}/SoyToJsSrcCompiler.jar" update="true">
<manifest>
<attribute name="Main-Class" value="com.google.template.soy.SoyToJsSrcCompiler" />
</manifest>
<zipfileset src="${soy.jar.file}" />
</jar>
</target>
<!-- Target to create a standalone executable SoyParseInfoGenerator.jar. -->
<target name="SoyParseInfoGenerator"
depends="compile"
depends="jar"
description="Creates a standalone executable SoyParseInfoGenerator.jar.">
<executable-jar executable.name="SoyParseInfoGenerator" />
<jar destfile="${build.dir}/SoyParseInfoGenerator.jar" update="true">
<manifest>
<attribute name="Main-Class" value="com.google.template.soy.SoyParseInfoGenerator" />
</manifest>
<zipfileset src="${soy.jar.file}" />
</jar>
</target>
<!-- Target to create a standalone executable SoyMsgExtractor.jar. -->
<target name="SoyMsgExtractor"
depends="compile"
depends="jar"
description="Creates a standalone executable SoyMsgExtractor.jar.">
<executable-jar executable.name="SoyMsgExtractor" />
<jar destfile="${build.dir}/SoyMsgExtractor.jar" update="true">
<manifest>
<attribute name="Main-Class" value="com.google.template.soy.SoyMsgExtractor" />
</manifest>
<zipfileset src="${soy.jar.file}" />
</jar>
</target>
<!-- Target to create a standalone executable SoyToJavaSrcCompilerExperimental.jar. -->
<target name="SoyToJavaSrcCompilerExperimental"
depends="compile"
depends="jar"
description="Creates a standalone executable SoyToJavaSrcCompilerExperimental.jar.">
<executable-jar executable.name="SoyToJavaSrcCompilerExperimental" />
<jar destfile="${build.dir}/SoyToJavaSrcCompilerExperimental.jar" update="true">
<manifest>
<attribute name="Main-Class" value="com.google.template.soy.SoyToJavaSrcCompilerExperimental" />
</manifest>
<zipfileset src="${soy.jar.file}" />
</jar>
</target>
<!--*******************************************************************************************-->
<!-- Below are targets for the examples. -->
<property name="examples.dir" value="${basedir}/examples" />
<property name="javascript.examples.dir" value="${basedir}/javascript/examples" />
<property name="java.examples.dir" value="${basedir}/java/src/com/google/template/soy/examples" />
<property name="build.examples.dir" value="${build.dir}/examples" />
<!--
Uses SoyToJsSrcCompiler to compile simple.soy.
In 'build/examples', generates the following file:
simple_generated.js
Should be same as the file in 'javascript/examples'.
-->
<target name="js-simple-example"
depends="SoyToJsSrcCompiler"
description="Uses SoyToJsSrcCompiler to compile simple.soy.">
<mkdir dir="${build.examples.dir}" />
<java jar="${build.dir}/SoyToJsSrcCompiler.jar"
fork="true" failonerror="true">
<arg value="--outputPathFormat" />
<arg value="${build.examples.dir}/{INPUT_FILE_NAME_NO_EXT}_generated.js" />
<arg value="${examples.dir}/simple.soy" />
</java>
</target>
<!--
Uses SoyToJsSrcCompiler to compile simple.soy and features.soy for locales en and x-zz.
In 'build/examples', generates the following files:
simple_generated_en.js
features_generated_en.js
simple_generated_x-zz.js
features_generated_x-zz.js
Should be same as the files in 'javascript/examples'.
-->
<target name="js-features-example"
depends="SoyToJsSrcCompiler"
description="Uses SoyToJsSrcCompiler to compile simple.soy and features.soy for locales en and x-zz.">
<mkdir dir="${build.examples.dir}" />
<java jar="${build.dir}/SoyToJsSrcCompiler.jar"
fork="true" failonerror="true">
<arg value="--outputPathFormat" />
<arg value="${build.examples.dir}/{INPUT_FILE_NAME_NO_EXT}_generated_{LOCALE}.js" />
<arg value="--locales" />
<arg value="en,x-zz" />
<arg value="--messageFilePathFormat" />
<arg value="${examples.dir}/examples_translated_{LOCALE}.xlf" />
<arg value="--compileTimeGlobalsFile" />
<arg value="${javascript.examples.dir}/features-usage_globals.txt" />
<arg value="--inputPrefix" />
<arg value="${examples.dir}/" /> <!-- note the trailing slash -->
<arg value="simple.soy" />
<arg value="features.soy" />
</java>
</target>
<!--
Creates an executable jar for rendering the simple examples from Java.
In 'build/examples', generates the following file:
SimpleUsage.jar
This can be executed to render the simple examples from Java:
java -jar SimpleUsage.jar
-->
<target name="java-simple-example"
depends="jar"
description="Creates an executable jar for rendering the simple examples from Java.">
<mkdir dir="${build.examples.dir}" />
<jar destfile="${build.examples.dir}/SimpleUsage.jar" update="true">
<manifest>
<attribute name="Main-Class" value="com.google.template.soy.examples.SimpleUsage" />
</manifest>
<zipfileset src="${soy.jar.file}" />
<!-- Resources. -->
<fileset file="${examples.dir}/simple.soy" />
</jar>
</target>
<!--
Uses SoyParseInfoGenerator to generate parse info for features.soy.
In 'build/examples', generates the following file:
FeaturesSoyInfo.java
Should be same as the file in 'java/src/com/google/template/soy/examples'.
-->
<target name="parse-info-generation-example"
depends="SoyParseInfoGenerator"
description="Uses SoyParseInfoGenerator to generate parse info for features.soy.">
<mkdir dir="${build.examples.dir}" />
<java jar="${build.dir}/SoyParseInfoGenerator.jar"
fork="true" failonerror="true">
<arg value="--outputDirectory" />
<arg value="${build.examples.dir}" />
<arg value="--javaPackage" />
<arg value="com.google.template.soy.examples" />
<arg value="--javaClassNameSource" />
<arg value="filename" />
<arg value="${examples.dir}/features.soy" />
</java>
</target>
<!--
Creates an executable jar for rendering the features examples from Java.
In 'build/examples', generates the following file:
FeaturesUsage.jar
This can be executed to render the features examples from Java:
java -jar FeaturesUsage.jar
java -jar FeaturesUsage.jar -locale x-zz
-->
<target name="java-features-example"
depends="jar"
description="Creates an executable jar for rendering the features examples from Java.">
<mkdir dir="${build.examples.dir}" />
<jar destfile="${build.examples.dir}/FeaturesUsage.jar" update="true">
<manifest>
<attribute name="Main-Class" value="com.google.template.soy.examples.FeaturesUsage" />
</manifest>
<zipfileset src="${soy.jar.file}" />
<!-- Resources. -->
<fileset file="${examples.dir}/simple.soy" />
<fileset file="${examples.dir}/features.soy" />
<fileset file="${examples.dir}/examples_translated_en.xlf" />
<fileset file="${examples.dir}/examples_translated_x-zz.xlf" />
<fileset file="${java.examples.dir}/FeaturesUsage_globals.txt" />
</jar>
</target>
<!--
Uses SoyMsgExtractor to extract messages from simple.soy and features.soy.
In 'build/examples', generates the following file:
examples_extracted.xlf
Should be same as the file in 'examples'.
-->
<target name="msg-extraction-example"
depends="SoyMsgExtractor"
description="Uses SoyMsgExtractor to extract messages from simple.soy and features.soy.">
<mkdir dir="${build.examples.dir}" />
<java jar="${build.dir}/SoyMsgExtractor.jar"
fork="true" failonerror="true">
<arg value="--outputFile" />
<arg value="${build.examples.dir}/examples_extracted.xlf" />
<arg value="--inputPrefix" />
<arg value="${examples.dir}/" /> <!-- note the trailing slash -->
<arg value="simple.soy" />
<arg value="features.soy" />
</java>
</target>
@@ -1,3 +1,18 @@
// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'examples'
This directory contains example Soy files shared by both the JS and Java usage examples.
For the JavaScript usage examples, see 'javascript/examples'.
@@ -12,3 +27,8 @@ For the Java usage examples, see 'src/com/google/template/soy/examples'.
+ examples_extracted.xlf
The XLIFF extracted messages file generated by SoyMsgExtractor when it is executed on
simple.soy and features.soy (with source language 'en' and no target language).
Note: For an example Ant target (and command line args) that generates this file, please
see target 'msg-extraction-example' within the top-level 'build.xml'.
+ examples_translated_en.xlf, examples_translated_x-zz.xlf
Translated versions of the XLIFF messages file (received from translators).
@@ -264,6 +264,30 @@ public Builder setCompileTimeGlobals(File compileTimeGlobalsFile) throws IOExcep
return this;
}
/**
* Sets the resource file containing compile-time globals.
*
* <p> Each line of the file should have the format
* <pre>
* &lt;global_name&gt; = &lt;primitive_data&gt;
* </pre>
* where primitive_data is a valid Soy expression literal for a primitive type (null, boolean,
* integer, float, or string). Empty lines and lines beginning with "//" are ignored. The file
* should be encoded in UTF-8.
*
* <p> If you need to generate a file in this format from Java, consider using the utility
* {@code SoyUtils.generateCompileTimeGlobalsFile()}.
*
* @param compileTimeGlobalsResource The resource containing compile-time globals.
* @return This builder.
* @throws IOException If there is an error reading the compile-time globals file.
*/
public Builder setCompileTimeGlobals(URL compileTimeGlobalsResource) throws IOException {
this.options.setCompileTimeGlobals(compileTimeGlobalsResource);
return this;
}
}
@@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.template.soy.SoyFileSet;
@@ -51,8 +52,8 @@
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
@@ -68,16 +69,14 @@ private FeaturesUsage() {}
/** The string to prepend to the usage message generated by Flags. */
private static final String USAGE_PREFIX =
"Usage:\n" +
"java com.google.template.soy.examples.FeaturesUsage \\\n" +
" [--locale=<locale>] \n";
"java com.google.template.soy.examples.FeaturesUsage [-locale <locale>]\n";
/** Prefix for XLIFF file path (ends with [locale].xlf). */
private static final String XLIFF_FILE_PATH_PREFIX =
"java/com/google/template/soy/examples/examples_translated_";
/** Prefix for XLIFF resource (ends with [locale].xlf). */
private static final String XLIFF_RESOURCE_PREFIX = "examples_translated_";
@Option(name = "--locale",
usage = "The locale to render templates in. The corresponding XLIFF file " +
XLIFF_FILE_PATH_PREFIX + "<locale>.xlf must exist. If not provided, the" +
@Option(name = "-locale",
usage = "The locale to render templates in. The corresponding XLIFF resource " +
XLIFF_RESOURCE_PREFIX + "<locale>.xlf must exist. If not provided, the" +
" messages from the Soy source will be used.")
private String locale = "";
@@ -114,22 +113,21 @@ private void execMain(String[] args) throws IOException {
SoyFileSet.Builder sfsBuilder = injector.getInstance(SoyFileSet.Builder.class);
SoyFileSet sfs = sfsBuilder
.add(new File("template/soy/examples/simple.soy"))
.add(new File("template/soy/examples/features.soy"))
.setCompileTimeGlobals(
new File("java/com/google/template/soy/examples/FeaturesUsage_globals.txt"))
.add(Resources.getResource("simple.soy"))
.add(Resources.getResource("features.soy"))
.setCompileTimeGlobals(Resources.getResource("FeaturesUsage_globals.txt"))
.build();
SoyTofu tofu = sfs.compileToJavaObj().forNamespace("soy.examples.features");
SoyMsgBundle msgBundle;
if (locale.length() > 0) {
// Use translations from an XLIFF file.
SoyMsgBundleHandler msgBundleHandler = injector.getInstance(SoyMsgBundleHandler.class);
File xliffFile = new File(XLIFF_FILE_PATH_PREFIX + locale + ".xlf");
msgBundle = msgBundleHandler.createFromFile(xliffFile);
URL xliffResource = Resources.getResource(XLIFF_RESOURCE_PREFIX + locale + ".xlf");
msgBundle = msgBundleHandler.createFromResource(xliffResource);
if (msgBundle.getLocaleString() == null) {
throw new IOException(
"Error reading message resource \"" + XLIFF_FILE_PATH_PREFIX + locale + ".xlf\".");
"Error reading message resource \"" + XLIFF_RESOURCE_PREFIX + locale + ".xlf\".");
}
} else {
// Use the messages from the Soy source files.
Oops, something went wrong.

0 comments on commit 9693a80

Please sign in to comment.