Skip to content

Commit ea1db94

Browse files
committed
Poprawki procesu uruchamiania
1 parent b120e3b commit ea1db94

File tree

5 files changed

+100
-55
lines changed

5 files changed

+100
-55
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>org.kohsuke</groupId>
4545
<artifactId>github-api</artifactId>
46-
<version>1.119</version>
46+
<version>1.133</version>
4747
</dependency>
4848
</dependencies>
4949

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,27 @@ public final class Files {
3333
/**
3434
* Folder, gdzie znajdują się pliki programu.
3535
*/
36-
public static final File WORKDIR
37-
= new File(System.getProperty("user.dir")); //NOI18N
36+
public static final Path WORKDIR
37+
= Paths.get(System.getProperty("user.dir")); //NOI18N
3838
/**
3939
* Folder, gdzie znajdują się pliki zawierające dane do wczytania
4040
* przez program.
4141
*/
42-
public static final File DATA_DIR = WORKDIR;
42+
public static final Path DATA_DIR = WORKDIR;
4343
/**
4444
* Folder, gdzie znajdują się pliki zawierające dane do wczytania
4545
* przez program o rozszerzeniu XML.
4646
*/
47-
public static final File XML_DIR = DATA_DIR; //NOI18N
47+
public static final Path XML_DIR = DATA_DIR; //NOI18N
4848
/**
4949
* Plik, który przechowuje dane na temat indeksów.
5050
*/
51-
public static final File INDICES_XML = new File(Files.XML_DIR, "indices.xml");
51+
public static final Path INDICES_XML = XML_DIR.resolve("indices.xml");
5252

5353
public static final Path TEMP_DIR = Paths.get(System.getProperty("java.io.tmpdir"), "eMetrykant");
54-
55-
public static final File UPDATE_WIN = new File(Files.WORKDIR, "update.bat");
56-
public static final File UPDATE_UNIX = new File(Files.WORKDIR, "update");
57-
public static final Path SELF = WORKDIR.toPath().resolve("eMetrykant.jar");
54+
public static final Path UPDATE_WIN = Files.WORKDIR.resolve("update.bat");
55+
public static final Path UPDATE_UNIX = Files.WORKDIR.resolve("update");
56+
public static final Path SELF = WORKDIR.resolve("eMetrykant.jar");
5857

5958
private Files() {}
6059
}

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@
1717
package pl.koder95.eme;
1818

1919
import javafx.application.Application;
20+
import javafx.application.Platform;
2021
import javafx.fxml.FXMLLoader;
22+
import javafx.geometry.Pos;
2123
import javafx.scene.Parent;
2224
import javafx.scene.Scene;
25+
import javafx.scene.control.Label;
26+
import javafx.scene.control.ProgressIndicator;
2327
import javafx.scene.image.Image;
28+
import javafx.scene.layout.Background;
29+
import javafx.scene.layout.HBox;
30+
import javafx.scene.layout.VBox;
31+
import javafx.scene.paint.Color;
32+
import javafx.stage.Modality;
2433
import javafx.stage.Stage;
34+
import javafx.stage.StageStyle;
2535
import pl.koder95.eme.au.AutoUpdateTask;
2636
import pl.koder95.eme.core.*;
2737
import pl.koder95.eme.core.spi.CabinetAnalyzer;
2838
import pl.koder95.eme.core.spi.FilingCabinet;
2939
import pl.koder95.eme.dfs.IndexList;
3040

31-
import java.awt.*;
32-
import java.io.File;
3341
import java.text.Collator;
3442
import java.util.Arrays;
3543
import java.util.Locale;
@@ -86,9 +94,6 @@ public static void releaseMemory() {
8694
@Override
8795
public void init() throws Exception {
8896
super.init();
89-
if (Files.UPDATE_WIN.exists() && !Files.UPDATE_WIN.delete()) Files.UPDATE_WIN.deleteOnExit();
90-
if (Files.UPDATE_UNIX.exists() && !Files.UPDATE_UNIX.delete()) Files.UPDATE_UNIX.deleteOnExit();
91-
/*
9297
Arrays.stream(IndexList.values()).forEach(IndexList::load);
9398

9499
FilingCabinet cabinet = new TreeFilingCabinet();
@@ -100,19 +105,38 @@ public void init() throws Exception {
100105
worker.load();
101106

102107
root = FXMLLoader.load(ClassLoader.getSystemResource("pl/koder95/eme/fx/PersonalDataView.fxml"));
103-
*/
104108
task = new AutoUpdateTask();
105109
}
106110

107111
@Override
108112
public void start(Stage primaryStage) {
109-
/*
110113
primaryStage.getIcons().add(new Image(FAVICON_PATH));
111114
primaryStage.setTitle("eMetrykant " + Version.get());
112115
primaryStage.setScene(new Scene(root));
113116
primaryStage.setOnCloseRequest(event -> System.exit(0));
114-
primaryStage.show();
115-
*/
116-
task.run();
117+
118+
Stage secondary = new Stage();
119+
secondary.initStyle(StageStyle.UNDECORATED);
120+
121+
ProgressIndicator updating = new ProgressIndicator();
122+
Label title = new Label();
123+
Label message = new Label();
124+
updating.progressProperty().bind(task.progressProperty());
125+
title.textProperty().bind(task.titleProperty());
126+
message.textProperty().bind(task.messageProperty());
127+
128+
VBox texts = new VBox(5d, title, message);
129+
texts.setAlignment(Pos.CENTER_LEFT);
130+
VBox progressing = new VBox(updating);
131+
progressing.setAlignment(Pos.CENTER_RIGHT);
132+
HBox root = new HBox(5d, progressing, texts);
133+
root.setAlignment(Pos.CENTER);
134+
135+
secondary.setScene(new Scene(root));
136+
secondary.setAlwaysOnTop(true);
137+
secondary.setMinWidth(300);
138+
secondary.setMinHeight(150);
139+
secondary.show();
140+
Platform.runLater(task);
117141
}
118142
}

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

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,93 @@
55
import javafx.scene.control.Alert;
66
import org.kohsuke.github.GHAsset;
77
import org.kohsuke.github.GHRelease;
8-
import pl.koder95.eme.Files;
98

109
import java.io.*;
1110
import java.net.URL;
1211
import java.net.URLConnection;
1312
import java.nio.channels.Channels;
1413
import java.nio.channels.FileChannel;
1514
import java.nio.channels.ReadableByteChannel;
15+
import java.nio.file.Files;
1616
import java.nio.file.Path;
17+
import java.nio.file.StandardOpenOption;
1718
import java.text.NumberFormat;
1819
import java.util.Enumeration;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122
import java.util.zip.ZipEntry;
2223
import java.util.zip.ZipFile;
2324

24-
import static pl.koder95.eme.Files.SELF;
25-
import static pl.koder95.eme.Files.TEMP_DIR;
25+
import static pl.koder95.eme.Files.*;
2626

2727
public class AutoUpdateTask extends Task<Object> {
28+
2829
@Override
2930
protected Object call() throws Exception {
3031
GHAsset zip;
3132
try {
3233
GitHubRepositoryController controller = new GitHubRepositoryController("koder95", "eMetrykant");
3334
controller.init();
3435
GHRelease latest = controller.getRepository().getLatestRelease();
35-
zip = latest.listAssets().toList().stream().reduce(null, (r, c) -> {
36-
System.out.println(c);
37-
return c.getName().contains("eMetrykant") && c.getName().endsWith(".zip")? c : r;
38-
});
36+
zip = getGithubAsset(latest);
3937
} catch (IOException e) {
4038
e.printStackTrace();
41-
Alert a = new Alert(Alert.AlertType.ERROR, "Nie można pobrać eMetrykanta.");
42-
a.show();
39+
showException(e);
4340
return null;
4441
}
4542

4643
// pobieranie zip'a
4744
Path downloaded = downloadZip(zip.getBrowserDownloadUrl(), TEMP_DIR, zip.getName(), zip.getSize());
4845
// rozpakowywanie do folderu tymczasowego
4946
if (extractZip(downloaded, TEMP_DIR, true)) {
50-
// TODO: tworzenie mapy aktualizacji
5147
Map<Path, Path> updateMap = new HashMap<>();
52-
updateMap.put(TEMP_DIR.resolve("eMetrykant.jar"), SELF);
53-
54-
restart(updateMap);
48+
Files.list(TEMP_DIR).forEach(path -> {
49+
updateMap.put(path, WORKDIR.resolve(path.getFileName()));
50+
});
51+
generateUpdateScript(UPDATE_WIN, updateMap);
5552
}
53+
restart();
5654
return null;
5755
}
5856

57+
private void showException(Exception e) {
58+
showException(e, "Nie można pobrać eMetrykanta.");
59+
}
60+
61+
private void showException(Exception e, String title) {
62+
Alert a = new Alert(Alert.AlertType.ERROR);
63+
a.setTitle(title == null? "Wyjątek" : title);
64+
a.setHeaderText(e.getClass().getName());
65+
a.setContentText(e.getLocalizedMessage());
66+
a.show();
67+
}
68+
69+
private GHAsset getGithubAsset(GHRelease latest) throws IOException {
70+
return latest.listAssets().toList().stream().reduce(null, (r, c) -> {
71+
System.out.println(c);
72+
return isMainZip(c) ? c : r;
73+
});
74+
}
75+
76+
private boolean isMainZip(GHAsset c) {
77+
return c.getName().contains("eMetrykant") && c.getName().endsWith(".zip");
78+
}
79+
5980
private Path downloadZip(String url, Path dir, String name, long size) throws IOException {
6081
NumberFormat pF = NumberFormat.getPercentInstance();
61-
String title = "Pobieranie " + name;
62-
updateTitle(title);
82+
updateTitle("Pobieranie " + name);
6383
updateProgress(0, 1);
6484

6585
URLConnection connection = new URL(url).openConnection();
6686
Path forDownload = dir.resolve(name);
67-
java.nio.file.Files.createFile(forDownload);
87+
if (Files.notExists(forDownload.getParent()))
88+
Files.createDirectories(forDownload.getParent());
89+
if (Files.notExists(forDownload))
90+
Files.createFile(forDownload);
6891

69-
try (InputStream in = connection.getInputStream();
70-
ReadableByteChannel rbc = Channels.newChannel(in);
71-
FileChannel channel = FileChannel.open(forDownload)) {
92+
try (ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
93+
FileChannel channel = FileChannel.open(forDownload, StandardOpenOption.WRITE)) {
7294
channel.truncate(size);
73-
in.mark((int) size);
7495
long workDone = 0;
7596
long count = size > 100? size / 100: 1;
7697
while (workDone < size) {
@@ -91,15 +112,14 @@ private boolean extractZip(Path forExtract, Path dir, boolean deleteZip) throws
91112
updateProgress(0, 1);
92113
updateMessage("");
93114
try (ZipFile zip = new ZipFile(forExtract.toFile())) {
94-
updateProgress(0, java.nio.file.Files.size(forExtract));
115+
updateProgress(0, Files.size(forExtract));
95116
double total = 0;
96117
if (dir == null) dir = forExtract.getParent();
97118
Enumeration<? extends ZipEntry> entries = zip.entries();
98119
while (entries.hasMoreElements()) {
99120
ZipEntry entry = entries.nextElement();
100-
System.out.println(entry);
101121
if (entry.isDirectory()) {
102-
if (java.nio.file.Files.isDirectory(java.nio.file.Files.createDirectories(dir.resolve(entry.getName()))))
122+
if (Files.isDirectory(Files.createDirectories(dir.resolve(entry.getName()))))
103123
System.out.println("Directory created!");
104124
}
105125

@@ -114,15 +134,15 @@ private boolean extractZip(Path forExtract, Path dir, boolean deleteZip) throws
114134
Path outFile = dir.resolve(entry.getName());
115135

116136
if (!entry.isDirectory()) {
117-
if (java.nio.file.Files.isRegularFile(java.nio.file.Files.createFile(outFile))) {
137+
if (Files.isRegularFile(Files.createFile(outFile))) {
118138
System.out.println("New file created: " + outFile);
119139
}
120140
}
121141
else continue;
122142

123143
System.out.println("Entry: " + entry);
124144
InputStream input = zip.getInputStream(entry);
125-
try (OutputStream output = java.nio.file.Files.newOutputStream(outFile)) {
145+
try (OutputStream output = Files.newOutputStream(outFile)) {
126146
while (input.available() > 0) {
127147
int b = input.read();
128148
output.write(b);
@@ -134,24 +154,22 @@ private boolean extractZip(Path forExtract, Path dir, boolean deleteZip) throws
134154
}
135155
}
136156
updateProgress(Double.NaN, 0);
137-
if (deleteZip) return java.nio.file.Files.deleteIfExists(forExtract);
157+
if (deleteZip) return Files.deleteIfExists(forExtract);
138158
return false;
139159
}
140160

141-
private static void restart(Map<Path, Path> updateMap) {
161+
private static void restart() {
142162
Platform.exit();
143163
try {
144-
File update = Files.UPDATE_WIN;
145-
generateUpdateScript(update, updateMap);
146-
new ProcessBuilder(update.getPath()).start();
164+
new ProcessBuilder(UPDATE_WIN.toString()).start();
147165
} catch (IOException e) {
148166
e.printStackTrace();
149167
}
150168
System.exit(0);
151169
}
152170

153-
private static void generateUpdateScript(File update, Map<Path, Path> updateMap) {
154-
try (BufferedWriter writer = new BufferedWriter(new FileWriter(update))) {
171+
private static void generateUpdateScript(Path update, Map<Path, Path> updateMap) {
172+
try (BufferedWriter writer = Files.newBufferedWriter(update, StandardOpenOption.WRITE)) {
155173
generateWinUpdateScript(writer, updateMap);
156174
} catch (IOException e) {
157175
e.printStackTrace();
@@ -167,17 +185,21 @@ private static void generateWinUpdateScript(BufferedWriter writer, Map<Path, Pat
167185
for (Map.Entry<Path, Path> entry : updateMap.entrySet()) {
168186
Path oldFile = entry.getValue();
169187
Path newFile = entry.getKey();
170-
if (java.nio.file.Files.exists(newFile)) {
188+
if (Files.exists(newFile)) {
171189
writer.write("copy " + '"');
172190
writer.write(newFile.toString());
173191
writer.write('"' + " " + '"');
174192
writer.write(oldFile.toString());
175193
writer.write('"' + " /y");
176194
writer.newLine();
195+
writer.write("rmdir -r " + '"');
196+
writer.write(oldFile.getParent().toString());
197+
writer.write('"' + " /y");
198+
writer.newLine();
177199
} else throw new FileNotFoundException("Nie znaleziono odpowiednich plików do aktualizacji.");
178200
}
179201
final String javaBin = '"' + System.getProperty("java.home") + File.separator + "bin" + File.separator;
180-
writer.write(javaBin + "java.exe\" -jar " + '"' + Files.SELF + '"');
202+
writer.write(javaBin + "java.exe\" -jar " + '"' + SELF + '"');
181203
writer.newLine();
182204
}
183205
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ private GitHub createNewConnection() throws IOException {
1111
return new GitHubBuilder().withRateLimitHandler(new RateLimitHandler() {
1212
@Override
1313
public void onError(IOException e, HttpURLConnection uc) {
14-
e.printStackTrace();
1514
System.out.println(uc);
15+
e.printStackTrace();
1616
}
1717
}).build();
1818
}

0 commit comments

Comments
 (0)