Skip to content
Merged
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
1 change: 1 addition & 0 deletions app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ actions:
build: ./mvnw spotless:apply package -DskipTests
run: ./target/binary/bin/jpm
test: ./mvnw test
format: ./mvnw spotless:apply
buildj: javac -cp {{deps}} -d classes --source-path src/main/java src/main/java/org/codejive/jpm/Main.java
runj: java -cp classes:{{deps}} org.codejive.jpm.Main
54 changes: 35 additions & 19 deletions src/main/java/org/codejive/jpm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import org.codejive.jpm.config.UserConfig;
import org.codejive.jpm.search.Search.Backends;
import org.codejive.jpm.util.FileUtils;
import org.codejive.jpm.util.SyncResult;
import org.codejive.jpm.util.Version;
import org.jline.consoleui.elements.InputValue;
Expand Down Expand Up @@ -612,13 +614,15 @@ String actionName() {
}

static class DepsMixin {
private static final String DEFAULT_DIRECTORY = "deps";

// Cached user configuration loaded from ~/.config/jpm/config.yml or ~/.jpmcfg.yml
private transient UserConfig userConfig;

@Option(
names = {"-d", "--directory"},
description = "Directory to copy artifacts to (default: 'deps')")
Path directory;
String directory;

@Option(
names = {"-L", "--no-links"},
Expand All @@ -636,7 +640,7 @@ static class DepsMixin {
names = {"-c", "--cache"},
description =
"Directory where downloaded artifacts will be cached (default: value of JPM_CACHE environment variable; whatever is set in Maven's settings.xml or $HOME/.m2/repository")
Path cacheDir;
String cacheDir;

/**
* Loads and caches the user configuration. Priority: --config option > JPM_CONFIG env var >
Expand All @@ -658,21 +662,19 @@ UserConfig getUserConfig() {
* @return The cache directory path or null to use Maven default
*/
Path getCacheDir() {
if (cacheDir != null) {
return cacheDir;
String dir = cacheDir;
if (dir == null) {
dir = getUserConfig().cache();
}
Path userConfigCache = getUserConfig().cache();
if (userConfigCache != null) {
return userConfigCache;
if (dir == null) {
dir = System.getenv("JPM_CACHE");
}
String envCache = System.getenv("JPM_CACHE");
if (envCache != null && !envCache.isEmpty()) {
if (dir != null && !dir.isEmpty()) {
try {
return Path.of(envCache);
return FileUtils.expandHomePath(dir);
} catch (InvalidPathException e) {
System.err.println(
"Warning: Invalid path in JPM_CACHE environment variable, ignoring: "
+ envCache);
"Warning: The specified cache path is invalid, ignoring: " + dir);
}
}
return null;
Expand All @@ -685,14 +687,28 @@ Path getCacheDir() {
* @return The directory path
*/
Path getDirectory() {
if (directory != null) {
return directory; // User explicitly set via CLI
String dir = directory;
if (dir == null) {
dir = getUserConfig().directory();
}
if (dir == null) {
dir = DEFAULT_DIRECTORY;
}
if (dir == null || dir.isEmpty()) {
return null;
}
Path result;
try {
result = FileUtils.expandHomePath(dir);
} catch (InvalidPathException e) {
throw new IllegalArgumentException(
"The specified output directory path is invalid: " + dir);
}
Path userConfigDir = getUserConfig().directory();
if (userConfigDir != null) {
return userConfigDir; // From user config
if (Files.isRegularFile(result)) {
throw new IllegalArgumentException(
"The specified output directory is a file, not a directory: " + dir);
}
return Path.of("deps"); // Hardcoded default
return result;
}

/**
Expand Down Expand Up @@ -812,7 +828,7 @@ private static void printStats(SyncResult stats) {

static CommandLine.IExecutionExceptionHandler errorHandler =
(ex, commandLine, parseResult) -> {
System.err.println("Error: " + ex.getMessage());
System.err.println("Error: " + ex);
if (verbose) {
ex.printStackTrace();
} else {
Expand Down
29 changes: 6 additions & 23 deletions src/main/java/org/codejive/jpm/config/UserConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* options.
*/
public class UserConfig {
private Path cache;
private Path directory;
private String cache;
private String directory;
private Boolean noLinks;
private final Map<String, String> repositories = new LinkedHashMap<>();

Expand All @@ -26,11 +26,11 @@ public class UserConfig {
/** The fallback user config file location. */
public static final String USER_CONFIG_FILE_FALLBACK = ".jpmcfg.yml";

public Path cache() {
public String cache() {
return cache;
}

public Path directory() {
public String directory() {
return directory;
}

Expand Down Expand Up @@ -152,8 +152,7 @@ private static UserConfig readFromReader(Reader in) {
if (config.containsKey("cache")) {
Object cacheObj = config.get("cache");
if (cacheObj instanceof String) {
String cachePath = (String) cacheObj;
userConfig.cache = expandHomePath(cachePath);
userConfig.cache = (String) cacheObj;
} else {
System.err.println(
"Warning: 'cache' must be a string path, ignoring: " + cacheObj);
Expand All @@ -164,8 +163,7 @@ private static UserConfig readFromReader(Reader in) {
if (config.containsKey("directory")) {
Object dirObj = config.get("directory");
if (dirObj instanceof String) {
String dirPath = (String) dirObj;
userConfig.directory = expandHomePath(dirPath);
userConfig.directory = (String) dirObj;
} else {
System.err.println(
"Warning: 'directory' must be a string path, ignoring: " + dirObj);
Expand Down Expand Up @@ -207,19 +205,4 @@ private static UserConfig readFromReader(Reader in) {

return userConfig;
}

/**
* Expands a path starting with "~/" to use the user's home directory. Handles cross-platform
* paths.
*
* @param pathStr The path string to expand
* @return A Path with "~/" expanded to the user's home directory
*/
private static Path expandHomePath(String pathStr) {
if (pathStr.startsWith("~/")) {
String userHome = System.getProperty("user.home");
return Paths.get(userHome, pathStr.substring(2));
}
return Paths.get(pathStr);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/codejive/jpm/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,18 @@ public static Path safePath(String w) {
return null;
}
}

/**
* Expands a path starting with "~/" to use the user's home directory.
*
* @param pathStr The path string to expand
* @return A Path with "~/" expanded to the user's home directory
*/
public static Path expandHomePath(String pathStr) {
if (pathStr.startsWith("~/")) {
String userHome = System.getProperty("user.home");
return Paths.get(userHome, pathStr.substring(2));
}
return Paths.get(pathStr);
}
}
Loading