-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathMsBuildUtility.cs
1146 lines (995 loc) · 45.2 KB
/
MsBuildUtility.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.VisualStudio.Setup.Configuration;
using NuGet.Commands;
using NuGet.Common;
using NuGet.ProjectModel;
namespace NuGet.CommandLine
{
public static class MsBuildUtility
{
internal const int MsBuildWaitTime = 2 * 60 * 1000; // 2 minutes in milliseconds
private const string NuGetProps = "NuGet.CommandLine.NuGet.props";
private const string NuGetTargets = "NuGet.CommandLine.NuGet.targets";
private static readonly XNamespace MSBuildNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
private readonly static string[] MSBuildVersions = new string[] { "14", "12", "4" };
private readonly static string[] ArchitectureFolderNames = new string[] { "arm64", "amd64" };
public static bool IsMsBuildBasedProject(string projectFullPath)
{
return projectFullPath.EndsWith("proj", StringComparison.OrdinalIgnoreCase);
}
public static int Build(string msbuildDirectory,
string args)
{
var msbuildPath = GetMsbuild(msbuildDirectory);
if (!File.Exists(msbuildPath))
{
throw new CommandException(
string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString(nameof(NuGetResources.MsBuildDoesNotExistAtPath)),
msbuildPath));
}
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = false,
FileName = msbuildPath,
Arguments = args,
RedirectStandardOutput = false,
RedirectStandardError = false
};
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit();
return process.ExitCode;
}
}
/// <summary>
/// Returns the closure of project references for projects specified in <paramref name="projectPaths"/>.
/// </summary>
public static async Task<DependencyGraphSpec> GetProjectReferencesAsync(
MsBuildToolset msbuildToolset,
string[] projectPaths,
int timeOut,
IConsole console,
bool recursive,
string solutionDirectory,
string solutionName,
string restoreConfigFile,
string[] sources,
string packagesDirectory,
RestoreLockProperties restoreLockProperties)
{
var msbuildPath = GetMsbuild(msbuildToolset.Path);
if (!File.Exists(msbuildPath))
{
throw new CommandException(
string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString(nameof(NuGetResources.MsBuildDoesNotExistAtPath)),
msbuildPath));
}
var nugetExePath = Assembly.GetEntryAssembly().Location;
// Check for the non-ILMerged path
var buildTasksPath = Path.Combine(Path.GetDirectoryName(nugetExePath), "NuGet.Build.Tasks.dll");
if (File.Exists(buildTasksPath))
{
nugetExePath = buildTasksPath;
}
using (var inputTargetPath = new TempFile(".nugetinputs.targets"))
using (var nugetPropsPath = new TempFile(".nugetrestore.props"))
using (var entryPointTargetPath = new TempFile(".nugetrestore.targets"))
using (var resultsPath = new TempFile(".output.dg"))
{
// Read NuGet.props and NuGet.targets from nuget.exe and write it to disk for msbuild.exe
ExtractResource(NuGetProps, nugetPropsPath);
ExtractResource(NuGetTargets, entryPointTargetPath);
// Build a .targets file of all restore inputs, this is needed to avoid going over the limit on command line arguments.
var properties = new Dictionary<string, string>()
{
{ "RestoreUseCustomAfterTargets", "true" },
{ "RestoreGraphOutputPath", resultsPath },
{ "RestoreRecursive", recursive.ToString(CultureInfo.CurrentCulture).ToLowerInvariant() },
{ "RestoreProjectFilterMode", "exclusionlist" },
};
var inputTargetXML = GetRestoreInputFile(entryPointTargetPath, properties, projectPaths);
inputTargetXML.Save(inputTargetPath);
// Create msbuild parameters and include global properties that cannot be set in the input targets path
var arguments = GetMSBuildArguments(entryPointTargetPath, nugetPropsPath, inputTargetPath, nugetExePath, solutionDirectory, solutionName, restoreConfigFile, sources, packagesDirectory, msbuildToolset, restoreLockProperties, EnvironmentVariableWrapper.Instance);
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = false,
FileName = msbuildPath,
Arguments = arguments,
RedirectStandardError = true,
RedirectStandardOutput = true
};
console.LogDebug($"{processStartInfo.FileName} {processStartInfo.Arguments}");
using (var process = Process.Start(processStartInfo))
{
var errors = new StringBuilder();
var output = new StringBuilder();
var excluded = new string[] { "msb4011", entryPointTargetPath };
// Read console output
var errorTask = ConsumeStreamReaderAsync(process.StandardError, errors, filter: null);
var outputTask = ConsumeStreamReaderAsync(process.StandardOutput, output, filter: (line) => IsIgnoredOutput(line, excluded));
// Run msbuild
var finished = process.WaitForExit(timeOut);
// Handle timeouts
if (!finished)
{
try
{
process.Kill();
}
catch (Exception ex)
{
throw new CommandException(
LocalizedResourceManager.GetString(nameof(NuGetResources.Error_CannotKillMsBuild)) + " : " +
ex.Message,
ex);
}
}
// Read all console output from msbuild.
await Task.WhenAll(outputTask, errorTask);
// By default log msbuild output so that it is only
// displayed under -Verbosity detailed
var logLevel = LogLevel.Verbose;
if (process.ExitCode != 0 || !finished)
{
// If a problem occurred log all msbuild output as an error
// so that the user can see it.
// By default this runs with /v:q which means that only
// errors and warnings will be in the output.
logLevel = LogLevel.Error;
}
// MSBuild writes errors to the output stream, parsing the console output to find
// the errors would be error prone so here we log all output combined with any
// errors on the error stream (haven't seen the error stream used to date)
// to give the user the complete info.
await console.LogAsync(logLevel, output.ToString() + errors.ToString());
if (!finished)
{
// MSBuild timed out
throw new CommandException(
LocalizedResourceManager.GetString(nameof(NuGetResources.Error_MsBuildTimedOut)));
}
await outputTask;
if (process.ExitCode != 0)
{
// Do not continue if msbuild failed.
throw new ExitCodeException(1);
}
}
DependencyGraphSpec spec = null;
if (File.Exists(resultsPath) && new FileInfo(resultsPath).Length != 0)
{
spec = DependencyGraphSpec.Load(resultsPath);
File.Delete(resultsPath);
}
else
{
spec = new DependencyGraphSpec();
}
return spec;
}
}
public static string GetMSBuildArguments(
string entryPointTargetPath,
string nugetPropsPath,
string inputTargetPath,
string nugetExePath,
string solutionDirectory,
string solutionName,
string restoreConfigFile,
string[] sources,
string packagesDirectory,
MsBuildToolset toolset,
RestoreLockProperties restoreLockProperties,
IEnvironmentVariableReader reader)
{
// args for MSBuild.exe
var args = new List<string>()
{
EscapeQuoted(inputTargetPath),
"/t:GenerateRestoreGraphFile",
"/nologo",
"/nr:false"
};
// Set the msbuild verbosity level if specified
var msbuildVerbosity = reader.GetEnvironmentVariable("NUGET_RESTORE_MSBUILD_VERBOSITY");
if (string.IsNullOrEmpty(msbuildVerbosity))
{
args.Add("/v:q");
}
else
{
args.Add($"/v:{msbuildVerbosity} ");
}
AddProperty(args, "NuGetPropsFile", nugetPropsPath);
// Override the target under ImportsAfter with the current NuGet.targets version.
AddProperty(args, "NuGetRestoreTargets", entryPointTargetPath);
AddProperty(args, "RestoreUseCustomAfterTargets", bool.TrueString);
AddProperty(args, "DisableCheckingDuplicateNuGetItems", bool.TrueString);
// Set path to nuget.exe or the build task
AddProperty(args, "RestoreTaskAssemblyFile", nugetExePath);
// Settings
AddRestoreSources(args, sources);
AddPropertyIfHasValue(args, "RestoreSolutionDirectory", solutionDirectory);
AddPropertyIfHasValue(args, "RestoreConfigFile", restoreConfigFile);
AddPropertyIfHasValue(args, "RestorePackagesPath", packagesDirectory);
AddPropertyIfHasValue(args, "SolutionDir", solutionDirectory);
AddPropertyIfHasValue(args, "SolutionName", solutionName);
// If the MSBuild version used does not support SkipNonextentTargets and BuildInParallel
// use the performance optimization
// When BuildInParallel is used with ContinueOnError it does not continue in some scenarios
if (toolset.ParsedVersion.CompareTo(new Version(15, 5)) < 0)
{
AddProperty(args, "RestoreBuildInParallel", bool.FalseString);
AddProperty(args, "RestoreUseSkipNonexistentTargets", bool.FalseString);
}
// Checking the SDK uses MSBuild intrinsic functions that were added in 16.5
if (toolset.ParsedVersion.CompareTo(new Version(16, 5)) < 0)
{
AddProperty(args, "NuGetExeSkipSdkAnalysisLevelCheck", bool.TrueString);
}
// Add additional args to msbuild if needed
var msbuildAdditionalArgs = reader.GetEnvironmentVariable("NUGET_RESTORE_MSBUILD_ARGS");
if (!string.IsNullOrEmpty(msbuildAdditionalArgs))
{
args.Add(msbuildAdditionalArgs);
}
AddPropertyIfHasValue(args, "RestorePackagesWithLockFile", restoreLockProperties.RestorePackagesWithLockFile);
AddPropertyIfHasValue(args, "NuGetLockFilePath", restoreLockProperties.NuGetLockFilePath);
if (restoreLockProperties.RestoreLockedMode)
{
AddProperty(args, "RestoreLockedMode", bool.TrueString);
}
return string.Join(" ", args);
}
private static void AddRestoreSources(List<string> args, string[] sources)
{
if (sources.Length != 0)
{
var isMono = RuntimeEnvironmentHelper.IsMono && !RuntimeEnvironmentHelper.IsWindows;
var sourceBuilder = new StringBuilder();
if (isMono)
{
sourceBuilder.Append("/p:RestoreSources=\\\"");
}
else
{
sourceBuilder.Append("/p:RestoreSources=\"");
}
for (var i = 0; i < sources.Length; i++)
{
if (isMono)
{
sourceBuilder.Append(sources[i])
.Append("\\;");
}
else
{
sourceBuilder.Append(sources[i])
.Append(";");
}
}
if (isMono)
{
sourceBuilder.Append("\\\" ");
}
else
{
sourceBuilder.Append("\" ");
}
args.Add(sourceBuilder.ToString());
}
}
public static XDocument GetRestoreInputFile(string restoreTargetPath, Dictionary<string, string> properties, IEnumerable<string> projectPaths)
{
return GenerateMSBuildFile(
new XElement(MSBuildNamespace + "PropertyGroup", properties.Select(e => new XElement(MSBuildNamespace + e.Key, e.Value))),
new XElement(MSBuildNamespace + "ItemGroup", projectPaths.Select(GetRestoreGraphProjectInputItem)),
new XElement(MSBuildNamespace + "Import", new XAttribute(XName.Get("Project"), restoreTargetPath)));
}
public static XDocument GenerateMSBuildFile(params XElement[] elements)
{
return new XDocument(
new XDeclaration("1.0", "utf-8", "no"),
new XElement(MSBuildNamespace + "Project",
new XAttribute("ToolsVersion", "14.0"),
elements));
}
private static XElement GetRestoreGraphProjectInputItem(string path)
{
return new XElement(MSBuildNamespace + "RestoreGraphProjectInputItems", new XAttribute(XName.Get("Include"), path));
}
private static bool IsIgnoredOutput(string line, string[] excluded)
{
return excluded.All(p => line.IndexOf(p, StringComparison.OrdinalIgnoreCase) >= 0);
}
private static async Task ConsumeStreamReaderAsync(StreamReader reader, StringBuilder lines, Func<string, bool> filter)
{
await Task.Yield();
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (filter == null ||
!filter(line))
{
lines.AppendLine(line);
}
}
}
/// <summary>
/// Gets the list of project files in a solution, using XBuild's solution parser.
/// </summary>
/// <param name="solutionFile">The solution file. </param>
/// <returns>The list of project files (in full path) in the solution.</returns>
public static IEnumerable<string> GetAllProjectFileNamesWithXBuild(string solutionFile)
{
try
{
var assembly = Assembly.Load(
"Microsoft.Build.Engine, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
var solutionParserType = assembly.GetType("Mono.XBuild.CommandLine.SolutionParser");
if (solutionParserType == null)
{
throw new CommandException(
LocalizedResourceManager.GetString("Error_CannotGetXBuildSolutionParser"));
}
var getAllProjectFileNamesMethod = solutionParserType.GetMethod(
"GetAllProjectFileNames",
new Type[] { typeof(string) });
if (getAllProjectFileNamesMethod == null)
{
throw new CommandException(
LocalizedResourceManager.GetString("Error_CannotGetGetAllProjectFileNamesMethod"));
}
var names = (IEnumerable<string>)getAllProjectFileNamesMethod.Invoke(
null, new object[] { solutionFile });
return names;
}
catch (Exception ex)
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("Error_SolutionFileParseError"),
solutionFile,
ex.Message);
throw new CommandException(message, ex);
}
}
/// <summary>
/// Gets the list of project files in a solution, using MSBuild API.
/// </summary>
/// <param name="solutionFile">The solution file. </param>
/// <param name="msbuildPath">The directory that contains msbuild.</param>
/// <returns>The list of project files (in full path) in the solution.</returns>
public static IEnumerable<string> GetAllProjectFileNamesWithMsBuild(
string solutionFile,
string msbuildPath)
{
try
{
var solution = new Solution(solutionFile, msbuildPath);
return solution.Projects.Where(project => !project.IsSolutionFolder)
.Select(project => project.AbsolutePath);
}
catch (Exception ex)
{
var exMessage = ex.Message;
if (ex.InnerException != null)
exMessage += " " + ex.InnerException.Message;
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString("Error_SolutionFileParseError"),
solutionFile,
exMessage);
throw new CommandException(message, ex);
}
}
public static IEnumerable<string> GetAllProjectFileNames(
string solutionFile,
string pathToMsbuildDir)
{
if (RuntimeEnvironmentHelper.IsMono &&
(pathToMsbuildDir.Contains("xbuild") || GetMsbuild(pathToMsbuildDir).Contains("xbuild")))
{
return GetAllProjectFileNamesWithXBuild(solutionFile);
}
return GetAllProjectFileNamesWithMsBuild(solutionFile, pathToMsbuildDir);
}
/// <summary>
/// Returns the msbuild directory. If <paramref name="userVersion"/> is "latest", then the directory containing
/// the highest installed msbuild version is returned. If <paramref name="userVersion"/> is null,
/// the Env variable has priority over the highest installed version. Otherwise, the directory containing msbuild
/// whose version matches <paramref name="userVersion"/> is returned. If no match is found,
/// an exception will be thrown. Note that we use Microsoft.Build types as
/// </summary>
/// <param name="userVersion">version string as passed by user (so may be empty)</param>
/// <param name="console">The console used to output messages.</param>
/// <returns>The msbuild directory.</returns>
public static MsBuildToolset GetMsBuildToolset(string userVersion, IConsole console)
{
var currentDirectoryCache = Directory.GetCurrentDirectory();
var installedToolsets = new List<MsBuildToolset>();
MsBuildToolset toolset = null;
try
{
// If Mono, test well known paths and bail if found
toolset = GetMsBuildFromMonoPaths(userVersion);
if (toolset != null)
{
return toolset;
}
// If the userVersion is not specified, favor the value in the $Path Env variable
if (string.IsNullOrEmpty(userVersion))
{
var msbuildExe = GetMSBuild(EnvironmentVariableWrapper.Instance);
if (msbuildExe != null)
{
var msBuildDirectory = GetNonArchitectureDirectory(msbuildExe);
var msbuildVersion = FileVersionInfo.GetVersionInfo(msbuildExe)?.FileVersion;
return toolset = new MsBuildToolset(msbuildVersion, msBuildDirectory);
}
}
using (var projectCollection = LoadProjectCollection())
{
var installed = ((dynamic)projectCollection)?.Toolsets;
if (installed != null)
{
foreach (var item in installed)
{
installedToolsets.Add(new MsBuildToolset(version: item.ToolsVersion, path: item.ToolsPath));
}
installedToolsets = installedToolsets.ToList();
}
}
// In a non-Mono environment, we have the potential for SxS installs of MSBuild 15.1+. Let's add these here.
if (!RuntimeEnvironmentHelper.IsMono)
{
var installedSxsToolsets = GetInstalledSxsToolsets();
if (installedToolsets == null)
{
installedToolsets = installedSxsToolsets;
}
else if (installedSxsToolsets != null)
{
installedToolsets.AddRange(installedSxsToolsets);
}
}
if (!installedToolsets.Any())
{
throw new CommandException(
LocalizedResourceManager.GetString(
nameof(NuGetResources.Error_CannotFindMsbuild)));
}
toolset = GetMsBuildDirectoryInternal(
userVersion, console, installedToolsets.OrderByDescending(t => t), (IEnvironmentVariableReader reader) => GetMSBuild(reader));
Directory.SetCurrentDirectory(currentDirectoryCache);
return toolset;
}
finally
{
LogToolsetToConsole(console, toolset);
}
}
internal static string GetNonArchitectureDirectory(string msbuildExe)
{
var msbuildFile = Path.GetFileName(msbuildExe);
var directory = Path.GetDirectoryName(msbuildExe);
var directoryInfo = new DirectoryInfo(directory);
var directoryName = directoryInfo.Name;
var parentDirectory = directoryInfo.Parent.FullName;
//Given Visual Studio 2022 or later, the PATH environment variable in Developer Command Prompt contains the architecture specific path of msbuild.exe.
// e.g. C:\Program Files\Microsoft Visual Studio\2022\Preview\\MSBuild\Current\Bin\arm64
//Using the architecture specific path will cause some runtime error when loading assembly, e.g."Microsoft.Build.Framework.dll".
//
//This method is to get the non-architecture specific path of msbuild.exe if the msbuildexe is in the architecture specific folder.
// C:\Program Files\Microsoft Visual Studio\2022\Preview\\MSBuild\Current\Bin\arm64
// => C:\Program Files\Microsoft Visual Studio\2022\Preview\\MSBuild\Current\Bin
//If msbuildExe is already in the non-architecture specific folder, just return the directory.
foreach (var architecture in ArchitectureFolderNames)
{
if (directoryName.Equals(architecture, StringComparison.OrdinalIgnoreCase))
{
if (File.Exists(Path.Combine(parentDirectory, msbuildFile)))
{
return parentDirectory;
}
else
{
throw new CommandException(
string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString(nameof(NuGetResources.Error_CannotFindNonArchitectureSpecificMsbuild)),
directory));
}
}
}
return directory;
}
/// <summary>
/// This method is called by GetMsBuildToolset(). This method is not intended to be called directly.
/// It's marked public so that it can be called by unit tests.
/// </summary>
/// <param name="userVersion">version string as passed by user (so may be empty)</param>
/// <param name="console">console for status reporting</param>
/// <param name="installedToolsets">all msbuild toolsets discovered by caller</param>
/// <param name="getMsBuildPathInPathVar">delegate to provide msbuild exe discovered in path environemtnb var/s
/// (using a delegate allows for testability)</param>
/// <returns>directory to use for msbuild exe</returns>
public static MsBuildToolset GetMsBuildDirectoryInternal(
string userVersion,
IConsole console,
IEnumerable<MsBuildToolset> installedToolsets,
Func<IEnvironmentVariableReader, string> getMsBuildPathInPathVar)
{
MsBuildToolset toolset;
var toolsetsContainingMSBuild = GetToolsetsContainingValidMSBuildInstallation(installedToolsets);
if (string.Equals(userVersion, "latest", StringComparison.OrdinalIgnoreCase))
{
//If "latest", take the default(highest) path, ignoring $PATH
toolset = toolsetsContainingMSBuild.FirstOrDefault();
}
else if (string.IsNullOrEmpty(userVersion))
{
var msbuildPathInPath = getMsBuildPathInPathVar(EnvironmentVariableWrapper.Instance);
toolset = GetToolsetFromPath(msbuildPathInPath, toolsetsContainingMSBuild);
}
else
{
toolset = GetToolsetFromUserVersion(userVersion, toolsetsContainingMSBuild);
}
return toolset;
}
private static IEnumerable<MsBuildToolset> GetToolsetsContainingValidMSBuildInstallation(IEnumerable<MsBuildToolset> installedToolsets)
{
return installedToolsets.Where(e => e.IsValid);
}
/// <summary>
/// Fetch project collection type from the GAC--this will service MSBuild 14 (and any toolsets included with 14).
/// </summary>
/// <returns>ProjectCollection instance to use for toolset enumeration</returns>
private static IDisposable LoadProjectCollection()
{
foreach (var version in MSBuildVersions)
{
try
{
var msBuildTypesAssembly = Assembly.Load($"Microsoft.Build, Version={version}.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
var projectCollectionType = msBuildTypesAssembly.GetType("Microsoft.Build.Evaluation.ProjectCollection", throwOnError: true);
return Activator.CreateInstance(projectCollectionType) as IDisposable;
}
catch (Exception)
{
}
}
return null;
}
/// <summary>
/// Try to find msbuild for mono from hard code path
/// </summary>
/// <param name="userVersion">version string as passed by user (so may be empty)</param>
/// <returns></returns>
public static MsBuildToolset GetMsBuildFromMonoPaths(string userVersion)
{
// Mono always tell user we are on unix even when user is on Mac.
if (!RuntimeEnvironmentHelper.IsMono)
{
return null;
}
//Use mscorlib to find mono and msbuild directory
var systemLibLocation = Path.GetDirectoryName(typeof(object).Assembly.Location);
var msbuildBasePathOnMono = Path.GetFullPath(Path.Combine(systemLibLocation, "..", "msbuild"));
//Combine msbuild version paths
var msBuildPathOnMono14 = Path.Combine(msbuildBasePathOnMono, "14.1", "bin");
var msBuildPathOnMono15 = Path.Combine(msbuildBasePathOnMono, "15.0", "bin");
if (string.IsNullOrEmpty(userVersion) || string.Equals(userVersion, "latest", StringComparison.OrdinalIgnoreCase))
{
return new[] {
new MsBuildToolset(version: "15.0", path: msBuildPathOnMono15),
new MsBuildToolset(version: "14.1", path: msBuildPathOnMono14)}
.FirstOrDefault(t => Directory.Exists(t.Path));
}
else
{
switch (userVersion)
{
case "14.1": return new MsBuildToolset(version: "14.1", path: msBuildPathOnMono14);
case "15":
case "15.0": return new MsBuildToolset(version: userVersion, path: msBuildPathOnMono15);
}
}
return null;
}
/// <summary>
/// Gets the (first) path of MSBuild to appear in environment variable PATH.
/// </summary>
/// <returns>The path of MSBuild in PATH environment variable. Returns null if MSBuild location does not exist
/// in the variable string.</returns>
private static string GetMsBuildPathInPathVar(IEnvironmentVariableReader reader)
{
var path = reader.GetEnvironmentVariable("PATH");
var paths = path?.Split(new char[] { ';' });
return paths?.Select(p =>
{
// Strip leading/trailing quotes
if (p.Length > 0 && p[0] == '\"')
{
p = p.Substring(1);
}
if (p.Length > 0 && p[p.Length - 1] == '\"')
{
p = p.Substring(0, p.Length - 1);
}
return p;
}).FirstOrDefault(p =>
{
try
{
return File.Exists(Path.Combine(p, "msbuild.exe"));
}
catch
{
return false;
}
});
}
/// <summary>
/// Gets the msbuild toolset found in/under the path passed.
/// </summary>
/// <param name="msBuildPath">The msbuild path as found in PATH env var. Can be null.</param>
/// <param name="installedToolsets">List of installed toolsets,
/// ordered by ToolsVersion, from highest to lowest.</param>
/// <returns>The matching toolset.</returns>
private static MsBuildToolset GetToolsetFromPath(
string msBuildPath,
IEnumerable<MsBuildToolset> installedToolsets)
{
MsBuildToolset selectedToolset;
if (string.IsNullOrEmpty(msBuildPath))
{
// We have no path for a specifically requested msbuild. Use the highest installed version.
selectedToolset = installedToolsets.FirstOrDefault();
}
else
{
// Search by path. We use a StartsWith match because a toolset's path may have an architecture specialization.
// e.g.
// c:\Program Files (x86)\MSBuild\14.0\Bin
// is specified in the path (a path which we have validated contains an msbuild.exe) and the toolset is located at
// c:\Program Files (x86)\MSBuild\14.0\Bin\amd64
selectedToolset = installedToolsets.FirstOrDefault(
t => t.Path.StartsWith(msBuildPath, StringComparison.OrdinalIgnoreCase));
if (selectedToolset == null)
{
// No match. Fail silently. Use the highest installed version in this case
selectedToolset = installedToolsets.FirstOrDefault();
}
}
if (selectedToolset == null)
{
throw new CommandException(
LocalizedResourceManager.GetString(
nameof(NuGetResources.Error_MSBuildNotInstalled)));
}
return selectedToolset;
}
private static MsBuildToolset GetToolsetFromUserVersion(
string userVersion,
IEnumerable<MsBuildToolset> installedToolsets)
{
// Version.TryParse only take decimal string like "14.0", "14" need to be converted.
var versionParts = userVersion.Split('.');
var major = versionParts.Length > 0 ? versionParts[0] : "0";
var minor = versionParts.Length > 1 ? versionParts[1] : "0";
var userVersionString = string.Join(".", major, minor);
// First match by string comparison
var selectedToolset = installedToolsets.FirstOrDefault(
t => string.Equals(userVersion, t.Version, StringComparison.OrdinalIgnoreCase));
if (selectedToolset != null)
{
return selectedToolset;
}
// Then match by Major & Minor version numbers. And we want an actual parsing of t.ToolsVersion,
// without the safe fallback to 0.0 built into t.ParsedToolsVersion.
selectedToolset = installedToolsets.FirstOrDefault(t =>
{
Version parsedUserVersion;
Version parsedToolsVersion;
if (Version.TryParse(userVersionString, out parsedUserVersion) &&
Version.TryParse(t.Version, out parsedToolsVersion))
{
return parsedToolsVersion.Major == parsedUserVersion.Major &&
parsedToolsVersion.Minor == parsedUserVersion.Minor;
}
return false;
});
if (selectedToolset == null)
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString(
nameof(NuGetResources.Error_CannotFindMsbuild)),
userVersion);
throw new CommandException(message);
}
return selectedToolset;
}
private static void LogToolsetToConsole(IConsole console, MsBuildToolset toolset)
{
if (console == null || toolset == null)
{
return;
}
if (console.Verbosity == Verbosity.Detailed)
{
console.WriteLine(
LocalizedResourceManager.GetString(
nameof(NuGetResources.MSBuildAutoDetection_Verbose)),
toolset.Version,
toolset.Path);
}
else
{
console.WriteLine(
LocalizedResourceManager.GetString(
nameof(NuGetResources.MSBuildAutoDetection)),
toolset.Version,
toolset.Path);
}
}
public static Lazy<MsBuildToolset> GetMsBuildDirectoryFromMsBuildPath(string msbuildPath, string msbuildVersion, IConsole console)
{
if (msbuildPath != null)
{
if (msbuildVersion != null)
{
console?.WriteWarning(LocalizedResourceManager.GetString(
nameof(NuGetResources.Warning_MsbuildPath)),
msbuildPath, msbuildVersion);
}
console?.WriteLine(LocalizedResourceManager.GetString(
nameof(NuGetResources.MSbuildFromPath)),
msbuildPath);
if (!Directory.Exists(msbuildPath))
{
var message = string.Format(
CultureInfo.CurrentCulture,
LocalizedResourceManager.GetString(
nameof(NuGetResources.MsbuildPathNotExist)),
msbuildPath);
throw new CommandException(message);
}
return new Lazy<MsBuildToolset>(() => new MsBuildToolset(msbuildVersion, msbuildPath));
}
else
{
return new Lazy<MsBuildToolset>(() => GetMsBuildToolset(msbuildVersion, console));
}
}
private static void AddProperty(List<string> args, string property, string value)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(NuGetResources.ArgumentNullOrEmpty, nameof(value));
}
AddPropertyIfHasValue(args, property, value);
}
private static void AddPropertyIfHasValue(List<string> args, string property, string value)
{
if (!string.IsNullOrEmpty(value))
{
args.Add($"/p:{property}={EscapeQuoted(value)}");
}
}
private static void ExtractResource(string resourceName, string targetPath)
{
using (var input = typeof(MsBuildUtility).Assembly.GetManifestResourceStream(resourceName))
{
using (var output = File.OpenWrite(targetPath))
{
input.CopyTo(output);
}
}
}
private static List<MsBuildToolset> GetInstalledSxsToolsets()
{
ISetupConfiguration configuration;
try
{
configuration = new SetupConfiguration();
}
catch (Exception)
{
return null; // No COM class
}
var enumerator = configuration.EnumInstances();
if (enumerator == null)
{
return null;
}
var setupInstances = new List<MsBuildToolset>();
while (true)
{
var fetchedInstances = new ISetupInstance[3];
int fetched;
enumerator.Next(fetchedInstances.Length, fetchedInstances, out fetched);
if (fetched == 0)
{
break;
}
// fetched will return the value 3 even if only one instance returned
var index = 0;
while (index < fetched)
{
if (fetchedInstances[index] != null)
{
setupInstances.Add(new MsBuildToolset(fetchedInstances[index]));
}
index++;
}
}
if (setupInstances.Count == 0)
{
return null;
}
return setupInstances;
}
/// <summary>
/// Escapes a string so that it can be safely passed as a command line argument when starting a msbuild process.
/// Source: http://stackoverflow.com/a/12364234
/// </summary>
public static string Escape(string argument)
{
if (argument == string.Empty)
{
return "\"\"";
}
var escaped = Regex.Replace(argument, @"(\\*)""", @"$1\$0");
escaped = Regex.Replace(
escaped,
@"^(.*\s.*?)(\\*)$", @"""$1$2$2""",
RegexOptions.Singleline);
return escaped;
}
public static string EscapeQuoted(string argument)
{
if (argument == string.Empty)
{