Skip to content

Commit

Permalink
Support .xtb translation autodiscovery (#172)
Browse files Browse the repository at this point in the history
This is the next commit in adding first-class support for xtb files
for closure compilation from Java apps and libraries. If the newly
added "auto" flag is set, the closure task will find the first .xtb
file that matches the desired locale and use that.

Future work will merge discovered xtb files, both across dependencies
and locales.

Co-authored-by: Colin Alworth <[email protected]>
  • Loading branch information
treblereel and niloc132 authored Jun 20, 2022
1 parent 487473f commit 47710a8
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ public Collection<String> getExterns() {
return Collections.emptySet();
}

@Override
public Optional<File> getTranslationsFile() {
ConfigValueProvider.ConfigNode node = config.findNode("translationsFile");
return Optional.ofNullable(node).map(this::useFileConfig);
}

@Override
public boolean getCheckAssertions() {
return Boolean.parseBoolean(getString("checkAssertions"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface Config {
String getString(String key);
Expand All @@ -24,8 +23,6 @@ public interface Config {

Collection<String> getExterns();

Optional<File> getTranslationsFile();

boolean getCheckAssertions();

boolean getRewritePolyfills();
Expand Down
2 changes: 2 additions & 0 deletions j2cl-maven-plugin/src/it/translationsfile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<module>tests/pom_cz_in_resource.xml</module>
<module>tests/pom_fr_meta_inf.xml</module>
<module>tests/pom_executions.xml</module>
<module>tests/pom_cz_auto.xml</module>
<module>tests/pom_de_in_java_package-auto.xml</module>
</modules>

</project>
4 changes: 3 additions & 1 deletion j2cl-maven-plugin/src/it/translationsfile/tests/pom_cz.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
</execution>
</executions>
<configuration>
<translationsFile>${project.basedir}/MyTranslationBundle_cz.xtb</translationsFile>
<translationsFile>
<file>${project.basedir}/MyTranslationBundle_cz.xtb</file>
</translationsFile>
<defines>
<goog.LOCALE>cz</goog.LOCALE>
<holder.value>Asi se něco stalo!</holder.value>
Expand Down
85 changes: 85 additions & 0 deletions j2cl-maven-plugin/src/it/translationsfile/tests/pom_cz_auto.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>translation.test</groupId>
<artifactId>cz-auto</artifactId>
<packaging>jar</packaging>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>com.google.jsinterop</groupId>
<artifactId>base</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-annotations</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<classifier>sources</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>test-js</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- In order to use htmlunit, we must compile to remove es6 classes -->
<compilationLevel>ADVANCED</compilationLevel>
</configuration>
</execution>
</executions>
<configuration>
<translationsFile>
<auto>true</auto>
</translationsFile>
<defines>
<goog.LOCALE>cz</goog.LOCALE>
<holder.value>Asi se něco stalo!</holder.value>
</defines>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
</execution>
</executions>
<configuration>
<translationsFile>${project.basedir}/src/main/resources/MyTranslationBundle_cz.xtb</translationsFile>
<translationsFile>
<file>${project.basedir}/src/main/resources/MyTranslationBundle_cz.xtb</file>
</translationsFile>
<defines>
<goog.LOCALE>cz</goog.LOCALE>
<holder.value>Asi se něco stalo!</holder.value>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>translation.test</groupId>
<artifactId>de_in_java_package_auto</artifactId>
<packaging>jar</packaging>
<version>1.0</version>

<dependencies>

<dependency>
<groupId>com.google.jsinterop</groupId>
<artifactId>base</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-annotations</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<classifier>sources</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>test-js</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- In order to use htmlunit, we must compile to remove es6 classes -->
<compilationLevel>ADVANCED</compilationLevel>
</configuration>
</execution>
</executions>
<configuration>
<translationsFile>
<auto>true</auto>
</translationsFile>
<defines>
<goog.LOCALE>de</goog.LOCALE>
<holder.value>Ich schätze, es ist etwas passiert!</holder.value>
</defines>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
</execution>
</executions>
<configuration>
<translationsFile>${project.basedir}/src/main/java/example/test/MyTranslationBundle_de.xtb</translationsFile>
<translationsFile>
<file>${project.basedir}/src/main/java/example/test/MyTranslationBundle_de.xtb</file>
</translationsFile>
<defines>
<goog.LOCALE>de</goog.LOCALE>
<holder.value>Ich schätze, es ist etwas passiert!</holder.value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
<goal>test</goal>
</goals>
<configuration>
<translationsFile>${project.basedir}/MyTranslationBundle_cz.xtb</translationsFile>
<translationsFile>
<file>${project.basedir}/MyTranslationBundle_cz.xtb</file>
</translationsFile>
<initialScriptFilename>test-js-cz.js</initialScriptFilename>
<defines>
<goog.LOCALE>cz</goog.LOCALE>
Expand All @@ -81,7 +83,9 @@
<configuration>
<!-- In order to use htmlunit, we must compile to remove es6 classes -->
<initialScriptFilename>test-js-fr.js</initialScriptFilename>
<translationsFile>${project.basedir}/MyTranslationBundle_fr.xtb</translationsFile>
<translationsFile>
<file>${project.basedir}/MyTranslationBundle_fr.xtb</file>
</translationsFile>
<defines>
<goog.LOCALE>cz</goog.LOCALE>
<holder.value>Je suppose qu'il s'est passé quelque chose !</holder.value>
Expand All @@ -96,7 +100,7 @@
<configuration>
<!-- In order to use htmlunit, we must compile to remove es6 classes -->
<initialScriptFilename>test-js-de_in_java_package.js</initialScriptFilename>
<translationsFile>${project.basedir}/src/main/java/example/test/MyTranslationBundle_de.xtb</translationsFile>
<translationsFile><file>${project.basedir}/src/main/java/example/test/MyTranslationBundle_de.xtb</file></translationsFile>
<defines>
<goog.LOCALE>de</goog.LOCALE>
<holder.value>Ich schätze, es ist etwas passiert!</holder.value>
Expand Down
4 changes: 3 additions & 1 deletion j2cl-maven-plugin/src/it/translationsfile/tests/pom_fr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
</execution>
</executions>
<configuration>
<translationsFile>${project.basedir}/MyTranslationBundle_fr.xtb</translationsFile>
<translationsFile>
<file>${project.basedir}/MyTranslationBundle_fr.xtb</file>
</translationsFile>
<defines>
<goog.LOCALE>cz</goog.LOCALE>
<holder.value>Je suppose qu'il s'est passé quelque chose !</holder.value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
</execution>
</executions>
<configuration>
<translationsFile>${project.basedir}/src/main/resources/META-INF/MyTranslationBundle_fr.xtb</translationsFile>
<translationsFile>
<file>${project.basedir}/src/main/resources/META-INF/MyTranslationBundle_fr.xtb</file>
</translationsFile>
<defines>
<goog.LOCALE>cz</goog.LOCALE>
<holder.value>Je suppose qu'il s'est passé quelque chose !</holder.value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -138,7 +139,7 @@ public class BuildMojo extends AbstractBuildMojo {
* Closure flag: "Source of translated messages. Currently only supports XTB."
*/
@Parameter
protected String translationsFile;
protected TranslationsFileConfig translationsFile;

/**
* Closure flag: "Determines the set of builtin externs to load. Options: BROWSER, CUSTOM. Defaults to BROWSER."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public class TestMojo extends AbstractBuildMojo {
* Closure flag: "Source of translated messages. Currently only supports XTB."
*/
@Parameter
protected String translationsFile;
protected TranslationsFileConfig translationsFile;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.vertispan.j2cl.mojo;

public class TranslationsFileConfig {

private boolean auto;
private String file;

public boolean isAuto() {
return auto;
}

public void setAuto(boolean auto) {
this.auto = auto;
}

public String getFile() {
return file;
}

public void setFile(String file) {
this.file = file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ private Xpp3Dom findNodeWithKey(Xpp3Dom config, String prefix, String remaining)
if (index == -1) {
return null;// failed to find it so far, must not be present
}
return findNodeWithKey(config, prefix.substring(0, index), prefix.substring(index + 1) + '.' + remaining);
String nextRemaining;
if (remaining.isEmpty()) {
nextRemaining = prefix.substring(index + 1);
} else {
nextRemaining = prefix.substring(index + 1) + '.' + remaining;
}
return findNodeWithKey(config, prefix.substring(0, index), nextRemaining);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ClosureTask extends TaskFactory {
private static final Path PUBLIC = Paths.get("public");

private static final PathMatcher JS_SOURCES = withSuffix(".js");

private static final PathMatcher XTB = withSuffix(".xtb");
private static final PathMatcher NATIVE_JS_SOURCES = withSuffix(".native.js");
private static final PathMatcher EXTERNS_SOURCES = withSuffix(".externs.js");

Expand Down Expand Up @@ -176,7 +178,17 @@ public Task resolve(Project project, Config config) {
CompilerOptions.LanguageMode languageOut = CompilerOptions.LanguageMode.fromString(config.getLanguageOut());
//TODO probably kill this, or at least make it work like an import via another task so we detect changes
Collection<String> externs = config.getExterns();
Optional<File> translationsfile = config.getTranslationsFile();

TranslationsFileProcessor translationsFileProcessor = TranslationsFileProcessor.get(config);
List<Input> xtbInputs = Stream.concat(
Stream.of(project),
scope(project.getDependencies(), Dependency.Scope.RUNTIME).stream()
)
.map(p -> input(p, OutputTypes.BYTECODE))
// Only include the .xtb
.map(i -> i.filter(XTB))
.collect(Collectors.toList());

boolean checkAssertions = config.getCheckAssertions();
boolean rewritePolyfills = config.getRewritePolyfills();
boolean sourcemapsEnabled = config.getSourcemapsEnabled();
Expand Down Expand Up @@ -240,7 +252,7 @@ public void execute(TaskContext context) throws Exception {
entrypoint,
defines,
externs,
translationsfile,
translationsFileProcessor.getTranslationsFile(xtbInputs, context.log()),
true,//TODO have this be passed in,
checkAssertions,
rewritePolyfills,
Expand Down
Loading

0 comments on commit 47710a8

Please sign in to comment.