-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Oops.
- Loading branch information
Showing
6 changed files
with
250 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,6 @@ | |
.metadata | ||
|
||
# Common | ||
target/ | ||
/target/ | ||
dist/ | ||
*_runtime.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/co/casterlabs/caffeinated/updater/target/LinuxTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/co/casterlabs/caffeinated/updater/target/MacTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/main/java/co/casterlabs/caffeinated/updater/target/Target.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/co/casterlabs/caffeinated/updater/target/WindowsTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |