Skip to content

Commit

Permalink
(chocolatey#2503) Export via serialization of class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheCakeIsNaOH committed Apr 28, 2024
1 parent 85ab916 commit 0ebf572
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using chocolatey.infrastructure.app.attributes;
using chocolatey.infrastructure.commandline;
using chocolatey.infrastructure.app.configuration;
Expand Down Expand Up @@ -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<PackagesConfigFilePackageSetting>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.

using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace chocolatey.infrastructure.app.configuration
Expand Down Expand Up @@ -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; }

Expand All @@ -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; }

Expand All @@ -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; }
}
}
}

0 comments on commit 0ebf572

Please sign in to comment.