Skip to content

Commit

Permalink
Refactored and reworked on the MainController
Browse files Browse the repository at this point in the history
  • Loading branch information
Guleri24 committed Aug 28, 2022
1 parent 8a5587d commit 6f90fc7
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions src/main/java/com/horizonxe/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,73 @@ public class MainController {
public AnchorPane mainView;
private File executableFile;
private File dataFile;
private boolean isWindows;
private boolean isOSWindows;

public void initialize() throws IOException {
newFile("exec");
// Creates executable file Exec for creating scripts of the maven command
createOsSpecificFile("exec");

writer = Files.newBufferedWriter(Paths.get(String.valueOf(executableFile)));
writer.write("");
writer.flush();
initData();
}

private void newFile(String type) throws IOException {
isWindows = System.getProperty("os.name")
private void initData() throws IOException {
// Creates Data file for storing projects and modules
createOsSpecificFile("data");

String row;
BufferedReader csvReader = new BufferedReader(new FileReader(dataFile));
while ((row = csvReader.readLine()) != null) {
String[] data = row.split(",");
if (data.length > 1) {
if (!data[0].isBlank())
projects.add(data[0]);
if (!data[1].isBlank())
modules.add(data[1]);
} else {
projects.add(data[0]);
}
}
csvReader.close();

if (projects.isEmpty())
projects.add("Empty");
if (modules.isEmpty())
modules.add("Empty");

projectMenu.setItems(FXCollections.observableArrayList(projects));
moduleMenu.setItems(FXCollections.observableArrayList(modules));
}

private void createOsSpecificFile(String type) throws IOException {
isOSWindows = System.getProperty("os.name")
.toLowerCase().startsWith("windows");
String username = System.getProperty("user.name");
String windowsPath = "C:\\Users\\" + username + "\\FisGlobal\\";
String linuxPath = "/home/" + username + "/FisGlobal/";
switch (type) {
case "exec" -> {
if (isWindows) {
if (isOSWindows) {
executableFile = new File(windowsPath + "exec.bat");
} else {
executableFile = new File(linuxPath + "exec.sh");
}

// Checks for permission and creation of exec file
executableFile.getParentFile().mkdir();
executableFile.createNewFile();
executableFile.setExecutable(true);
}
case "data" -> {
if (isWindows) {
if (isOSWindows) {
dataFile = new File(windowsPath + "data.csv");
} else {
dataFile = new File(linuxPath + "data.csv");
}

// Checks for permission and creation of data file
dataFile.getParentFile().mkdir();
dataFile.createNewFile();
dataFile.setExecutable(true);
Expand All @@ -80,50 +113,24 @@ public void writeData() {
}

private void writeData(BufferedWriter writer, String project, String module) throws IOException {
String executeCmds = "ls "; // Add execute before all commands here
writer.append(executeCmds).append(" && mvn -DProj ").append(project).append(" -DMod ").append(module);
}

private void initData() throws IOException {
newFile("data");
String row;

BufferedReader csvReader = new BufferedReader(new FileReader(dataFile));
while ((row = csvReader.readLine()) != null) {
String[] data = row.split(",");
if (data.length > 1) {
if (!data[0].isBlank())
projects.add(data[0]);
if (!data[1].isBlank())
modules.add(data[1]);
} else {
projects.add(data[0]);
}
}
csvReader.close();

if (projects.isEmpty())
projects.add("Empty");
if (modules.isEmpty())
modules.add("Empty");

projectMenu.setItems(FXCollections.observableArrayList(projects));
moduleMenu.setItems(FXCollections.observableArrayList(modules));
String preMavenBuildCommands = "ls "; // Add commands which needed to be executed before maven build command
writer.append(preMavenBuildCommands)
.append(" && mvn -DProj ")
.append(project)
.append(" -DMod ")
.append(module);
}

@FXML
public void triggerCmd() throws IOException {
writeData();
Stage stage = (Stage) mainView.getScene().getWindow();
writer.close();
if (isWindows) {
if (isOSWindows) {
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"" + executableFile.getAbsolutePath() + "\"");
stage.close();
} else {
stage.close();
/*
Runtime.getRuntime().exec(System.getenv("TERM") + " -e bash -c " + file.getAbsolutePath() + ";read");
*/
Runtime.getRuntime().exec("alacritty" + " -e bash -c " + executableFile.getAbsolutePath() + ";read");

}
Expand All @@ -132,14 +139,11 @@ public void triggerCmd() throws IOException {
@FXML
public void addData() throws IOException {
Stage stage = (Stage) mainView.getScene().getWindow();
if (isWindows) {
if (isOSWindows) {
Runtime.getRuntime().exec("cmd /c start excel /K \"" + dataFile.getAbsolutePath() + "\"");
stage.close();
} else {
stage.close();
/*
Runtime.getRuntime().exec(System.getenv("TERM") + " -e bash -c " + file.getAbsolutePath() + ";read");
*/
Runtime.getRuntime().exec("gio " + " open " + dataFile.getAbsolutePath());
}
}
Expand Down

0 comments on commit 6f90fc7

Please sign in to comment.