Пример #1
0
 void ValidateAttribute(XElement element, XAttribute attribute, MSBuildLanguageElement resolvedElement, MSBuildLanguageAttribute resolvedAttribute)
 {
     if (string.IsNullOrWhiteSpace(attribute.Value))
     {
         if (resolvedAttribute.Required)
         {
             AddError($"Required attribute has empty value", attribute.GetNameRegion());
         }
         else
         {
             AddWarning($"Attribute has empty value", attribute.GetNameRegion());
         }
         return;
     }
 }
Пример #2
0
        void ValidateUsingTaskHasAssembly(XElement element)
        {
            XAttribute taskFactoryAtt = null;
            XAttribute asmNameAtt     = null;
            XAttribute asmFileAtt     = null;

            foreach (var att in element.Attributes)
            {
                switch (att.Name.Name.ToLowerInvariant())
                {
                case "assemblyfile":
                    asmFileAtt = att;
                    break;

                case "assemblyname":
                    asmNameAtt = att;
                    break;

                case "taskfactory":
                    taskFactoryAtt = att;
                    break;
                }
            }

            if (asmNameAtt == null && asmFileAtt == null)
            {
                AddError(
                    $"UsingTask must have AssemblyName or AssemblyFile attribute",
                    element.GetNameRegion());
            }
            else if (taskFactoryAtt != null && asmNameAtt != null)
            {
                AddError(
                    $"UsingTask with TaskFactory cannot have AssemblyName attribute",
                    asmNameAtt.GetNameRegion());
            }
            else if (taskFactoryAtt != null && asmFileAtt == null)
            {
                AddError(
                    $"UsingTask with TaskFactory must have AssemblyFile attribute",
                    element.GetNameRegion());
            }
            else if (asmNameAtt != null && asmFileAtt != null)
            {
                AddError(
                    $"UsingTask may not have both AssemblyName and AssemblyFile attributes",
                    asmNameAtt.GetNameRegion());
            }

            XElement parameterGroup = null, taskBody = null;

            foreach (var child in element.Elements)
            {
                if (child.NameEquals("ParameterGroup", true))
                {
                    if (parameterGroup != null)
                    {
                        AddError(
                            $"UsingTask may only have one ParameterGroup",
                            child.GetNameRegion());
                    }
                    parameterGroup = child;
                }
                if (child.NameEquals("Task", true))
                {
                    if (taskBody != null)
                    {
                        AddError(
                            $"UsingTask may only have one Task body",
                            child.GetNameRegion());
                    }
                    taskBody = child;
                }
            }

            if (taskFactoryAtt == null)
            {
                if (taskBody != null)
                {
                    AddError(
                        $"UsingTask without TaskFactory attribute cannot have Task element",
                        taskBody.GetNameRegion());
                }
                else if (parameterGroup != null)
                {
                    AddError(
                        $"UsingTask without TaskFactory attribute cannot have ParameterGroup element",
                        parameterGroup.GetNameRegion());
                }
            }
            else
            {
                if (taskBody == null)
                {
                    AddError(
                        $"UsingTask with TaskFactory attribute must have Task element",
                        element.GetNameRegion());
                }

                if (taskBody != null)
                {
                    switch (taskFactoryAtt.Value?.ToLowerInvariant())
                    {
                    case "codetaskfactory":
                        if (string.Equals(asmFileAtt.Value, "$(RoslynCodeTaskFactory)"))
                        {
                            goto case "roslyncodetaskfactory";
                        }
                        break;

                    case "roslyncodetaskfactory":
                        ValidateRoslynCodeTaskFactory(element, taskBody, parameterGroup);
                        break;

                    case null:
                        AddError(
                            $"UsingTask with Task element must have TaskFactory attribute",
                            taskBody.GetNameRegion());
                        break;
                    }
                }
            }
        }