PageRenderTime 30ms CodeModel.GetById 26ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Mercurial.Net/ShowConfigCommand.cs

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