Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code review feedback in a patch #1

Open
wants to merge 2 commits into
base: xtb_auto_discovery
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,6 @@ public Collection<String> getExterns() {
return Collections.emptySet();
}

@Override
public Map<String, Object> getTranslationsFile() {
ConfigValueProvider.ConfigNode translationsFile = config.findNode("translationsFile");
if (translationsFile == null) {
return Collections.emptyMap();
}
return translationsFile.getChildren().stream()
.collect(Collectors.toMap(ConfigValueProvider.ConfigNode::getName, e -> {
if(e.getName().equals("file")) {
return e.readFile();
} else {
return e.readString();
}
}));
}

@Override
public boolean getCheckAssertions() {
return Boolean.parseBoolean(getString("checkAssertions"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vertispan.j2cl.build.task;


import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
Expand All @@ -24,8 +23,6 @@ public interface Config {

Collection<String> getExterns();

Map<String, Object> getTranslationsFile();

boolean getCheckAssertions();

boolean getRewritePolyfills();
Expand Down
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 @@ -18,6 +18,7 @@
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 @@ -100,7 +101,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
new TranslationsFileProcessor(config),
Optional.empty(),
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 @@ -179,18 +179,15 @@ public Task resolve(Project project, Config config) {
//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();

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()));
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();
Expand Down Expand Up @@ -255,7 +252,7 @@ public void execute(TaskContext context) throws Exception {
entrypoint,
defines,
externs,
translationsFileProcessor,
translationsFileProcessor.getTranslationsFile(xtbInputs, context.log()),
true,//TODO have this be passed in,
checkAssertions,
rewritePolyfills,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.vertispan.j2cl.build.provided;

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

public class TranslationsFileProcessor {
public interface TranslationsFileProcessor {
public static TranslationsFileProcessor get(Config config) {
File file = config.getFile("translationsFile.file");
boolean auto = Boolean.parseBoolean(config.getString("translationsFile.auto"));

private final Config config;

private XTBLookup lookup = new TranslationsFileNotDefined();

private List<Input> jsSources;

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();
}
}
if ((auto || file != null) && !config.getCompilationLevel().equals("ADVANCED")) {
return new TranslationsFileNotDefined(true);
}
if (file != null) {
return (inputs, log) -> Optional.of(file);
} else if (auto) {
return new ProjectLookup(config);
}
return new TranslationsFileNotDefined(false);
}

public Optional<File> getTranslationsFile() {
return lookup.getFile();
}

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

private interface XTBLookup {
Optional<File> getTranslationsFile(List<Input> inputs, BuildLog log);

Optional<File> getFile();
}
class TranslationsFileNotDefined implements TranslationsFileProcessor {
private final boolean shouldWarn;

private class TranslationsFileNotDefined implements XTBLookup {
public TranslationsFileNotDefined(boolean shouldWarn) {
this.shouldWarn = shouldWarn;
}

@Override
public Optional<File> getFile() {
public Optional<File> getTranslationsFile(List<Input> inputs, BuildLog log) {
if (shouldWarn) {
log.warn("translationsFile only works in the ADVANCED optimization level, in other levels the default messages values will be used");
}

return Optional.empty();
}
}

private class ExplicitlyDefined implements XTBLookup {

@Override
public Optional<File> getFile() {
System.out.println("getFile " + config.getTranslationsFile().get("file"));
class ProjectLookup implements TranslationsFileProcessor {
private final String locale;

return Optional.of((File) config.getTranslationsFile().get("file"));
public ProjectLookup(Config config) {
locale = config.getString("defines.goog.LOCALE");
}
}

private class ProjectLookup implements XTBLookup {

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

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

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

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

Expand Down Expand Up @@ -117,13 +101,11 @@ public Optional<File> getFile() {
return Optional.of(xtb);
}
}
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
} catch (ParserConfigurationException | SAXException | IOException e) {
log.error("Error while reading xtb files ", e);
throw new RuntimeException(e);
}
log.warn("No matching locales for " + locale);
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.javascript.jscomp.*;
import com.google.javascript.jscomp.Compiler;
import com.vertispan.j2cl.build.DiskCache;
import com.vertispan.j2cl.build.provided.TranslationsFileProcessor;
import com.vertispan.j2cl.build.task.BuildLog;
import com.vertispan.j2cl.build.task.Input;

Expand All @@ -16,6 +15,7 @@
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 @@ -53,7 +53,7 @@ public boolean compile(
List<String> entrypoints,
Map<String, String> defines,
Collection<String> externFiles,
TranslationsFileProcessor translationsFile,
Optional<File> translationsFile,
boolean exportTestFunctions,
boolean checkAssertions,
boolean rewritePolyfills,
Expand Down Expand Up @@ -106,7 +106,7 @@ public boolean compile(
jscompArgs.add(extern);
}

translationsFile.getTranslationsFile().ifPresent(file -> {
translationsFile.ifPresent(file -> {
jscompArgs.add("--translations_file");
jscompArgs.add(file.getAbsolutePath());
});
Expand Down