Skip to content

Commit

Permalink
Adding UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Harrington committed Jun 10, 2024
1 parent 8998303 commit 0fbb2c1
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/default/
/main/
21 changes: 18 additions & 3 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/8.1.1/userguide/building_java_projects.html
*/
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'com.github.johnrengelman:shadow:8.1.1'
}
}

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'java-library'


repositories {
Expand All @@ -23,6 +31,7 @@ dependencies {

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:31.1-jre'
implementation 'com.google.code.gson:gson:2.8.6'
}

// Apply a specific Java toolchain to ease working on different environments.
Expand All @@ -38,13 +47,19 @@ tasks.named('test') {
}
application {
// Define the main class for the application.
mainClass = 'com.commonwealthrobotics.HatRackMain'
mainClass = 'com.commonwealthrobotics.Main'
}

shadowJar {
archiveBaseName.set('lib')
archiveClassifier.set('')
archiveVersion.set('')
}
jar {
manifest {
attributes 'Implementation-Title': 'BowlerStudioUpdater',
'Implementation-Version': '0.0.1',
'Main-Class': 'com.commonwealthrobotics.HatRackMain'
'Main-Class': 'com.commonwealthrobotics.Main'

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
package com.commonwealthrobotics;
/**
* Sample Skeleton for 'ui.fxml' Controller Class
*/

import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class LatestFromGithubLaunchUI {
public static String[] argsFromSystem;
public static String[] args;

public static Stage stage;

public static String latestVersionString = "";
public static String myVersionString = "None";
public static long size = 0;
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;

@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;

@FXML // fx:id="progress"
private ProgressBar progress; // Value injected by FXMLLoader

@FXML // fx:id="previousVersion"
private Label previousVersion; // Value injected by FXMLLoader
@FXML // fx:id="previousVersion"
private Label binary; // Value injected by FXMLLoader
@FXML // fx:id="currentVersion"
private Label currentVersion; // Value injected by FXMLLoader

@FXML // fx:id="yesButton"
private Button yesButton; // Value injected by FXMLLoader

@FXML // fx:id="noButton"
private Button noButton; // Value injected by FXMLLoader

private static HashMap<String, Object> database;

private String bindir;

private File bindirFile;

private File myVersionFile;

private String myVersionFileString;

private static String downloadURL;

@FXML
void onNo(ActionEvent event) {
System.out.println("No path");
launchApplication();
}

@FXML
void onYes(ActionEvent event) {
System.out.println("Yes path");
yesButton.setDisable(true);
noButton.setDisable(true);
new Thread(() -> {

try {
URL url = new URL(downloadURL);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
ProcessInputStream pis = new ProcessInputStream(is, (int) size);
pis.addListener(new Listener() {
@Override
public void process(double percent) {
Platform.runLater(() -> {
progress.setProgress(percent);
});
}
});
File folder = new File(bindir + latestVersionString + "/");
File exe = new File(bindir + latestVersionString + "/" + args[2]);

if (!folder.exists() || !exe.exists() || size != exe.length()) {
folder.mkdirs();
exe.createNewFile();
byte dataBuffer[] = new byte[1024];
int bytesRead;
FileOutputStream fileOutputStream = new FileOutputStream(exe.getAbsoluteFile());
while ((bytesRead = pis.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
fileOutputStream.close();
pis.close();

}
if (folder.exists() && exe.exists() && size == exe.length())
myVersionString = latestVersionString;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
launchApplication();
}).start();
}

public void launchApplication() {
Platform.runLater(() -> stage.close());
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String command = args[3];

for (int i = 4; i < args.length; i++) {
command += " " + args[i];
}
try {
myVersionFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(myVersionFileString));
writer.write(myVersionString);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String fc =!isWin()?
command + " " + bindir + myVersionString + "/" + args[2]+"":
command + " \"" + bindir + myVersionString + "/" + args[2]+"\"";


String finalCommand=fc;
System.out.println("Running:\n\n"+finalCommand+"\n\n");
new Thread(() -> {
try {
Process process = Runtime.getRuntime().exec(finalCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null && process.isAlive()) {
System.out.println(line);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
reader.close();
System.out.println("LatestFromGithubLaunch clean exit");
System.exit(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}).start();
}

private boolean isWin() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}

private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

public static void readCurrentVersion(String url) throws IOException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
// Create the type, this tells GSON what datatypes to instantiate when parsing
// and saving the json
Type TT_mapStringString = new TypeToken<HashMap<String, Object>>() {
}.getType();
// chreat the gson object, this is the parsing factory
Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
database = gson.fromJson(jsonText, TT_mapStringString);
latestVersionString = (String) database.get("tag_name");
@SuppressWarnings("unchecked")
List<Map<String, Object>> assets = (List<Map<String, Object>>) database.get("assets");
for (Map<String, Object> key : assets) {
if (((String) key.get("name")).contentEquals(args[2])) {
downloadURL = (String) key.get("browser_download_url");
size = ((Double) key.get("size")).longValue();
System.out.println(downloadURL + " Size " + size + " bytes");
}
}
} finally {
is.close();
}
}

@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert progress != null : "fx:id=\"progress\" was not injected: check your FXML file 'ui.fxml'.";
assert previousVersion != null : "fx:id=\"previousVersion\" was not injected: check your FXML file 'ui.fxml'.";
assert currentVersion != null : "fx:id=\"currentVersion\" was not injected: check your FXML file 'ui.fxml'.";

try {
readCurrentVersion("https://api.github.com/repos/" + args[0] + "/" + args[1] + "/releases/latest");
binary.setText(args[0] + "\n" + args[1] + "\n" + args[2] + "\n" + (size / 1000000) + " Mb");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stage.setTitle("Auto-Updater for " + args[1]);
currentVersion.setText(latestVersionString);
bindir = System.getProperty("user.home") + "/bin/" + args[1] + "Install/";
myVersionFileString = bindir + "currentversion.txt";
myVersionFile = new File(myVersionFileString);
bindirFile = new File(bindir);
if (!bindirFile.exists())
bindirFile.mkdirs();
if (!myVersionFile.exists()) {

onYes(null);
} else {
try {
myVersionString = new String(Files.readAllBytes(Paths.get(myVersionFileString))).trim();
previousVersion.setText(myVersionString);
if (myVersionString.length() < 3) {
onYes(null);
return;
}
} catch (IOException e) {
e.printStackTrace();
}
}

if (myVersionString.contentEquals(latestVersionString)) {
//launchApplication();
}

}
}
5 changes: 5 additions & 0 deletions lib/src/main/java/com/commonwealthrobotics/Listener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.commonwealthrobotics;

public interface Listener{
void process(double percent);
}
42 changes: 42 additions & 0 deletions lib/src/main/java/com/commonwealthrobotics/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package com.commonwealthrobotics;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage stage) throws Exception {
LatestFromGithubLaunchUI.stage=stage;
Parent root = FXMLLoader.load(Main.class.getResource("ui.fxml"));

Scene scene = new Scene(root, 600, 523);


stage.setScene(scene);
stage.show();
}
public static void main(String [] args) {
LatestFromGithubLaunchUI.argsFromSystem=args;

LatestFromGithubLaunchUI.args=new String[] {
"CommonWealthRobotics",
"BowlerStudio",
"BowlerStudio.jar",
"/home/hephaestus/bin/java8/bin/java",
"-Dprism.forceGPU=true",
"-jar"
};
launch(args);
}

}



Loading

0 comments on commit 0fbb2c1

Please sign in to comment.