mirror of
https://gitlab.com/kauron/jstudy
synced 2024-12-22 08:23:33 +01:00
Auto-update from Gitlab's latest tag
This commit is contained in:
parent
c608f52c61
commit
1b5f07bb7d
7 changed files with 111 additions and 6 deletions
|
@ -5,13 +5,15 @@
|
|||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources-filtered" type="java-resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.jfoenix:jfoenix:8.0.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.json:json:20190722" level="project" />
|
||||
</component>
|
||||
</module>
|
18
pom.xml
18
pom.xml
|
@ -14,7 +14,7 @@
|
|||
</repositories>
|
||||
|
||||
<name>jstudy</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<url>http://gitlab.com/kauron/jstudy</url>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -27,8 +27,24 @@
|
|||
<artifactId>jfoenix</artifactId>
|
||||
<version>8.0.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20190722</version>
|
||||
<type>bundle</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>false</filtering>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/resources-filtered</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
|
|
@ -24,16 +24,25 @@ import javafx.scene.layout.BorderPane;
|
|||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Window;
|
||||
import javafx.stage.WindowEvent;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Controller implements Initializable {
|
||||
private static final String PROJECT_URL = "https://gitlab.com/kauron/jstudy";
|
||||
|
||||
@FXML
|
||||
private TabPane tabPane;
|
||||
|
||||
|
@ -45,6 +54,7 @@ public class Controller implements Initializable {
|
|||
|
||||
private final BooleanProperty tabIsTable = new SimpleBooleanProperty(false);
|
||||
private final Map<Tab, TableController> tabMap = new HashMap<>();
|
||||
private String updateURL, updateFileName;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
|
@ -65,6 +75,75 @@ public class Controller implements Initializable {
|
|||
}
|
||||
})
|
||||
);
|
||||
new Thread(this::checkUpdate).start();
|
||||
}
|
||||
|
||||
private void checkUpdate() {
|
||||
// Check new version via gitlab's REST API
|
||||
String newVersion;
|
||||
File jarFile;
|
||||
BufferedReader br;
|
||||
try {
|
||||
URL apiUrl = new URL("https://gitlab.com/api/v4/projects/9264549/releases");
|
||||
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
if (connection.getResponseCode() != 200) {
|
||||
System.err.println("Error connecting to Gitlab API");
|
||||
}
|
||||
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
String output = br.lines().collect(Collectors.joining());
|
||||
JSONArray tags = new JSONArray(output);
|
||||
JSONObject latest = tags.getJSONObject(0);
|
||||
newVersion = latest.getString("tag_name").substring(1);
|
||||
if (!isNewVersion(AppPrefs.getVersion(), newVersion))
|
||||
return;
|
||||
String desc = latest.getString("description");
|
||||
Matcher matcher = Pattern.compile("\\[(.*\\.jar)]" +
|
||||
"\\((/uploads/.*/.*\\.jar)\\)").matcher(desc);
|
||||
if (!matcher.find())
|
||||
return;
|
||||
updateURL = matcher.group(2);
|
||||
updateFileName = matcher.group(1);
|
||||
// Ask user whether to update or not.
|
||||
Platform.runLater(this::onUpdate);
|
||||
}
|
||||
|
||||
private void onUpdate() {
|
||||
Optional<ButtonType> res = new Alert(Alert.AlertType.INFORMATION,
|
||||
"There is an update ready. Would you like to download it and open it?",
|
||||
ButtonType.YES, ButtonType.NO).showAndWait();
|
||||
if (!res.isPresent() || !res.get().equals(ButtonType.YES))
|
||||
return;
|
||||
try (InputStream inputStream = new URL(PROJECT_URL + updateURL).openStream();
|
||||
ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(updateFileName)) {
|
||||
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
// Launch new version
|
||||
try {
|
||||
new ProcessBuilder("java", "-jar", updateFileName).start();
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
onQuit(null);
|
||||
}
|
||||
|
||||
private boolean isNewVersion(String version, String newVersion) {
|
||||
String[] o = version.split("\\.");
|
||||
String[] n = newVersion.split("\\.");
|
||||
for (int i = 0; i < o.length; i++) {
|
||||
int a = Integer.parseInt(n[i]);
|
||||
int b = Integer.parseInt(o[i]);
|
||||
if (a != b)
|
||||
return a > b;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@FXML
|
||||
|
@ -248,7 +327,7 @@ public class Controller implements Initializable {
|
|||
protected void onAboutAction(ActionEvent event) {
|
||||
if (Desktop.isDesktopSupported()) {
|
||||
try {
|
||||
Desktop.getDesktop().browse(URI.create("https://gitlab.com/kauron/jstudy"));
|
||||
Desktop.getDesktop().browse(URI.create(PROJECT_URL));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import es.kauron.jstudy.Main;
|
|||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Scanner;
|
||||
import java.util.prefs.BackingStoreException;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
|
@ -33,6 +34,12 @@ public class AppPrefs {
|
|||
flushPrefs();
|
||||
}
|
||||
|
||||
public static String getVersion() {
|
||||
String v = new Scanner(AppPrefs.class.getResourceAsStream("/version.txt")).nextLine();
|
||||
assert v.matches("\\d\\.\\d\\.\\d");
|
||||
return v;
|
||||
}
|
||||
|
||||
public static class BooleanPref extends SimpleBooleanProperty {
|
||||
private final String name;
|
||||
|
||||
|
|
|
@ -9,5 +9,6 @@
|
|||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Maven: com.jfoenix:jfoenix:8.0.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.json:json:20190722" level="project" />
|
||||
</component>
|
||||
</module>
|
1
src/main/resources-filtered/version.txt
Normal file
1
src/main/resources-filtered/version.txt
Normal file
|
@ -0,0 +1 @@
|
|||
${version}
|
1
version
1
version
|
@ -1 +0,0 @@
|
|||
0.4.5
|
Loading…
Reference in a new issue