From 895dd2c24dc7bfc8bcb43b084d243e3dca1e1b96 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Fri, 23 Sep 2022 13:53:58 -0500 Subject: [PATCH] (#2503) Export via serialization of class Instead of exporting via building an xml document manually, this creates a PackagesConfigFilePackageSetting and serializes that to create the xml document that is saved. This allows for usage of the same class as is used to read in packages.config files to export those files. The reason the various "Specified" members are added to the PackagesConfigFilePackageSetting class is so if an element is not set during export, it will not show up at all in the resulting serialized packages.config file. --- .../commands/ChocolateyExportCommand.cs | 32 +-- .../PackagesConfigFilePackageSetting.cs | 183 +++++++++++++++++- 2 files changed, 201 insertions(+), 14 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyExportCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyExportCommand.cs index ce8fd569a5..9c1b8e6234 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyExportCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyExportCommand.cs @@ -22,6 +22,7 @@ namespace chocolatey.infrastructure.app.commands using System.IO; using System.Text; using System.Xml; + using System.Xml.Serialization; using attributes; using commandline; using configuration; @@ -136,34 +137,39 @@ public void DryRun(ChocolateyConfiguration configuration) public void Run(ChocolateyConfiguration configuration) { - var packageResults = _nugetService.GetInstalledPackages(configuration); - var settings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) }; + var installedPackages = _nugetService.GetInstalledPackages(configuration); + var xmlWriterSettings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) }; FaultTolerance.TryCatchWithLoggingException( () => { + var packagesConfig = new PackagesConfigFileSettings(); + packagesConfig.Packages = new HashSet(); + using (var stringWriter = new StringWriter()) { - using (var xw = XmlWriter.Create(stringWriter, settings)) + using (var xw = XmlWriter.Create(stringWriter, xmlWriterSettings)) { - xw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); - xw.WriteStartElement("packages"); - - foreach (var packageResult in packageResults) + foreach (var packageResult in installedPackages) { - xw.WriteStartElement("package"); - xw.WriteAttributeString("id", packageResult.PackageMetadata.Id); + var packageElement = new PackagesConfigFilePackageSetting + { + Id = packageResult.PackageMetadata.Id + }; if (configuration.ExportCommand.IncludeVersionNumbers) { - xw.WriteAttributeString("version", packageResult.PackageMetadata.Version.ToString()); + packageElement.Version = packageResult.PackageMetadata.Version.ToString(); } - xw.WriteEndElement(); + packagesConfig.Packages.Add(packageElement); } - xw.WriteEndElement(); - xw.Flush(); + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add("", ""); + + var packagesConfigSerializer = new XmlSerializer(typeof(PackagesConfigFileSettings)); + packagesConfigSerializer.Serialize(xw, packagesConfig, ns); } var fullOutputFilePath = _fileSystem.GetFullPath(configuration.ExportCommand.OutputFilePath); diff --git a/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs b/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs index 5c4e8d5c1c..13d86d1d67 100644 --- a/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs +++ b/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs @@ -17,6 +17,7 @@ namespace chocolatey.infrastructure.app.configuration { using System; + using System.ComponentModel; using System.Xml.Serialization; /// @@ -44,46 +45,130 @@ public sealed class PackagesConfigFilePackageSetting [XmlAttribute(AttributeName = "applyPackageParametersToDependencies")] public bool ApplyPackageParametersToDependencies { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ApplyPackageParametersToDependenciesSpecified + { + get { return ApplyPackageParametersToDependencies; } + } + [XmlAttribute(AttributeName = "applyInstallArgumentsToDependencies")] public bool ApplyInstallArgumentsToDependencies { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ApplyInstallArgumentsToDependenciesSpecified + { + get { return ApplyInstallArgumentsToDependencies; } + } + [XmlAttribute(AttributeName = "forceX86")] public bool ForceX86 { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ForceX86Specified + { + get { return ForceX86; } + } + [XmlAttribute(AttributeName = "ignoreDependencies")] public bool IgnoreDependencies { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool IgnoreDependenciesSpecified + { + get { return IgnoreDependencies; } + } + [XmlAttribute(AttributeName = "disabled")] public bool Disabled { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool DisabledSpecified + { + get { return Disabled; } + } + [XmlAttribute(AttributeName = "pinPackage")] public bool PinPackage { get; set; } - + + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool PinPackageSpecified + { + get { return PinPackage; } + } + [System.ComponentModel.DefaultValue(-1)] [XmlAttribute(AttributeName = "executionTimeout")] public int ExecutionTimeout { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ExecutionTimeoutSpecified + { + get { return ExecutionTimeout != 0; } + } + [XmlAttribute(AttributeName = "force")] public bool Force { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ForceSpecified + { + get { return Force; } + } + [XmlAttribute(AttributeName = "prerelease")] public bool Prerelease { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool PrereleaseSpecified + { + get { return Prerelease; } + } + [XmlAttribute(AttributeName = "overrideArguments")] public bool OverrideArguments { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool OverrideArgumentsSpecified + { + get { return OverrideArguments; } + } + [XmlAttribute(AttributeName = "notSilent")] public bool NotSilent { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool NotSilentSpecified + { + get { return NotSilent; } + } + [XmlAttribute(AttributeName = "allowDowngrade")] public bool AllowDowngrade { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool AllowDowngradeSpecified + { + get { return AllowDowngrade; } + } + [XmlAttribute(AttributeName = "forceDependencies")] public bool ForceDependencies { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ForceDependenciesSpecified + { + get { return ForceDependencies; } + } + [XmlAttribute(AttributeName = "skipAutomationScripts")] public bool SkipAutomationScripts { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool SkipAutomationScriptsSpecified + { + get { return SkipAutomationScripts; } + } + [XmlAttribute(AttributeName = "user")] public string User { get; set; } @@ -99,15 +184,39 @@ public sealed class PackagesConfigFilePackageSetting [XmlAttribute(AttributeName = "ignoreChecksums")] public bool IgnoreChecksums { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool IgnoreChecksumsSpecified + { + get { return IgnoreChecksums; } + } + [XmlAttribute(AttributeName = "allowEmptyChecksums")] public bool AllowEmptyChecksums { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool AllowEmptyChecksumsSpecified + { + get { return AllowEmptyChecksums; } + } + [XmlAttribute(AttributeName = "allowEmptyChecksumsSecure")] public bool AllowEmptyChecksumsSecure { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool AllowEmptyChecksumsSecureSpecified + { + get { return AllowEmptyChecksumsSecure; } + } + [XmlAttribute(AttributeName = "requireChecksums")] public bool RequireChecksums { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool RequireChecksumsSpecified + { + get { return RequireChecksums; } + } + [XmlAttribute(AttributeName = "downloadChecksum")] public string DownloadChecksum { get; set; } @@ -123,40 +232,112 @@ public sealed class PackagesConfigFilePackageSetting [XmlAttribute(AttributeName = "ignorePackageExitCodes")] public bool IgnorePackageExitCodes { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool IgnorePackageExitCodesSpecified + { + get { return IgnorePackageExitCodes; } + } + [XmlAttribute(AttributeName = "usePackageExitCodes")] public bool UsePackageExitCodes { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool UsePackageExitCodesSpecified + { + get { return UsePackageExitCodes; } + } + [XmlAttribute(AttributeName = "stopOnFirstFailure")] public bool StopOnFirstFailure { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool StopOnFirstFailureSpecified + { + get { return StopOnFirstFailure; } + } + [XmlAttribute(AttributeName = "exitWhenRebootDetected")] public bool ExitWhenRebootDetected { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ExitWhenRebootDetectedSpecified + { + get { return ExitWhenRebootDetected; } + } + [XmlAttribute(AttributeName = "ignoreDetectedReboot")] public bool IgnoreDetectedReboot { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool IgnoreDetectedRebootSpecified + { + get { return IgnoreDetectedReboot; } + } + [XmlAttribute(AttributeName = "disableRepositoryOptimizations")] public bool DisableRepositoryOptimizations { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool DisableRepositoryOptimizationsSpecified + { + get { return DisableRepositoryOptimizations; } + } + [XmlAttribute(AttributeName = "acceptLicense")] public bool AcceptLicense { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool AcceptLicenseSpecified + { + get { return AcceptLicense; } + } + [XmlAttribute(AttributeName = "confirm")] public bool Confirm { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool ConfirmSpecified + { + get { return Confirm; } + } + [XmlAttribute(AttributeName = "limitOutput")] public bool LimitOutput { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool LimitOutputSpecified + { + get { return LimitOutput; } + } + [XmlAttribute(AttributeName = "cacheLocation")] public string CacheLocation { get; set; } [XmlAttribute(AttributeName = "failOnStderr")] public bool FailOnStderr { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool FailOnStderrSpecified + { + get { return FailOnStderr; } + } + [XmlAttribute(AttributeName = "useSystemPowershell")] public bool UseSystemPowershell { get; set; } + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool UseSystemPowershellSpecified + { + get { return UseSystemPowershell; } + } + [XmlAttribute(AttributeName = "noProgress")] public bool NoProgress { get; set; } + + [XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)] + public bool NoProgressSpecified + { + get { return NoProgress; } + } } }