/// <summary> /// Initializes a new instance of the <see cref="Project"/> class. /// </summary> /// <param name="serializer">The serializer to use for saving the project.</param> /// <param name="buildSettings">Contains build-related settings.</param> protected Project(ProjectSerializer serializer, BuildSettings buildSettings) { Tracer.VerifyNonNullArgument(serializer, "serializer"); Tracer.VerifyNonNullArgument(buildSettings, "buildSettings"); this.serializer = serializer; this.buildSettings = buildSettings; }
//========================================================================================== // Methods //========================================================================================== /// <summary> /// Applies the changes made on the property page to the bound objects. /// </summary> /// <returns> /// true if the changes were successfully applied and the property page is current with the bound objects; /// false if the changes were applied, but the property page cannot determine if its state is current with the objects. /// </returns> protected override bool ApplyChanges() { if (this.clonedBoundObject == null) { return false; } // Unbind from the cloned object this.UnbindObject(); // Apply the changes on the cloned object back to the real object this.Project.BuildSettings = this.clonedBoundObject; // Reclone the object by setting it to null (it will be recloned in get_BoundObject) this.clonedBoundObject = null; this.BindObject(); return true; }
/// <summary> /// Saves the attached project in the specified encoding. /// </summary> /// <param name="encoding">The encoding of the file. If null, <see cref="Encoding.UTF8"/> is used.</param> /// <param name="forceSave">Indicates whether to ignore the attached project's dirty flag when determining whether to save.</param> /// <returns>true if successful; otherwise, false.</returns> public bool Save(Encoding encoding, bool forceSave) { if (encoding == null) { encoding = Encoding.UTF8; } // If a project hasn't been attached yet, there's nothing to save. if (this.Project == null) { return(false); } // Check the dirty state of the project to see if we even need to save. if (!this.Project.IsDirty && !forceSave) { Tracer.WriteLineInformation(classType, "Save", "The project doesn't need to be saved."); return(true); } // At this point we know we have to save the project. string filePath = this.Project.FilePath; try { using (StreamWriter streamWriter = new StreamWriter(filePath, false, encoding)) { ProjectFileXmlWriter writer = new ProjectFileXmlWriter(streamWriter); writer.WriteStartDocument(); // <VisualStudioProject> writer.WriteStartElement(ElementNames.VisualStudioProject); // <Project> writer.WriteStartElement(this.ProjectElementName); this.WriteProjectAttributes(writer); // <BuildSettings> BuildSettings buildSettings = this.Project.BuildSettings; writer.WriteStartElement(ElementNames.BuildSettings); this.WriteBuildSettingsAttributes(writer); writer.WriteEndElement(); // <Configurations> writer.WriteStartElement(ElementNames.Configurations); foreach (ProjectConfiguration config in this.Project.ConfigurationProvider.ProjectConfigurations) { this.WriteConfigurationNode(writer, config); } writer.WriteEndElement(); // <References> writer.WriteStartElement(this.ReferencesElementName); foreach (ReferenceFileNode reference in this.Project.ReferencesNode.Children) { writer.WriteStartElement(this.ReferenceElementName); writer.WriteAttributeString(AttributeNames.RelativePath, reference.RelativePath); writer.WriteEndElement(); } writer.WriteEndElement(); // <Files> writer.WriteStartElement(ElementNames.Files); this.WriteFilesInNode(writer, this.Project.RootNode); writer.WriteEndElement(); writer.WriteEndDocument(); // Clear the project's dirty state. this.Project.ClearDirty(); } } catch (Exception e) { if (!this.SilentFailures) { string title = Package.Instance.Context.NativeResources.GetString(ResId.IDS_E_PROJECTFILESAVE_TITLE, filePath); string message = Package.Instance.Context.NativeResources.GetString(ResId.IDS_E_PROJECTFILESAVE, e.Message); Package.Instance.Context.ShowErrorMessageBox(title, message); } Tracer.Fail("There was an error in saving the file {0}: {1}", filePath, e.ToString()); return(false); } return(true); }