|
| 1 | +// Copyright (c) Jeff Kluge. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the MIT license. |
| 4 | + |
| 5 | +using Microsoft.Build.Evaluation; |
| 6 | +using Microsoft.Build.Execution; |
| 7 | +using Microsoft.Build.Framework; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | + |
| 11 | +namespace Microsoft.Build.Utilities.ProjectCreation |
| 12 | +{ |
| 13 | + internal static class BuildHost |
| 14 | + { |
| 15 | + private static readonly IDictionary<string, string> EmptyGlobalProperties = new Dictionary<string, string>(capacity: 0); |
| 16 | + |
| 17 | +#if !NET8_0 |
| 18 | + private static readonly IDictionary<string, string?> EmptyGlobalPropertiesWithNull = new Dictionary<string, string?>(capacity: 0); |
| 19 | +#endif |
| 20 | + |
| 21 | + public static bool Restore( |
| 22 | + string projectFullPath, |
| 23 | + ProjectCollection projectCollection, |
| 24 | + IDictionary<string, string>? globalProperties, |
| 25 | + BuildOutput buildOutput, |
| 26 | + out IDictionary<string, TargetResult>? targetOutputs) |
| 27 | + { |
| 28 | + Dictionary<string, string> restoreGlobalProperties = new(globalProperties ?? projectCollection.GlobalProperties); // IMPORTANT: Make a copy of the global properties here so as not to modify the ones passed in |
| 29 | + |
| 30 | + restoreGlobalProperties["ExcludeRestorePackageImports"] = "true"; |
| 31 | + restoreGlobalProperties["MSBuildRestoreSessionId"] = Guid.NewGuid().ToString("D"); |
| 32 | + |
| 33 | + BuildRequestDataFlags buildRequestDataFlags = BuildRequestDataFlags.ClearCachesAfterBuild | BuildRequestDataFlags.SkipNonexistentTargets | BuildRequestDataFlags.IgnoreMissingEmptyAndInvalidImports; |
| 34 | + |
| 35 | + return BuildProjectFromFullPath(projectFullPath, ["Restore"], restoreGlobalProperties, [.. projectCollection.Loggers, buildOutput], buildRequestDataFlags, out targetOutputs); |
| 36 | + } |
| 37 | + |
| 38 | + public static bool TryBuild( |
| 39 | + string projectFullPath, |
| 40 | + ProjectCollection projectCollection, |
| 41 | + BuildOutput buildOutput, |
| 42 | + out IDictionary<string, TargetResult>? targetOutputs, |
| 43 | + bool restore = false, |
| 44 | + string? target = null, |
| 45 | + IDictionary<string, string>? globalProperties = null) |
| 46 | + { |
| 47 | + return Build(projectFullPath, restore, target is null ? Array.Empty<string>() : [target], projectCollection, globalProperties, buildOutput, out targetOutputs); |
| 48 | + } |
| 49 | + |
| 50 | + public static bool TryBuild( |
| 51 | + string projectFullPath, |
| 52 | + ProjectCollection projectCollection, |
| 53 | + BuildOutput buildOutput, |
| 54 | + out IDictionary<string, TargetResult>? targetOutputs, |
| 55 | + bool restore = false, |
| 56 | + string[]? targets = null, |
| 57 | + IDictionary<string, string>? globalProperties = null) |
| 58 | + { |
| 59 | + return Build(projectFullPath, restore, targets ?? Array.Empty<string>(), projectCollection, globalProperties, buildOutput, out targetOutputs); |
| 60 | + } |
| 61 | + |
| 62 | + public static bool TryBuild( |
| 63 | + ProjectInstance projectInstance, |
| 64 | + ProjectCollection projectCollection, |
| 65 | + BuildOutput buildOutput, |
| 66 | + out IDictionary<string, TargetResult>? targetOutputs, |
| 67 | + string? target = null, |
| 68 | + IDictionary<string, string>? globalProperties = null) |
| 69 | + { |
| 70 | + return Build(projectInstance, target is null ? Array.Empty<string>() : [target], projectCollection, globalProperties, buildOutput, out targetOutputs); |
| 71 | + } |
| 72 | + |
| 73 | + public static bool TryBuild( |
| 74 | + ProjectInstance projectInstance, |
| 75 | + ProjectCollection projectCollection, |
| 76 | + BuildOutput buildOutput, |
| 77 | + out IDictionary<string, TargetResult>? targetOutputs, |
| 78 | + string[]? targets = null, |
| 79 | + IDictionary<string, string>? globalProperties = null) |
| 80 | + { |
| 81 | + return Build(projectInstance, targets ?? Array.Empty<string>(), projectCollection, globalProperties, buildOutput, out targetOutputs); |
| 82 | + } |
| 83 | + |
| 84 | + private static bool Build( |
| 85 | + string projectFullPath, |
| 86 | + bool restore, |
| 87 | + string[] targets, |
| 88 | + ProjectCollection projectCollection, |
| 89 | + IDictionary<string, string>? globalProperties, |
| 90 | + BuildOutput buildOutput, |
| 91 | + out IDictionary<string, TargetResult>? targetOutputs) |
| 92 | + { |
| 93 | + targetOutputs = null; |
| 94 | + |
| 95 | + if (restore) |
| 96 | + { |
| 97 | + if (!Restore(projectFullPath, projectCollection, globalProperties, buildOutput, out _)) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return BuildProjectFromFullPath(projectFullPath, targets, globalProperties, [.. projectCollection.Loggers, buildOutput], BuildRequestDataFlags.None, out targetOutputs); |
| 104 | + } |
| 105 | + |
| 106 | + private static bool Build( |
| 107 | + ProjectInstance projectInstance, |
| 108 | + string[] targets, |
| 109 | + ProjectCollection projectCollection, |
| 110 | + IDictionary<string, string>? globalProperties, |
| 111 | + BuildOutput buildOutput, |
| 112 | + out IDictionary<string, TargetResult>? targetOutputs) |
| 113 | + { |
| 114 | + targetOutputs = null; |
| 115 | + |
| 116 | + return BuildProjectFromProjectInstance(projectInstance, targets, globalProperties, [.. projectCollection.Loggers, buildOutput], BuildRequestDataFlags.None, out targetOutputs); |
| 117 | + } |
| 118 | + |
| 119 | + private static bool BuildProjectFromProjectInstance( |
| 120 | + ProjectInstance projectInstance, |
| 121 | + string[] targets, |
| 122 | + IDictionary<string, string>? globalProperties, |
| 123 | + IEnumerable<ILogger> loggers, |
| 124 | + BuildRequestDataFlags buildRequestDataFlags, |
| 125 | + out IDictionary<string, TargetResult>? targetOutputs) |
| 126 | + { |
| 127 | + targetOutputs = null; |
| 128 | + |
| 129 | + BuildResult buildResult = BuildManagerHost.Build( |
| 130 | + projectInstance, |
| 131 | + targets, |
| 132 | + globalProperties ?? EmptyGlobalProperties, |
| 133 | + loggers, |
| 134 | + buildRequestDataFlags); |
| 135 | + |
| 136 | + if (buildResult.Exception != null) |
| 137 | + { |
| 138 | + throw buildResult.Exception; |
| 139 | + } |
| 140 | + |
| 141 | + targetOutputs = buildResult.ResultsByTarget; |
| 142 | + |
| 143 | + return buildResult.OverallResult == BuildResultCode.Success; |
| 144 | + } |
| 145 | + |
| 146 | + private static bool BuildProjectFromFullPath( |
| 147 | + string projectFullPath, |
| 148 | + string[] targets, |
| 149 | + IDictionary<string, string>? globalProperties, |
| 150 | + IEnumerable<ILogger> loggers, |
| 151 | + BuildRequestDataFlags buildRequestDataFlags, |
| 152 | + out IDictionary<string, TargetResult>? targetOutputs) |
| 153 | + { |
| 154 | + targetOutputs = null; |
| 155 | + |
| 156 | + BuildResult buildResult = BuildManagerHost.Build( |
| 157 | + projectFullPath, |
| 158 | + targets, |
| 159 | + GetGlobalProperties(globalProperties), |
| 160 | + loggers, |
| 161 | + buildRequestDataFlags); |
| 162 | + |
| 163 | + if (buildResult.Exception != null) |
| 164 | + { |
| 165 | + throw buildResult.Exception; |
| 166 | + } |
| 167 | + |
| 168 | + if (targetOutputs != null) |
| 169 | + { |
| 170 | + foreach (KeyValuePair<string, TargetResult> targetResult in buildResult.ResultsByTarget) |
| 171 | + { |
| 172 | + targetOutputs[targetResult.Key] = targetResult.Value; |
| 173 | + } |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + targetOutputs = buildResult.ResultsByTarget; |
| 178 | + } |
| 179 | + |
| 180 | + return buildResult.OverallResult == BuildResultCode.Success; |
| 181 | + |
| 182 | +#if NET8_0 |
| 183 | + IDictionary<string, string> |
| 184 | +#else |
| 185 | + IDictionary<string, string?> |
| 186 | +#endif |
| 187 | + GetGlobalProperties(IDictionary<string, string>? globalProperties) |
| 188 | + { |
| 189 | +#if NET8_0 |
| 190 | + return globalProperties ?? EmptyGlobalProperties; |
| 191 | +#else |
| 192 | + if (globalProperties is null) |
| 193 | + { |
| 194 | + return EmptyGlobalPropertiesWithNull; |
| 195 | + } |
| 196 | + |
| 197 | + Dictionary<string, string?> finalGlobalProperties = new(globalProperties.Count); |
| 198 | + |
| 199 | + foreach (var kvp in globalProperties) |
| 200 | + { |
| 201 | + finalGlobalProperties[kvp.Key] = kvp.Value; |
| 202 | + } |
| 203 | + |
| 204 | + return finalGlobalProperties; |
| 205 | +#endif |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments