/// <summary> /// Writes the <Configuration> node for the specified configuration. /// </summary> /// <param name="writer">The <see cref="ProjectFileXmlWriter"/> to use.</param> /// <param name="configuration">The project configuration to write.</param> protected void WriteConfigurationNode(ProjectFileXmlWriter writer, ProjectConfiguration configuration) { writer.WriteStartElement(ElementNames.Configuration); writer.WriteAttributeString(AttributeNames.Name, configuration.Name); writer.WriteAttributeString(AttributeNames.RelativeOutputDirectory, configuration.RelativeOutputDirectory); writer.WriteAttributeString(AttributeNames.RelativeIntermediateDirectory, configuration.RelativeIntermediateDirectory); writer.WriteEndElement(); }
/// <summary> /// Recursively writes all of the <File> nodes to the project file, one for each file /// in the parent node. /// </summary> /// <param name="writer">The file to which to write.</param> /// <param name="parent">The parent node to write.</param> protected void WriteFilesInNode(ProjectFileXmlWriter writer, FolderNode parent) { foreach (Node node in parent.Children) { if (node.IsFolder && !node.IsVirtual) { FolderNode folderNode = (FolderNode)node; // Recurse this.WriteFilesInNode(writer, folderNode); } else if (node.IsFile) { writer.WriteStartElement(ElementNames.File); writer.WriteAttributeString(AttributeNames.RelativePath, node.RelativePath); writer.WriteEndElement(); } } }
/// <summary> /// Writes the attributes for the project root node. /// </summary> /// <param name="writer">The writer to use.</param> protected virtual void WriteProjectAttributes(ProjectFileXmlWriter writer) { writer.WriteAttributeString(AttributeNames.ProductVersion, Package.Instance.ProductVersion.ToString()); writer.WriteAttributeString(AttributeNames.ProjectGuid, this.Project.ProjectGuid.ToString("B").ToUpper(CultureInfo.InvariantCulture)); writer.WriteAttributeString(AttributeNames.SchemaVersion, this.SchemaVersion.ToString()); }
/// <summary> /// Writes the attributes for the project root node. /// </summary> /// <param name="writer">The <see cref="ProjectFileXmlWriter"/> to use.</param> protected virtual void WriteBuildSettingsAttributes(ProjectFileXmlWriter writer) { writer.WriteAttributeString(AttributeNames.OutputName, this.Project.BuildSettings.OutputName); }
/// <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; }
/// <summary> /// Writes the attributes for the project root node. /// </summary> /// <param name="writer">The writer to use.</param> protected override void WriteBuildSettingsAttributes(ProjectFileXmlWriter writer) { base.WriteBuildSettingsAttributes(writer); WixBuildSettings buildSettings = this.Project.BuildSettings; writer.WriteAttributeString(WixAttributeNames.OutputType, buildSettings.OutputType.ToString()); }
/// <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); }