2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3 * Copyright (C) 2009, Rolenun <rolenun@gmail.com>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
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
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.
40 using System
.Collections
;
41 using System
.Collections
.Generic
;
42 using System
.Collections
.ObjectModel
;
44 using System
.Reflection
;
49 namespace GitSharp
.CLI
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.
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)
63 public class CommandCatalog
66 /// Stores the command catalog.
68 private SortedList
<String
, CommandRef
> commands
= new SortedList
<string, CommandRef
>();
71 /// Creates the command catalog from the Commands.xml file.
73 public CommandCatalog()
75 const string commandsXmlPath
= "GitSharp.CLI.Resources.Commands.xml";
76 Stream fileStream
= Assembly
.GetExecutingAssembly().GetManifestResourceStream(commandsXmlPath
);
77 var doc
= new XmlDocument();
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
);
91 /// Returns all commands starting with a specified string, sorted by command name.
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
))
102 return toSortedArray(matches
);
106 /// Create and loads the command name into the command catalog.
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
)
114 Type commandType
= Type
.GetType(commandName
);
115 if (commandType
== null)
118 clazz
= Activator
.CreateInstance(commandType
) as TextBuiltin
;
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
);
129 commands
.Add(cr
.getName(), cr
);
133 /// Locates a single command by its user friendly name.
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);
145 /// Returns all known commands, sorted by command name.
147 public IList
<CommandRef
> All()
149 return commands
.Values
;
153 /// Returns all common commands, sorted by command name.
155 public List
<CommandRef
> Common()
157 List
<CommandRef
> common
= new List
<CommandRef
>();
158 foreach (CommandRef c
in commands
.Values
)
164 return toSortedArray(common
);
168 /// Returns all incomplete commands, sorted by command name.
170 public List
<CommandRef
> Incomplete()
172 List
<CommandRef
> incomplete
= new List
<CommandRef
>();
173 foreach (CommandRef c
in commands
.Values
)
179 return toSortedArray(incomplete
);
183 /// Returns all complete commands, sorted by command name.
185 public List
<CommandRef
> Complete()
187 List
<CommandRef
> complete
= new List
<CommandRef
>();
188 foreach (CommandRef c
in commands
.Values
)
194 return toSortedArray(complete
);
198 /// Sorts a list of specified commands by command name.
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
)
205 delegate(CommandRef ref1
, CommandRef ref2
)
207 return ref1
.getName().CompareTo(ref2
.getName());