Пример #1
0
        /// <summary>
        /// Generate a replacement .designer.cs file for the given markup file, overwriting the existing
        /// .designer.cs file if there is one.
        /// </summary>
        public static bool GenerateDesignerForFilename(ICompileContext compileContext, string filename, IEnumerable <TagRegistration> tagRegistrations, AssemblyLoader assemblies, string assemblyDirectory, string rootPath)
        {
            string designer;
            string designerFilename = filename + ".designer.cs";

            // Load the markup from the .aspx or .ascx file.
            MarkupReader markup = new MarkupReader();
            MarkupInfo   markupInfo;

            try
            {
                markupInfo = markup.LoadMarkup(compileContext, filename, tagRegistrations, assemblies, assemblyDirectory, rootPath);
            }
            catch (Exception e)
            {
                compileContext.Error("{0}: Failed to load markup file:\r\n{1}", filename, e.Message);
                compileContext.Verbose("Stopping file processing due to exception.  Stack trace:\r\n{0}", e.StackTrace);
                return(false);
            }

            // If we're not inheriting a real class, there's no reason for a designer file to exist.
            if (markupInfo.ClassType == null)
            {
                compileContext.Verbose("Skipping generating designer file because markup does not have an Inherits=\"...\" attribute.", filename);
                return(true);
            }

            // Generate the output text for the new .designer.cs file.
            try
            {
                DesignerWriter designerWriter = new DesignerWriter();
                designer = designerWriter.CreateDesigner(compileContext, markupInfo);
            }
            catch (Exception e)
            {
                compileContext.Error("{0}: Cannot regenerate designer file:\r\n{1}", filename, e.Message);
                compileContext.Verbose("Stopping file processing due to exception.  Stack trace:\r\n{0}", e.StackTrace);
                return(false);
            }

            // Save the output .designer.cs file to disk.
            try
            {
                File.WriteAllText(designerFilename, designer, Encoding.UTF8);
            }
            catch (Exception e)
            {
                compileContext.Error("{0}: Cannot open designer file for writing:\r\n{1}", designerFilename, e.Message);
                compileContext.Verbose("Stopping file processing due to exception.  Stack trace:\r\n{0}", e.StackTrace);
                return(false);
            }

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Verify the current .designer.cs file for the given markup file.
        /// </summary>
        /// <returns>True if the file passes inspection, false if it fails.</returns>
        public static bool VerifyDesignerForFilename(ICompileContext compileContext, string filename, IEnumerable <TagRegistration> tagRegistrations, AssemblyLoader assemblies, string assemblyDirectory, string rootPath)
        {
            DesignerInfo designerInfo;
            string       designerFilename = filename + ".designer.cs";

            // Load the markup from the .aspx or .ascx file.
            MarkupReader markup = new MarkupReader();
            MarkupInfo   markupInfo;

            try
            {
                markupInfo = markup.LoadMarkup(compileContext, filename, tagRegistrations, assemblies, assemblyDirectory, rootPath);
            }
            catch (Exception e)
            {
                compileContext.Error("{0}: Failed to load markup file:\r\n{1}", filename, e.Message);
                compileContext.Verbose("Stopping file processing due to exception.  Stack trace:\r\n{0}", e.StackTrace);
                return(false);
            }

            if (markupInfo.ClassType == null)
            {
                compileContext.Verbose("Skipping verification of .designer file, because markup has no Inherits=\"...\" attribute and therefore has no .designer file.", filename);
                return(true);
            }

            compileContext.Verbose(string.Empty);

            // Read and parse the current .designer.cs file.
            try
            {
                DesignerReader designerReader = new DesignerReader();
                designerInfo = designerReader.LoadDesignerFile(compileContext, designerFilename);
            }
            catch (Exception e)
            {
                compileContext.Error("{0}: Cannot load designer file:\r\n{1}", filename, e.Message);
                compileContext.Verbose("Stopping file processing due to exception.  Stack trace:\r\n{0}", e.StackTrace);
                return(false);
            }

            compileContext.Verbose(string.Empty);

            // And finally compare the expectations of the markup against the reality of the .designer.cs file.
            return(CompareMarkupInfoToDesignerInfo(compileContext, filename, markupInfo, designerInfo));
        }
        public async System.Threading.Tasks.Task <byte[]> GenerateDesignerFile(ProjectItem projectItem, string contents, string outputAssemblyPath)
        {
            // get list of referenced assemblies.
            var project             = projectItem.ContainingProject;
            var unconfiguredProject = project.GetUnconfiguredProject();
            var lockService         = unconfiguredProject.ProjectService.Services.ProjectLockService;

            var configuredProject = await unconfiguredProject.GetSuggestedConfiguredProjectAsync();

            List <ITaskItem> assemblyReferenceItems;

            // need to gather the referenced assemblies.
            using (var access = await lockService.WriteLockAsync())
            {
                Microsoft.Build.Evaluation.Project msBuildProject = await access.GetProjectAsync(configuredProject);

                // party on it, respecting the type of lock you've acquired.
                var assemblyReferenceGatherer = new AssemblyReferenceGatherer();
                assemblyReferenceItems = assemblyReferenceGatherer.GetAssemblyReferences(msBuildProject.Xml).ToList();
            }

            var projDir = project.GetProjectPath();

            var referencedAssemblies = new ReferencedAssembliesContext(assemblyReferenceItems, outputAssemblyPath);


            string currentDir      = System.IO.Path.GetDirectoryName(project.FullName);
            string websiteRootPath = currentDir; // todo - this could point to a dnn website we are targeting?

            List <TagRegistration> tagRegistrations = referencedAssemblies.StandardTagRegistrations.ToList();

            UserControlTagRegistrationResolver.ResolveUserControls(this, tagRegistrations, referencedAssemblies, websiteRootPath, currentDir);

            Verbose("Begin processing \"{0}\"...", projectItem.Name);
            Verbose("");
            VerboseNesting++;
            BeginFile(projectItem.Name);

            //bool succeeded = GenerateDesignerForFilename(document.FullName, tagRegistrations, referencedAssemblies, websiteRootPath);

            bool succeeded = false;
            // var filename = document.FullName;

            string designer = null;
            // string designerFilename = filename + ".designer.cs";

            // Load the markup from the .aspx or .ascx file.
            MarkupReader markup     = new MarkupReader();
            MarkupInfo   markupInfo = null;

            try
            {
                markupInfo = markup.LoadMarkup(this, tagRegistrations, referencedAssemblies, websiteRootPath, contents, projectItem.Name);
            }
            catch (Exception e)
            {
                Error("{0}: Failed to load markup file:\r\n{1}", projectItem.Name, e.Message);
                Verbose("Stopping file processing due to exception.  Stack trace:\r\n{0}", e.StackTrace);
                succeeded = false;
                return(null);
            }

            // If we're not inheriting a real class, there's no reason for a designer file to exist.
            if (string.IsNullOrWhiteSpace(markupInfo.InheritsClassName))
            {
                Verbose("Skipping generating designer file because markup does not have an Inherits=\"...\" attribute.", projectItem.Name);
                succeeded = true;
                return(null);
            }

            // Generate the output text for the new .designer.cs file.
            try
            {
                DesignerWriter designerWriter = new DesignerWriter();
                designer = designerWriter.CreateDesigner(this, markupInfo);
            }
            catch (Exception e)
            {
                Error("{0}: Cannot regenerate designer file:\r\n{1}", projectItem.Name, e.Message);
                Verbose("Stopping file processing due to exception.  Stack trace:\r\n{0}", e.StackTrace);
                succeeded = false;
                return(null);
            }

            // Save the output .designer.cs file to disk.

            var utfBytes = Encoding.UTF8.GetBytes(designer);

            EndFile(projectItem.Name, succeeded);
            VerboseNesting--;
            Verbose("");
            Verbose("End processing \"{0}\".", projectItem.Name);

            return(utfBytes);
        }