From c5db79ed9b7f15a9b14fcfb18a9dc83a74b017a4 Mon Sep 17 00:00:00 2001 From: "Joel W. Reed" Date: Thu, 12 Jul 2007 20:07:58 -0400 Subject: [PATCH] use CommandRegistry in Driver do command routine --- tools/tf/CommandRegistry.cs | 58 ++++++++++----- tools/tf/Driver.cs | 174 ++------------------------------------------ 2 files changed, 45 insertions(+), 187 deletions(-) diff --git a/tools/tf/CommandRegistry.cs b/tools/tf/CommandRegistry.cs index 4969b1e..dd6b8c5 100644 --- a/tools/tf/CommandRegistry.cs +++ b/tools/tf/CommandRegistry.cs @@ -28,13 +28,17 @@ using System; using System.Collections; +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; public class CommandRegistry { - static public void Display() + static SortedList commands = new SortedList(); + static SortedList commandAttributes = new SortedList(); + + static CommandRegistry() { Assembly assembly = Assembly.GetAssembly(typeof(CommandRegistry)); Type commandType = typeof(Command); @@ -42,31 +46,47 @@ public class CommandRegistry System.Type[] types = assembly.GetTypes(); foreach (Type t in types) { - // if (t.BaseType == commandType) - // Console.WriteLine(t.ToString()); - + if (t.BaseType != commandType) continue; CommandAttribute[] attributes = t.GetCustomAttributes(typeof(CommandAttribute), false) as CommandAttribute[]; foreach (CommandAttribute attribute in attributes) { - Console.Write(" " + attribute.Name); - if (!String.IsNullOrEmpty(attribute.Alias)) Console.Write(" (alias {0})", attribute.Alias); - Console.WriteLine(); - - string description = attribute.Description; - int x = 0; + commands.Add(attribute.Name, t); + if (!String.IsNullOrEmpty(attribute.Alias)) + commands.Add(attribute.Alias, t); - while (x < description.Length) - { - int y = Math.Min(x + 60, description.Length); - int z = description.IndexOf(' ', y) + 1; - if (z == 0) z = description.Length; + commandAttributes.Add(attribute.Name, attribute); + } + } + } - Console.WriteLine(" " + description.Substring(x, z - x)); - x = z; - } + static public System.Type GetCommandType(string name) + { + System.Type t = commands[name]; + return t; + } - Console.WriteLine(); + static public void ShowHelp() + { + foreach (CommandAttribute attribute in commandAttributes.Values) + { + Console.Write(" " + attribute.Name); + if (!String.IsNullOrEmpty(attribute.Alias)) Console.Write(" (alias {0})", attribute.Alias); + Console.WriteLine(); + + string description = attribute.Description; + int x = 0; + + while (x < description.Length) + { + int y = Math.Min(x + 60, description.Length); + int z = description.IndexOf(' ', y) + 1; + if (z == 0) z = description.Length; + + Console.WriteLine(" " + description.Substring(x, z - x)); + x = z; } + + Console.WriteLine(); } } } diff --git a/tools/tf/Driver.cs b/tools/tf/Driver.cs index c40b6a1..d0b37ed 100644 --- a/tools/tf/Driver.cs +++ b/tools/tf/Driver.cs @@ -126,33 +126,7 @@ partial class Driver : ICertificatePolicy Console.WriteLine("Available subcommands:"); Console.WriteLine(); - CommandRegistry.Display(); - return; - - Console.WriteLine(" add"); - Console.WriteLine(" cache"); - Console.WriteLine(" changeset"); - Console.WriteLine(" checkin"); - Console.WriteLine(" checkout (alias edit)"); - Console.WriteLine(" configure (alias config)"); - Console.WriteLine(" delete"); - Console.WriteLine(" diff"); - Console.WriteLine(" dir"); - Console.WriteLine(" get"); - Console.WriteLine(" history"); - Console.WriteLine(" labels"); - Console.WriteLine(" ls-files"); - Console.WriteLine(" online"); - Console.WriteLine(" permission"); - Console.WriteLine(" properties"); - Console.WriteLine(" rename"); - Console.WriteLine(" status"); - Console.WriteLine(" treeclean"); - Console.WriteLine(" undo"); - Console.WriteLine(" view"); - Console.WriteLine(" workfold"); - Console.WriteLine(" workspace"); - Console.WriteLine(" workspaces"); + CommandRegistry.ShowHelp(); } public void ProcessBranch() {} @@ -219,151 +193,15 @@ partial class Driver : ICertificatePolicy public void ProcessCommand(VersionControlServer vcs, string cmd, string[] cmdArgs) { - Command command = null; - - switch (cmd) + Type commandType = CommandRegistry.GetCommandType(cmd); + if (commandType == null) { - case "add": - command = new AddCommand(cmdArgs, Options, vcs); - break; - case "branch": - ProcessBranch(); - break; - case "branches": - ProcessBranches(); - break; - case "changeset": - command = new ChangesetCommand(cmdArgs, Options, vcs); - break; - case "checkin": - command = new CheckinCommand(cmdArgs, Options, vcs); - break; - case "checkout": - command = new CheckoutCommand(cmdArgs, Options, vcs); - break; - case "edit": // command alias - command = new CheckoutCommand(cmdArgs, Options, vcs); - break; - case "del": // command alias - command = new DeleteCommand(cmdArgs, Options, vcs); - break; - case "delete": - command = new DeleteCommand(cmdArgs, Options, vcs); - break; - case "diff": // command alias - command = new DifferenceCommand(cmdArgs, Options, vcs); - break; - case "difference": - command = new DifferenceCommand(cmdArgs, Options, vcs); - break; - case "dir": - command = new DirCommand(cmdArgs, Options, vcs); - break; - case "get": - command = new GetCommand(cmdArgs, Options, vcs); - break; - case "help": - ShowHelp(); - break; - case "hist": // command alias - command = new HistoryCommand(cmdArgs, Options, vcs); - break; - case "history": - command = new HistoryCommand(cmdArgs, Options, vcs); - break; - case "label": - command = new LabelCommand(cmdArgs, Options, vcs); - break; - case "labels": - command = new LabelsCommand(cmdArgs, Options, vcs); - break; - case "lock": - ProcessLock(); - break; - case "ls-files": - command = new LsFilesCommand(cmdArgs, Options, vcs); - break; - case "online": - command = new OnlineCommand(cmdArgs, Options, vcs); - break; - case "merge": - ProcessMerge(); - break; - case "merges": - ProcessMerges(); - break; - case "move": - command = new RenameCommand(cmdArgs, Options, vcs); - break; - case "perm": - command = new PermissionCommand(cmdArgs, Options, vcs); - break; - case "permission": - command = new PermissionCommand(cmdArgs, Options, vcs); - break; - case "prop": - command = new PropertiesCommand(cmdArgs, Options, vcs); - break; - case "properties": - command = new PropertiesCommand(cmdArgs, Options, vcs); - break; - case "ren": - command = new RenameCommand(cmdArgs, Options, vcs); - break; - case "rename": - command = new RenameCommand(cmdArgs, Options, vcs); - break; - case "resolve": - ProcessResolve(); - break; - case "shelve": - ProcessShelve(); - break; - case "shelvesets": - ProcessShelvesets(); - break; - case "stat": - command = new StatusCommand(cmdArgs, Options, vcs); - break; - case "status": - command = new StatusCommand(cmdArgs, Options, vcs); - break; - case "treeclean": - command = new TreeCleanCommand(cmdArgs, Options, vcs); - break; - case "undel": // alias for undelete - ProcessUndelete(); - break; - case "undelete": - ProcessUndelete(); - break; - case "undo": - command = new UndoCommand(cmdArgs, Options, vcs); - break; - case "unlabel": - command = new UnlabelCommand(cmdArgs, Options, vcs); - break; - case "unshelve": - ProcessUnshelve(); - break; - case "view": - command = new ViewCommand(cmdArgs, Options, vcs); - break; - case "workfold": - command = new WorkfoldCommand(cmdArgs, Options, vcs); - break; - case "workspace": - command = new WorkspaceCommand(cmdArgs, Options, vcs); - break; - case "workspaces": - command = new WorkspacesCommand(cmdArgs, Options, vcs); - break; - default: Console.WriteLine("Unknown command: " + cmd); - break; + return; } - if (command != null) command.Run(); + Command command = (Command) Activator.CreateInstance(commandType, new object[]{cmdArgs, Options, vcs}); + command.Run(); } public string GetServerUrl() -- 2.11.4.GIT