Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
Oops.
  • Loading branch information
e3ndr committed Oct 29, 2024
1 parent 5e557dd commit 5d256b8
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
.metadata

# Common
target/
/target/
dist/
*_runtime.*
62 changes: 30 additions & 32 deletions src/main/java/co/casterlabs/caffeinated/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ProcessBuilder.Redirect;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Files;
import java.util.Scanner;

import co.casterlabs.caffeinated.updater.target.Target;
import co.casterlabs.caffeinated.updater.util.FileUtil;
Expand Down Expand Up @@ -158,7 +156,7 @@ public static void downloadAndInstallUpdate(UpdaterDialog dialog) throws Updater

public static void launch(UpdaterDialog dialog) throws UpdaterException {
try {
String updaterCommandLine = co.casterlabs.commons.platform.ProcessUtil.tryGetCommandLine(co.casterlabs.commons.platform.ProcessUtil.getPid());
String updaterCommandLine = target.getUpdaterLaunchFile().getAbsolutePath();
FastLogger.logStatic("Updater CommandLine: %s", updaterCommandLine);

File expectUpdaterFile = new File(appDirectory, "expect-updater");
Expand All @@ -172,35 +170,35 @@ public static void launch(UpdaterDialog dialog) throws UpdaterException {
// TODO look for the build_ok file before trusting the process. (kill & let
// the user know it's dead)

if (Platform.osDistribution == OSDistribution.MACOS) {
// On MacOS we do not want to keep the updater process open as it'll stick in
// the dock. So we start the process and kill the updater to make sure that
// doesn't happen.
FastLogger.logStatic(LogLevel.INFO, "The process will now exit, this is so the updater's icon doesn't stick in the dock.");
pb.start();
dialog.close();
System.exit(0);
return;
}

Process proc = pb
.redirectOutput(Redirect.PIPE)
.start();

try (Scanner in = new Scanner(proc.getInputStream())) {
boolean hasAlreadyStarted = false;
while (true) {
String line = in.nextLine();
System.out.println(line);

if (!hasAlreadyStarted && line.contains("Starting the UI")) {
// Look for "Starting the UI" before we close the dialog.
FastLogger.logStatic(LogLevel.INFO, "UI Started!");
dialog.close();
System.exit(0);
}
}
} catch (Exception ignored) {}
// if (Platform.osDistribution == OSDistribution.MACOS) {
// On MacOS we do not want to keep the updater process open as it'll stick in
// the dock. So we start the process and kill the updater to make sure that
// doesn't happen.
FastLogger.logStatic(LogLevel.INFO, "The process will now exit, this is so the updater's icon doesn't stick in the dock.");
pb.start();
dialog.close();
System.exit(0);
return;
// }

// Process proc = pb
// .redirectOutput(Redirect.PIPE)
// .start();
//
// try (Scanner in = new Scanner(proc.getInputStream())) {
// boolean hasAlreadyStarted = false;
// while (true) {
// String line = in.nextLine();
// System.out.println(line);
//
// if (!hasAlreadyStarted && line.contains("Starting the UI")) {
// // Look for "Starting the UI" before we close the dialog.
// FastLogger.logStatic(LogLevel.INFO, "UI Started!");
// dialog.close();
// System.exit(0);
// }
// }
// } catch (Exception ignored) {}
} catch (Exception e) {
throw new UpdaterException(UpdaterException.Error.LAUNCH_FAILED, "Could not launch update :(", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package co.casterlabs.caffeinated.updater.target;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import co.casterlabs.caffeinated.updater.Updater;
import co.casterlabs.caffeinated.updater.window.UpdaterDialog;
import co.casterlabs.commons.platform.OSDistribution;
import co.casterlabs.commons.platform.Platform;

public class LinuxTarget implements Target {

@Override
public OSDistribution supportedOS() {
return OSDistribution.LINUX;
}

@Override
public List<String> supportedTargets() {
return Arrays.asList("aarch64", "arm", "ppc64le", "x86_64");
}

@Override
public String getDownloadName() {
return String.format("Linux-%s.tar.gz", Platform.archTarget);
}

@Override
public String getLaunchCommand() {
return Updater.appDirectory + "/Casterlabs-Caffeinated";
}

@Override
public void updateUpdater(UpdaterDialog dialog) throws IOException, InterruptedException {
throw new UnsupportedOperationException();
}

@Override
public File getUpdaterLaunchFile() {
return new File("/Casterlabs-Caffeinated");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package co.casterlabs.caffeinated.updater.target;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import co.casterlabs.caffeinated.updater.Updater;
import co.casterlabs.caffeinated.updater.window.UpdaterDialog;
import co.casterlabs.commons.platform.OSDistribution;
import co.casterlabs.commons.platform.Platform;

public class MacTarget implements Target {

@Override
public OSDistribution supportedOS() {
return OSDistribution.MACOS;
}

@Override
public List<String> supportedTargets() {
return Arrays.asList("aarch64", "x86_64");
}

@Override
public String getDownloadName() {
return String.format("macOS-%s.tar.gz", Platform.archTarget);
}

@Override
public String getLaunchCommand() {
return Updater.appDirectory + "/Casterlabs-Caffeinated.app/Contents/MacOS/Casterlabs-Caffeinated";
}

@Override
public void updateUpdater(UpdaterDialog dialog) throws IOException, InterruptedException {
throw new UnsupportedOperationException();
}

@Override
public File getUpdaterLaunchFile() {
return new File("../MacOS/Casterlabs-Caffeinated");
}

}
38 changes: 38 additions & 0 deletions src/main/java/co/casterlabs/caffeinated/updater/target/Target.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package co.casterlabs.caffeinated.updater.target;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import co.casterlabs.caffeinated.updater.window.UpdaterDialog;
import co.casterlabs.commons.platform.OSDistribution;
import co.casterlabs.commons.platform.Platform;

public interface Target {

public OSDistribution supportedOS();

public List<String> supportedTargets();

public String getDownloadName();

public String getLaunchCommand();

public void updateUpdater(UpdaterDialog dialog) throws IOException, InterruptedException;

public File getUpdaterLaunchFile();

public static Target get() {
List<Target> targets = Arrays.asList(new LinuxTarget(), new MacTarget(), new WindowsTarget());

for (Target target : targets) {
if (target.supportedOS() == Platform.osDistribution && target.supportedTargets().contains(Platform.archTarget)) {
return target;
}
}

return targets.get(0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package co.casterlabs.caffeinated.updater.target;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import co.casterlabs.caffeinated.updater.Updater;
import co.casterlabs.caffeinated.updater.util.WebUtil;
import co.casterlabs.caffeinated.updater.window.UpdaterDialog;
import co.casterlabs.commons.platform.OSDistribution;

public class WindowsTarget implements Target {

@Override
public OSDistribution supportedOS() {
return OSDistribution.WINDOWS_NT;
}

@Override
public List<String> supportedTargets() {
return Arrays.asList("x86_64");
}

@Override
public String getDownloadName() {
return "Windows-x86_64.zip";
}

@Override
public String getLaunchCommand() {
return Updater.appDirectory + "/Casterlabs-Caffeinated.exe";
}

@Override
public void updateUpdater(UpdaterDialog dialog) throws IOException, InterruptedException {
HttpResponse<InputStream> response = WebUtil.sendRawHttpRequest(HttpRequest.newBuilder().uri(URI.create("https://cdn.casterlabs.co/dist/Caffeinated-Installer.exe")), BodyHandlers.ofInputStream());

final File tempInstaller = new File(System.getProperty("java.io.tmpdir"), "Caffeinated-Installer.exe");

tempInstaller.delete();
tempInstaller.createNewFile();

dialog.setStatus("Downloading installer...");

try (InputStream source = response.body();
OutputStream dest = new FileOutputStream(tempInstaller)) {

double totalSize = Long.parseLong(response.headers().firstValue("Content-Length").orElse("0"));
int totalRead = 0;

byte[] buffer = new byte[2048];
int read = 0;

while ((read = source.read(buffer)) != -1) {
dest.write(buffer, 0, read);
totalRead += read;

double progress = totalRead / totalSize;

dialog.setStatus(String.format("Downloading installer... (%.0f%%)", progress * 100));
dialog.setProgress(progress);
}

dest.flush();
dialog.setProgress(-1);

Runtime.getRuntime().exec(new String[] {
"powershell",
"-Command",
"\"Start-Process '" + tempInstaller.getCanonicalPath() + "' -Verb RunAs\""
});
TimeUnit.SECONDS.sleep(2);
System.exit(0);
}
}

@Override
public File getUpdaterLaunchFile() {
return new File("Casterlabs-Caffeinated.exe");
}

}

0 comments on commit 5d256b8

Please sign in to comment.