PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net/ShowConfigCommand.cs

#
C# | 71 lines | 39 code | 7 blank | 25 comment | 3 complexity | da9aa02d967365f5bc7183b7ab0b2bb3 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. namespace Mercurial
  5. {
  6. /// <summary>
  7. /// This class implements the "hg showconfig" command (<see href="http://www.selenic.com/mercurial/hg.1.html#showconfig"/>):
  8. /// show combined config settings from all hgrc files.
  9. /// </summary>
  10. public sealed class ShowConfigCommand : MercurialCommandBase<ShowConfigCommand>, IMercurialCommand<IEnumerable<ConfigurationEntry>>
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="ShowConfigCommand"/> class.
  14. /// </summary>
  15. public ShowConfigCommand()
  16. : base("showconfig")
  17. {
  18. }
  19. #region IMercurialCommand<IEnumerable<ConfigurationEntry>> Members
  20. /// <summary>
  21. /// Gets the result of executing the command as a collection of <see cref="ConfigurationEntry"/> objects.
  22. /// </summary>
  23. public IEnumerable<ConfigurationEntry> Result
  24. {
  25. get;
  26. private set;
  27. }
  28. #endregion
  29. /// <summary>
  30. /// This method should parse and store the appropriate execution result output
  31. /// according to the type of data the command line client would return for
  32. /// the command.
  33. /// </summary>
  34. /// <param name="exitCode">
  35. /// The exit code from executing the command line client.
  36. /// </param>
  37. /// <param name="standardOutput">
  38. /// The standard output from executing the command line client.
  39. /// </param>
  40. /// <remarks>
  41. /// Note that as long as you descend from <see cref="MercurialCommandBase{T}"/> you're not required to call
  42. /// the base method at all.
  43. /// </remarks>
  44. protected override void ParseStandardOutputForResults(int exitCode, string standardOutput)
  45. {
  46. var re = new Regex(@"^(?<section>[^.]+)\.(?<name>[^=]+)=(?<value>.*)$", RegexOptions.None);
  47. var entries = new List<ConfigurationEntry>();
  48. using (var reader = new StringReader(standardOutput))
  49. {
  50. string line;
  51. while ((line = reader.ReadLine()) != null)
  52. {
  53. Match ma = re.Match(line);
  54. if (ma.Success)
  55. {
  56. entries.Add(
  57. new ConfigurationEntry(ma.Groups["section"].Value.Trim(), ma.Groups["name"].Value.Trim(), ma.Groups["value"].Value.Trim()));
  58. }
  59. }
  60. }
  61. Result = entries;
  62. }
  63. }
  64. }