|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.ObjectModel; |
| 5 | +using System.CommandLine; |
| 6 | +using System.CommandLine.Parsing; |
| 7 | + |
| 8 | +namespace Microsoft.DotNet.Cli.Utils; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Represents all of the parsed and forwarded arguments that the SDK should pass to MSBuild. |
| 12 | +/// </summary> |
| 13 | +public sealed class MSBuildArgs |
| 14 | +{ |
| 15 | + private MSBuildArgs(ReadOnlyDictionary<string, string>? properties, ReadOnlyDictionary<string, string>? restoreProperties, string[]? targets, string[]? otherMSBuildArgs) |
| 16 | + { |
| 17 | + GlobalProperties = properties; |
| 18 | + RestoreGlobalProperties = restoreProperties; |
| 19 | + RequestedTargets = targets; |
| 20 | + OtherMSBuildArgs = otherMSBuildArgs is not null |
| 21 | + ? [.. otherMSBuildArgs] |
| 22 | + : new List<string>(); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The set of <c>-p</c> flags that should be passed to MSBuild. |
| 27 | + /// </summary> |
| 28 | + public ReadOnlyDictionary<string, string>? GlobalProperties { get; } |
| 29 | + /// <summary> |
| 30 | + /// The set of <c>-rp</c> flags that should be passed to MSBuild for restore operations only. |
| 31 | + /// If this is non-empty, all <see cref="GlobalProperties"/> flags should be passed as <c>-rp</c> as well. |
| 32 | + /// </summary> |
| 33 | + public ReadOnlyDictionary<string, string>? RestoreGlobalProperties { get; private set; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// The ordered list of targets that should be passed to MSBuild. |
| 37 | + /// </summary> |
| 38 | + public string[]? RequestedTargets { get; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// All non <c>-p</c> and <c>-rp</c> arguments that should be passed to MSBuild. |
| 42 | + /// </summary> |
| 43 | + public List<string> OtherMSBuildArgs { get; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Takes all of the unstructured properties and arguments that have been accrued from the command line |
| 47 | + /// processing of the SDK and returns a structured set of MSBuild arguments grouped by purpose. |
| 48 | + /// </summary> |
| 49 | + /// <param name="forwardedAndUserFacingArgs">the complete set of forwarded MSBuild arguments and un-parsed, potentially MSBuild-relevant arguments</param> |
| 50 | + /// <returns></returns> |
| 51 | + public static MSBuildArgs AnalyzeMSBuildArguments(IEnumerable<string> forwardedAndUserFacingArgs, params Option[] options) |
| 52 | + { |
| 53 | + var fakeCommand = new System.CommandLine.Command("dotnet"); |
| 54 | + foreach (var option in options) |
| 55 | + { |
| 56 | + fakeCommand.Options.Add(option); |
| 57 | + } |
| 58 | + |
| 59 | + var propertyParsingConfiguration = new CommandLineConfiguration(fakeCommand) |
| 60 | + { |
| 61 | + EnablePosixBundling = false |
| 62 | + }; |
| 63 | + var parseResult = propertyParsingConfiguration.Parse([..forwardedAndUserFacingArgs]); |
| 64 | + var globalProperties = parseResult.GetResult("--property") is OptionResult propResult ? propResult.GetValueOrDefault<ReadOnlyDictionary<string, string>?>() : null; |
| 65 | + var restoreProperties = parseResult.GetResult("--restoreProperty") is OptionResult restoreResult ? restoreResult.GetValueOrDefault<ReadOnlyDictionary<string, string>?>() : null; |
| 66 | + var requestedTargets = parseResult.GetResult("--target") is OptionResult targetResult ? targetResult.GetValueOrDefault<string[]?>() : null; |
| 67 | + var otherMSBuildArgs = parseResult.UnmatchedTokens.ToArray(); |
| 68 | + return new MSBuildArgs( |
| 69 | + properties: globalProperties, |
| 70 | + restoreProperties: restoreProperties, |
| 71 | + targets: requestedTargets, |
| 72 | + otherMSBuildArgs: otherMSBuildArgs); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public static MSBuildArgs FromProperties(ReadOnlyDictionary<string, string>? properties) |
| 77 | + { |
| 78 | + return new MSBuildArgs(properties, null, null, null); |
| 79 | + } |
| 80 | + |
| 81 | + public static MSBuildArgs FromOtherArgs(params ReadOnlySpan<string> args) |
| 82 | + { |
| 83 | + return new MSBuildArgs(null, null, null, args.ToArray()); |
| 84 | + } |
| 85 | + |
| 86 | + public static readonly MSBuildArgs ForHelp = new(null, null, null, ["--help"]); |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Completely replaces the MSBuild arguments with the provided <paramref name="newArgs"/>. |
| 90 | + /// </summary> |
| 91 | + public MSBuildArgs CloneWithExplicitArgs(string[] newArgs) |
| 92 | + { |
| 93 | + return new MSBuildArgs( |
| 94 | + properties: GlobalProperties, |
| 95 | + restoreProperties: RestoreGlobalProperties, |
| 96 | + targets: RequestedTargets, |
| 97 | + otherMSBuildArgs: newArgs); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Adds new arbitrary MSBuild flags to the end of the existing MSBuild arguments. |
| 102 | + /// </summary> |
| 103 | + public MSBuildArgs CloneWithAdditionalArgs(params string[] additionalArgs) |
| 104 | + { |
| 105 | + if (additionalArgs is null || additionalArgs.Length == 0) |
| 106 | + { |
| 107 | + // If there are no additional args, we can just return the current instance. |
| 108 | + return new MSBuildArgs(GlobalProperties, RestoreGlobalProperties, RequestedTargets, OtherMSBuildArgs.ToArray()); |
| 109 | + } |
| 110 | + |
| 111 | + return new MSBuildArgs(GlobalProperties, RestoreGlobalProperties, RequestedTargets, [.. OtherMSBuildArgs, .. additionalArgs]); |
| 112 | + } |
| 113 | + |
| 114 | + public MSBuildArgs CloneWithAdditionalRestoreProperties(ReadOnlyDictionary<string, string>? additionalRestoreProperties) |
| 115 | + { |
| 116 | + if (additionalRestoreProperties is null || additionalRestoreProperties.Count == 0) |
| 117 | + { |
| 118 | + // If there are no additional restore properties, we can just return the current instance. |
| 119 | + return new MSBuildArgs(GlobalProperties, RestoreGlobalProperties, RequestedTargets, OtherMSBuildArgs.ToArray()); |
| 120 | + } |
| 121 | + if (RestoreGlobalProperties is null) |
| 122 | + { |
| 123 | + return new MSBuildArgs(GlobalProperties, additionalRestoreProperties, RequestedTargets, OtherMSBuildArgs.ToArray()); |
| 124 | + } |
| 125 | + |
| 126 | + var newRestoreProperties = new Dictionary<string, string>(RestoreGlobalProperties, StringComparer.OrdinalIgnoreCase); |
| 127 | + foreach (var kvp in additionalRestoreProperties) |
| 128 | + { |
| 129 | + newRestoreProperties[kvp.Key] = kvp.Value; |
| 130 | + } |
| 131 | + return new MSBuildArgs(GlobalProperties, new(newRestoreProperties), RequestedTargets, OtherMSBuildArgs.ToArray()); |
| 132 | + } |
| 133 | + |
| 134 | + public MSBuildArgs CloneWithAdditionalProperties(ReadOnlyDictionary<string, string>? additionalProperties) |
| 135 | + { |
| 136 | + if (additionalProperties is null || additionalProperties.Count == 0) |
| 137 | + { |
| 138 | + // If there are no additional properties, we can just return the current instance. |
| 139 | + return new MSBuildArgs(GlobalProperties, RestoreGlobalProperties, RequestedTargets, OtherMSBuildArgs.ToArray()); |
| 140 | + } |
| 141 | + if (GlobalProperties is null) |
| 142 | + { |
| 143 | + return new MSBuildArgs(additionalProperties, RestoreGlobalProperties, RequestedTargets, OtherMSBuildArgs.ToArray()); |
| 144 | + } |
| 145 | + |
| 146 | + var newProperties = new Dictionary<string, string>(GlobalProperties, StringComparer.OrdinalIgnoreCase); |
| 147 | + foreach (var kvp in additionalProperties) |
| 148 | + { |
| 149 | + newProperties[kvp.Key] = kvp.Value; |
| 150 | + } |
| 151 | + return new MSBuildArgs(new(newProperties), RestoreGlobalProperties, RequestedTargets, OtherMSBuildArgs.ToArray()); |
| 152 | + } |
| 153 | + |
| 154 | + public MSBuildArgs CloneWithAdditionalTarget(string additionalTarget) |
| 155 | + { |
| 156 | + string[] newTargets = RequestedTargets is not null |
| 157 | + ? [.. RequestedTargets, additionalTarget] |
| 158 | + : [ additionalTarget ]; |
| 159 | + return new MSBuildArgs(GlobalProperties, RestoreGlobalProperties, newTargets, OtherMSBuildArgs.ToArray()); |
| 160 | + } |
| 161 | + |
| 162 | + public void ApplyPropertiesToRestore() |
| 163 | + { |
| 164 | + if (RestoreGlobalProperties is null) |
| 165 | + { |
| 166 | + RestoreGlobalProperties = GlobalProperties; |
| 167 | + return; |
| 168 | + } |
| 169 | + else if (GlobalProperties is not null && GlobalProperties.Count > 0) |
| 170 | + { |
| 171 | + // If we have restore properties, we need to merge the global properties into them. |
| 172 | + // We ensure the restore properties overwrite the properties to align with expected MSBuild semantics. |
| 173 | + var newdict = new Dictionary<string, string>(GlobalProperties, StringComparer.OrdinalIgnoreCase); |
| 174 | + foreach (var restoreKvp in RestoreGlobalProperties) |
| 175 | + { |
| 176 | + newdict[restoreKvp.Key] = restoreKvp.Value; |
| 177 | + } |
| 178 | + RestoreGlobalProperties = new(newdict); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments