Skip to content

Commit

Permalink
transpiler, maven-plugin: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Dec 9, 2023
1 parent 2b9bf30 commit 3ff1de6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
*/
@Mojo(name = "compile", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true, //
requiresDependencyCollection = ResolutionScope.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE)
@SuppressWarnings({"null", "PMD.GuardLogStatement", "PMD.CouplingBetweenObjects"})
@SuppressWarnings({
"null", "PMD.GuardLogStatement", "PMD.CouplingBetweenObjects", "PMD.CyclomaticComplexity"})
public class JaclineCompileMojo extends AbstractMojo {
private static final Pattern FILE_DEDUP_SUFFIX = Pattern.compile("(\\-[0-9]+)?(\\.[a-z]+)$");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,38 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.jspecify.nullness.Nullable;

import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.javascript.jscomp.SourceMap.LocationMapping;
import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
import com.kohlschutter.jacline.IOUtil;

@SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
public class SourceMapLocationMapping implements LocationMapping, Closeable {
private static final String META_INF_JACLINE = "/META-INF/jacline/";
private static final String TARGET = "/target/classes";
private static final String TARGET_ECLIPSE = "/target-eclipse/classes";

private static final HashFunction SHA_256 = Hashing.sha256();

private Map<String, FileSystem> fileSystems = new HashMap<>();
private Map<String, String> prefixHash = new HashMap<>();
private List<FileSystem> fileSystemsToClose = new ArrayList<>();
private final Map<String, FileSystem> fileSystems = new HashMap<>();
private final Map<String, String> prefixHash = new HashMap<>();
private final List<FileSystem> fileSystemsToClose = new ArrayList<>();

private String mainSourceMapContents;
private final Path sourceMapWorkDir;
private final String hostPrefix;

public SourceMapLocationMapping(Path sourceMapWorkDir, URI prefix) throws IOException {
this.sourceMapWorkDir = sourceMapWorkDir;
hostPrefix = stripTrailingSlash(prefix.resolve("/").toASCIIString());
if (Files.exists(sourceMapWorkDir)) {
IOUtil.deleteRecursively(sourceMapWorkDir.resolve("sourcemaps/jacline"));
}
Files.createDirectories(sourceMapWorkDir);

hostPrefix = stripTrailingSlash(prefix.resolve("/").toASCIIString());
}

private static String stripTrailingSlash(String s) {
Expand Down Expand Up @@ -129,8 +130,9 @@ private Path resolveRelativePath(Path base, String path) {
return base.resolve(path);
}

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
private void addFile(Path sourceFile, String mappedPath) throws IOException {
Path targetPath = resolveRelativePath(sourceMapWorkDir, mappedPath);
Path targetPath = resolveRelativePath(Objects.requireNonNull(sourceMapWorkDir), mappedPath);
Files.createDirectories(targetPath.getParent());

try {
Expand All @@ -140,9 +142,10 @@ private void addFile(Path sourceFile, String mappedPath) throws IOException {
}
}

private void addFile(String source, String mappedPath) {
Path targetPath = resolveRelativePath(sourceMapWorkDir, mappedPath);
targetPath.toFile().getParentFile().mkdirs();
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
private void addFile(String source, String mappedPath) throws IOException {
Path targetPath = resolveRelativePath(Objects.requireNonNull(sourceMapWorkDir), mappedPath);
Files.createDirectories(targetPath.getParent());

try (Writer out = Files.newBufferedWriter(targetPath)) {
out.write(source);
Expand Down Expand Up @@ -228,7 +231,9 @@ private FileSystem getFileSystem(String fsPart) {
} catch (FileSystemAlreadyExistsException e1) {
fs = FileSystems.getFileSystem(u);
} catch (IOException e1) {
throw new IllegalStateException(e);
IllegalStateException ex = new IllegalStateException(e);
ex.addSuppressed(e1);
throw ex;
}
}
if (close) {
Expand All @@ -253,11 +258,8 @@ private Path locationToPath(String location) {
}
}

public String getMainSourceMapContents() {
return mainSourceMapContents;
}

public String initMainSourceMap(String outputFileName, String sourceMapContents) {
public String initMainSourceMap(String outputFileName, String sourceMapContents)
throws IOException {
String mapped = "/sourcemaps/jacline/by-content/" + SHA_256.hashString(sourceMapContents,
StandardCharsets.UTF_8) + "/" + projectNameFromLastPathElement(outputFileName) + ".map";
addFile(sourceMapContents, mapped);
Expand Down

0 comments on commit 3ff1de6

Please sign in to comment.