Skip to content

Commit 9ca6f01

Browse files
committed
(chocolateyGH-357) Initial working version of Export Command
- This will need some refactoring, but at least functional - This was the output from the live-stream that was done here: https://www.youtube.com/watch?v=jFozwL8qizU
1 parent 8daccd4 commit 9ca6f01

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

src/chocolatey/chocolatey.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Link>Properties\SolutionVersion.cs</Link>
102102
</Compile>
103103
<Compile Include="AssemblyExtensions.cs" />
104+
<Compile Include="infrastructure.app\commands\ChocolateyExportCommand.cs" />
104105
<Compile Include="infrastructure.app\commands\ChocolateyInfoCommand.cs" />
105106
<Compile Include="infrastructure.app\configuration\EnvironmentSettings.cs" />
106107
<Compile Include="infrastructure.app\domain\GenericRegistryKey.cs" />
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Xml;
5+
using chocolatey.infrastructure.app.attributes;
6+
using chocolatey.infrastructure.app.configuration;
7+
using chocolatey.infrastructure.app.services;
8+
using chocolatey.infrastructure.commandline;
9+
using chocolatey.infrastructure.commands;
10+
11+
// TODO: Don't output list of packages to console
12+
// TODO: Update top level help document with export command
13+
namespace chocolatey.infrastructure.app.commands
14+
{
15+
[CommandFor("export", "exports list of currently installed packages")]
16+
public class ChocolateyExportCommand : ICommand
17+
{
18+
private readonly INugetService _nugetService;
19+
20+
public ChocolateyExportCommand(INugetService nugetService)
21+
{
22+
_nugetService = nugetService;
23+
}
24+
25+
public void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
26+
{
27+
// TODO: Add optionset for two new arguments
28+
}
29+
30+
public void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
31+
{
32+
if (unparsedArguments.Count > 0)
33+
{
34+
throw new ApplicationException("Please see the help menu for the export command");
35+
}
36+
37+
configuration.ListCommand.LocalOnly = true;
38+
39+
// TODO: Handle parsing of --output-file-name, --include-version-numbers
40+
}
41+
42+
public void handle_validation(ChocolateyConfiguration configuration)
43+
{
44+
}
45+
46+
public void help_message(ChocolateyConfiguration configuration)
47+
{
48+
// TODO: Add actual help command
49+
this.Log().Info("Export Command");
50+
}
51+
52+
public bool may_require_admin_access()
53+
{
54+
return false;
55+
}
56+
57+
public void noop(ChocolateyConfiguration configuration)
58+
{
59+
// TODO: Implement this
60+
throw new NotImplementedException();
61+
}
62+
63+
public void run(ChocolateyConfiguration configuration)
64+
{
65+
var packageResults = _nugetService.list_run(configuration).ToList();
66+
67+
var settings = new XmlWriterSettings { Indent = true };
68+
69+
// TODO: Use FileSystem abstraction, rather than direct
70+
using (var xw = XmlWriter.Create("c:/temp/packages.config", settings))
71+
{
72+
xw.WriteStartDocument();
73+
xw.WriteStartElement("packages");
74+
75+
foreach (var packageResult in packageResults)
76+
{
77+
xw.WriteStartElement("package");
78+
xw.WriteAttributeString("id", packageResult.Package.Id);
79+
// TODO: Add parameter to specify whether to include version number or not
80+
//xw.WriteAttributeString("version", package.Version.ToString());
81+
xw.WriteEndElement();
82+
}
83+
84+
xw.WriteEndElement();
85+
}
86+
}
87+
}
88+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public ChocolateyConfiguration()
5050
PinCommand = new PinCommandConfiguration();
5151
OutdatedCommand = new OutdatedCommandConfiguration();
5252
Proxy = new ProxyConfiguration();
53+
ExportCommand = new ExportCommandConfiguration();
5354
#if DEBUG
5455
AllowUnofficialBuild = true;
5556
#endif
@@ -334,6 +335,8 @@ private void append_output(StringBuilder propertyValues, string append)
334335
/// </remarks>
335336
public OutdatedCommandConfiguration OutdatedCommand { get; set; }
336337

338+
public ExportCommandConfiguration ExportCommand { get; set; }
339+
337340
/// <summary>
338341
/// Configuration related specifically to proxies.
339342
/// </summary>
@@ -538,4 +541,9 @@ public sealed class ProxyConfiguration
538541
public string BypassList { get; set; }
539542
public bool BypassOnLocal { get; set; }
540543
}
544+
545+
[Serializable]
546+
public sealed class ExportCommandConfiguration
547+
{
548+
}
541549
}

src/chocolatey/infrastructure.app/registration/ContainerBinding.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public void RegisterComponents(Container container)
9292
new ChocolateyApiKeyCommand(container.GetInstance<IChocolateyConfigSettingsService>()),
9393
new ChocolateyUnpackSelfCommand(container.GetInstance<IFileSystem>()),
9494
new ChocolateyVersionCommand(container.GetInstance<IChocolateyPackageService>()),
95-
new ChocolateyUpdateCommand(container.GetInstance<IChocolateyPackageService>())
95+
new ChocolateyUpdateCommand(container.GetInstance<IChocolateyPackageService>()),
96+
new ChocolateyExportCommand(container.GetInstance<INugetService>())
9697
};
9798
return list.AsReadOnly();
9899
}, Lifestyle.Singleton);

0 commit comments

Comments
 (0)