Beispiel #1
0
        public static bool ParseAndEvaluate(string condition, IExpressionContext context)
        {
            if (String.IsNullOrEmpty(condition))
            {
                return(true);
            }

            try {
                ConditionExpression ce = ParseCondition(condition);

                if (!ce.TryEvaluateToBool(context, out bool result))
                {
                    throw new InvalidProjectFileException(String.Format("Can not evaluate \"{0}\" to bool.", condition));
                }

                return(result);
            } catch (ExpressionParseException epe) {
                throw new InvalidProjectFileException(
                          String.Format("Unable to parse condition \"{0}\" : {1}", condition, epe.Message),
                          epe);
            } catch (ExpressionEvaluationException epe) {
                throw new InvalidProjectFileException(
                          String.Format("Unable to evaluate condition \"{0}\" : {1}", condition, epe.Message),
                          epe);
            }
        }
Beispiel #2
0
        public override bool TryEvaluateToBool(IExpressionContext context, out bool result)
        {
            var canEvaluate = expression.TryEvaluateToBool(context, out result);

            result = !result;

            return(canEvaluate);
        }
        public override bool TryEvaluateToBool(IExpressionContext context, out bool result)
        {
            // Short-circuiting, check only left expr, right
            // would be required only if left == true
            if (!left.TryEvaluateToBool(context, out result))
            {
                return(false);
            }

            if (!result)
            {
                return(true);
            }

            return(right.TryEvaluateToBool(context, out result));
        }
Beispiel #4
0
        public override bool TryEvaluateToBool(IExpressionContext context, out bool result)
        {
            if (left.TryEvaluateToBool(context, out bool l) && right.TryEvaluateToBool(context, out bool r))
            {
                result = BoolCompare(l, r, op);
                return(true);
            }

            if (left.TryEvaluateToVersion(context, out Version vl))
            {
                if (right.TryEvaluateToVersion(context, out Version vr))
                {
                    result = VersionCompare(vl, vr, op);
                    return(true);
                }
                else if (right.TryEvaluateToNumber(context, out float fr))
                {
                    result = VersionCompare(vl, fr, op);
                    return(true);
                }
            }
            else if (left.TryEvaluateToNumber(context, out float fl))
            {
                if (right.TryEvaluateToNumber(context, out float fr))
                {
                    result = NumberCompare(fl, fr, op);
                    return(true);
                }
                else if (right.TryEvaluateToVersion(context, out Version vr))
                {
                    result = VersionCompare(fl, vr, op);
                    return(true);
                }
            }

            if (!left.TryEvaluateToString(context, out string ls) || !right.TryEvaluateToString(context, out string rs))
            {
                result = false;
                return(false);
            }

            result = StringCompare(ls, rs, op);
            return(true);
        }
Beispiel #5
0
        public override bool TryEvaluateToBool(IExpressionContext context, out bool result)
        {
            if (left.TryEvaluateToBool(context, out bool l) && right.TryEvaluateToBool(context, out bool r))
            {
                result = BoolCompare(l, r, op);
                return(true);
            }

            if (left.TryEvaluateToVersion(context, out Version vl))
            {
                if (right.TryEvaluateToVersion(context, out Version vr))
                {
                    result = VersionCompare(vl, vr, op);
                    return(true);
                }
                else if (right.TryEvaluateToNumber(context, out float fr))
                {
                    result = VersionCompare(vl, fr, op);
                    return(true);
                }
            }
            else if (left.TryEvaluateToNumber(context, out float fl))
            {
                if (right.TryEvaluateToNumber(context, out float fr))
                {
                    result = NumberCompare(fl, fr, op);
                    return(true);
                }
                else if (right.TryEvaluateToVersion(context, out Version vr))
                {
                    result = VersionCompare(fl, vr, op);
                    return(true);
                }
            }

            if (!left.TryEvaluateToString(context, out string ls) || !right.TryEvaluateToString(context, out string rs))
            {
                result = false;
                return(false);
            }

            // Special case MSBuildToolsVersion when it is set to Current.
            if (op != RelationOperator.Equal || op != RelationOperator.NotEqual)
            {
                if (IsMSBuildCurrentToolsVersion(left, ls))
                {
                    if (TryGetVisualStudioVersion(context, out vl))
                    {
                        if (right.TryEvaluateToVersion(context, out Version vr))
                        {
                            result = VersionCompare(vl, vr, op);
                            return(true);
                        }
                        else if (right.TryEvaluateToNumber(context, out float fr))
                        {
                            result = VersionCompare(vl, fr, op);
                            return(true);
                        }
                    }
                }
                else if (IsMSBuildCurrentToolsVersion(right, rs))
                {
                    if (TryGetVisualStudioVersion(context, out Version vr))
                    {
                        if (left.TryEvaluateToVersion(context, out vl))
                        {
                            result = VersionCompare(vl, vr, op);
                            return(true);
                        }
                        else if (left.TryEvaluateToNumber(context, out float fl))
                        {
                            result = VersionCompare(fl, vr, op);
                            return(true);
                        }
                    }
                }
            }

            result = StringCompare(ls, rs, op);
            return(true);
        }