|
1 | 1 | using System.Collections.Immutable; |
2 | | -using System.Diagnostics; |
3 | 2 | using System.IO; |
4 | 3 | using System.Linq; |
5 | 4 | using System.Xml.Linq; |
@@ -29,31 +28,37 @@ public override void Initialize(AnalysisContext context) |
29 | 28 | var projectFile = globalOptions.TryGetValue("build_property.MSBuildProjectFullPath", out var fullPath) ? |
30 | 29 | fullPath : null; |
31 | 30 |
|
32 | | - if (!string.IsNullOrEmpty(projectFile) && File.Exists(projectFile)) |
| 31 | + var designTimeBuild = globalOptions.TryGetValue("build_property.DesignTimeBuild", out var dtb) && |
| 32 | + bool.TryParse(dtb, out var isDtb) && isDtb ? true : false; |
| 33 | + |
| 34 | + // Don't clean during DTB since the project file might be edited in-memory and not yet saved. |
| 35 | + if (designTimeBuild || |
| 36 | + string.IsNullOrEmpty(projectFile) || |
| 37 | + !File.Exists(projectFile)) |
| 38 | + return; |
| 39 | + |
| 40 | + var doc = XDocument.Load(projectFile, LoadOptions.PreserveWhitespace); |
| 41 | + var references = doc.Descendants() |
| 42 | + .Where(e => e.Name.LocalName == "PackageReference" && e.Elements().Any()) |
| 43 | + .Where(e => e.Attribute("PrivateAssets")?.Value == "all" || e.Element("PrivateAssets")?.Value == "all"); |
| 44 | + |
| 45 | + var cleaned = 0; |
| 46 | + |
| 47 | + foreach (var item in references) |
| 48 | + { |
| 49 | + var attributes = item.Attributes().ToArray(); |
| 50 | + item.RemoveAll(); |
| 51 | + item.Add(attributes); |
| 52 | + if (item.Attribute("PrivateAssets") == null) |
| 53 | + item.Add(new XAttribute("PrivateAssets", "all")); |
| 54 | + |
| 55 | + cleaned++; |
| 56 | + } |
| 57 | + |
| 58 | + if (cleaned > 0) |
33 | 59 | { |
34 | | - var doc = XDocument.Load(projectFile, LoadOptions.PreserveWhitespace); |
35 | | - var references = doc.Descendants() |
36 | | - .Where(e => e.Name.LocalName == "PackageReference" && e.Elements().Any()) |
37 | | - .Where(e => e.Attribute("PrivateAssets")?.Value == "all" || e.Element("PrivateAssets")?.Value == "all"); |
38 | | - |
39 | | - var cleaned = 0; |
40 | | - |
41 | | - foreach (var item in references) |
42 | | - { |
43 | | - var attributes = item.Attributes().ToArray(); |
44 | | - item.RemoveAll(); |
45 | | - item.Add(attributes); |
46 | | - if (item.Attribute("PrivateAssets") == null) |
47 | | - item.Add(new XAttribute("PrivateAssets", "all")); |
48 | | - |
49 | | - cleaned++; |
50 | | - } |
51 | | - |
52 | | - if (cleaned > 0) |
53 | | - { |
54 | | - ctx.ReportDiagnostic(Diagnostic.Create(SupportedDiagnostics[0], null, cleaned)); |
55 | | - doc.Save(projectFile); |
56 | | - } |
| 60 | + ctx.ReportDiagnostic(Diagnostic.Create(SupportedDiagnostics[0], null, cleaned)); |
| 61 | + doc.Save(projectFile); |
57 | 62 | } |
58 | 63 | }); |
59 | 64 | } |
|
0 commit comments