Look in path for windows applications

This commit is contained in:
crschnick 2023-07-21 17:01:17 +00:00
parent d01bf6bd48
commit c92dbf762d
3 changed files with 46 additions and 39 deletions

View file

@ -5,6 +5,7 @@ import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellControl;
import io.xpipe.core.process.ShellDialects;
import java.nio.file.Files;
import java.nio.file.Path;
@ -90,13 +91,35 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue {
}
}
public abstract static class WindowsFullPathType extends ExternalApplicationType {
public abstract static class WindowsType extends ExternalApplicationType {
public WindowsFullPathType(String id) {
private final String executable;
public WindowsType(String id, String executable) {
super(id);
this.executable = executable;
}
protected abstract Optional<Path> determinePath();
protected abstract Optional<Path> determineInstallationPath();
private Optional<Path> determineFromPath() {
// Try to locate if it is in the Path
try (var cc = LocalStore.getShell()
.command(ShellDialects.getPlatformDefault().getWhichCommand("code.cmd"))
.start()) {
var out = cc.readStdoutDiscardErr();
var exit = cc.getExitCode();
if (exit == 0) {
var first = out.lines().findFirst();
if (first.isPresent()) {
return first.map(Path::of);
}
}
} catch (Exception ex) {
ErrorEvent.fromThrowable(ex).omit().handle();
}
return Optional.empty();
}
@Override
public boolean isSelectable() {
@ -105,7 +128,7 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue {
@Override
public boolean isAvailable() {
var path = determinePath();
var path = determineInstallationPath();
return path.isPresent() && Files.exists(path.get());
}
}

View file

@ -1,12 +1,9 @@
package io.xpipe.app.prefs;
import io.xpipe.app.ext.PrefsChoiceValue;
import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.util.ApplicationHelper;
import io.xpipe.app.util.WindowsRegistry;
import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellDialects;
import java.io.IOException;
import java.nio.file.Path;
@ -17,14 +14,14 @@ import java.util.function.Supplier;
public interface ExternalEditorType extends PrefsChoiceValue {
ExternalEditorType NOTEPAD = new WindowsFullPathType("app.notepad") {
ExternalEditorType NOTEPAD = new WindowsType("app.notepad", "notepad") {
@Override
protected Optional<Path> determinePath() {
protected Optional<Path> determineInstallationPath() {
return Optional.of(Path.of(System.getenv("SystemRoot") + "\\System32\\notepad.exe"));
}
};
ExternalEditorType VSCODE_WINDOWS = new WindowsFullPathType("app.vscode") {
ExternalEditorType VSCODE_WINDOWS = new WindowsType("app.vscode", "code.cmd") {
@Override
public boolean canOpenDirectory() {
@ -32,23 +29,7 @@ public interface ExternalEditorType extends PrefsChoiceValue {
}
@Override
protected Optional<Path> determinePath() {
// Try to locate if it is in the Path
try (var cc = LocalStore.getShell()
.command(ShellDialects.getPlatformDefault().getWhichCommand("code.cmd"))
.start()) {
var out = cc.readStdoutDiscardErr();
var exit = cc.getExitCode();
if (exit == 0) {
var first = out.lines().findFirst();
if (first.isPresent()) {
return first.map(Path::of);
}
}
} catch (Exception ex) {
ErrorEvent.fromThrowable(ex).omit().handle();
}
protected Optional<Path> determineInstallationPath() {
return Optional.of(Path.of(System.getenv("LOCALAPPDATA"))
.resolve("Programs")
.resolve("Microsoft VS Code")
@ -61,10 +42,10 @@ public interface ExternalEditorType extends PrefsChoiceValue {
return false;
}
};
ExternalEditorType NOTEPADPLUSPLUS_WINDOWS = new WindowsFullPathType("app.notepad++") {
ExternalEditorType NOTEPADPLUSPLUS_WINDOWS = new WindowsType("app.notepad++", "notepad++") {
@Override
protected Optional<Path> determinePath() {
protected Optional<Path> determineInstallationPath() {
Optional<String> launcherDir;
launcherDir = WindowsRegistry.readString(WindowsRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\Notepad++", null)
.map(p -> p + "\\notepad++.exe");
@ -175,11 +156,14 @@ public interface ExternalEditorType extends PrefsChoiceValue {
}
}
abstract class WindowsFullPathType extends ExternalApplicationType.WindowsFullPathType
abstract class WindowsType extends ExternalApplicationType.WindowsType
implements ExternalEditorType {
public WindowsFullPathType(String id) {
super(id);
private final String executable;
public WindowsType(String id, String executable) {
super(id, executable);
this.executable = executable;
}
public boolean detach() {
@ -188,7 +172,7 @@ public interface ExternalEditorType extends PrefsChoiceValue {
@Override
public void launch(Path file) throws Exception {
var path = determinePath();
var path = determineInstallationPath();
if (path.isEmpty()) {
throw new IOException("Unable to find installation of " + toTranslatedString());
}

View file

@ -99,16 +99,16 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
return OsType.getLocal().equals(OsType.WINDOWS);
}
};
abstract class WindowsFullPathType extends ExternalApplicationType.WindowsFullPathType
abstract class WindowsType extends ExternalApplicationType.WindowsType
implements ExternalTerminalType {
public WindowsFullPathType(String id) {
super(id);
public WindowsType(String id, String executable) {
super(id, executable);
}
@Override
public void launch(String name, String file, boolean elevated) throws Exception {
var path = determinePath();
var path = determineInstallationPath();
if (path.isEmpty()) {
throw new IOException("Unable to find installation of " + toTranslatedString());
}
@ -120,7 +120,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
protected abstract String createCommand(ShellControl shellControl, String name, String path, String file);
}
ExternalTerminalType TABBY_WINDOWS = new WindowsFullPathType("app.tabbyWindows") {
ExternalTerminalType TABBY_WINDOWS = new WindowsType("app.tabbyWindows", "tabby") {
@Override
protected String createCommand(ShellControl shellControl, String name, String path, String file) {
@ -129,7 +129,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
}
@Override
protected Optional<Path> determinePath() {
protected Optional<Path> determineInstallationPath() {
Optional<String> launcherDir;
launcherDir = WindowsRegistry.readString(
WindowsRegistry.HKEY_CURRENT_USER,