Skip to content

Commit 6391e05

Browse files
committed
(#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.
1 parent 0907c07 commit 6391e05

File tree

2 files changed

+200
-13
lines changed

2 files changed

+200
-13
lines changed

src/chocolatey/infrastructure.app/commands/ChocolateyExportCommand.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.IO;
2121
using System.Text;
2222
using System.Xml;
23+
using System.Xml.Serialization;
2324
using chocolatey.infrastructure.app.attributes;
2425
using chocolatey.infrastructure.commandline;
2526
using chocolatey.infrastructure.app.configuration;
@@ -136,34 +137,39 @@ public void DryRun(ChocolateyConfiguration configuration)
136137

137138
public void Run(ChocolateyConfiguration configuration)
138139
{
139-
var packageResults = _nugetService.GetInstalledPackages(configuration);
140-
var settings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) };
140+
var installedPackages = _nugetService.GetInstalledPackages(configuration);
141+
var xmlWriterSettings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) };
141142

142143
FaultTolerance.TryCatchWithLoggingException(
143144
() =>
144145
{
146+
var packagesConfig = new PackagesConfigFileSettings();
147+
packagesConfig.Packages = new HashSet<PackagesConfigFilePackageSetting>();
148+
145149
using (var stringWriter = new StringWriter())
146150
{
147-
using (var xw = XmlWriter.Create(stringWriter, settings))
151+
using (var xw = XmlWriter.Create(stringWriter, xmlWriterSettings))
148152
{
149-
xw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
150-
xw.WriteStartElement("packages");
151-
152-
foreach (var packageResult in packageResults)
153+
foreach (var packageResult in installedPackages)
153154
{
154-
xw.WriteStartElement("package");
155-
xw.WriteAttributeString("id", packageResult.PackageMetadata.Id);
155+
var packageElement = new PackagesConfigFilePackageSetting
156+
{
157+
Id = packageResult.PackageMetadata.Id
158+
};
156159

157160
if (configuration.ExportCommand.IncludeVersionNumbers)
158161
{
159-
xw.WriteAttributeString("version", packageResult.PackageMetadata.Version.ToString());
162+
packageElement.Version = packageResult.PackageMetadata.Version.ToString();
160163
}
161164

162-
xw.WriteEndElement();
165+
packagesConfig.Packages.Add(packageElement);
163166
}
164167

165-
xw.WriteEndElement();
166-
xw.Flush();
168+
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
169+
ns.Add("", "");
170+
171+
var packagesConfigSerializer = new XmlSerializer(typeof(PackagesConfigFileSettings));
172+
packagesConfigSerializer.Serialize(xw, packagesConfig, ns);
167173
}
168174

169175
var fullOutputFilePath = _fileSystem.GetFullPath(configuration.ExportCommand.OutputFilePath);

src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// limitations under the License.
1616

1717
using System;
18+
using System.ComponentModel;
1819
using System.Xml.Serialization;
1920

2021
namespace chocolatey.infrastructure.app.configuration
@@ -44,46 +45,130 @@ public sealed class PackagesConfigFilePackageSetting
4445
[XmlAttribute(AttributeName = "applyPackageParametersToDependencies")]
4546
public bool ApplyPackageParametersToDependencies { get; set; }
4647

48+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
49+
public bool ApplyPackageParametersToDependenciesSpecified
50+
{
51+
get { return ApplyPackageParametersToDependencies; }
52+
}
53+
4754
[XmlAttribute(AttributeName = "applyInstallArgumentsToDependencies")]
4855
public bool ApplyInstallArgumentsToDependencies { get; set; }
4956

57+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
58+
public bool ApplyInstallArgumentsToDependenciesSpecified
59+
{
60+
get { return ApplyInstallArgumentsToDependencies; }
61+
}
62+
5063
[XmlAttribute(AttributeName = "forceX86")]
5164
public bool ForceX86 { get; set; }
5265

66+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
67+
public bool ForceX86Specified
68+
{
69+
get { return ForceX86; }
70+
}
71+
5372
[XmlAttribute(AttributeName = "ignoreDependencies")]
5473
public bool IgnoreDependencies { get; set; }
5574

75+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
76+
public bool IgnoreDependenciesSpecified
77+
{
78+
get { return IgnoreDependencies; }
79+
}
80+
5681
[XmlAttribute(AttributeName = "disabled")]
5782
public bool Disabled { get; set; }
5883

84+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
85+
public bool DisabledSpecified
86+
{
87+
get { return Disabled; }
88+
}
89+
5990
[XmlAttribute(AttributeName = "pinPackage")]
6091
public bool PinPackage { get; set; }
6192

93+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
94+
public bool PinPackageSpecified
95+
{
96+
get { return PinPackage; }
97+
}
98+
6299
[System.ComponentModel.DefaultValue(-1)]
63100
[XmlAttribute(AttributeName = "executionTimeout")]
64101
public int ExecutionTimeout { get; set; }
65102

103+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
104+
public bool ExecutionTimeoutSpecified
105+
{
106+
get { return ExecutionTimeout != 0; }
107+
}
108+
66109
[XmlAttribute(AttributeName = "force")]
67110
public bool Force { get; set; }
68111

112+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
113+
public bool ForceSpecified
114+
{
115+
get { return Force; }
116+
}
117+
69118
[XmlAttribute(AttributeName = "prerelease")]
70119
public bool Prerelease { get; set; }
71120

121+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
122+
public bool PrereleaseSpecified
123+
{
124+
get { return Prerelease; }
125+
}
126+
72127
[XmlAttribute(AttributeName = "overrideArguments")]
73128
public bool OverrideArguments { get; set; }
74129

130+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
131+
public bool OverrideArgumentsSpecified
132+
{
133+
get { return OverrideArguments; }
134+
}
135+
75136
[XmlAttribute(AttributeName = "notSilent")]
76137
public bool NotSilent { get; set; }
77138

139+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
140+
public bool NotSilentSpecified
141+
{
142+
get { return NotSilent; }
143+
}
144+
78145
[XmlAttribute(AttributeName = "allowDowngrade")]
79146
public bool AllowDowngrade { get; set; }
80147

148+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
149+
public bool AllowDowngradeSpecified
150+
{
151+
get { return AllowDowngrade; }
152+
}
153+
81154
[XmlAttribute(AttributeName = "forceDependencies")]
82155
public bool ForceDependencies { get; set; }
83156

157+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
158+
public bool ForceDependenciesSpecified
159+
{
160+
get { return ForceDependencies; }
161+
}
162+
84163
[XmlAttribute(AttributeName = "skipAutomationScripts")]
85164
public bool SkipAutomationScripts { get; set; }
86165

166+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
167+
public bool SkipAutomationScriptsSpecified
168+
{
169+
get { return SkipAutomationScripts; }
170+
}
171+
87172
[XmlAttribute(AttributeName = "user")]
88173
public string User { get; set; }
89174

@@ -99,15 +184,39 @@ public sealed class PackagesConfigFilePackageSetting
99184
[XmlAttribute(AttributeName = "ignoreChecksums")]
100185
public bool IgnoreChecksums { get; set; }
101186

187+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
188+
public bool IgnoreChecksumsSpecified
189+
{
190+
get { return IgnoreChecksums; }
191+
}
192+
102193
[XmlAttribute(AttributeName = "allowEmptyChecksums")]
103194
public bool AllowEmptyChecksums { get; set; }
104195

196+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
197+
public bool AllowEmptyChecksumsSpecified
198+
{
199+
get { return AllowEmptyChecksums; }
200+
}
201+
105202
[XmlAttribute(AttributeName = "allowEmptyChecksumsSecure")]
106203
public bool AllowEmptyChecksumsSecure { get; set; }
107204

205+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
206+
public bool AllowEmptyChecksumsSecureSpecified
207+
{
208+
get { return AllowEmptyChecksumsSecure; }
209+
}
210+
108211
[XmlAttribute(AttributeName = "requireChecksums")]
109212
public bool RequireChecksums { get; set; }
110213

214+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
215+
public bool RequireChecksumsSpecified
216+
{
217+
get { return RequireChecksums; }
218+
}
219+
111220
[XmlAttribute(AttributeName = "downloadChecksum")]
112221
public string DownloadChecksum { get; set; }
113222

@@ -123,40 +232,112 @@ public sealed class PackagesConfigFilePackageSetting
123232
[XmlAttribute(AttributeName = "ignorePackageExitCodes")]
124233
public bool IgnorePackageExitCodes { get; set; }
125234

235+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
236+
public bool IgnorePackageExitCodesSpecified
237+
{
238+
get { return IgnorePackageExitCodes; }
239+
}
240+
126241
[XmlAttribute(AttributeName = "usePackageExitCodes")]
127242
public bool UsePackageExitCodes { get; set; }
128243

244+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
245+
public bool UsePackageExitCodesSpecified
246+
{
247+
get { return UsePackageExitCodes; }
248+
}
249+
129250
[XmlAttribute(AttributeName = "stopOnFirstFailure")]
130251
public bool StopOnFirstFailure { get; set; }
131252

253+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
254+
public bool StopOnFirstFailureSpecified
255+
{
256+
get { return StopOnFirstFailure; }
257+
}
258+
132259
[XmlAttribute(AttributeName = "exitWhenRebootDetected")]
133260
public bool ExitWhenRebootDetected { get; set; }
134261

262+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
263+
public bool ExitWhenRebootDetectedSpecified
264+
{
265+
get { return ExitWhenRebootDetected; }
266+
}
267+
135268
[XmlAttribute(AttributeName = "ignoreDetectedReboot")]
136269
public bool IgnoreDetectedReboot { get; set; }
137270

271+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
272+
public bool IgnoreDetectedRebootSpecified
273+
{
274+
get { return IgnoreDetectedReboot; }
275+
}
276+
138277
[XmlAttribute(AttributeName = "disableRepositoryOptimizations")]
139278
public bool DisableRepositoryOptimizations { get; set; }
140279

280+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
281+
public bool DisableRepositoryOptimizationsSpecified
282+
{
283+
get { return DisableRepositoryOptimizations; }
284+
}
285+
141286
[XmlAttribute(AttributeName = "acceptLicense")]
142287
public bool AcceptLicense { get; set; }
143288

289+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
290+
public bool AcceptLicenseSpecified
291+
{
292+
get { return AcceptLicense; }
293+
}
294+
144295
[XmlAttribute(AttributeName = "confirm")]
145296
public bool Confirm { get; set; }
146297

298+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
299+
public bool ConfirmSpecified
300+
{
301+
get { return Confirm; }
302+
}
303+
147304
[XmlAttribute(AttributeName = "limitOutput")]
148305
public bool LimitOutput { get; set; }
149306

307+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
308+
public bool LimitOutputSpecified
309+
{
310+
get { return LimitOutput; }
311+
}
312+
150313
[XmlAttribute(AttributeName = "cacheLocation")]
151314
public string CacheLocation { get; set; }
152315

153316
[XmlAttribute(AttributeName = "failOnStderr")]
154317
public bool FailOnStderr { get; set; }
155318

319+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
320+
public bool FailOnStderrSpecified
321+
{
322+
get { return FailOnStderr; }
323+
}
324+
156325
[XmlAttribute(AttributeName = "useSystemPowershell")]
157326
public bool UseSystemPowershell { get; set; }
158327

328+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
329+
public bool UseSystemPowershellSpecified
330+
{
331+
get { return UseSystemPowershell; }
332+
}
333+
159334
[XmlAttribute(AttributeName = "noProgress")]
160335
public bool NoProgress { get; set; }
336+
337+
[XmlIgnore, EditorBrowsable(EditorBrowsableState.Never)]
338+
public bool NoProgressSpecified
339+
{
340+
get { return NoProgress; }
341+
}
161342
}
162343
}

0 commit comments

Comments
 (0)