diff --git a/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/ShellInstaller.java b/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/ShellInstaller.java index 04e26e3..a12972e 100644 --- a/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/ShellInstaller.java +++ b/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/ShellInstaller.java @@ -1,32 +1,60 @@ package io.github.jagodevreede.sdkmanui.install; -import io.github.jagodevreede.sdkmanui.ApplicationVersion; -import io.github.jagodevreede.sdkmanui.service.ServiceRegistry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.List; + +import static java.io.File.separator; -public class ShellInstaller extends UiInstaller { - private static final Logger logger = LoggerFactory.getLogger(ShellInstaller.class); - private static final ServiceRegistry SERVICE_REGISTRY = ServiceRegistry.INSTANCE; - private final File installFolder = new File(SERVICE_REGISTRY.getApi().getBaseFolder(), "ui"); +@SuppressWarnings({"ConstantConditions", "ResultOfMethodCallIgnored"}) +class ShellInstaller extends UiInstaller { + static final String BEGIN_MARKER = "# BEGIN SDKMAN-UI"; + static final String END_MARKER = "# END SDKMAN-UI"; + static final List rcFiles = List.of(".bashrc", ".zshrc"); - @SuppressWarnings({"ConstantConditions", "ResultOfMethodCallIgnored"}) @Override public void updateScriptAndVersion() { try { - Files.copy(ApplicationVersion.class.getClassLoader() + Files.copy(UiInstaller.class.getClassLoader() .getResourceAsStream("sdkui.sh"), new File(installFolder, "sdkui.sh").toPath(), StandardCopyOption.REPLACE_EXISTING); new File(installFolder, "sdkui.sh").setExecutable(true, false); - Files.copy(ApplicationVersion.class.getClassLoader() + Files.copy(UiInstaller.class.getClassLoader() .getResourceAsStream("update.sh"), new File(installFolder, "update.sh").toPath(), StandardCopyOption.REPLACE_EXISTING); new File(installFolder, "update.sh").setExecutable(true, false); - Files.copy(ApplicationVersion.class.getClassLoader() + Files.copy(UiInstaller.class.getClassLoader() .getResourceAsStream("version.txt"), new File(installFolder, "version.txt").toPath(), StandardCopyOption.REPLACE_EXISTING); + rcFiles.forEach(f -> { + File rcFile = new File(System.getProperty("user.home") + separator + f); + if (rcFile.exists()) { + addToRcFile(rcFile); + } + }); + } catch (IOException e) { + SERVICE_REGISTRY.getPopupView().showError(e); + } + } + + void addToRcFile(File rcFile) { + try (var templateStream = ShellInstaller.class.getResourceAsStream("/template/bash_shell")) { + String template = new String(templateStream.readAllBytes(), StandardCharsets.UTF_8); + Path filePath = rcFile.toPath(); + String content = Files.readString(filePath); + + if ((content.contains(BEGIN_MARKER) && !content.contains(END_MARKER)) || (!content.contains(BEGIN_MARKER) && content.contains(END_MARKER))) { + SERVICE_REGISTRY.getPopupView() + .showError("Corrupt rc file " + rcFile.getAbsolutePath() + "\nUnable to add SDKMAN-UI"); + } + if (!content.contains(BEGIN_MARKER)) { + Files.writeString(filePath, content + "\n" + BEGIN_MARKER + "\n" + template + "\n" + END_MARKER); + } else { + String beforeContent = content.substring(0, content.indexOf(BEGIN_MARKER)); + String afterContent = content.substring(content.indexOf(END_MARKER) + END_MARKER.length()); + Files.writeString(filePath, beforeContent + BEGIN_MARKER + "\n" + template + "\n" + END_MARKER + afterContent); + } } catch (IOException e) { SERVICE_REGISTRY.getPopupView().showError(e); } diff --git a/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/UiInstaller.java b/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/UiInstaller.java index 7ff17b8..3b055fb 100644 --- a/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/UiInstaller.java +++ b/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/UiInstaller.java @@ -17,14 +17,17 @@ import java.util.Optional; public abstract class UiInstaller { - private static final Logger logger = LoggerFactory.getLogger(ShellInstaller.class); - protected static final ServiceRegistry SERVICE_REGISTRY = ServiceRegistry.INSTANCE; + private static final Logger logger = LoggerFactory.getLogger(UiInstaller.class); + protected final ServiceRegistry SERVICE_REGISTRY = ServiceRegistry.INSTANCE; protected final File installFolder = new File(SERVICE_REGISTRY.getApi().getBaseFolder(), "ui"); public static Optional getInstance() { if (OsHelper.isWindows()) { return Optional.of(new WindowsInstaller()); } + if (OsHelper.isMac()) { + return Optional.of(new ShellInstaller()); + } return Optional.empty(); } diff --git a/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/WindowsInstaller.java b/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/WindowsInstaller.java index 4365985..50f6b8f 100644 --- a/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/WindowsInstaller.java +++ b/sdkman-ui/src/main/java/io/github/jagodevreede/sdkmanui/install/WindowsInstaller.java @@ -1,16 +1,11 @@ package io.github.jagodevreede.sdkmanui.install; -import io.github.jagodevreede.sdkmanui.ApplicationVersion; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; class WindowsInstaller extends UiInstaller { - private static final Logger logger = LoggerFactory.getLogger(WindowsInstaller.class); @SuppressWarnings({"ConstantConditions", "ResultOfMethodCallIgnored"}) @Override @@ -20,11 +15,11 @@ public void updateScriptAndVersion() { new File(installFolder, "update.cmd").delete(); new File(installFolder, "version.txt").delete(); try { - Files.copy(ApplicationVersion.class.getClassLoader() + Files.copy(UiInstaller.class.getClassLoader() .getResourceAsStream("sdkui.cmd"), new File(installFolder, "sdkui.cmd").toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.copy(ApplicationVersion.class.getClassLoader() + Files.copy(UiInstaller.class.getClassLoader() .getResourceAsStream("update.cmd"), new File(installFolder, "update.cmd").toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.copy(ApplicationVersion.class.getClassLoader() + Files.copy(UiInstaller.class.getClassLoader() .getResourceAsStream("version.txt"), new File(installFolder, "version.txt").toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { SERVICE_REGISTRY.getPopupView().showError(e); diff --git a/sdkman-ui/src/main/resources/META-INF/native-image-mac/resource-config.json b/sdkman-ui/src/main/resources/META-INF/native-image-mac/resource-config.json index 0a14bdf..122fc1d 100644 --- a/sdkman-ui/src/main/resources/META-INF/native-image-mac/resource-config.json +++ b/sdkman-ui/src/main/resources/META-INF/native-image-mac/resource-config.json @@ -9,10 +9,19 @@ }, { "pattern":"\\Qmain.fxml\\E" - }, + }, + { + "pattern":"\\Qsdkui.sh\\E" + }, { "pattern":"\\Qsimplelogger.properties\\E" - }, + }, + { + "pattern":"\\Qupdate.sh\\E" + }, + { + "pattern":"\\Qtemplate/bash_shell\\E" + }, { "pattern":"\\Qversion.txt\\E" } diff --git a/sdkman-ui/src/main/resources/sdkui.sh b/sdkman-ui/src/main/resources/sdkui.sh old mode 100644 new mode 100755 index e13b09e..287f96d --- a/sdkman-ui/src/main/resources/sdkui.sh +++ b/sdkman-ui/src/main/resources/sdkui.sh @@ -1,13 +1,9 @@ #!/usr/bin/env bash -if not exist "%homedrive%%homepath%\.sdkman" mkdir %homedrive%%homepath%\.sdkman -if not exist "%homedrive%%homepath%\.sdkman\tmp" mkdir %homedrive%%homepath%\.sdkman\tmp -if exist "%homedrive%%homepath%\.sdkman\tmp\exit-script.cmd" del %homedrive%%homepath%\.sdkman\tmp\exit-script.cmd +mkdir -p ~/.sdkman/tmp +rm -f ~/.sdkman/tmp/exit-script.cmd -sdkman-ui.exe +~/.sdkman/ui/sdkman-ui 2> /dev/null -:waitloop -IF EXIST "%homedrive%%homepath%\.sdkman\tmp\exit-script.cmd" GOTO waitloopend -timeout /t 1 /nobreak > nul -goto waitloop -:waitloopend -call %homedrive%%homepath%\.sdkman\tmp\exit-script.cmd +if [ -f ~/.sdkman/tmp/exit-script.cmd ]; then + source ~/.sdkman/tmp/exit-script.cmd +fi diff --git a/sdkman-ui/src/main/resources/template/bash_shell b/sdkman-ui/src/main/resources/template/bash_shell new file mode 100644 index 0000000..23c9860 --- /dev/null +++ b/sdkman-ui/src/main/resources/template/bash_shell @@ -0,0 +1,3 @@ +function sdkui { + source ~/.sdkman/ui/sdkui.sh +} \ No newline at end of file diff --git a/sdkman-ui/src/main/resources/update.sh b/sdkman-ui/src/main/resources/update.sh old mode 100644 new mode 100755 diff --git a/sdkman-ui/src/test/java/io/github/jagodevreede/sdkmanui/install/ShellInstallerTest.java b/sdkman-ui/src/test/java/io/github/jagodevreede/sdkmanui/install/ShellInstallerTest.java new file mode 100644 index 0000000..f7f9dff --- /dev/null +++ b/sdkman-ui/src/test/java/io/github/jagodevreede/sdkmanui/install/ShellInstallerTest.java @@ -0,0 +1,55 @@ +package io.github.jagodevreede.sdkmanui.install; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static io.github.jagodevreede.sdkmanui.install.ShellInstaller.BEGIN_MARKER; +import static io.github.jagodevreede.sdkmanui.install.ShellInstaller.END_MARKER; +import static org.assertj.core.api.Assertions.assertThat; + +class ShellInstallerTest { + @TempDir + Path tempDir; + Path tempFile; + + ShellInstaller subject = new ShellInstaller(); + + @BeforeEach + void createTempFile() throws IOException { + tempFile = tempDir.resolve("test.txt"); + Files.writeString(tempFile, "Start of file\n"); + } + + @Test + void checkWithCleanFile() throws IOException { + subject.addToRcFile(tempFile.toFile()); + + String fileContent = Files.readString(tempFile); + + assertThat(fileContent) + .containsOnlyOnce("Start of file") + .containsOnlyOnce(BEGIN_MARKER) + .containsOnlyOnce("function") + .containsOnlyOnce(END_MARKER); + } + + @Test + void checkUpdate() throws IOException { + Files.writeString(tempFile, "Start of file\n" + BEGIN_MARKER + "\n" + "old stuff to be overwritten\n" + END_MARKER); + + subject.addToRcFile(tempFile.toFile()); + + String fileContent = Files.readString(tempFile); + + assertThat(fileContent) + .containsOnlyOnce("Start of file") + .containsOnlyOnce(BEGIN_MARKER) + .containsOnlyOnce("function") + .containsOnlyOnce(END_MARKER); + } +} \ No newline at end of file