Skip to content

Commit d418d75

Browse files
committed
#1 Disable run button while searching git repositories and write script file
1 parent 6481bf2 commit d418d75

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import com.github.introfog.gitwave.model.StageFactory.FxmlStageHolder;
2525
import com.github.introfog.gitwave.model.dto.ParameterDto;
2626

27+
import java.io.File;
2728
import javafx.fxml.FXML;
2829
import javafx.scene.control.Button;
2930
import javafx.scene.control.ButtonType;
3031
import javafx.scene.control.Label;
3132
import javafx.scene.control.Menu;
3233
import javafx.scene.control.MenuItem;
34+
import javafx.scene.control.ProgressIndicator;
3335
import javafx.scene.control.SeparatorMenuItem;
3436
import javafx.scene.control.TableView;
3537
import javafx.scene.control.TextField;
@@ -62,6 +64,11 @@ public class MainController extends BaseController {
6264
@FXML
6365
private SeparatorMenuItem updateMenuItemSeparator;
6466

67+
@FXML
68+
private ProgressIndicator runProgress;
69+
@FXML
70+
private Button run;
71+
6572
@Override
6673
public void initialize(FxmlStageHolder fxmlStageHolder) {
6774
super.initialize(fxmlStageHolder);
@@ -108,8 +115,11 @@ protected void runCommand() {
108115
AppConfig.getInstance().setLastRunFolder(directoryTabController.getDirectory());
109116

110117
new Thread(() -> {
111-
CommandExecutor.searchGitReposAndExecuteCommand(directoryTabController.getDirectory(),
112-
commandTabController.getCommandWithParameters());
118+
switchRunButton(true);
119+
final File scriptFile = CommandExecutor.searchGitRepositoriesAndCreateScriptFile(
120+
directoryTabController.getDirectory(), commandTabController.getCommandWithParameters());
121+
switchRunButton(false);
122+
CommandExecutor.executeScriptFileWithCommand(scriptFile);
113123
}).start();
114124
}
115125
}
@@ -133,4 +143,9 @@ protected void openUpdate() {
133143
protected void foundIssue() {
134144
AppConfig.getInstance().getHostServices().showDocument(AppConstants.LINK_TO_GIT_CONTRIBUTING_FILE);
135145
}
146+
147+
private void switchRunButton(boolean inProgress) {
148+
run.setDisable(inProgress);
149+
runProgress.setVisible(inProgress);
150+
}
136151
}

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,58 @@ private CommandExecutor() {
3333
// Empty constructor
3434
}
3535

36-
public static void searchGitReposAndExecuteCommand(String folderPath, String command) {
37-
LOGGER.info("Start searching git repositories in '{}' path and execute '{}' command", folderPath, command);
36+
public static File searchGitRepositoriesAndCreateScriptFile(String folderPath, String command) {
3837
File folder = new File(folderPath);
3938
if (folder.exists() && folder.isDirectory()) {
39+
LOGGER.info("Start searching git repositories in '{}' path.", folderPath);
4040
List<File> repositoriesToRunCommand = new ArrayList<>();
4141
searchGitRepositories(folder, repositoriesToRunCommand);
42-
File scriptFile = null;
42+
LOGGER.info("'{}' git repositories were found to run command.", repositoriesToRunCommand.size());
4343
try {
44-
scriptFile = createAndFillTempFileWithCommand(command, repositoriesToRunCommand);
45-
executeCommand(scriptFile);
44+
return createAndFillScriptFileWithCommand(command, repositoriesToRunCommand);
4645
} catch (IOException e) {
47-
LOGGER.error("Something goes wrong with creation or executing of temp script file with git command.", e);
48-
} finally {
49-
if (scriptFile != null && scriptFile.exists()) {
50-
if (scriptFile.delete()) {
51-
LOGGER.info("Temp file '{}' was removed.", scriptFile.getAbsolutePath());
52-
} else {
53-
LOGGER.warn("Temp script file '{}' with git command wasn't removed.", scriptFile.getAbsolutePath());
54-
}
55-
}
46+
LOGGER.error("Something goes wrong with creation temp script file with command.", e);
5647
}
5748
} else {
58-
LOGGER.error("Specified folder either doesn't exist or isn't a directory, running git command was skipped.");
49+
LOGGER.error("'{}' folder either doesn't exist or isn't a directory, running command was skipped.", folderPath);
50+
}
51+
return null;
52+
}
53+
54+
public static void executeScriptFileWithCommand(File scriptFile) {
55+
try {
56+
LOGGER.info("Start executing script file '{}'.", scriptFile.getAbsolutePath());
57+
executeScriptFile(scriptFile);
58+
LOGGER.info("Finish executing git command.");
59+
} catch (IOException e) {
60+
LOGGER.error("Something goes wrong with executing temp script file with command.", e);
61+
} finally {
62+
if (scriptFile != null && scriptFile.exists()) {
63+
if (scriptFile.delete()) {
64+
LOGGER.info("Temp script file '{}' was removed.", scriptFile.getAbsolutePath());
65+
} else {
66+
LOGGER.warn("Temp script file '{}' with command wasn't removed.", scriptFile.getAbsolutePath());
67+
}
68+
}
5969
}
6070
}
6171

62-
private static void executeCommand(File scriptFile) throws IOException {
72+
private static void executeScriptFile(File scriptFile) throws IOException {
6373
Process powerShellProcess = Runtime.getRuntime().exec(constructCmdCommand(scriptFile));
6474
String line;
6575

6676
BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
6777
while ((line = stdout.readLine()) != null) {
68-
LOGGER.info("Standard output of executed git command '{}'", line);
78+
LOGGER.info("Standard output of executed command '{}'", line);
6979
}
7080
stdout.close();
7181

7282
BufferedReader stderr = new BufferedReader(new InputStreamReader(
7383
powerShellProcess.getErrorStream()));
7484
while ((line = stderr.readLine()) != null) {
75-
LOGGER.error("Error output of executed git command '{}'", line);
85+
LOGGER.error("Error output of executed command '{}'", line);
7686
}
7787
stderr.close();
78-
79-
LOGGER.info("Finish executing git command.");
8088
}
8189

8290
private static String[] constructCmdCommand(File scriptFile) {
@@ -91,14 +99,14 @@ private static String[] constructCmdCommand(File scriptFile) {
9199
return cmdCommand.toArray(new String[]{});
92100
}
93101

94-
private static File createAndFillTempFileWithCommand(String gitCommand, List<File> repositoriesToRunCommand) throws IOException {
102+
private static File createAndFillScriptFileWithCommand(String gitCommand, List<File> repositoriesToRunCommand) throws IOException {
95103
File tempDir = new File("temp");
96104
if (!tempDir.exists() && !tempDir.mkdirs()) {
97105
LOGGER.error("Failed to create the temp directory '{}'.", "temp");
98106
}
99107

100108
File scriptFile = File.createTempFile("script", ".sh", tempDir);
101-
LOGGER.info("Temp file '{}' was created", scriptFile.getAbsolutePath());
109+
LOGGER.info("Temp script file '{}' was created", scriptFile.getAbsolutePath());
102110

103111
FileWriter writer = new FileWriter(scriptFile);
104112
writer.write("#!/bin/bash\n");
@@ -110,7 +118,7 @@ private static File createAndFillTempFileWithCommand(String gitCommand, List<Fil
110118
writer.write(gitCommand + "\n");
111119
}
112120
writer.close();
113-
LOGGER.info("Git command '{}' was written to temp file.", gitCommand);
121+
LOGGER.info("Command '{}' was written to temp script file.", gitCommand);
114122
return scriptFile;
115123
}
116124

src/main/resources/com/github/introfog/gitwave/view/main.fxml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<?import javafx.scene.control.Menu?>
77
<?import javafx.scene.control.MenuBar?>
88
<?import javafx.scene.control.MenuItem?>
9+
<?import javafx.scene.control.ProgressIndicator?>
910
<?import javafx.scene.control.Separator?>
1011
<?import javafx.scene.control.SeparatorMenuItem?>
1112
<?import javafx.scene.control.Tab?>
@@ -122,7 +123,7 @@
122123
</TabPane>
123124
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" GridPane.vgrow="NEVER">
124125
<children>
125-
<Button layoutX="386.0" layoutY="36.0" mnemonicParsing="false" onAction="#runCommand" text="4. Run" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0">
126+
<Button fx:id="run" layoutX="386.0" layoutY="36.0" mnemonicParsing="false" onAction="#runCommand" text="4. Run" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0">
126127
<font>
127128
<Font name="Verdana" size="12.0" />
128129
</font></Button>
@@ -131,6 +132,7 @@
131132
<Font name="Verdana" size="12.0" />
132133
</font></Hyperlink>
133134
<Separator layoutY="17.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
135+
<ProgressIndicator fx:id="runProgress" layoutX="345.0" layoutY="9.0" prefHeight="20.0" prefWidth="20.0" visible="false" />
134136
</children>
135137
</AnchorPane>
136138
</children>

0 commit comments

Comments
 (0)