-
Notifications
You must be signed in to change notification settings - Fork 1
/
NAntBuild.cs
265 lines (243 loc) · 8.44 KB
/
NAntBuild.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// <copyright from='2011' to='2011' company='SIL International'>
// Copyright (c) 2011, SIL International. All Rights Reserved.
//
// Distributable under the terms of either the Eclipse Public License (EPL-1.0) or the
// GNU Lesser General Public License (LGPLv3), as specified in the LICENSING.txt file.
// </copyright>
using System;
using System.IO;
using System.Threading;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;
using Constants = EnvDTE.Constants;
namespace SIL.FwNantVSPackage
{
public class NAntBuild: IDisposable
{
private IServiceProvider Parent { get; set; }
private PaneWriter m_OutputBuild;
private NantRunner m_nantRunner;
private AddinLogListener m_LogListener;
internal event NantRunner.BuildStatusHandler BuildStatusChange;
private const string m_BuildPaneName = "NAnt build";
public NAntBuild(IServiceProvider parent)
{
Parent = parent;
}
#region IDisposable Members & Co.
/// ------------------------------------------------------------------------------------
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting
/// unmanaged resources.
/// </summary>
/// ------------------------------------------------------------------------------------
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="T:SIL.FwNantVSPackage.NAntBuild"/> is reclaimed by garbage collection.
/// </summary>
/// ------------------------------------------------------------------------------------
~NAntBuild()
{
Dispose(false);
// The base class finalizer is called automatically.
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Disposes the specified disposing.
/// </summary>
/// <param name="disposing">if set to <c>true</c> [disposing].</param>
/// ------------------------------------------------------------------------------------
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Dispose managed resources here
if (m_OutputBuild != null)
m_OutputBuild.Dispose();
if (m_LogListener != null)
m_LogListener.Dispose();
if (m_nantRunner != null)
m_nantRunner.Dispose();
}
m_OutputBuild = null;
m_LogListener = null;
m_nantRunner = null;
}
#endregion
private string NAnt
{
get
{
using (var addinOptions = new AddinOptions(Parent))
return addinOptions.NAntExe;
}
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Tries to find the buildfile based on the passed in path
/// </summary>
/// <param name="path">Path of the solution</param>
/// ------------------------------------------------------------------------------------
private string RetrieveBuildFile(string path)
{
using (var options = new AddinOptions(Parent))
{
// try the solution path
var buildFile = path != null ?
Path.Combine(Path.GetDirectoryName(path), options.Buildfile) : string.Empty;
if (File.Exists(buildFile))
return buildFile;
// try to find the right base directory based on the project path
foreach (var baseDir in options.BaseDirectories)
{
var dirToTest = baseDir + "\\";
if (path == null || path.ToLower().StartsWith(dirToTest.ToLower()))
{
return Path.GetFullPath(Path.Combine(baseDir, options.Buildfile));
}
}
// no success, so take first base directory that we have, or just build file
return Path.GetFullPath(options.BaseDirectories.Length > 0 &&
Directory.Exists(options.BaseDirectories[0])?
Path.Combine(options.BaseDirectories[0], options.Buildfile) :
options.Buildfile);
}
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Start the build. This involves activating the output window, saving all files
/// and printing a message.
/// </summary>
/// <param name="msg"></param>
/// ------------------------------------------------------------------------------------
private void StartBuild(string msg)
{
OutputBuild.Clear();
DTE.Windows.Item(Constants.vsWindowKindOutput).Activate();
DTE.ExecuteCommand("File.SaveAll", string.Empty);
OutputBuild.Write(msg);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Gets the target framework.
/// </summary>
/// ------------------------------------------------------------------------------------
private string TargetFramework
{
get
{
string targetFramework;
if (string.IsNullOrEmpty(Settings.Default.TargetFramework))
targetFramework = string.Empty;
else
targetFramework = "-t:" + Settings.Default.TargetFramework;
return targetFramework;
}
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Gets the name of the build pane.
/// </summary>
/// <value>The name of the build pane.</value>
/// ------------------------------------------------------------------------------------
public string BuildPaneName
{
get { return m_BuildPaneName; }
}
/// ------------------------------------------------------------------------------------------
/// <summary>
/// Use this PaneWriter for all output messages.
/// </summary>
/// <value>The output build.</value>
/// ------------------------------------------------------------------------------------------
public PaneWriter OutputBuild
{
get
{
if (m_OutputBuild == null)
{
m_OutputBuild = new PaneWriter(DTE, m_BuildPaneName);
}
return m_OutputBuild;
}
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Gets or sets the DTE.
/// </summary>
/// ------------------------------------------------------------------------------------
public DTE2 DTE
{
get { return Parent.GetService(typeof(DTE)) as DTE2; }
}
/// ------------------------------------------------------------------------------------------
/// <summary>
/// Returns <c>true</c> if a build is currently running
/// </summary>
/// ------------------------------------------------------------------------------------------
public bool IsRunning
{
get { return m_nantRunner != null && m_nantRunner.IsRunning; }
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Cancel the current build
/// </summary>
/// ------------------------------------------------------------------------------------
public void CancelBuild()
{
if (IsRunning)
m_nantRunner.Abort();
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Runs Nant.
/// </summary>
/// <param name="cmdLine">The CMD line.</param>
/// ------------------------------------------------------------------------------------
public void RunNant(string cmdLine)
{
if (string.IsNullOrWhiteSpace(cmdLine))
return;
try
{
var solution = Parent.GetService(typeof(SVsSolution)) as IVsSolution;
object solutionPath;
solution.GetProperty((int)__VSPROPID.VSPROPID_SolutionFileName, out solutionPath);
var buildFile = RetrieveBuildFile(solutionPath as string);
cmdLine = string.Format("-e+ -buildfile:\"{0}\" {1} {2}", buildFile, TargetFramework, cmdLine);
var workingDir = Path.GetFullPath(Path.GetDirectoryName(buildFile));
StartBuild(string.Format("------ Build started: {0} ------\n", cmdLine));
OutputBuild.Activate();
m_LogListener = new AddinLogListener(this);
m_nantRunner = new NantRunner(NAnt, cmdLine, workingDir,
m_LogListener,
BuildStatusChange);
m_nantRunner.Run();
}
catch (ThreadAbortException)
{
throw;
}
catch (Exception e)
{
if (m_nantRunner != null && m_nantRunner.IsRunning)
m_nantRunner.Abort();
OutputBuild.Write("\nINTERNAL ERROR\n\t");
OutputBuild.WriteLine(e.Message);
}
}
}
}