コード例 #1
0
ファイル: Common.cs プロジェクト: dazinator/Redesigner
        /// <summary>
        /// For the given set of .aspx or .ascx files, generate all of their designer files.
        /// </summary>
        /// <param name="compileContext">The context in which errors are to be reported.</param>
        /// <param name="filenames">The filenames to generate.</param>
        /// <param name="rootPath">The root disk path of the website (usually the same as the path to "web.config").</param>
        /// <param name="websiteDllFileName">The disk path to the website's DLL.</param>
        public static bool GenerateDesignerFiles(ICompileContext compileContext, IEnumerable <string> filenames, string rootPath, string websiteDllFileName)
        {
            int filenameCount = filenames.Count();

            compileContext.BeginTask(filenameCount);

            // Load and parse the "web.config".
            WebConfigReader webConfigReader = new WebConfigReader();

            try
            {
                webConfigReader.LoadWebConfig(compileContext, Path.Combine(rootPath, WebConfigFilename), rootPath);
            }
            catch (Exception e)
            {
                compileContext.Error("Cannot load {0}:\r\n{1}", Path.Combine(rootPath, WebConfigFilename), e.Message);
                return(false);
            }

            // Load any assemblies we know we'll need.  This includes the default assemblies, any declared
            // in the web.config, and, of course, the website's DLL itself.
            AssemblyLoader assemblyLoader = new AssemblyLoader();
            List <string>  assemblyNames  = new List <string>();

            assemblyNames.AddRange(_standardTagRegistrations.Where(r => !string.IsNullOrEmpty(r.AssemblyFilename)).Select(r => r.AssemblyFilename).Distinct());
            assemblyNames.AddRange(webConfigReader.TagRegistrations.Where(r => !string.IsNullOrEmpty(r.AssemblyFilename)).Select(r => r.AssemblyFilename).Distinct());
            string dllFullPath = Path.GetFullPath(websiteDllFileName);

            assemblyNames.Add(dllFullPath);
            string assemblyDirectory = Path.GetDirectoryName(dllFullPath);

            assemblyLoader.PreloadAssemblies(compileContext, assemblyNames, assemblyDirectory);
            assemblyLoader.PrimaryAssembly = assemblyLoader[dllFullPath];

            // Add the default tag registrations, including those from System.Web and any declared in the "web.config".
            List <TagRegistration> tagRegistrations = new List <TagRegistration>();

            tagRegistrations.AddRange(_standardTagRegistrations);
            tagRegistrations.AddRange(webConfigReader.TagRegistrations);

            // Spin through any user controls that were declared in the web.config and connect them to their actual
            // .NET class types via reflection.
            compileContext.Verbose("Resolving user controls declared in the web.config.");
            compileContext.VerboseNesting++;
            ResolveUserControls(compileContext, tagRegistrations, assemblyLoader, assemblyDirectory, rootPath, rootPath);
            compileContext.VerboseNesting--;
            compileContext.Verbose(string.Empty);

            // Now that all the setup is done, load and parse each individual markup file into its own .designer.cs output file.
            bool result = true;

            foreach (string filename in filenames)
            {
                compileContext.Verbose("Begin processing \"{0}\"...", filename);
                compileContext.Verbose("");
                compileContext.VerboseNesting++;

                compileContext.BeginFile(filename);
                bool succeeded = GenerateDesignerForFilename(compileContext, filename, tagRegistrations, assemblyLoader, assemblyDirectory, rootPath);
                result &= succeeded;
                compileContext.EndFile(filename, succeeded);

                compileContext.VerboseNesting--;
                compileContext.Verbose("");
                compileContext.Verbose("End processing \"{0}\".", filename);
            }
            return(result);
        }