Update README.txt
[GitSharp.git] / Git / CommandCatalog.cs
blobe0b76cc78a7a027301220597a494f191ba4c59e1
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3 * Copyright (C) 2009, Rolenun <rolenun@gmail.com>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 using System;
40 using System.Collections;
41 using System.Collections.Generic;
42 using System.Collections.ObjectModel;
43 using System.IO;
44 using System.Reflection;
45 using System.Text;
46 using System.Xml;
47 using GitSharp;
49 namespace GitSharp.CLI
52 /// <summary>
53 /// List of all commands known by the command line tools.
54 /// Commands are implementations of the TextBuiltin class, with a required
55 /// command attribute to insert additional documentation and add some extra
56 /// information such as if the command is common and completed.
57 ///
58 /// Commands may be registered by adding them to the Commands.xml file.
59 /// The Commands.xml file should contain:
60 /// a. The command name including namespace.
61 /// b. The website address for command specific online help.(optional)
62 /// </summary>
63 public class CommandCatalog
65 /// <summary>
66 /// Stores the command catalog.
67 /// </summary>
68 private SortedList<String, CommandRef> commands = new SortedList<string, CommandRef>();
70 /// <summary>
71 /// Creates the command catalog from the Commands.xml file.
72 /// </summary>
73 public CommandCatalog()
75 const string commandsXmlPath = "GitSharp.CLI.Resources.Commands.xml";
76 Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(commandsXmlPath);
77 var doc = new XmlDocument();
78 doc.Load(fileStream);
80 XmlNodeList xmlNodeList = doc.SelectNodes("/root/CommandList/Command");
81 foreach (XmlNode node in xmlNodeList)
83 XmlElement nameElement = node["Name"];
84 XmlElement helpElement = node["Help"];
85 if (nameElement != null)
86 Load(nameElement.InnerText, helpElement.InnerText);
90 /// <summary>
91 /// Returns all commands starting with a specified string, sorted by command name.
92 /// </summary>
93 public List<CommandRef> StartsWith(String s)
95 List<CommandRef> matches = new List<CommandRef>();
96 foreach (CommandRef c in commands.Values)
98 if (c.getName().StartsWith(s))
99 matches.Add(c);
102 return toSortedArray(matches);
105 /// <summary>
106 /// Create and loads the command name into the command catalog.
107 /// </summary>
108 /// <param name="commandName">Specifies the command name to load.</param>
109 /// <param name="commandHelp">Specifies the command's website for faster reference.</param>
110 public void Load(String commandName, String commandHelp)
112 TextBuiltin clazz;
114 Type commandType = Type.GetType(commandName);
115 if (commandType == null)
116 return;
118 clazz = Activator.CreateInstance(commandType) as TextBuiltin;
119 if (clazz == null)
120 return;
122 int index = clazz.ToString().LastIndexOf(".");
123 string cmdName = clazz.ToString().Substring(index + 1).ToLower();
124 clazz.setCommandName(cmdName);
125 clazz.setCommandHelp(commandHelp);
127 CommandRef cr = new CommandRef(clazz);
128 if (cr != null)
129 commands.Add(cr.getName(), cr);
132 /// <summary>
133 /// Locates a single command by its user friendly name.
134 /// </summary>
135 /// <param name="name">Specifies the name of the command.</param>
136 /// <returns>Returns the CommandRef containing the command's information.</returns>
137 public CommandRef Get(String name)
139 CommandRef value = null;
140 commands.TryGetValue(name, out value);
141 return value;
144 /// <summary>
145 /// Returns all known commands, sorted by command name.
146 /// </summary>
147 public IList<CommandRef> All()
149 return commands.Values;
152 /// <summary>
153 /// Returns all common commands, sorted by command name.
154 /// </summary>
155 public List<CommandRef> Common()
157 List<CommandRef> common = new List<CommandRef>();
158 foreach (CommandRef c in commands.Values)
160 if (c.isCommon())
161 common.Add(c);
164 return toSortedArray(common);
167 /// <summary>
168 /// Returns all incomplete commands, sorted by command name.
169 /// </summary>
170 public List<CommandRef> Incomplete()
172 List<CommandRef> incomplete = new List<CommandRef>();
173 foreach (CommandRef c in commands.Values)
175 if (!c.isComplete())
176 incomplete.Add(c);
179 return toSortedArray(incomplete);
182 /// <summary>
183 /// Returns all complete commands, sorted by command name.
184 /// </summary>
185 public List<CommandRef> Complete()
187 List<CommandRef> complete = new List<CommandRef>();
188 foreach (CommandRef c in commands.Values)
190 if (c.isComplete())
191 complete.Add(c);
194 return toSortedArray(complete);
197 /// <summary>
198 /// Sorts a list of specified commands by command name.
199 /// </summary>
200 /// <param name="c">Specifies the list of commands to be sorted.</param>
201 /// <returns>Returns the sorted list of commands.</returns>
202 private List<CommandRef> toSortedArray(List<CommandRef> c)
204 c.Sort(
205 delegate(CommandRef ref1, CommandRef ref2)
207 return ref1.getName().CompareTo(ref2.getName());
210 return c;