Skip to content

Commit

Permalink
feat: Can now be installed for bash and zsh
Browse files Browse the repository at this point in the history
  • Loading branch information
jagodevreede committed Nov 10, 2024
1 parent 742c7b2 commit 5905cfe
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UiInstaller> getInstance() {
if (OsHelper.isWindows()) {
return Optional.of(new WindowsInstaller());
}
if (OsHelper.isMac()) {
return Optional.of(new ShellInstaller());
}
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
16 changes: 6 additions & 10 deletions sdkman-ui/src/main/resources/sdkui.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions sdkman-ui/src/main/resources/template/bash_shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function sdkui {
source ~/.sdkman/ui/sdkui.sh
}
Empty file modified sdkman-ui/src/main/resources/update.sh
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5905cfe

Please sign in to comment.