Skip to content

Commit ea27861

Browse files
committed
v0.4.4
Działający proces własnej aktualizacji
1 parent 78f4e83 commit ea27861

File tree

6 files changed

+84
-50
lines changed

6 files changed

+84
-50
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<url>https://github.com/koder95/eMetrykant</url>
1010
<groupId>pl.koder95</groupId>
1111
<artifactId>eMetrykant</artifactId>
12-
<version>0.4.3</version>
12+
<version>0.4.4</version>
1313
<scm>
1414
<url>https://github.com/koder95/eMetrykant</url>
1515
<tag>search, metric, church</tag>

src/main/java/pl/koder95/eme/Main.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javafx.geometry.Pos;
2222
import javafx.scene.Parent;
2323
import javafx.scene.Scene;
24+
import javafx.scene.control.Alert;
2425
import javafx.scene.control.Label;
2526
import javafx.scene.control.ProgressBar;
2627
import javafx.scene.image.Image;
@@ -35,6 +36,7 @@
3536
import pl.koder95.eme.dfs.IndexList;
3637
import pl.koder95.eme.git.RepositoryInfo;
3738

39+
import java.io.IOException;
3840
import java.text.Collator;
3941
import java.util.Arrays;
4042
import java.util.Locale;
@@ -45,7 +47,7 @@
4547
* Klasa uruchamiająca i inicjująca podstawowe elementy aplikacji.
4648
*
4749
* @author Kamil Jan Mularski [@koder95]
48-
* @version 0.4.3, 2024-12-01
50+
* @version 0.4.4, 2024-12-02
4951
* @since 0.0.201
5052
*/
5153
public class Main extends Application {
@@ -82,10 +84,12 @@ public static void main(String[] args) {
8284
if (args[0].equals("-v")) {
8385
System.out.println(Version.get());
8486
System.exit(0);
87+
return;
8588
}
8689
else if (args[0].equals("-u")) {
8790
SelfUpdate su = new SelfUpdate();
8891
new Thread(su).start();
92+
return;
8993
}
9094
}
9195
Main.launch(args);
@@ -105,7 +109,7 @@ public static void releaseMemory() {
105109
@Override
106110
public void init() throws Exception {
107111
super.init();
108-
RepositoryInfo.get().reload();
112+
task = new SelfUpdateTask();
109113
if (getParameters().getUnnamed().isEmpty()) {
110114
Arrays.stream(IndexList.values()).forEach(IndexList::load);
111115
}
@@ -119,7 +123,6 @@ public void init() throws Exception {
119123
worker.load();
120124

121125
root = FXMLLoader.load(ClassLoader.getSystemResource("pl/koder95/eme/fx/PersonalDataView.fxml"), BUNDLE);
122-
task = new SelfUpdateTask();
123126
}
124127

125128
@Override

src/main/java/pl/koder95/eme/au/SelfUpdate.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@
2626

2727
/**
2828
* Klasa odpowiedzialna za aktualizowanie własnej wersji do najnowszej.
29+
* Działa w następujący sposób:
30+
* <ol>
31+
* <li>Usuwanie zawartości folderu tymczasowego, gdzie pobrane zostaną pliki ostatniego wydania.</li>
32+
* <li>Pobieranie najnowszego wydania programu w postaci archiwum ZIP.</li>
33+
* <li>Wypakowanie zawartości archiwum do folderu tymczasowego.</li>
34+
* <li>Usunięcie pobranego archiwum ZIP.</li>
35+
* <li>Wygenerowanie skryptu samoaktualizacji.</li>
36+
* <li>Uruchomienie skryptu i zamknięcie programu.</li>
37+
* </ol>
38+
* Generator skryptu aktualizującego powinien zadbać o uruchomienie programu po skończeniu aktualizacji.
2939
*
3040
* @author Kamil Jan Mularski [@koder95]
31-
* @version 0.4.3, 2024-12-01
41+
* @version 0.4.4, 2024-12-02
3242
* @since 0.4.3
3343
*/
3444
public class SelfUpdate implements Runnable {
@@ -56,26 +66,33 @@ public class SelfUpdate implements Runnable {
5666
* Tworzy domyślną instancję klasy.
5767
*/
5868
public SelfUpdate() {
59-
this((workDone, max) -> {
60-
double percent = 100 * workDone.doubleValue()/max.doubleValue();
61-
System.out.println("Processing... " + (int) percent);
62-
}, msg -> System.out.println("INFO: " + msg), title -> System.out.println('\n' + title + '\n'));
69+
this((workDone, max) -> {}, msg -> {}, title -> System.out.println('\n' + title + '\n'));
6370
}
6471

6572
@Override
6673
public void run() {
74+
updateTitle.accept("Rozpoczęcie aktualizacji");
6775
try {
68-
// usuwanie plików z katalogu tymczasowego:
76+
updateMessage.accept("Usuwanie plików z katalogu tymczasowego...");
6977
clear();
70-
// pobieranie zip'a i rozpakowywanie do folderu tymczasowego:
78+
updateMessage.accept("Przygotowywanie do pobierania...");
7179
prepare();
7280
} catch (IOException e) {
7381
throw new RuntimeException(e);
7482
}
83+
updateTitle.accept("Ponowne uruchamianie w celu zapisania zmian...");
84+
updateProgress.accept(0, 0);
85+
updateMessage.accept("");
86+
try {
87+
Thread.sleep(200);
88+
} catch (InterruptedException e) {
89+
// ignoruj
90+
}
7591
restart(); // restartowanie z wywołaniem skryptu
7692
}
7793

7894
private void clear() throws IOException {
95+
if (Files.notExists(TEMP_DIR)) return;
7996
List<Exception> exceptions = new LinkedList<>();
8097
try (Stream<Path> paths = Files.walk(TEMP_DIR).sorted(Comparator.reverseOrder())) {
8198
paths.forEach(path -> {
@@ -105,16 +122,25 @@ private void restart() {
105122
}
106123

107124
private void prepare() throws IOException {
125+
updateMessage.accept("Tworzenie katalogu tymczasowego...");
126+
Files.createDirectories(TEMP_DIR);
127+
updateMessage.accept(Files.exists(TEMP_DIR)? "Stworzono katalog tymczasowy." : "Błąd tworzenia katalogu tymczasowego!");
128+
updateMessage.accept("Sprawdzanie ostatniej wersji...");
129+
RepositoryInfo.get().reload();
108130
if (extractZip(downloadZip())) {
109131
Map<Path, Path> updateMap = new HashMap<>();
110132
try (Stream<Path> paths = Files.list(TEMP_DIR)) {
111-
paths.forEach(path -> updateMap.put(path, WORKDIR.resolve(path.getFileName().toString())));
133+
paths.forEach(path -> {
134+
String fileName = path.getFileName().toString();
135+
if (fileName.startsWith("eMetrykant") && fileName.endsWith(".jar"))
136+
fileName = SELF.getFileName().toString();
137+
updateMap.put(path, WORKDIR.resolve(fileName));
138+
});
112139
}
113140
updateScriptGenerator.generateUpdateScript(updateMap);
114141
}
115142
}
116143

117-
118144
private Path downloadZip() throws IOException {
119145
updateTitle.accept("Pobieranie " + RepositoryInfo.get().getLatestReleaseName());
120146
return tryTransfer(RepositoryInfo.get().getLatestReleaseBrowserURL());
@@ -172,7 +198,7 @@ private boolean extractZip(Path forExtract) throws IOException {
172198
Files.createFile(outFile);
173199

174200
if (Files.isRegularFile(outFile)) {
175-
tryTransfer(zip, entry, outFile, workDone, total);
201+
workDone = tryTransfer(zip, entry, outFile, workDone, total);
176202
}
177203
} else if (Files.notExists(outFile)) {
178204
Files.createDirectories(outFile);
@@ -199,20 +225,21 @@ private static long getTotal(ZipFile zip) throws IOException {
199225
return total;
200226
}
201227

202-
private void tryTransfer(ZipFile zip, ZipEntry entry, Path outFile, long workDone, long total) throws IOException {
228+
private long tryTransfer(ZipFile zip, ZipEntry entry, Path outFile, long workDone, long total) throws IOException {
203229
try (InputStream input = zip.getInputStream(entry);
204230
OutputStream output = Files.newOutputStream(outFile)) {
205-
transfer(input, output, workDone, total);
231+
return transfer(input, output, workDone, total);
206232
}
207233
}
208234

209-
private void transfer(InputStream input, OutputStream output, long workDone, long total) throws IOException {
235+
private long transfer(InputStream input, OutputStream output, long workDone, long total) throws IOException {
210236
while (input.available() > 0) {
211237
int b = input.read();
212238
output.write(b);
213239
output.flush();
214240
updateProgress.accept(++workDone, total);
215241
updateMessage.accept(NumberFormat.getPercentInstance().format((double) workDone / total));
216242
}
243+
return workDone;
217244
}
218245
}

src/main/java/pl/koder95/eme/au/SelfUpdateTask.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,24 @@
2020

2121
/**
2222
* Klasa definiuje metody samoaktualizacji, które współpracują z JavaFX. Jest to szczególna implementacja
23-
* klasy abstrakcyjnej {@link Task}, której metoda {@link #call()} zwraca zawsze {@code null} i działa
24-
* w następujący sposób:
25-
* <ol>
26-
* <li>Usuwanie zawartości folderu tymczasowego, gdzie pobrane zostaną pliki ostatniego wydania.</li>
27-
* <li>Pobieranie najnowszego wydania programu w postaci archiwum ZIP.</li>
28-
* <li>Wypakowanie zawartości archiwum do folderu tymczasowego.</li>
29-
* <li>Usunięcie pobranego archiwum ZIP.</li>
30-
* <li>Wygenerowanie skryptu samoaktualizacji.</li>
31-
* <li>Uruchomienie skryptu i zamknięcie programu.</li>
32-
* </ol>
23+
* klasy abstrakcyjnej {@link Task}, której metoda {@link #call()} zwraca zawsze {@code null}.
3324
* Generator skryptu aktualizującego powinien zadbać o uruchomienie programu po skończeniu aktualizacji.
25+
*
26+
* @author Kamil Jan Mularski [@koder95]
27+
* @version 0.4.4, 2024-12-02
28+
* @since 0.4.1
29+
* @see SelfUpdate
3430
*/
3531
public class SelfUpdateTask extends Task<Void> {
3632

33+
private final SelfUpdate su = new SelfUpdate(this::updateProgress, this::updateMessage, this::updateTitle);
34+
3735
/**
3836
* @return zawsze {@code null}
3937
*/
4038
@Override
4139
protected Void call() throws Exception {
42-
new SelfUpdate(this::updateProgress, this::updateMessage, this::updateTitle).run();
40+
su.run();
4341
return null;
4442
}
4543

src/main/java/pl/koder95/eme/au/UpdateScriptGenerator.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Klasa dostarcza metody do generowania skryptów aktualizacyjnych.
3434
*
3535
* @author Kamil Jan Mularski [@koder95]
36-
* @version 0.4.3, 2024-12-01
36+
* @version 0.4.4, 2024-12-02
3737
* @since 0.4.1
3838
*/
3939
public class UpdateScriptGenerator {
@@ -58,9 +58,7 @@ public Path getPath() {
5858
* @return nowa instancja generatora skryptu
5959
*/
6060
public static UpdateScriptGenerator create(Path out) {
61-
return new UpdateScriptGenerator(IS_WINDOWS_OS?
62-
out.endsWith(WIN_EXT)? out : Paths.get(out + WIN_EXT)
63-
: out);
61+
return new UpdateScriptGenerator(IS_WINDOWS_OS && !out.endsWith(WIN_EXT)? Paths.get(out + WIN_EXT) : out);
6462
}
6563

6664
/**
@@ -71,7 +69,7 @@ public static UpdateScriptGenerator create(Path out) {
7169
public void generateUpdateScript(Map<Path, Path> updateMap) throws IOException {
7270
if (Files.notExists(path)) Files.createFile(path);
7371
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardOpenOption.WRITE)) {
74-
if (path.endsWith(WIN_EXT)) {
72+
if (IS_WINDOWS_OS) {
7573
generateWinUpdateScript(writer, updateMap);
7674
} else {
7775
generateUnixScript(writer, updateMap);
@@ -92,15 +90,21 @@ private void generateUnixScript(BufferedWriter writer, Map<Path, Path> updateMap
9290
writer.write(newFile.toString());
9391
writer.write('"' + " " + '"');
9492
writer.write(oldFile.toString());
95-
writer.write('"' + " /y");
93+
writer.write('"' + " 2>/dev/null");
9694
writer.newLine();
95+
} else throw new FileNotFoundException(BUNDLE.getString("THR_UPDATE_FILES_NOT_FOUND"));
96+
}
97+
for (Path newFile : updateMap.keySet()) {
98+
if (Files.exists(newFile)) {
9799
writer.write("rmdir -r " + '"');
98-
writer.write(oldFile.getParent().toString());
99-
writer.write('"' + " /y");
100+
writer.write(newFile.getParent().toString());
101+
writer.write('"' + " 2>/dev/null");
100102
writer.newLine();
101103
} else throw new FileNotFoundException(BUNDLE.getString("THR_UPDATE_FILES_NOT_FOUND"));
102104
}
103-
writer.write("java -jar \"" + SELF + '"');
105+
writer.write("rm -rf \"" + this.path + "\" 2>/dev/null");
106+
writer.newLine();
107+
writer.write('"' + System.getProperty("java.home") + "\\bin\\java\" -jar \"" + SELF + '"');
104108
writer.newLine();
105109
}
106110

@@ -118,15 +122,21 @@ private void generateWinUpdateScript(BufferedWriter writer, Map<Path, Path> upda
118122
writer.write(newFile.toString());
119123
writer.write('"' + " " + '"');
120124
writer.write(oldFile.toString());
121-
writer.write('"' + " /y");
125+
writer.write('"' + " > nul");
122126
writer.newLine();
123-
writer.write("rmdir -r " + '"');
124-
writer.write(oldFile.getParent().toString());
125-
writer.write('"' + " /y");
127+
} else throw new FileNotFoundException(BUNDLE.getString("THR_UPDATE_FILES_NOT_FOUND"));
128+
}
129+
for (Path newFile : updateMap.keySet()) {
130+
if (Files.exists(newFile)) {
131+
writer.write("rmdir /s /q " + '"');
132+
writer.write(newFile.getParent().toString());
133+
writer.write('"' + " > nul");
126134
writer.newLine();
127135
} else throw new FileNotFoundException(BUNDLE.getString("THR_UPDATE_FILES_NOT_FOUND"));
128136
}
129-
writer.write("java -jar \"" + SELF + '"');
137+
writer.write("del \"" + this.path + "\" /q");
138+
writer.newLine();
139+
writer.write('"' + System.getProperty("java.home") + "\\bin\\java.exe\" -jar \"" + SELF + '"');
130140
writer.newLine();
131141
}
132142
}

src/main/java/pl/koder95/eme/git/GitHubRepositoryController.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Klasa dostarcza metod kontrolujących połączenie z repozytorium GitHub.
2828
*
2929
* @author Kamil Jan Mularski [@koder95]
30-
* @version 0.4.1, 2021-11-07
30+
* @version 0.4.4, 2024-12-02
3131
* @since 0.4.1
3232
*/
3333
public class GitHubRepositoryController {
@@ -92,13 +92,9 @@ private void selectRepository() throws IOException {
9292
/**
9393
* Wprowadza podstawowe ustawienia do połączenia z repozytorium: wybiera użytkownika i repozytorium.
9494
*/
95-
public void init() {
96-
try {
97-
selectUser();
98-
selectRepository();
99-
} catch (IOException e) {
100-
throw new ExceptionInInitializerError(e);
101-
}
95+
public void init() throws IOException {
96+
selectUser();
97+
selectRepository();
10298
}
10399

104100
/**

0 commit comments

Comments
 (0)