/// <summary> /// Create the library file that contains INotifier and Notification extensions /// has to be created before EnvDte can add the interface <see cref="RgenLib.Extensions.General.AddInterfaceIfNotExists"/> to classes /// </summary> /// <remarks> /// /// </remarks> public void RenderLibrary() { //Check for existing class var prj = ProjectItem.Project; const string className = NotifyPropertyLibrary.DefaultClassName; var classes = prj.GetCodeElements<CodeClass>(); var classFullname = prj.DefaultNamespace.DotJoin(className); List<CodeClass> matchingClass; classes.TryGetValue(classFullname, out matchingClass); ProjectItem classItem; if (matchingClass == 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 = matchingClass.First().ProjectItem; } //Open file for editing var wasOpen = classItem.IsOpen[Constants.vsViewKindCode]; if (!wasOpen) { classItem.Open(Constants.vsViewKindCode); } var textDoc = classItem.Document.ToTextDocument(); var writer = new ManagerType.Writer(_manager) { TargetRange = new TaggedRange { StartPoint = textDoc.StartPoint, EndPoint = textDoc.EndPoint, SegmentType = SegmentTypes.Region }, InsertStart = textDoc.StartPoint }; if (ManagerType.GeneratedSegment.IsAnyOutdated(writer)) { //generate text if outdated var code = new NotifyPropertyLibrary(prj.DefaultNamespace).RenderToString(); writer.Content = code; writer.InsertOrReplace(AlwaysInsert); classItem.Save(); } //restore to previous state. Close if was not open initially if (!wasOpen) { classItem.Document.Close(); } }
/// <summary> /// Create the library file that contains INotifier and Notification extensions /// has to be created before EnvDte can add the interface <see cref="RgenLib.Extensions.General.AddInterfaceIfNotExists"/> to classes /// </summary> /// <remarks> /// /// </remarks> public void RenderLibrary() { //Check for existing class var prj = ProjectItem.Project; const string className = NotifyPropertyLibrary.DefaultClassName; var classes = prj.GetCodeElements <CodeClass>(); var classFullname = prj.DefaultNamespace.DotJoin(className); List <CodeClass> matchingClass; classes.TryGetValue(classFullname, out matchingClass); ProjectItem classItem; if (matchingClass == 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 = matchingClass.First().ProjectItem; } //Open file for editing var wasOpen = classItem.IsOpen[Constants.vsViewKindCode]; if (!wasOpen) { classItem.Open(Constants.vsViewKindCode); } var textDoc = classItem.Document.ToTextDocument(); var writer = new ManagerType.Writer(_manager) { TargetRange = new TaggedRange { StartPoint = textDoc.StartPoint, EndPoint = textDoc.EndPoint, SegmentType = SegmentTypes.Region }, InsertStart = textDoc.StartPoint }; if (ManagerType.GeneratedSegment.IsAnyOutdated(writer)) { //generate text if outdated var code = new NotifyPropertyLibrary(prj.DefaultNamespace).RenderToString(); writer.Content = code; writer.InsertOrReplace(AlwaysInsert); classItem.Save(); } //restore to previous state. Close if was not open initially if (!wasOpen) { classItem.Document.Close(); } }