예제 #1
0
        /// <summary>
        /// Create the library file that contains INotifier and Notification extensions
        /// has to be created before EnvDte can add the interface <see cref="NotifyPropertyChangedRgen.Extensions.AddInterfaceIfNotExists"/> to classes
        /// </summary>
        /// <remarks>
        ///
        /// </remarks>
        public void RenderLibrary()
        {
            //Check for existing class
            var prj = ProjectItem.Project;

            const string     className       = LibraryRenderer.DefaultClassName;
            var              classes         = prj.GetCodeElements <CodeClass>();
            var              classFullname   = prj.DefaultNamespace.DotJoin(className);
            List <CodeClass> matchingClasses = null;

            classes.TryGetValue(classFullname, out matchingClasses);

            ProjectItem classItem = null;

            if (matchingClasses == null)
            {
                //Class not found, generate
                var filePath = Path.Combine(prj.FullPath, className + ".cs");
                //if file exists, warn then quit
                if (File.Exists(filePath))
                {
                    MessageBox.Show(string.Format("Trying to add {0} to project, but file {1} already exists", className, filePath));
                    return;
                }
                //Create new empty file
                File.WriteAllText(filePath, "");
                prj.CheckOut();
                //Add it to the project
                classItem = prj.DteObject.ProjectItems.AddFromFile(filePath);
            }
            else
            {
                //Class found, get corresponding project item
                classItem = matchingClasses.First().ProjectItem;
            }

            //Open file for editing
            var wasOpen = classItem.IsOpen[Constants.vsViewKindCode];

            if (!wasOpen)
            {
                classItem.Open(Constants.vsViewKindCode);
            }
            var textDoc = classItem.Document.ToTextDocument();

            var genInfo = new Writer()
            {
                SearchStart = textDoc.StartPoint, InsertStart = textDoc.StartPoint, SearchEnd = textDoc.EndPoint, SegmentType = Types.Region
            };

            if (Manager.IsAnyOutdated(genInfo))
            {
                //generate text if outdated
                var extRgen = new LibraryRenderer(prj.DefaultNamespace);

                var code = extRgen.RenderToString();
                genInfo.Content = code;
                Manager.InsertOrReplace(genInfo);
                classItem.Save("");
            }

            //restore to previous state
            if (!wasOpen)
            {
                classItem.Document.Close(vsSaveChanges.vsSaveChangesPrompt);
            }
        }
        /// <summary>
        /// Create the library file that contains INotifier and Notification extensions
        /// has to be created before EnvDte can add the interface <see cref="NotifyPropertyChangedRgen.Extensions.AddInterfaceIfNotExists"/> to classes
        /// </summary>
        /// <remarks>
        /// 
        /// </remarks>
        public void RenderLibrary()
        {
            //Check for existing class
            var prj = ProjectItem.Project;
            
            const string className = LibraryRenderer.DefaultClassName;
            var classes = prj.GetCodeElements<CodeClass>();
            var classFullname = prj.DefaultNamespace.DotJoin(className);
            List<CodeClass> matchingClasses = null;
            classes.TryGetValue(classFullname, out matchingClasses);

            ProjectItem classItem = null;
            if (matchingClasses == null) {
                //Class not found, generate
                var filePath = Path.Combine(prj.FullPath, className + ".cs");
                //if file exists, warn then quit
                if (File.Exists(filePath)) {
                    MessageBox.Show(string.Format("Trying to add {0} to project, but file {1} already exists", className, filePath));
                    return;
                }
                //Create new empty file
                File.WriteAllText(filePath, "");
                prj.CheckOut();
                //Add it to the project
                classItem = prj.DteObject.ProjectItems.AddFromFile(filePath);
            }
            else {
                //Class found, get corresponding project item
                classItem = matchingClasses.First().ProjectItem;
            }

            //Open file for editing
            var wasOpen = classItem.IsOpen[Constants.vsViewKindCode];
            if (!wasOpen) {
                classItem.Open(Constants.vsViewKindCode);
            }
            var textDoc = classItem.Document.ToTextDocument();

            var genInfo = new Writer() { SearchStart = textDoc.StartPoint, InsertStart = textDoc.StartPoint, SearchEnd = textDoc.EndPoint, SegmentType = Types.Region };
            if (Manager.IsAnyOutdated(genInfo)) {
                //generate text if outdated
                var extRgen = new LibraryRenderer(prj.DefaultNamespace);

                var code = extRgen.RenderToString();
                genInfo.Content = code;
                Manager.InsertOrReplace(genInfo);
                classItem.Save("");
            }

            //restore to previous state
            if (!wasOpen) {
                classItem.Document.Close(vsSaveChanges.vsSaveChangesPrompt);
            }
        }