Skip to content

Commit 0e30175

Browse files
committed
#2 Stop run command when close the app window
1 parent 0308df7 commit 0e30175

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/main/java/com/github/introfog/gitwave/controller/main/MainController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public void initialize(FxmlStageHolder fxmlStageHolder) {
7676
primaryStage.setOnCloseRequest(event -> {
7777
event.consume();
7878
if (DialogFactory.createCloseConfirmationAlert() == ButtonType.OK) {
79+
AppConfig.getInstance().closeApp();
7980
primaryStage.close();
8081
};
8182
});

src/main/java/com/github/introfog/gitwave/model/AppConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.File;
2828
import java.io.IOException;
2929
import java.util.List;
30+
import java.util.concurrent.atomic.AtomicBoolean;
3031
import javafx.application.HostServices;
3132
import org.slf4j.Logger;
3233
import org.slf4j.LoggerFactory;
@@ -41,6 +42,8 @@ public final class AppConfig {
4142
private final ConfigDto config;
4243
private HostServices hostServices;
4344

45+
private final AtomicBoolean appWasClosed = new AtomicBoolean(false);
46+
4447

4548
private AppConfig() {
4649
this.config = AppConfig.initConfig();
@@ -102,6 +105,14 @@ public void updateExistedCommand(CommandDto initial, CommandDto current) {
102105
saveConfig();
103106
}
104107

108+
public synchronized boolean appWasClosed() {
109+
return appWasClosed.get();
110+
}
111+
112+
public synchronized void closeApp() {
113+
appWasClosed.set(true);
114+
}
115+
105116
private void saveConfig() {
106117
File configDir = new File(CONFIG_DIR_PATH);
107118
if (!configDir.exists() && !configDir.mkdirs()) {

src/main/java/com/github/introfog/gitwave/model/CommandExecutor.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ public static File searchGitRepositoriesAndCreateScriptFile(File directory, Stri
3737
LOGGER.info("Start searching git repositories in '{}' path.", directory.getAbsolutePath());
3838
List<File> repositoriesToRunCommand = new ArrayList<>();
3939
searchGitRepositories(directory, repositoriesToRunCommand);
40+
if (AppConfig.getInstance().appWasClosed()) {
41+
return null;
42+
}
4043
LOGGER.info("'{}' git repositories were found to run command.", repositoriesToRunCommand.size());
4144
try {
42-
return createAndFillScriptFileWithCommand(command, repositoriesToRunCommand);
45+
final File scriptFile = createAndFillScriptFileWithCommand(command, repositoriesToRunCommand);
46+
if (AppConfig.getInstance().appWasClosed()) {
47+
removeScriptFile(scriptFile);
48+
}
49+
return scriptFile;
4350
} catch (IOException e) {
4451
LOGGER.error("Something goes wrong with creation temp script file with command.", e);
4552
}
@@ -48,20 +55,17 @@ public static File searchGitRepositoriesAndCreateScriptFile(File directory, Stri
4855
}
4956

5057
public static void executeScriptFileWithCommand(File scriptFile) {
58+
if (scriptFile == null || AppConfig.getInstance().appWasClosed()) {
59+
return;
60+
}
5161
try {
5262
LOGGER.info("Start executing script file '{}'.", scriptFile.getAbsolutePath());
5363
executeScriptFile(scriptFile);
5464
LOGGER.info("Finish executing git command.");
5565
} catch (IOException e) {
5666
LOGGER.error("Something goes wrong with executing temp script file with command.", e);
5767
} finally {
58-
if (scriptFile != null && scriptFile.exists()) {
59-
if (scriptFile.delete()) {
60-
LOGGER.info("Temp script file '{}' was removed.", scriptFile.getAbsolutePath());
61-
} else {
62-
LOGGER.warn("Temp script file '{}' with command wasn't removed.", scriptFile.getAbsolutePath());
63-
}
64-
}
68+
removeScriptFile(scriptFile);
6569
}
6670
}
6771

@@ -107,6 +111,9 @@ private static File createAndFillScriptFileWithCommand(String gitCommand, List<F
107111
writer.write("#!/bin/bash\n");
108112
writer.write("echo -e \"\\033[0;32m\" \"" + gitCommand + "\" \"\\033[0m\"\n");
109113
for (File currentFolder : repositoriesToRunCommand) {
114+
if (AppConfig.getInstance().appWasClosed()) {
115+
break;
116+
}
110117
writer.write("cd \"" + currentFolder.getAbsolutePath().replace("\\", "\\\\") + "\"\n");
111118
writer.write("echo -e \"\\033[0;36m\" $PWD \"\\033[0m\"\n");
112119

@@ -118,6 +125,9 @@ private static File createAndFillScriptFileWithCommand(String gitCommand, List<F
118125
}
119126

120127
private static void searchGitRepositories(File folder, List<File> repositoriesToRunCommand) {
128+
if (AppConfig.getInstance().appWasClosed()) {
129+
return;
130+
}
121131
if (isGitRepository(folder)) {
122132
repositoriesToRunCommand.add(folder);
123133
} else {
@@ -136,6 +146,16 @@ private static void searchGitRepositories(File folder, List<File> repositoriesTo
136146
}
137147
}
138148

149+
private static void removeScriptFile(File scriptFile) {
150+
if (scriptFile != null && scriptFile.exists()) {
151+
if (scriptFile.delete()) {
152+
LOGGER.info("Temp script file '{}' was removed.", scriptFile.getAbsolutePath());
153+
} else {
154+
LOGGER.warn("Temp script file '{}' with command wasn't removed.", scriptFile.getAbsolutePath());
155+
}
156+
}
157+
}
158+
139159
private static boolean isGitRepository(File folder) {
140160
File gitFolder = new File(folder, ".git");
141161
return gitFolder.exists() && gitFolder.isDirectory();

0 commit comments

Comments
 (0)