Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
treblereel committed Jun 13, 2022
1 parent 46ed6a5 commit 3592365
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,19 @@ public Collection<String> getExterns() {
}

@Override
public Optional<TranslationsFileConfiguration> getTranslationsFile() {
ConfigValueProvider.ConfigNode node = config.findNode("translationsFile");
if(node != null) {
List<ConfigValueProvider.ConfigNode> children = node.getChildren();
if(children.isEmpty() || children.size() > 1) {
throw new IllegalStateException("TranslationsFile configuration must have 'auto' or 'file' declaration.");
}

for (ConfigValueProvider.ConfigNode child : children) {
if (child.getName().equals("auto")) {
return Optional.of(new TranslationsFileConfiguration(this, Boolean.parseBoolean(child.readString())));
} else if(child.getName().equals("file")) {
return Optional.of(new TranslationsFileConfiguration(this, useFileConfig(child)));
}
}
public Map<String, Object> getTranslationsFile() {
ConfigValueProvider.ConfigNode translationsFile = config.findNode("translationsFile");
if (translationsFile == null) {
return Collections.emptyMap();
}
return Optional.empty();
return translationsFile.getChildren().stream()
.collect(Collectors.toMap(ConfigValueProvider.ConfigNode::getName, e -> {
if(e.getName().equals("file")) {
return e.readFile();
} else {
return e.readString();
}
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.vertispan.j2cl.build.task;

import com.vertispan.j2cl.build.TranslationsFileConfiguration;

import java.io.File;
import java.nio.file.Path;
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 @@ -26,7 +24,7 @@ public interface Config {

Collection<String> getExterns();

Optional<TranslationsFileConfiguration> getTranslationsFile();
Map<String, Object> getTranslationsFile();

boolean getCheckAssertions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -97,7 +96,7 @@ public Task resolve(Project project, Config config) {
Collections.emptyList(),
Collections.emptyMap(),
Collections.emptyList(),//TODO actually pass these in when we can restrict and cache them sanely
Optional.empty(),
new TranslationsFileProcessor(config),
true,//TODO have this be passed in,
true,//default to true, will have no effect anyway
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.javascript.jscomp.CompilationLevel;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.DependencyOptions;
import com.vertispan.j2cl.build.TranslationsFileConfiguration;
import com.vertispan.j2cl.build.task.*;
import com.vertispan.j2cl.tools.Closure;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -170,19 +169,19 @@ 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<TranslationsFileConfiguration> translationsfile = config.getTranslationsFile();
translationsfile.ifPresent(file -> file.setProjectInputs(

TranslationsFileProcessor translationsFileProcessor = new TranslationsFileProcessor(config);
translationsFileProcessor.setProjectInputs(
Stream.concat(
Stream.of(project),
scope(project.getDependencies(), com.vertispan.j2cl.build.task.Dependency.Scope.RUNTIME).stream()
)
.map(p ->
input(p, OutputTypes.BYTECODE)
)
// Only include the .xtb
.map(i -> i.filter(XTB))
.collect(Collectors.toList())));
Stream.of(project),
scope(project.getDependencies(), com.vertispan.j2cl.build.task.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();
Expand Down Expand Up @@ -249,7 +248,7 @@ public void execute(TaskContext context) throws Exception {
entrypoint,
defines,
externs,
translationsfile,
translationsFileProcessor,
true,//TODO have this be passed in,
checkAssertions,
rewritePolyfills,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Special task that takes all jszips and makes a single bundle file from them. Might not be necessary, if we
Expand Down Expand Up @@ -55,7 +54,7 @@ public Task resolve(Project project, Config config) {
Collections.emptyList(),
Collections.emptyMap(),
Collections.emptyList(),
Optional.empty(),
new TranslationsFileProcessor(config),
true,
true,
false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.vertispan.j2cl.build;
package com.vertispan.j2cl.build.provided;

import com.vertispan.j2cl.build.task.CachedPath;
import com.vertispan.j2cl.build.task.Config;
import com.vertispan.j2cl.build.task.Input;
import com.vertispan.j2cl.build.task.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
Expand All @@ -18,46 +16,80 @@
import java.util.Optional;
import java.util.stream.Collectors;

public class TranslationsFileConfiguration {
public class TranslationsFileProcessor {

private boolean auto;
private final Config config;

private File file;
private XTBLookup lookup = new TranslationsFileNotDefined();

private Config config;
List<com.vertispan.j2cl.build.task.Input> jsSources;
private List<Input> jsSources;

public TranslationsFileConfiguration(Config config, File file) {
this.file = file;
public TranslationsFileProcessor(Config config) {
this.config = config;
if (!config.getTranslationsFile().isEmpty()) {
if (!config.getCompilationLevel().equals("ADVANCED")) {
//Do we have logger ?
System.out.println("translationsFile only works in the ADVANCED optimization level, in other levels the default messages values will be used");
} else {
if (config.getTranslationsFile().containsKey("file")) {
//translation file explicitly defined
lookup = new ExplicitlyDefined();
} else if (config.getTranslationsFile().containsKey("auto") && config.getTranslationsFile().get("auto").equals("true")) {
// we have to perform Project wide lookup
lookup = new ProjectLookup();
}
}
}
}

public TranslationsFileConfiguration(Config config, boolean auto) {
this.auto = auto;
this.config = config;
public Optional<File> getTranslationsFile() {
return lookup.getFile();
}

public void setProjectInputs(List<Input> collect) {
this.jsSources = collect;
}

private interface XTBLookup {

Optional<File> getFile();
}

public Optional<File> getFile() {
String locale = config.getDefines().get("goog.LOCALE");
private class TranslationsFileNotDefined implements XTBLookup {

if(locale == null) {
System.out.println("No goog.LOCALE set, skipping .xtb lookup");
@Override
public Optional<File> getFile() {
return Optional.empty();
}
}

private class ExplicitlyDefined implements XTBLookup {

@Override
public Optional<File> getFile() {
System.out.println("getFile " + config.getTranslationsFile().get("file"));

return Optional.of((File) config.getTranslationsFile().get("file"));
}
}

private class ProjectLookup implements XTBLookup {

if (auto) {
@Override
public Optional<File> getFile() {
List<File> temp = jsSources.stream()
.map(Input::getFilesAndHashes)
.flatMap(Collection::stream)
.map(CachedPath::getAbsolutePath)
.map(Path::toFile)
.collect(Collectors.toList());

if(temp.isEmpty()) {
if (temp.isEmpty()) {
System.out.println("no .xtb files was found");
}

String locale = config.getDefines().get("goog.LOCALE");

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

Expand All @@ -75,13 +107,13 @@ public Optional<File> getFile() {
doc.getDocumentElement().normalize();
NodeList translationbundleNode = doc.getElementsByTagName("translationbundle");

if(translationbundleNode.getLength() == 0) {
if (translationbundleNode.getLength() == 0) {
throw new RuntimeException(String.format("%s file has no translationbundle declaration", xtb));
}

String lang = translationbundleNode.item(0).getAttributes().getNamedItem("lang").getNodeValue();

if(locale.equals(lang)) {
if (locale.equals(lang)) {
return Optional.of(xtb);
}
}
Expand All @@ -92,11 +124,8 @@ public Optional<File> getFile() {
} catch (SAXException e) {
throw new RuntimeException(e);
}
return Optional.empty();
}
return Optional.ofNullable(file);
}

public void setProjectInputs(List<Input> collect) {
jsSources = collect;
}
}
18 changes: 5 additions & 13 deletions j2cl-tasks/src/main/java/com/vertispan/j2cl/tools/Closure.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.javascript.jscomp.*;
import com.google.javascript.jscomp.Compiler;
import com.vertispan.j2cl.build.DiskCache;
import com.vertispan.j2cl.build.TranslationsFileConfiguration;
import com.vertispan.j2cl.build.provided.TranslationsFileProcessor;
import com.vertispan.j2cl.build.task.BuildLog;
import com.vertispan.j2cl.build.task.Input;

Expand All @@ -16,7 +16,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class Closure {
Expand Down Expand Up @@ -55,7 +54,7 @@ public boolean compile(
List<String> entrypoints,
Map<String, String> defines,
Collection<String> externFiles,
Optional<TranslationsFileConfiguration> translationsFile,
TranslationsFileProcessor translationsFile,
boolean exportTestFunctions,
boolean checkAssertions,
boolean rewritePolyfills,
Expand Down Expand Up @@ -113,16 +112,9 @@ public boolean compile(
jscompArgs.add(extern);
}

translationsFile.ifPresent(file -> {
if(compilationLevel.equals(CompilationLevel.ADVANCED_OPTIMIZATIONS)) {
file.getFile().ifPresent(f -> {
jscompArgs.add("--translations_file");
jscompArgs.add(f.getAbsolutePath());
});

} else {
log.warn("translationsFile only works in the ADVANCED optimization level, in other levels the default messages values will be used");
}
translationsFile.getTranslationsFile().ifPresent(file -> {
jscompArgs.add("--translations_file");
jscompArgs.add(file.getAbsolutePath());
});

jscompArgs.add("--compilation_level");
Expand Down

0 comments on commit 3592365

Please sign in to comment.