Skip to content

Commit 55a41b2

Browse files
committed
Cancel build facilities
* Cancel build before saving a project item. This fix trying to save a read only file being compiled. * Added the possibility to double click the icon to cancel current background compilation.
1 parent 58f1716 commit 55a41b2

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

BuildOnSave/BuildOnSave.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ sealed class BuildOnSave
3535
readonly DocumentEvents _documentEvents;
3636
readonly BuildEvents _buildEvents;
3737
readonly CommandEvents _buildSolutionEvent;
38-
38+
readonly CommandEvents _saveCommandEvent;
39+
readonly CommandEvents _saveAsCommandEvent;
3940

4041
// state
4142
SolutionOptions _solutionOptions;
@@ -49,7 +50,8 @@ public BuildOnSave(DTE dte, OleMenuCommandService commandService)
4950
_buildEvents = _events.BuildEvents;
5051
var guid = typeof(VSConstants.VSStd97CmdID).GUID.ToString("B");
5152
_buildSolutionEvent = _dte.Events.CommandEvents[guid, (int)VSConstants.VSStd97CmdID.BuildSln];
52-
53+
_saveCommandEvent = _dte.Events.CommandEvents[guid, (int)VSConstants.VSStd97CmdID.SaveProjectItem];
54+
_saveAsCommandEvent = _dte.Events.CommandEvents[guid, (int)VSConstants.VSStd97CmdID.SaveProjectItemAs];
5355

5456
_topMenu = new MenuCommand(delegate { },
5557
new CommandID(CommandSet, TopMenuCommandId));
@@ -180,6 +182,10 @@ void connectDriver(SolutionOptions options)
180182
_buildSolutionEvent.BeforeExecute += driver.onBeforeBuildSolutionCommand;
181183
_buildSolutionEvent.AfterExecute += driver.onAfterBuildSolutionCommand;
182184

185+
// Cancel background build link to following commands
186+
_saveCommandEvent.BeforeExecute += driver.onBeforeSaveCommand;
187+
_saveAsCommandEvent.BeforeExecute += driver.onBeforeSaveAsCommand;
188+
183189
_driver_ = driver;
184190

185191
Log.D("driver connected");
@@ -199,6 +205,10 @@ void disconnectDriver()
199205
_buildSolutionEvent.BeforeExecute -= driver.onBeforeBuildSolutionCommand;
200206
_buildSolutionEvent.AfterExecute -= driver.onAfterBuildSolutionCommand;
201207

208+
// Cancel background build link to following commands
209+
_saveCommandEvent.BeforeExecute -= driver.onBeforeSaveCommand;
210+
_saveAsCommandEvent.BeforeExecute -= driver.onBeforeSaveAsCommand;
211+
202212
_driver_.Dispose();
203213
_driver_ = null;
204214

BuildOnSave/Driver.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public Driver(DTE dte, SolutionOptions options, BackgroundBuild2 backgroundBuild
3535
_options = options;
3636
_backgroundBuild = backgroundBuild;
3737
_ui = ui;
38+
_ui._onIconDoubleClick += () => {
39+
cancelBackgroundBuild();
40+
};
41+
3842
_context = SynchronizationContext.Current;
3943
}
4044

@@ -74,16 +78,26 @@ public void onBuildBegin(vsBuildScope scope, vsBuildAction action)
7478
dumpState();
7579
Log.D("VS build begin {scope}, {action}", scope, action);
7680

77-
prepareForVSBuild();
81+
cancelBackgroundBuild();
7882
}
7983

80-
void prepareForVSBuild()
84+
public void onBeforeSaveCommand(string guid, int id, object customIn, object customOut, ref bool cancelDefault)
8185
{
82-
// immediately reflect in the UI that we are not wanted anymore!
83-
_ui.setBuildStatus(BuildStatus.Indeterminate);
86+
cancelBackgroundBuild();
87+
}
8488

89+
public void onBeforeSaveAsCommand(string guid, int id, object customIn, object customOut, ref bool cancelDefault)
90+
{
91+
cancelBackgroundBuild();
92+
}
93+
94+
void cancelBackgroundBuild()
95+
{
8596
_buildAgain = false;
8697
_backgroundBuild.cancelAndWait();
98+
99+
// immediately reflect in the UI that we are not wanted anymore!
100+
_ui.setBuildStatus(BuildStatus.Indeterminate);
87101
}
88102

89103
public void onBuildDone(vsBuildScope scope, vsBuildAction action)

BuildOnSave/DriverUI.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Drawing;
33
using System.Windows.Forms;
44
using EnvDTE;
@@ -30,6 +30,10 @@ sealed class DriverUI : IDisposable
3030
BuildStatus _status = BuildStatus.Indeterminate;
3131
bool _processing;
3232

33+
DateTime _lastCLickTime;
34+
public delegate void OnIconDoubleClick();
35+
public OnIconDoubleClick _onIconDoubleClick;
36+
3337
const string BarButtonControlCaption = "BuildOnSave Status";
3438

3539
public DriverUI(DTE dte, Window outputWindow, OutputWindowPane pane)
@@ -52,8 +56,17 @@ public DriverUI(DTE dte, Window outputWindow, OutputWindowPane pane)
5256
control.BeginGroup = true;
5357
control.Click += (CommandBarButton ctrl, ref bool d) =>
5458
{
59+
TimeSpan ts = DateTime.Now - _lastCLickTime;
60+
_lastCLickTime = DateTime.Now;
61+
62+
if (ts.TotalMilliseconds < 500)
63+
{
64+
//consider double click => cancel current build if any
65+
_onIconDoubleClick();
66+
}
67+
5568
_outputWindow.Visible = true;
56-
pane.Activate();
69+
_pane.Activate();
5770
};
5871
}
5972
else

BuildOnSave/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
33
<Metadata>
4-
<Identity Id="BuildOnSave.Armin Sander.31ad4701-38ab-4dfa-9173-256d95ffed3d" Version="0.35.1" Language="en-US" Publisher="Armin Sander" />
4+
<Identity Id="BuildOnSave.Armin Sander.31ad4701-38ab-4dfa-9173-256d95ffed3d" Version="0.35.2" Language="en-US" Publisher="Armin Sander" />
55
<DisplayName>BuildOnSave</DisplayName>
66
<Description xml:space="preserve">Builds your solution when a file is saved.</Description>
77
<License>LICENSE.txt</License>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ BuildOnSave is an extension for Visual Studio 2017 and 2019 that builds the curr
1010

1111
- There is a menu named BuildOnSave in the Visual Studio menu bar with options to disable BuildOnSave and to specify what exactly should be built. Options are stored per solution.
1212
- A "BuildOnSave" pane is shown as soon a build starts.
13-
- An icon is added to the Standard toolbar that shows the build status for background builds. **red** for build failed, **yellow** to show that the build is indeterminate, and **green** means the build succeeded. If the icon shows an outline instead of a filled circle, the build process is active. Clicking the icon opens the BuildOnSave pane.
13+
- An icon is added to the Standard toolbar that shows the build status for background builds. **red** for build failed, **yellow** to show that the build is indeterminate, and **green** means the build succeeded. If the icon shows an outline instead of a filled circle, the build process is active. Clicking the icon opens the BuildOnSave pane. Double clicking the icon cancel the current background build.
1414

1515
### Options
1616

0 commit comments

Comments
 (0)