Skip to content

Commit

Permalink
chore: changed many File references to Path
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse committed Jun 15, 2022
1 parent a77e83a commit 82d955c
Show file tree
Hide file tree
Showing 38 changed files with 209 additions and 198 deletions.
8 changes: 4 additions & 4 deletions src/main/java/dev/jbang/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ public static Configuration defaults() {
}

public static Configuration get(Path catalogPath) {
return get(ResourceRef.forNamedFile(catalogPath.toString(), catalogPath.toFile()));
return get(ResourceRef.forNamedFile(catalogPath.toString(), catalogPath));
}

private static Configuration get(ResourceRef ref) {
Path configPath = ref.getFile().toPath();
Path configPath = ref.getFile();
Configuration cfg = read(configPath);
cfg.storeRef = ref;
return cfg;
Expand All @@ -251,7 +251,7 @@ public static Configuration getMerged() {
Configuration result = defaults();
for (Path cfgFile : configFiles) {
Configuration cfg = read(cfgFile);
cfg.storeRef = ResourceRef.forFile(cfgFile.toFile());
cfg.storeRef = ResourceRef.forFile(cfgFile);
cfg.fallback = result;
result = cfg;
}
Expand All @@ -264,7 +264,7 @@ public static Configuration getBuiltin() {
String res = "classpath:/" + JBANG_CONFIG_PROPS;
ResourceRef cfgRef = ResourceRef.forResource(res);
if (cfgRef != null) {
Path catPath = cfgRef.getFile().toPath();
Path catPath = cfgRef.getFile();
Configuration cfg = read(catPath);
cfg.storeRef = cfgRef;
return cfg;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/dev/jbang/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String getScriptBase() {
return "classpath:" + ((baseRef != null) ? "/" + baseRef : "");
}
Path result;
Path catFile = catalogRef.getFile().toPath();
Path catFile = catalogRef.getFile();
if (baseRef != null) {
if (!Util.isRemoteRef(baseRef)) {
Path base = Paths.get(baseRef);
Expand Down Expand Up @@ -132,7 +132,7 @@ String relativize(String scriptRef) {
}

void write() throws IOException {
write(catalogRef.getFile().toPath(), this);
write(catalogRef.getFile(), this);
}

/**
Expand Down Expand Up @@ -161,7 +161,7 @@ public static Path getCatalogFile(Path catFile) {
if (catFile == null) {
Catalog catalog = findNearestCatalog(Util.getCwd());
if (catalog != null && !catalog.catalogRef.isClasspath()) {
catFile = catalog.catalogRef.getFile().toPath();
catFile = catalog.catalogRef.getFile();
} else {
// This is here as a backup for when the user catalog doesn't
// exist yet, because `findNearestCatalog()` only returns
Expand Down Expand Up @@ -235,7 +235,7 @@ public static Catalog getMerged(boolean includeImplicits) {
Catalog result = Catalog.empty();
for (Catalog catalog : catalogs) {
if (!includeImplicits
&& catalog.catalogRef.getFile().toPath().equals(Settings.getUserImplicitCatalogFile())) {
&& catalog.catalogRef.getFile().equals(Settings.getUserImplicitCatalogFile())) {
continue;
}
merge(catalog, result);
Expand Down Expand Up @@ -284,12 +284,12 @@ static Catalog findNearestCatalogWith(Path dir, Function<Catalog, Boolean> accep
}

public static Catalog get(Path catalogPath) {
return get(ResourceRef.forFile(catalogPath.toFile()));
return get(ResourceRef.forFile(catalogPath));
}

private static Catalog get(ResourceRef ref) {
Catalog catalog;
Path catalogPath = ref.getFile().toPath();
Path catalogPath = ref.getFile();
if (Util.isFresh() || !catalogCache.containsKey(catalogPath.toString())) {
if (Files.isDirectory(catalogPath)) {
catalogPath = catalogPath.resolve(Catalog.JBANG_CATALOG_JSON);
Expand Down Expand Up @@ -327,7 +327,7 @@ public static Catalog getBuiltin() {
String res = "classpath:/" + JBANG_CATALOG_JSON;
ResourceRef catRef = ResourceRef.forResource(res);
if (catRef != null) {
Path catPath = catRef.getFile().toPath();
Path catPath = catRef.getFile();
catalog = read(catPath);
catalog.catalogRef = catRef;
catalogCache.put(CACHE_BUILTIN, catalog);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static boolean install(String name, String scriptRef, boolean force, bool
}
}
if (!ctx.isAlias() && !DependencyUtil.looksLikeAGav(scriptRef) && !code.getResourceRef().isURL()) {
scriptRef = code.getResourceRef().getFile().getAbsolutePath();
scriptRef = code.getResourceRef().getFile().toAbsolutePath().toString();
}
if (code.needsBuild(ctx)) {
code.builder(ctx).build();
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/dev/jbang/cli/Edit.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ public Integer doCall() throws IOException {

private void watchForChanges(Code code, Callable<Object> action) throws IOException {
try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {
File orginalFile = code.getResourceRef().getFile();
if (!orginalFile.exists()) {
Path orginalFile = code.getResourceRef().getFile();
if (!Files.exists(orginalFile)) {
throw new ExitException(EXIT_UNEXPECTED_STATE,
"Cannot live edit " + code.getResourceRef().getOriginalResource());
}
Path watched = orginalFile.getAbsoluteFile().getParentFile().toPath();
Path watched = orginalFile.toAbsolutePath().getParent();
watched.register(watchService,
StandardWatchEventKinds.ENTRY_MODIFY);
info("Watching for changes in " + watched);
Expand All @@ -110,7 +110,7 @@ private void watchForChanges(Code code, Callable<Object> action) throws IOExcept
// but relative to the watched directory
final Path changed = watched.resolve((Path) event.context());
verboseMsg("Changed file: " + changed.toString());
if (Files.isSameFile(orginalFile.toPath(), changed)) {
if (Files.isSameFile(orginalFile, changed)) {
try {
action.call();
} catch (RuntimeException ee) {
Expand Down Expand Up @@ -259,19 +259,19 @@ private static void showStartingMsg(String ed, boolean showConfig) {

/** Create Project to use for editing **/
Path createProjectForLinkedEdit(SourceSet ss, RunContext ctx, boolean reload) throws IOException {
File originalFile = ss.getResourceRef().getFile();
Path originalFile = ss.getResourceRef().getFile();

List<String> dependencies = ss.getDependencies();
String cp = ctx.resolveClassPath(ss).getClassPath();
List<String> resolvedDependencies = Arrays.asList(cp.split(CP_SEPARATOR));

Path baseDir = Settings.getCacheDir(Cache.CacheClass.projects);

String name = originalFile.getName();
String name = originalFile.getFileName().toString();
name = Util.unkebabify(name);

Path tmpProjectDir = baseDir.resolve(name + "_jbang_" +
Util.getStableID(originalFile.getAbsolutePath()));
Util.getStableID(originalFile.toAbsolutePath().toString()));
Util.mkdirs(tmpProjectDir);
tmpProjectDir = tmpProjectDir.resolve(stripPrefix(name));
Util.mkdirs(tmpProjectDir);
Expand All @@ -280,27 +280,27 @@ Path createProjectForLinkedEdit(SourceSet ss, RunContext ctx, boolean reload) th
Util.mkdirs(srcDir);

Path srcFile = srcDir.resolve(name);
Util.createLink(srcFile, originalFile.toPath());
Util.createLink(srcFile, originalFile);

for (ResourceRef sourceRef : ss.getSources()) {
Path sfile = null;
Source src = ctx.createSource(sourceRef);
if (src.getJavaPackage().isPresent()) {
Path packageDir = srcDir.resolve(src.getJavaPackage().get().replace(".", File.separator));
Util.mkdirs(packageDir);
sfile = packageDir.resolve(sourceRef.getFile().getName());
sfile = packageDir.resolve(sourceRef.getFile().getFileName());
} else {
sfile = srcDir.resolve(sourceRef.getFile().getName());
sfile = srcDir.resolve(sourceRef.getFile().getFileName());
}
Path destFile = sourceRef.getFile().toPath().toAbsolutePath();
Path destFile = sourceRef.getFile().toAbsolutePath();
Util.createLink(sfile, destFile);
}

for (RefTarget ref : ss.getResources()) {
Path target = ref.to(srcDir);
;
Util.mkdirs(target.getParent());
Util.createLink(target, ref.getSource().getFile().toPath().toAbsolutePath());
Util.createLink(target, ref.getSource().getFile().toAbsolutePath());
}

// create build gradle
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/dev/jbang/cli/Export.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public int apply(ExportMixin exportMixin, Code code, RunContext ctx) throws IOEx

Path outputPath = exportMixin.getFileOutputPath();
// Copy the JAR or native binary
Path source = code.getJarFile().toPath();
Path source = code.getJarFile();
if (exportMixin.buildMixin.nativeImage) {
source = getImageName(source.toFile()).toPath();
source = getImageName(source);
}

if (outputPath.toFile().exists()) {
Expand Down Expand Up @@ -117,9 +117,9 @@ public int apply(ExportMixin exportMixin, Code code, RunContext ctx) throws IOEx
Path outputPath = exportMixin.getFileOutputPath();

// Copy the JAR or native binary
Path source = code.getJarFile().toPath();
Path source = code.getJarFile();
if (exportMixin.buildMixin.nativeImage) {
source = getImageName(source.toFile()).toPath();
source = getImageName(source);
}

if (outputPath.toFile().exists()) {
Expand Down Expand Up @@ -215,9 +215,9 @@ public int apply(ExportMixin exportMixin, Code code, RunContext ctx) throws IOEx
outputPath = Settings.getLocalMavenRepo();
}
// Copy the JAR or native binary
Path source = code.getJarFile().toPath();
Path source = code.getJarFile();
if (exportMixin.buildMixin.nativeImage) {
source = getImageName(source.toFile()).toPath();
source = getImageName(source);
}

if (!outputPath.toFile().isDirectory()) {
Expand Down Expand Up @@ -253,7 +253,8 @@ public int apply(ExportMixin exportMixin, Code code, RunContext ctx) throws IOEx
}
Path groupdir = outputPath.resolve(Paths.get(group.replace(".", "/")));

artifact = artifact != null ? artifact : Util.getBaseName(code.getResourceRef().getFile().getName());
artifact = artifact != null ? artifact
: Util.getBaseName(code.getResourceRef().getFile().getFileName().toString());
Path artifactDir = groupdir.resolve(artifact);

version = version != null ? version : "999-SNAPSHOT";
Expand Down Expand Up @@ -289,7 +290,8 @@ public int apply(ExportMixin exportMixin, Code code, RunContext ctx) throws IOEx

String pomfile = pomTemplate
.data("baseName",
Util.getBaseName(code.getResourceRef().getFile().getName()))
Util.getBaseName(
code.getResourceRef().getFile().getFileName().toString()))
.data("group", group)
.data("artifact", artifact)
.data("version", version)
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/dev/jbang/cli/ExportMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static dev.jbang.source.builders.BaseBuilder.getImageName;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand Down Expand Up @@ -42,7 +41,7 @@ Path getFileOutputPath() {
} else {
String outName = CatalogUtil.nameFromRef(scriptMixin.scriptOrFile);
if (buildMixin.nativeImage) {
outName = getImageName(new File(outName)).getName();
outName = getImageName(Paths.get(outName)).getFileName().toString();
} else {
outName += ".jar";
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/cli/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ScriptInfo(Code code, RunContext ctx) {
init(source);

if (ctx != null) {
applicationJar = code.getJarFile() == null ? null : code.getJarFile().getAbsolutePath();
applicationJar = code.getJarFile() == null ? null : code.getJarFile().toAbsolutePath().toString();
mainClass = ctx.getMainClassOr(code);
requestedJavaVersion = code.getJavaVersion();
availableJdkPath = Objects.toString(JdkManager.getCurrentJdk(requestedJavaVersion), null);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/cli/Init.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static Path resolveBaseName(String refTarget, String refSource, String outName)
private void renderQuteTemplate(Path outFile, ResourceRef templateRef, Map<String, Object> properties)
throws IOException {
Util.verboseMsg("Rendering template " + templateRef.getOriginalResource() + " to " + outFile);
renderQuteTemplate(outFile, templateRef.getFile().getAbsolutePath(), properties);
renderQuteTemplate(outFile, templateRef.getFile().toAbsolutePath().toString(), properties);
}

void renderQuteTemplate(Path outFile, String templatePath) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/cli/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static Map.Entry<String, String> splitFileRef(String fileRef) {
private static boolean refExists(Map.Entry<String, String> splitRef) {
String source = splitRef.getValue();
ResourceRef resourceRef = ResourceRef.forResource(source);
if (resourceRef == null || !resourceRef.getFile().canRead()) {
if (resourceRef == null || !Files.isReadable(resourceRef.getFile())) {
throw new ExitException(BaseCommand.EXIT_INVALID_INPUT,
"File could not be found or read: '" + source + "'");
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/dev/jbang/dependencies/ArtifactInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.jbang.dependencies;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate;
Expand All @@ -11,16 +12,16 @@
public class ArtifactInfo {

private final MavenCoordinate coordinate;
private final File file;
private final Path file;
private final long timestamp;

ArtifactInfo(MavenCoordinate coordinate, File file) {
ArtifactInfo(MavenCoordinate coordinate, Path file) {
this.coordinate = coordinate;
this.file = file;
this.timestamp = file.exists() ? file.lastModified() : 0;
this.timestamp = Files.exists(file) ? file.toFile().lastModified() : 0;
}

ArtifactInfo(MavenCoordinate coordinate, File file, long cachedTimestamp) {
ArtifactInfo(MavenCoordinate coordinate, Path file, long cachedTimestamp) {
this.coordinate = coordinate;
this.file = file;
this.timestamp = cachedTimestamp;
Expand All @@ -30,7 +31,7 @@ public MavenCoordinate getCoordinate() {
return coordinate;
}

public File getFile() {
public Path getFile() {
return file;
}

Expand All @@ -39,11 +40,11 @@ public long getTimestamp() {
}

public boolean isUpToDate() {
return file.canRead() && timestamp == file.lastModified();
return Files.isReadable(file) && timestamp == file.toFile().lastModified();
}

public String toString() {
String path = getFile().getAbsolutePath();
String path = getFile().toAbsolutePath().toString();
return getCoordinate() == null ? "<null>" : getCoordinate().toCanonicalForm() + "=" + path;
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/dev/jbang/dependencies/DependencyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import static dev.jbang.util.Util.warnMsg;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -38,7 +39,7 @@ private static Map<String, List<ArtifactInfo>> getCache() {
JsonDeserializer<ArtifactInfo> serializer = (json, typeOfT, context) -> {
JsonObject jsonObject = json.getAsJsonObject();
MavenCoordinate gav = MavenCoordinates.createCoordinate(jsonObject.get("gav").getAsString());
File file = new File(jsonObject.get("file").getAsString());
Path file = Paths.get(jsonObject.get("file").getAsString());
long ts = jsonObject.has("ts") ? jsonObject.get("ts").getAsLong() : 0;
return new ArtifactInfo(gav, file, ts);
};
Expand Down Expand Up @@ -71,7 +72,7 @@ public static void cache(String depsHash, List<ArtifactInfo> artifacts) {
JsonSerializer<ArtifactInfo> serializer = (src, typeOfSrc, context) -> {
JsonObject json = new JsonObject();
json.addProperty("gav", src.getCoordinate().toCanonicalForm());
json.addProperty("file", src.getFile().getPath());
json.addProperty("file", src.getFile().toString());
json.addProperty("ts", src.getTimestamp());
return json;
};
Expand Down Expand Up @@ -102,7 +103,7 @@ public static List<ArtifactInfo> findDependenciesByHash(String depsHash) {
return null;
}

public static ArtifactInfo findArtifactByPath(File artifactPath) {
public static ArtifactInfo findArtifactByPath(Path artifactPath) {
Map<String, List<ArtifactInfo>> cache = getCache();
Optional<ArtifactInfo> result = cache .values()
.stream()
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/jbang/dependencies/DependencyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public DependencyResolver addArtifacts(List<ArtifactInfo> artifacts) {
}

public DependencyResolver addClassPath(String classPath) {
return addArtifact(DependencyCache.findArtifactByPath(new File(classPath)));
// WARN need File here because it's more lenient about paths than Path!
return addArtifact(DependencyCache.findArtifactByPath(new File(classPath).toPath()));
}

public DependencyResolver addClassPaths(List<String> classPaths) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/dependencies/DependencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public static List<ArtifactInfo> resolveDependenciesViaAether(List<String> depId
infoMsgFmt("Done\n");

return artifacts.stream()
.map(mra -> new ArtifactInfo(mra.getCoordinate(), mra.asFile()))
.map(mra -> new ArtifactInfo(mra.getCoordinate(), mra.asFile().toPath()))
.collect(Collectors.toList());
} catch (ResolutionException nrr) {
Throwable cause = nrr.getCause();
Expand Down
Loading

0 comments on commit 82d955c

Please sign in to comment.