-
Notifications
You must be signed in to change notification settings - Fork 2
Implement action execution system with variable substitution and documentation #46
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
Changes from 5 commits
274aeb6
734664e
20b9ce4
2d737db
99a247e
bb07e68
fad7e80
f4533dd
e2028b5
ef72e32
f2ca4ff
e1873c1
9d3be8b
b2604ed
1114385
803171d
b48c27d
fcacf03
9337631
b99a5a3
2f60ed5
b675734
0099220
a9fd7ce
1f1da03
cfa8bde
6e29589
8958d74
ec07563
63c13a3
1a2632a
c652724
3a298b8
772c5da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package org.codejive.jpm.util; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** Utility class for executing scripts with path conversion and variable substitution. */ | ||
| public class ScriptUtils { | ||
|
|
||
| /** | ||
| * Executes a script command with variable substitution and path conversion. | ||
| * | ||
| * @param command The command to execute | ||
| * @param classpath The classpath to use for ${deps} substitution | ||
| * @return The exit code of the executed command | ||
| * @throws IOException if an error occurred during execution | ||
| * @throws InterruptedException if the execution was interrupted | ||
| */ | ||
| public static int executeScript(String command, List<Path> classpath) | ||
| throws IOException, InterruptedException { | ||
| String processedCommand = processCommand(command, classpath); | ||
|
|
||
| // Split command into tokens for ProcessBuilder | ||
| String[] commandTokens = parseCommand(processedCommand); | ||
|
|
||
| ProcessBuilder pb = new ProcessBuilder(commandTokens); | ||
| pb.inheritIO(); // Connect to current process's stdin/stdout/stderr | ||
| Process process = pb.start(); | ||
|
|
||
| return process.waitFor(); | ||
| } | ||
|
|
||
| /** | ||
| * Processes a command by performing variable substitution and path conversion. | ||
| * | ||
| * @param command The raw command | ||
| * @param classpath The classpath to use for ${deps} substitution | ||
| * @return The processed command | ||
| */ | ||
| private static String processCommand(String command, List<Path> classpath) { | ||
| String result = command; | ||
|
|
||
| // Substitute ${deps} with the classpath | ||
| if (result.contains("${deps}")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed all variable substitution from
All 35 tests still pass with the new syntax. See commit b48c27d. |
||
| String classpathStr = ""; | ||
| if (classpath != null && !classpath.isEmpty()) { | ||
| classpathStr = classpath.stream() | ||
| .map(Path::toString) | ||
| .collect(Collectors.joining(File.pathSeparator)); | ||
| } | ||
| result = result.replace("${deps}", classpathStr); | ||
| } | ||
|
|
||
| // Convert Unix-style paths to Windows if needed | ||
| if (isWindows()) { | ||
| result = convertPathsForWindows(result); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Converts Unix-style paths to Windows format. | ||
| * This is a simple heuristic that looks for patterns like "deps/*" and converts them. | ||
| */ | ||
| private static String convertPathsForWindows(String command) { | ||
| // Convert forward slashes in path-like patterns to backslashes | ||
| // This is a simple heuristic - in a real implementation you might want more sophisticated logic | ||
| return command.replaceAll("([a-zA-Z0-9_.-]+)/\\*", "$1\\\\*") | ||
| .replaceAll("([a-zA-Z0-9_.-]+)/([a-zA-Z0-9_.-]+)", "$1\\\\$2"); | ||
| } | ||
|
|
||
| /** | ||
| * Parses a command string into tokens for ProcessBuilder. | ||
| * This is a simple implementation that splits on spaces while respecting quotes. | ||
| */ | ||
| private static String[] parseCommand(String command) { | ||
| // Simple parsing - for a full implementation you'd want proper shell parsing | ||
| java.util.List<String> tokens = new java.util.ArrayList<>(); | ||
| boolean inQuotes = false; | ||
| StringBuilder currentToken = new StringBuilder(); | ||
|
|
||
| for (char c : command.toCharArray()) { | ||
| if (c == '"' || c == '\'') { | ||
| inQuotes = !inQuotes; | ||
| } else if (c == ' ' && !inQuotes) { | ||
| if (currentToken.length() > 0) { | ||
| tokens.add(currentToken.toString()); | ||
| currentToken.setLength(0); | ||
| } | ||
| } else { | ||
| currentToken.append(c); | ||
| } | ||
| } | ||
|
|
||
| if (currentToken.length() > 0) { | ||
| tokens.add(currentToken.toString()); | ||
| } | ||
|
|
||
| return tokens.toArray(new String[0]); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the current operating system is Windows. | ||
| */ | ||
| private static boolean isWindows() { | ||
| String os = System.getProperty("os.name") | ||
| .toLowerCase(Locale.ENGLISH) | ||
| .replaceAll("[^a-z0-9]+", ""); | ||
| return os.startsWith("win"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please change "compile" to "build"