Skip to content

Commit

Permalink
More browser improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed Feb 25, 2023
1 parent e0b34ff commit 26a7592
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.xpipe.app.ext.PrefsChoiceValue;
import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.store.ShellStore;
Expand Down Expand Up @@ -76,7 +77,7 @@ public PathApplication(String id, String executable) {
}

public boolean isAvailable() {
try (ShellProcessControl pc = ShellStore.local().create().start()) {
try (ShellProcessControl pc = LocalStore.getShell()) {
return pc.executeBooleanSimpleCommand(pc.getShellDialect().getWhichCommand(executable));
} catch (Exception e) {
ErrorEvent.fromThrowable(e).omit().handle();
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.util.ApplicationHelper;
import io.xpipe.app.util.MacOsPermissions;
import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.store.ShellStore;

import java.util.List;

Expand Down Expand Up @@ -133,7 +133,7 @@ public MacOsTerminalType() {

@Override
public void launch(String name, String command) throws Exception {
try (ShellProcessControl pc = ShellStore.local().create().start()) {
try (ShellProcessControl pc = LocalStore.getShell()) {
var suffix = command.equals(pc.getShellDialect().getNormalOpenCommand())
? "\"\""
: "\"" + command.replaceAll("\"", "\\\\\"") + "\"";
Expand All @@ -157,7 +157,7 @@ public void launch(String name, String command) throws Exception {
}

var format = custom.contains("$cmd") ? custom : custom + " $cmd";
try (var pc = ShellStore.local().create().start()) {
try (var pc = LocalStore.getShell()) {
var toExecute = format.replace("$cmd", command);
if (pc.getOsType().equals(OsType.WINDOWS)) {
toExecute = "start \"" + name + "\" " + toExecute;
Expand Down Expand Up @@ -187,7 +187,7 @@ public ITerm2Type() {

@Override
public void launch(String name, String command) throws Exception {
try (ShellProcessControl pc = ShellStore.local().create().start()) {
try (ShellProcessControl pc = LocalStore.getShell()) {
var cmd = String.format(
"""
osascript - "$@" <<EOF
Expand Down Expand Up @@ -225,7 +225,7 @@ public void launch(String name, String command) throws Exception {
return;
}

try (ShellProcessControl pc = ShellStore.local().create().start()) {
try (ShellProcessControl pc = LocalStore.getShell()) {
var cmd = String.format(
"""
osascript - "$@" <<EOF
Expand Down Expand Up @@ -258,7 +258,7 @@ public SimpleType(String id, String executable, String displayName) {

@Override
public void launch(String name, String command) throws Exception {
try (ShellProcessControl pc = ShellStore.local().create().start()) {
try (ShellProcessControl pc = LocalStore.getShell()) {
ApplicationHelper.checkSupport(pc, executable, displayName);

var toExecute = executable + " " + toCommand(name, command);
Expand All @@ -274,7 +274,7 @@ public void launch(String name, String command) throws Exception {
protected abstract String toCommand(String name, String command);

public boolean isAvailable() {
try (ShellProcessControl pc = ShellStore.local().create().start()) {
try (ShellProcessControl pc = LocalStore.getShell()) {
return pc.executeBooleanSimpleCommand(pc.getShellDialect().getWhichCommand(executable));
} catch (Exception e) {
ErrorEvent.fromThrowable(e).omit().handle();
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/io/xpipe/app/util/ApplicationHelper.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.xpipe.app.util;

import io.xpipe.app.issue.TrackEvent;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.ShellDialects;
import io.xpipe.core.store.ShellStore;
import io.xpipe.core.process.ShellProcessControl;

import java.io.IOException;
import java.util.List;
Expand All @@ -15,7 +15,7 @@ public static void executeLocalApplication(String s) throws Exception {
TrackEvent.withDebug("proc", "Executing local application")
.elements(args)
.handle();
try (var c = ShellStore.local().create().command(s).start()) {
try (var c = LocalStore.getShell().command(s).start()) {
c.discardOrThrow();
}
}
Expand All @@ -25,7 +25,7 @@ public static void executeLocalApplication(List<String> s) throws Exception {
TrackEvent.withDebug("proc", "Executing local application")
.elements(args)
.handle();
try (var c = ShellStore.local().create().command(s).start()) {
try (var c = LocalStore.getShell().command(s).start()) {
c.discardOrThrow();
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/io/xpipe/app/util/DesktopShortcuts.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.xpipe.app.util;

import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.OsType;
import io.xpipe.core.store.ShellStore;
import io.xpipe.core.util.XPipeInstallation;
Expand Down Expand Up @@ -53,7 +54,7 @@ private static void createMacOSShortcut(String target, String name) throws Excep
""",
target);

try (var pc = ShellStore.local().create().start()) {
try (var pc = LocalStore.getShell()) {
pc.executeSimpleCommand(
pc.getShellDialect().flatten(pc.getShellDialect().getMkdirsCommand(base + "/Contents/MacOS")));
pc.executeSimpleCommand(
Expand Down
188 changes: 95 additions & 93 deletions core/src/main/java/io/xpipe/core/impl/LocalStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,130 @@

import com.fasterxml.jackson.annotation.JsonTypeName;
import io.xpipe.core.process.ProcessControlProvider;
import io.xpipe.core.process.ShellDialects;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.store.FileSystem;
import io.xpipe.core.store.FileSystemStore;
import io.xpipe.core.store.MachineStore;
import io.xpipe.core.store.*;
import io.xpipe.core.util.JacksonizedValue;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@JsonTypeName("local")
public class LocalStore extends JacksonizedValue implements FileSystemStore, MachineStore {

private static ShellProcessControl local;

public static ShellProcessControl getShell() throws Exception {
if (local == null) {
local = new LocalStore().create().start();
}

return local;
}

@Override
public boolean isLocal() {
return true;
}

@Override
public FileSystem createFileSystem() {
return new FileSystem() {
@Override
public Optional<ShellProcessControl> getShell() {
return Optional.empty();
}

@Override
public FileSystem open() throws Exception {
return this;
}

@Override
public boolean exists(String file) {
return Files.exists(Path.of(file));
}

@Override
public void delete(String file) throws Exception {
Files.delete(Path.of(file));
}

@Override
public void copy(String file, String newFile) throws Exception {
Files.copy(Path.of(file), Path.of(newFile), StandardCopyOption.REPLACE_EXISTING);
}

@Override
public void move(String file, String newFile) throws Exception {
Files.move(Path.of(file), Path.of(newFile), StandardCopyOption.REPLACE_EXISTING);
}

@Override
public boolean mkdirs(String file) throws Exception {
try {
Files.createDirectories(Path.of(file));
return true;
} catch (Exception ex) {
return false;
}
}

@Override
public void touch(String file) throws Exception {
if (exists(file)) {
return;
}

Files.createFile(Path.of(file));
}

@Override
public boolean isDirectory(String file) throws Exception {
return Files.isDirectory(Path.of(file));
}

@Override
public Stream<FileEntry> listFiles(String file) throws Exception {
return Files.list(Path.of(file)).map(path -> {
try {
var date = Files.getLastModifiedTime(path);
var size = Files.isDirectory(path) ? 0 : Files.size(path);
return new FileEntry(
this,
path.toString(),
date.toInstant(),
Files.isDirectory(path),
Files.isHidden(path),
Files.isExecutable(path),
size);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}

@Override
public List<String> listRoots() throws Exception {
return StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false).map(path -> path.toString()).toList();
}
return new ConnectionFileSystem(ShellStore.local().create()) {

@Override
public InputStream openInput(String file) throws Exception {
var p = Path.of(file);
var p = wrap(file);
return Files.newInputStream(p);
}

@Override
public OutputStream openOutput(String file) throws Exception {
var p = Path.of(file);
var p = wrap(file);
return Files.newOutputStream(p);
}

@Override
public void close() throws IOException {}
private Path wrap(String file) {
for (var e : System.getenv().entrySet()) {
file = file.replace(
ShellDialects.getPlatformDefault().environmentVariable(e.getKey()),
e.getValue());
}
return Path.of(file);
}

// @Override
// public boolean exists(String file) {
// return Files.exists(wrap(file));
// }
//
// @Override
// public void delete(String file) throws Exception {
// Files.delete(wrap(file));
// }
//
// @Override
// public void copy(String file, String newFile) throws Exception {
// Files.copy(wrap(file), wrap(newFile), StandardCopyOption.REPLACE_EXISTING);
// }
//
// @Override
// public void move(String file, String newFile) throws Exception {
// Files.move(wrap(file), wrap(newFile), StandardCopyOption.REPLACE_EXISTING);
// }
//
// @Override
// public boolean mkdirs(String file) throws Exception {
// try {
// Files.createDirectories(wrap(file));
// return true;
// } catch (Exception ex) {
// return false;
// }
// }
//
// @Override
// public void touch(String file) throws Exception {
// if (exists(file)) {
// return;
// }
//
// Files.createFile(wrap(file));
// }
//
// @Override
// public boolean isDirectory(String file) throws Exception {
// return Files.isDirectory(wrap(file));
// }
//
// @Override
// public Stream<FileEntry> listFiles(String file) throws Exception {
// return Files.list(wrap(file)).map(path -> {
// try {
// var date = Files.getLastModifiedTime(path);
// var size = Files.isDirectory(path) ? 0 : Files.size(path);
// return new FileEntry(
// this,
// path.toString(),
// date.toInstant(),
// Files.isDirectory(path),
// Files.isHidden(path),
// Files.isExecutable(path),
// size);
// } catch (IOException e) {
// throw new UncheckedIOException(e);
// }
// });
// }
//
// @Override
// public List<String> listRoots() throws Exception {
// return StreamSupport.stream(
// FileSystems.getDefault().getRootDirectories().spliterator(), false)
// .map(path -> path.toString())
// .toList();
// }
};
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/io/xpipe/core/process/ShellDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ default String getExitCommand() {

String getExitCodeVariable();

String environmentVariable(String name);

default String getConcatenationOperator() {
return ";";
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/io/xpipe/core/store/ShellStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public interface ShellStore extends DataStore, StatefulDataStore, LaunchableStore, FileSystemStore {

public static MachineStore local() {
public static ShellStore local() {
return new LocalStore();
}

Expand Down

0 comments on commit 26a7592

Please sign in to comment.