-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMsBuild.cs
More file actions
344 lines (288 loc) · 11.1 KB
/
MsBuild.cs
File metadata and controls
344 lines (288 loc) · 11.1 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.ComponentModel;
using System.IO;
using dotnetCampus.Configurations;
using dotnetCampus.DotNETBuild.Context;
using dotnetCampus.DotNETBuild.Context.Exceptions;
namespace dotnetCampus.DotNETBuild.Utils
{
/// <summary>
/// 对 msbuild 命令行工具的封装
/// </summary>
public class MSBuild : DotNetBuildTool
{
/// <summary>
/// 对 msbuild 命令行工具的封装
/// </summary>
public MSBuild(IAppConfigurator appConfigurator) : base(appConfigurator)
{
}
/// <summary>
/// 执行编译
/// </summary>
public void Build(MSBuildCommandOptions options)
{
var configuration = options.MSBuildConfiguration;
var slnPath = options.SlnPath;
var msbuildPath = options.MSBuildPath;
var command = $"/m:{options.MaxCpuCount}";
command += $" /p:configuration={(configuration == MSBuildConfiguration.Release ? "release" : "debug")}";
if (options.ShouldRestore)
{
command += " -restore";
}
if (string.IsNullOrEmpty(slnPath))
{
slnPath = CompileConfiguration.SlnPath;
}
command += $" {ProcessCommand.ToArgumentPath(slnPath)}";
Build(command, msbuildPath);
}
/// <summary>
/// 执行编译
/// </summary>
/// <param name="parallel"></param>
/// <param name="configuration"></param>
/// <param name="slnPath"></param>
/// <param name="msbuildPath"></param>
public void Build(bool parallel = true, MSBuildConfiguration configuration = MSBuildConfiguration.Release,
string slnPath = "", string msbuildPath = "")
{
Build(new MSBuildCommandOptions()
{
MaxCpuCount = parallel ? 4 : 1,
MSBuildConfiguration = configuration,
SlnPath = slnPath,
MSBuildPath = msbuildPath
});
}
/// <summary>
/// 执行 msbuild 命令
/// </summary>
/// <param name="command"></param>
/// <param name="msbuildPath">默认使用最新版本</param>
public void Build(string command, string msbuildPath = "")
{
if (string.IsNullOrEmpty(msbuildPath))
{
msbuildPath = CompileConfiguration.MSBuildFile;
}
var (success, output) = ExecuteProcessCommand(msbuildPath, command);
if (!success)
{
throw new MSBuildCompileException(output);
}
}
/// <summary>
/// 获取 msbuild 命令行工具路径
/// </summary>
/// <returns></returns>
public string GetMSBuildFile()
{
if (!string.IsNullOrEmpty(CompileConfiguration.MSBuildFile))
{
return CompileConfiguration.MSBuildFile;
}
FindMSBuildFile();
return CompileConfiguration.MSBuildFile;
}
internal void FindMSBuildFile()
{
MSBuildFileFinder.FindInstalledMSBuildFilePath(CompileConfiguration);
}
/// <summary>
/// 构建配置
/// </summary>
public CompileConfiguration CompileConfiguration => AppConfigurator.Of<CompileConfiguration>();
/// <summary>
/// 构建选项
/// </summary>
public enum MSBuildConfiguration
{
/// <summary>
/// 发布版
/// </summary>
Release,
/// <summary>
/// 调试版
/// </summary>
Debug,
}
}
internal static class MSBuildFileFinder
{
/// <summary>
/// 寻找本机安装的 MSBuild 文件路径,填充到 <paramref name="compileConfiguration"/> 里
/// </summary>
/// <param name="compileConfiguration"></param>
public static void FindInstalledMSBuildFilePath(CompileConfiguration compileConfiguration)
{
var vs2019Community =
@"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe";
if (File.Exists(vs2019Community))
{
compileConfiguration.VS2019CommunityMSBuild = vs2019Community;
}
var vs2019Enterprise =
@"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe";
if (File.Exists(vs2019Enterprise))
{
compileConfiguration.VS2019EnterpriseMSBuild = vs2019Enterprise;
}
var vs2019Professional =
@"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe";
if (File.Exists(vs2019Professional))
{
compileConfiguration.VS2019ProfessionalMSBuild = vs2019Professional;
}
var vs2022EnterpriseMSBuild = @"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Current\Bin\MSBuild.exe";
if (File.Exists(vs2022EnterpriseMSBuild))
{
compileConfiguration.VS2022EnterpriseMSBuild = vs2022EnterpriseMSBuild;
}
var vs2022ProfessionalMSBuild = @"C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Current\Bin\MSBuild.exe";
if (File.Exists(vs2022ProfessionalMSBuild))
{
compileConfiguration.VS2022ProfessionalMSBuild = vs2022ProfessionalMSBuild;
}
var vs2022CommunityMSBuild = @"C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\MSBuild.exe";
if (File.Exists(vs2022CommunityMSBuild))
{
compileConfiguration.VS2022CommunityMSBuild = vs2022CommunityMSBuild;
}
var vs2026EnterpriseMSBuild = @"C:\Program Files\Microsoft Visual Studio\18\Professional\MSBuild\Current\Bin\amd64\MSBuild.exe";
if (File.Exists(vs2022EnterpriseMSBuild))
{
compileConfiguration.VS2026EnterpriseMSBuild = vs2026EnterpriseMSBuild;
}
var vs2026ProfessionalMSBuild = @"C:\Program Files\Microsoft Visual Studio\18\Professional\Msbuild\Current\Bin\amd64\MSBuild.exe";
if (File.Exists(vs2022ProfessionalMSBuild))
{
compileConfiguration.VS2026ProfessionalMSBuild = vs2026ProfessionalMSBuild;
}
var vs2026CommunityMSBuild = @"C:\Program Files\Microsoft Visual Studio\18\Community\Msbuild\Current\Bin\amd64\MSBuild.exe";
if (File.Exists(vs2022CommunityMSBuild))
{
compileConfiguration.VS2026CommunityMSBuild = vs2026CommunityMSBuild;
}
}
}
/// <summary>
/// 对 msbuild 命令行工具的封装
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("大小写命名错误,请使用 MSBuild 代替")]
public class MsBuild : DotNetBuildTool
{
/// <inheritdoc />
public MsBuild(IAppConfigurator appConfigurator) : base(appConfigurator)
{
}
/// <summary>
/// 执行编译
/// </summary>
public void Build(MsBuildCommandOptions options)
{
var configuration = options.MsBuildConfiguration;
var slnPath = options.SlnPath;
var msbuildPath = options.MsBuildPath;
var command = $"/m:{options.MaxCpuCount}";
command += $" /p:configuration={(configuration == MsBuildConfiguration.Release ? "release" : "debug")}";
if (options.ShouldRestore)
{
command += " -restore";
}
if (string.IsNullOrEmpty(slnPath))
{
slnPath = CompileConfiguration.SlnPath;
}
command += $" {ProcessCommand.ToArgumentPath(slnPath)}";
Build(command, msbuildPath);
}
/// <summary>
/// 执行编译
/// </summary>
/// <param name="parallel"></param>
/// <param name="configuration"></param>
/// <param name="slnPath"></param>
/// <param name="msbuildPath"></param>
public void Build(bool parallel = true, MsBuildConfiguration configuration = MsBuildConfiguration.Release,
string slnPath = "", string msbuildPath = "")
{
Build(new MsBuildCommandOptions()
{
MaxCpuCount = parallel ? 4 : 1,
MsBuildConfiguration = configuration,
SlnPath = slnPath,
MsBuildPath = msbuildPath
});
}
/// <summary>
/// 执行 msbuild 命令
/// </summary>
/// <param name="command"></param>
/// <param name="msbuildPath">默认使用最新版本</param>
public void Build(string command, string msbuildPath = "")
{
if (string.IsNullOrEmpty(msbuildPath))
{
msbuildPath = CompileConfiguration.MSBuildFile;
}
ExecuteProcessCommand(msbuildPath, command);
}
/// <summary>
/// 获取 msbuild 命令行工具路径
/// </summary>
/// <returns></returns>
public string GetMsBuildFile()
{
if (!string.IsNullOrEmpty(CompileConfiguration.MSBuildFile))
{
return CompileConfiguration.MSBuildFile;
}
FindMsBuildFile();
return CompileConfiguration.MSBuildFile;
}
internal void FindMsBuildFile()
{
MSBuildFileFinder.FindInstalledMSBuildFilePath(CompileConfiguration);
}
/// <summary>
/// 构建配置
/// </summary>
public CompileConfiguration CompileConfiguration => AppConfigurator.Of<CompileConfiguration>();
/// <summary>
/// 构建选项
/// </summary>
public enum MsBuildConfiguration
{
/// <summary>
/// 发布版
/// </summary>
Release,
/// <summary>
/// 调试版
/// </summary>
Debug,
}
}
#nullable enable
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("大小写命名错误,请使用 MSBuildCommandOptions 代替")]
public class MsBuildCommandOptions
{
public int MaxCpuCount { get; set; } = 1;
public MsBuild.MsBuildConfiguration MsBuildConfiguration { get; set; } = MsBuild.MsBuildConfiguration.Release;
public string? SlnPath { get; set; }
public string? MsBuildPath { get; set; }
public bool ShouldRestore { set; get; } = false;
}
public class MSBuildCommandOptions
{
public int MaxCpuCount { get; set; } = 1;
public MSBuild.MSBuildConfiguration MSBuildConfiguration { get; set; } = MSBuild.MSBuildConfiguration.Release;
public string? SlnPath { get; set; }
public string? MSBuildPath { get; set; }
public bool ShouldRestore { set; get; } = false;
}
}