|
| 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 | +} |
0 commit comments