Пример #1
0
        /// <summary>
        /// Create a .designer file for the given parsed markup.
        /// </summary>
        /// <param name="compileContext">The context in which errors should be reported.</param>
        /// <param name="markupInfo">The fully-parsed markup.</param>
        /// <returns>The contents of the .designer file, as a string.</returns>
        public string CreateDesigner(ICompileContext compileContext, MarkupInfo markupInfo)
        {
            compileContext.Verbose("");
            compileContext.Verbose("Generating designer file for {0} with the {1} controls found in the markup.",
                                   markupInfo.ClassType.FullName, markupInfo.OutputControls.Count());

            // Get the actual runtime version from the System.Web assembly itself.
            string runtimeVersion = markupInfo.Assemblies[Common.SystemWebAssemblyName].ImageRuntimeVersion;

            if (runtimeVersion.StartsWith("v"))
            {
                runtimeVersion = runtimeVersion.Substring(1);
            }

            // Construct the initial part of the .designer.cs file.
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat(DesignerStart, runtimeVersion + ".0", markupInfo.ClassType.Namespace, markupInfo.ClassType.Name);

            // We must skip Content declarations, since those are not emitted as output.
            Type contentControl = typeof(System.Web.UI.WebControls.Content);

            // Spit out all of the controls.
            foreach (OutputControl outputControl in markupInfo.OutputControls)
            {
                string controlName = outputControl.Name;
                Type   controlType = outputControl.ReflectedControl.ControlType;

                if (contentControl.IsAssignableFrom(controlType))
                {
                    continue;
                }

                if (string.IsNullOrEmpty(controlName))
                {
                    compileContext.Verbose("Skipping unnamed {0} control.", controlType.FullName);
                    continue;
                }

                compileContext.Verbose("Adding control declaration: {1} {0}.", controlName, controlType.FullName);

                stringBuilder.AppendFormat(Declaration, controlName, controlType.FullName);
            }

            stringBuilder.Append(DesignerEnd);

            compileContext.Verbose("End generation of designer file.");

            return(stringBuilder.ToString());
        }
Пример #2
0
        /// <summary>
        /// Compare a parsed markup file against a parsed designer file to determine if they match each other.
        /// </summary>
        /// <param name="compileContext">The context in which errors should be reported.</param>
        /// <param name="filename">The filename to use for reporting errors.</param>
        /// <param name="markupInfo">The markup file to compare.</param>
        /// <param name="designerInfo">The designer file to compare.</param>
        /// <returns>True if they match, false if they do not.</returns>
        private static bool CompareMarkupInfoToDesignerInfo(ICompileContext compileContext, string filename, MarkupInfo markupInfo, DesignerInfo designerInfo)
        {
            compileContext.Verbose("Comparing markup controls to .designer file properties...");

            compileContext.Verbose("Comparing classnames.");

            // First, make sure the type names match; we *should* be talking about the same classes here.
            if (markupInfo.ClassType == null)
            {
                compileContext.Error("{0}: Designer file exists, but markup file has no Inherits=\"...\" attribute.", filename);
                return(false);
            }
            if (markupInfo.ClassType.FullName != designerInfo.FullTypeName)
            {
                compileContext.Error("{0}: Designer file and markup file specify different type names (\"{1}\" in the markup, and \"{2}\" in the designer file.",
                                     filename, markupInfo.ClassType.FullName, designerInfo.FullTypeName);
                return(false);
            }

            // Build lookup tables for the property declarations in the designer file and in the markup file.
            // We'll use these to make searching for property matches that much faster, and to detect duplicates,
            // and to ensure that we're talking about the same set of properties in both files.

            compileContext.Verbose("Checking for duplicate control declarations.");

            Dictionary <string, OutputControl> markupPropertiesByName = new Dictionary <string, OutputControl>();
            Dictionary <string, DesignerPropertyDeclaration> designerProperiesByName = new Dictionary <string, DesignerPropertyDeclaration>();

            List <string> duplicateMarkupProperties = new List <string>();

            foreach (OutputControl outputControl in markupInfo.OutputControls)
            {
                if (string.IsNullOrEmpty(outputControl.Name))
                {
                    continue;
                }

                if (markupPropertiesByName.ContainsKey(outputControl.Name))
                {
                    duplicateMarkupProperties.Add(outputControl.Name);
                }
                else
                {
                    markupPropertiesByName.Add(outputControl.Name, outputControl);
                }
            }

            List <string> duplicateDesignerProperties = new List <string>();

            foreach (DesignerPropertyDeclaration propertyDeclaration in designerInfo.PropertyDeclarations)
            {
                if (designerProperiesByName.ContainsKey(propertyDeclaration.Name))
                {
                    duplicateDesignerProperties.Add(propertyDeclaration.Name);
                }
                else
                {
                    designerProperiesByName.Add(propertyDeclaration.Name, propertyDeclaration);
                }
            }

            // Check the lookup tables for duplicates.  There shouldn't be any.

            if (duplicateMarkupProperties.Count > 0)
            {
                compileContext.Error("{0}: Malformed markup error: Found multiple controls in the markup that have the same ID.  Stopping verification now due to invalid markup file.  Duplicate IDs: {1}",
                                     filename, Join(duplicateMarkupProperties, ", "));
            }
            if (duplicateDesignerProperties.Count > 0)
            {
                compileContext.Error("{0}: Malformed designer error: Found multiple property declarations in the .designer file that have the same name.  Stopping verification now due to invalid designer file.  Duplicate names: {1}",
                                     filename, Join(duplicateDesignerProperties, ", "));
            }
            if (duplicateMarkupProperties.Count > 0 || duplicateDesignerProperties.Count > 0)
            {
                return(false);
            }

            // Okay, now check to see if the markup or designer declare property names that the other doesn't have.

            compileContext.Verbose("Checking for missing control declarations.");

            Type          contentControl            = typeof(System.Web.UI.WebControls.Content);
            List <string> missingDesignerProperties = markupInfo.OutputControls
                                                      .Where(p => !string.IsNullOrEmpty(p.Name) && p.ReflectedControl.ControlType != contentControl && !designerProperiesByName.ContainsKey(p.Name))
                                                      .Select(p => p.Name)
                                                      .ToList();
            List <string> missingMarkupProperties = designerInfo.PropertyDeclarations
                                                    .Where(p => !string.IsNullOrEmpty(p.Name) && !markupPropertiesByName.ContainsKey(p.Name))
                                                    .Select(p => p.Name)
                                                    .ToList();

            if (missingDesignerProperties.Count > 0)
            {
                compileContext.Error("{0}: Missing property error: Found controls declared in the markup that do not exist in the .designer file.  Missing IDs: {1}",
                                     filename, Join(missingDesignerProperties, ", "));
            }
            if (missingMarkupProperties.Count > 0)
            {
                compileContext.Error("{0}: Missing control error: Found property declarations in the .designer file that have no control declaration in the markup.  Missing controls: {1}",
                                     filename, Join(missingMarkupProperties, ", "));
            }

            // We've now established that both files refer to the same set of names.  We now need to check
            // to make sure they all refer to the same control types.

            int numTypeMismatches = 0;

            compileContext.Verbose("Checking for type mismatches.");

            foreach (OutputControl outputControl in markupInfo.OutputControls)
            {
                if (string.IsNullOrEmpty(outputControl.Name) ||
                    outputControl.ReflectedControl.ControlType == contentControl ||
                    !designerProperiesByName.ContainsKey(outputControl.Name))
                {
                    continue;
                }

                DesignerPropertyDeclaration designerPropertyDeclaration = designerProperiesByName[outputControl.Name];
                if (designerPropertyDeclaration.PropertyTypeName != outputControl.ReflectedControl.ControlType.FullName)
                {
                    compileContext.Error("{0}: Type mismatch: Control \"{1}\" has type {2} in the markup but type {3} in the .designer file.",
                                         filename, outputControl.Name, outputControl.ReflectedControl.ControlType.FullName, designerPropertyDeclaration.PropertyTypeName);
                    numTypeMismatches++;
                }
            }

            if (missingDesignerProperties.Count > 0 || missingMarkupProperties.Count > 0 || numTypeMismatches > 0)
            {
                return(false);
            }

            // One last very touchy check:  All the properties exist in both files, and they have the same names and
            // same types --- but are they in the right order?  Visual Studio is very picky about the order, and if
            // they don't match, the Visual Studio designer will break.

            compileContext.Verbose("Checking for mis-ordered declarations.");

            for (int m = 0, d = 0; m < markupInfo.OutputControls.Count;)
            {
                OutputControl outputControl = markupInfo.OutputControls[m++];
                if (string.IsNullOrEmpty(outputControl.Name) ||
                    outputControl.ReflectedControl.ControlType == contentControl)
                {
                    continue;
                }

                DesignerPropertyDeclaration designerPropertyDeclaration = designerInfo.PropertyDeclarations[d++];

                if (designerPropertyDeclaration.Name != outputControl.Name ||
                    designerPropertyDeclaration.PropertyTypeName != outputControl.ReflectedControl.ControlType.FullName)
                {
                    compileContext.Error("{0}: Ordering error: All of the same controls exist in both the markup and the .designer file, but they do not appear in the same order.", filename);
                    return(false);
                }
            }

            compileContext.Verbose("{0}: Success!", filename);
            return(true);
        }