Skip to content
Permalink
Browse files

Add edit-prefs command, which opens the prefs directory in a text editor

By default, vim/VSCode is used depending on platform, but this can be
overridden with environment variables, or by command line parameter.

Also fixed bug where the java version in the windows wrapper was
wrong.
  • Loading branch information...
LadyCailin committed Oct 3, 2019
1 parent a77ca4b commit ff15ef91681e0f37338cd911baedad356e627f49
@@ -51,7 +51,7 @@ class Program {
p.WaitForExit();
}
int javaVersion = int.Parse(strOutput);
if(javaVersion > 7) {
if(javaVersion > 8) {
ZipArchive zipFile = ZipFile.OpenRead(jarLocation);
ZipArchiveEntry file = zipFile.Entries.Where((ZipArchiveEntry e) => {
return e.FullName.Equals("interpreter-helpers/modules");
@@ -115,6 +115,7 @@ public CommandExecutor(String command) {
private Thread outThread;
private Thread errThread;
private Thread inThread;
private boolean inheritStandards = false;

public CommandExecutor(String... command) {
args = command;
@@ -128,6 +129,11 @@ public CommandExecutor(String... command) {
*/
public CommandExecutor start() throws IOException {
ProcessBuilder builder = new ProcessBuilder(args);
if(inheritStandards) {
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
}
builder.directory(workingDir);
process = builder.start();
outThread = new Thread(() -> {
@@ -319,6 +325,7 @@ public CommandExecutor setSystemInputsAndOutputs() {
setSystemOut(new CloseShieldOutputStream(StreamUtils.GetSystemOut()));
setSystemErr(new CloseShieldOutputStream(StreamUtils.GetSystemErr()));
setSystemIn(new CloseShieldInputStream(System.in));
inheritStandards = true;
return this;
}

@@ -151,7 +151,8 @@ public static CmdlineToolCollection GetCommandLineTools() {
InstallCmdlineMode.class,
NewMode.class,
NewTypeMode.class,
JavaVersionMode.class
JavaVersionMode.class,
EditPrefsMode.class
};

@SuppressWarnings("ResultOfObjectAllocationIgnored")
@@ -1574,6 +1575,11 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce
}
}

@Override
public boolean startupExtensionManager() {
return false;
}

}

@tool("help-topic")
@@ -1635,4 +1641,76 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce
}

}

@tool("edit-prefs")
public static class EditPrefsMode extends AbstractCommandLineTool {

@Override
public ArgumentParser getArgumentParser() {
return ArgumentParser.GetParser()
.addDescription("Launches the prefs directory in a default text editor, or your defined editor."
+ " The default varies based on OS and installed/detectible editors.")
.addArgument(new ArgumentBuilder()
.setDescription("Waits for the editor to finish. This is implied for some known programs,"
+ " where that is necessary (for instance known command line editors) but may be"
+ " specified manually. This is generally not necessary for GUI editors that"
+ " open in a new window.")
.asFlag()
.setName("wait"))
.addArgument(new ArgumentBuilder()
.setDescription("Uses a different command to open the editor. This overrides the environment"
+ " value (if set), but uses the same format. The value should follow the format"
+ " \"command %s\" where %s is replaced with the path to the prefs directory for"
+ " this installation. Instead of setting this in command line mode, you can also"
+ " set the \"MS_EDITOR\" environment variable.")
.setUsageName("command")
.setOptionalAndDefault()
.setArgType(ArgumentBuilder.BuilderTypeNonFlag.STRING)
.setDefaultVal(""));
}

@Override
public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exception {
Implementation.forceServerType(Implementation.Type.SHELL);
if(!MethodScriptFileLocations.getDefault()
.getPreferencesDirectory().exists()) {
System.err.println("Prefs directory does not exist!");
System.exit(1);
}
String cmd;
boolean wait = false;
String[] needsWait = new String[] {"vim", "nano"};
if(OSUtils.GetOS() == OSUtils.OS.WINDOWS) {
cmd = "code.cmd %s";
} else {
cmd = "vim %s";
}


if(System.getenv("MS_EDITOR") != null && !System.getenv("MS_EDITOR").equals("")) {
cmd = System.getenv("MS_EDITOR");
}
if(!parsedArgs.getStringArgument().equals("")) {
cmd = parsedArgs.getStringArgument();
}

for(String nw : needsWait) {
if(cmd.startsWith(nw)) {
wait = true;
break;
}
}

CommandExecutor c = new CommandExecutor(String.format(cmd, "\"" + MethodScriptFileLocations.getDefault()
.getPreferencesDirectory()) + "\"");
c.setSystemInputsAndOutputs();
c.start();
if(parsedArgs.isFlagSet("wait") || wait) {
c.waitFor();
}

System.exit(0);
}

}
}
Binary file not shown.

0 comments on commit ff15ef9

Please sign in to comment.
You can’t perform that action at this time.