| File: Evaluation\Conditionals\StringExpressionNode.cs | Web Access |
| Project: Microsoft.Build.csproj (Microsoft.Build) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Diagnostics; using Microsoft.Build.Shared; #nullable disable namespace Microsoft.Build.Evaluation { /// <summary> /// Node representing a string /// </summary> [DebuggerDisplay("{DebuggerDisplay,nq}")] internal sealed class StringExpressionNode : OperandExpressionNode { internal int StateSlot { get; } private readonly string _value; /// <summary> /// Whether the string potentially has expandable content, /// such as a property expression or escaped character. /// </summary> private readonly bool _expandable; private readonly bool _isMSBuildToolsVersionExpansion; internal StringExpressionNode(string value, bool expandable, int stateSlot) { _value = value; _expandable = expandable; _isMSBuildToolsVersionExpansion = expandable && string.Equals(value, "$(MSBuildToolsVersion)", StringComparison.OrdinalIgnoreCase); StateSlot = stateSlot; } internal override bool TryBoolEvaluate(ConditionEvaluator.IConditionEvaluationState state, out bool result) { return ConversionUtilities.TryConvertStringToBool(GetExpandedValue(state), out result); } internal override bool TryNumericEvaluate(ConditionEvaluator.IConditionEvaluationState state, out double result) { if (ShouldBeTreatedAsVisualStudioVersion(state)) { result = ConversionUtilities.ConvertDecimalOrHexToDouble(MSBuildConstants.CurrentVisualStudioVersion); return true; } else { return ConversionUtilities.TryConvertDecimalOrHexToDouble(GetExpandedValue(state), out result); } } internal override bool TryVersionEvaluate(ConditionEvaluator.IConditionEvaluationState state, out Version result) { if (ShouldBeTreatedAsVisualStudioVersion(state)) { result = Version.Parse(MSBuildConstants.CurrentVisualStudioVersion); return true; } else { return Version.TryParse(GetExpandedValue(state), out result); } } /// <summary> /// Returns true if this node evaluates to an empty string, /// otherwise false. /// It may be cheaper to determine whether an expression will evaluate /// to empty than to fully evaluate it. /// Implementations should cache the result so that calls after the first are free. /// </summary> internal override bool EvaluatesToEmpty(ConditionEvaluator.IConditionEvaluationState state) { if (!_expandable) { return _value.Length == 0; } string cachedExpandedValue = state.GetCachedExpandedValue(StateSlot); if (cachedExpandedValue is null) { switch (_value.Length) { case 0: state.SetCachedExpandedValue(StateSlot, String.Empty); return true; // If the length is 1 or 2, it can't possibly be a property, item, or metadata, and it isn't empty. case 1: case 2: state.SetCachedExpandedValue(StateSlot, _value); return false; default: if (_value[1] != '(' || (_value[0] != '$' && _value[0] != '%' && _value[0] != '@') || _value[_value.Length - 1] != ')') { // This isn't just a property, item, or metadata value, and it isn't empty. return false; } break; } string expandBreakEarly = state.ExpandIntoStringBreakEarly(_value); if (expandBreakEarly is null) { // It broke early: we can't store the value, we just // know it's non empty return false; } // It didn't break early, the result is accurate, // so store it so the work isn't done again. cachedExpandedValue = expandBreakEarly; state.SetCachedExpandedValue(StateSlot, cachedExpandedValue); } return cachedExpandedValue.Length == 0; } /// <inheritdoc cref="GenericExpressionNode"/> internal override bool IsUnexpandedValueEmpty() => string.IsNullOrEmpty(_value); /// <summary> /// Value before any item and property expressions are expanded /// </summary> /// <returns></returns> internal override string GetUnexpandedValue(ConditionEvaluator.IConditionEvaluationState state) { return _value; } /// <summary> /// Value after any item and property expressions are expanded /// </summary> /// <returns></returns> internal override string GetExpandedValue(ConditionEvaluator.IConditionEvaluationState state) { if (!_expandable) { return _value; } string cachedExpandedValue = state.GetCachedExpandedValue(StateSlot); if (cachedExpandedValue is null) { cachedExpandedValue = state.ExpandIntoString(_value); state.SetCachedExpandedValue(StateSlot, cachedExpandedValue); } return cachedExpandedValue; } /// <summary> /// Should this node be treated as an expansion of VisualStudioVersion, rather than /// its literal meaning? /// </summary> /// <remarks> /// Needed to provide a compat shim for numeric/version comparisons /// on MSBuildToolsVersion, which were fine when it was a number /// but now cause the project to throw InvalidProjectException when /// ToolsVersion is "Current". https://github.com/dotnet/msbuild/issues/4150 /// </remarks> private bool ShouldBeTreatedAsVisualStudioVersion(ConditionEvaluator.IConditionEvaluationState state) => _isMSBuildToolsVersionExpansion && string.Equals(GetExpandedValue(state), MSBuildConstants.CurrentToolsVersion, StringComparison.Ordinal); internal override string DebuggerDisplay => $"\"{_value}\""; } }