Skip to content

Commit

Permalink
v1.1: + Live console output while compiling.
Browse files Browse the repository at this point in the history
+ No longer blocks when compiling.
+ Compilation can be cancalled.
+ Handle settings.xml error.
+ Do not save settings.xml if watched dirs are empty (like on a settings.xml read error).
  • Loading branch information
FutureMillennium committed May 28, 2024
1 parent d64a639 commit 3613d8b
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 40 deletions.
36 changes: 35 additions & 1 deletion MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

181 changes: 144 additions & 37 deletions MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Threading;

Expand All @@ -11,6 +12,11 @@ public partial class MainForm : Form {

DispatcherTimer timer1 = new DispatcherTimer();
bool blLoaded = false;
Thread compileThread;

WatchedDir activeWD;
String appName;
bool threadFinished = true;

public MainForm() {
InitializeComponent();
Expand All @@ -28,7 +34,8 @@ public MainForm() {
} else {
ver = version.Major + "." + version.Minor + "." + version.Build;
}
this.Text = "Compile Watchdog v" + ver + " by Zdeněk Gromnica";
appName = "Compile Watchdog v" + ver + " by Zdeněk Gromnica";
this.Text = appName;
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
Expand Down Expand Up @@ -95,6 +102,7 @@ private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) {
} else {
groupBox1.Visible = false;
}
MyRefresh();
}

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
Expand All @@ -111,17 +119,20 @@ private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
}
}

private void compileNowButton_Click(object sender, EventArgs e) {
private void compileAllNowButton_Click(object sender, EventArgs e) {
int nCompiled = 0;
for (int i = 0; i < watchedDirs.Count; i++) {
var wd = watchedDirs[i];
if (wd.enabled) {
wd.needsCompile = true;
nCompiled++;
HandleCompile(wd, i);
//HandleCompile(wd, i);
}
}
txtOuput.Text = "";
CompileAll();
timer1.Stop();
MyRefresh();
//MyRefresh();
if (nCompiled == 0) {
MessageBox.Show("Nothing to compile! Add directories by dragging them into the window and make sure they're checked in the list.", "Compile Watchdog", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
Expand All @@ -143,22 +154,9 @@ void Add(WatchedDir wd, bool addToWD = true) {
watchers.Add(watcher);
}

void HandleCompile(WatchedDir wd, int index) {
if (checkedListBox1.SelectedIndex == index) {
lastOutputTextBox.Text = "(Compiling…)";
this.Refresh();
}
Compile(wd);
if (wd.lastError.Length > 0) {
if (this.InvokeRequired) {
this.Invoke(new Action(() => {
RefocusRefresh(wd, index);
}));
} else {
RefocusRefresh(wd, index);
}
}
}
/*void HandleCompile(WatchedDir wd, int index) {
Compile(wd, index);
}*/

void RefocusRefresh(WatchedDir wd, int index) {
checkedListBox1.SelectedIndex = index;
Expand All @@ -167,11 +165,12 @@ void RefocusRefresh(WatchedDir wd, int index) {
this.Focus();
notifyIcon1.Visible = false;
}
MyRefresh();
}

void MyRefresh() {
if (checkedListBox1.SelectedIndex >= 0) {
if (watchedDirs[checkedListBox1.SelectedIndex].lastOutput.Length == 0 && watchedDirs[checkedListBox1.SelectedIndex].lastError.Length == 0) {
if (watchedDirs[checkedListBox1.SelectedIndex].lastOutput?.Length == 0 && watchedDirs[checkedListBox1.SelectedIndex].lastError?.Length == 0) {
lastOutputTextBox.Text = "(The output was empty.)";
} else {
lastOutputTextBox.Text = watchedDirs[checkedListBox1.SelectedIndex].lastOutput;
Expand All @@ -180,7 +179,27 @@ void MyRefresh() {
}
}

void Compile(WatchedDir wd) {
void HandleCompile(WatchedDir wd, int index) {
// If there's no compileThread or it's finished, start a new one:
if (compileThread == null || !compileThread.IsAlive || threadFinished) {
this.Text = "[COMPILING] " + appName;
if (checkedListBox1.SelectedIndex == index) {
lastOutputTextBox.Text = "(Compiling…)";
this.Refresh();
}
txtOuput.Text += "[COMPILING]" +Environment.NewLine + wd.path + "\\" + wd.compileCommand + "\r\n";
txtOuput.Visible = true;
cancelCompilationButton.Text = "&Cancel compilation";
cancelCompilationButton.Visible = true;
compileThread = new Thread(() => CompileThread(wd,index));
compileThread.Start();
threadFinished = false;
}
}

void CompileThread(WatchedDir wd, int index) {
threadFinished = false;
activeWD = wd;
// run compile command
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "cmd.exe";
Expand All @@ -190,17 +209,64 @@ void Compile(WatchedDir wd) {
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.OutputDataReceived += P_OutputDataReceived;
p.ErrorDataReceived += P_ErrorDataReceived;
p.StartInfo = psi;
p.Start();
wd.lastOutput = "";
wd.lastError = "";
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
wd.lastOutput = output;
wd.lastError = error;
//string output = p.StandardOutput.ReadToEnd();
//string error = p.StandardError.ReadToEnd();
//wd.lastOutput = output;
//wd.lastError = error;

wd.needsCompile = false;

//if (this.InvokeRequired) {
this.Invoke(new Action(() => {
txtOuput.Visible = false;
cancelCompilationButton.Visible = false;
threadFinished = true;
if (wd.lastError.Length > 0) {
this.Text = "[ERROR] " + appName;
RefocusRefresh(wd, index);
} else {
this.Text = appName;
MyRefresh();
CompileAll();
}
}));
//} else {
// RefocusRefresh(wd, index);
//}
}

private void P_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) {
if (e.Data == null) { return; }

this.Invoke(new Action(() => {
activeWD.lastOutput += e.Data + "\r\n";
txtOuput.Text += e.Data + "\r\n";
}));
}
private void P_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) {
if (e.Data == null) { return; }

this.Invoke(new Action(() => {
activeWD.lastError += e.Data + "\r\n";
txtOuput.Text += e.Data + "\r\n";
}));
}

void SaveSettings() {
if (watchedDirs == null || watchedDirs.Count == 0) {
return;
}

// filename is exe name + Settings.xml
var filename = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
if (filename.EndsWith(".exe")) {
Expand All @@ -222,14 +288,28 @@ void LoadSettings() {
}
filename += "Settings.xml";

if (System.IO.File.Exists(filename)) {
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(List<WatchedDir>));
System.IO.Stream s = new System.IO.FileStream(filename, System.IO.FileMode.Open);
watchedDirs = (List<WatchedDir>)xs.Deserialize(s);
s.Close();
foreach (WatchedDir wd in watchedDirs) {
Add(wd, false);
try {
if (System.IO.File.Exists(filename)) {
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(List<WatchedDir>));
System.IO.Stream s = new System.IO.FileStream(filename, System.IO.FileMode.Open);
watchedDirs = (List<WatchedDir>)xs.Deserialize(s);
s.Close();
foreach (WatchedDir wd in watchedDirs) {
Add(wd, false);
}
}
} catch (Exception ex) {
txtOuput.Text = "Error reading " + filename + Environment.NewLine + Environment.NewLine + ex.Message + Environment.NewLine;
if (ex.InnerException != null) {
txtOuput.Text += Environment.NewLine + ex.InnerException.Message + Environment.NewLine;
}
txtOuput.Text += Environment.NewLine + "Try editing the file and removing the problematic part, usually the output. The settings XML file won't be saved unless you add a new watched directory and quit.";
txtOuput.Visible = true;
cancelCompilationButton.Text = "O&K";
cancelCompilationButton.Visible = true;
compileNowButton.Visible = false;
popUpOnErorrCheckbox.Visible = false;
minimiseButton.Visible = false;
}
blLoaded = true;
}
Expand Down Expand Up @@ -262,14 +342,25 @@ private void OnChanged(object source, FileSystemEventArgs e) {
}
// compilation timer
void timer1_Tick(object sender, EventArgs e) {
for (int i = 0; i < watchedDirs.Count; i++) {
/*for (int i = 0; i < watchedDirs.Count; i++) {
var wd = watchedDirs[i];
if (wd.needsCompile) {
HandleCompile(wd, i);
}
}
}*/
txtOuput.Text = "";
CompileAll();
timer1.Stop();
MyRefresh();
//MyRefresh();
}

void CompileAll() {
for (int i = 0; i < watchedDirs.Count; i++) {
var wd = watchedDirs[i];
if (wd.enabled && wd.needsCompile) {
HandleCompile(wd, i);
}
}
}

private void minimiseButton_Click(object sender, EventArgs e) {
Expand All @@ -282,6 +373,22 @@ private void ignoreTextBox_TextChanged(object sender, EventArgs e) {
watchedDirs[checkedListBox1.SelectedIndex].ignore = ignoreTextBox.Text;
}
}

private void cancelCompilationButton_Click(object sender, EventArgs e) {
if (compileThread != null && compileThread.IsAlive) {
compileThread.Abort();
txtOuput.Text += "\r\nCompilation cancelled by user.\r\n";
}
for (int i = 0; i < watchedDirs.Count; i++) {
var wd = watchedDirs[i];
wd.needsCompile = false;
}
txtOuput.Visible = false;
cancelCompilationButton.Visible = false;
compileNowButton.Visible = true;
popUpOnErorrCheckbox.Visible = true;
minimiseButton.Visible = true;
}
}

public class WatchedDir {
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
Binary file modified bin/Release/CompileWatchdog.exe
Binary file not shown.
Binary file modified bin/Release/CompileWatchdog.pdb
Binary file not shown.

0 comments on commit 3613d8b

Please sign in to comment.