broken.wip
[tfs.git] / tools / tf / Command.cs
blobedf4d36b5d1d22bb635a9b917b55928ca62c044b
1 //
2 // Command.cs
3 //
4 // Authors:
5 // Joel Reed (joelwreed@gmail.com)
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.IO;
31 using System.Text;
32 using System.Text.RegularExpressions;
33 using System.Collections.Generic;
34 using Microsoft.TeamFoundation.Client;
35 using Microsoft.TeamFoundation.VersionControl.Client;
37 abstract class Command
39 private static bool runningOnUnix = true;
40 private string[] arguments;
41 private CommandOptions options = new CommandOptions();
42 private VersionControlServer vcs;
44 public string[] Arguments
46 get { return arguments; }
49 public CommandOptions Options
51 get { return options; }
54 static Command()
56 int p = (int) Environment.OSVersion.Platform;
57 if (!((p == 4) || (p == 128))) runningOnUnix = false;
60 public static StringComparer PathComparer
62 get {
63 if (!runningOnUnix) return StringComparer.CurrentCultureIgnoreCase;
64 return StringComparer.CurrentCulture;
68 public Command(string[] arguments, VersionControlServer vcs)
70 options.ProcessArgs(arguments);
71 this.arguments = arguments;
72 this.vcs = vcs;
75 public Workspace GetWorkspaceFromServer()
77 string name = Options.Workspace;
79 // if option passed use it
80 if (String.IsNullOrEmpty(name))
82 // guess based on current working directory
83 WorkspaceInfo info = Workstation.Current.GetLocalWorkspaceInfo(Environment.CurrentDirectory);
84 if (info != null) name = info.Name;
87 if (String.IsNullOrEmpty(name))
89 Console.WriteLine("Unable to determine the workspace");
90 Console.WriteLine(" hint: try adding /workspace:<name>");
91 Environment.Exit(1);
94 return VersionControlServer.GetWorkspace(name, Options.Username);
97 public Workspace GetWorkspaceFromCache()
99 string path = Environment.CurrentDirectory;
100 if (Arguments.Length > 1)
102 path = Path.GetFullPath(Arguments[1]);
105 WorkspaceInfo info = Workstation.Current.GetLocalWorkspaceInfo(path);
106 if (info == null && (!String.IsNullOrEmpty(Options.Workspace)))
108 string ownerName = String.Format("{0}\\{1}", Options.Domain, Options.Username).ToUpper();
109 info = Workstation.Current.GetLocalWorkspaceInfo(vcs, Options.Workspace,
110 ownerName);
113 if (info == null)
115 Console.WriteLine("Unable to determine the workspace.");
116 Console.WriteLine(" Path: " + path);
117 Console.WriteLine();
118 Console.WriteLine("Hints:");
119 Console.WriteLine(" Try adding /workspace:<name>");
120 Console.WriteLine(" Review command options prefixed with '/'. Invalid options are mistaken for paths.");
121 Environment.Exit(1);
124 return vcs.GetWorkspace(info);
127 public VersionSpec Version
129 get {
130 if (!String.IsNullOrEmpty(Options.Version))
131 return VersionSpec.ParseSingleSpec(Options.Version, Options.Username);
132 return VersionSpec.Latest;
136 public int WindowWidth
138 get {
139 // if output piped to a file, we don't want 0!
140 // this also throws on weird terminals on msclr
143 int w = Console.WindowWidth;
144 if (w != 0) return w - 1;
146 catch (IOException) {}
147 return 143;
151 public string Computer
153 get {
154 if (String.IsNullOrEmpty(Options.Computer)) return Environment.MachineName;
155 if (Options.Computer == "*") return null;
156 return Options.Computer;
160 public string Owner
162 get {
163 if (String.IsNullOrEmpty(Options.Owner)) return Options.Username;
164 if (Options.Owner == "*") return null;
165 return Options.Owner;
169 public VersionControlServer VersionControlServer
171 get { return vcs; }
174 public List<string> UnVerifiedFullPaths(string[] args)
176 List<string> paths = new List<string>();
177 for (int i = 1; i < args.Length; i++)
179 string fullPath = Path.GetFullPath(args[i]);
180 paths.Add(fullPath);
183 return paths;
186 public List<string> VerifiedFullPaths(string[] args)
188 List<string> paths = UnVerifiedFullPaths(args);
189 foreach (string path in paths)
191 if (!File.Exists(path) && !Directory.Exists(path))
193 Console.WriteLine("{0}: No such file or directory.", path);
194 Environment.Exit(-1);
198 return paths;
201 public void DeleteReadOnlyFile(string fullName)
203 File.SetAttributes(fullName, FileAttributes.Normal);
204 File.Delete(fullName);
207 public Regex WildcardToRegex(string exclusionList)
209 string[] wildcards = exclusionList.Split(',');
210 StringBuilder sb = new StringBuilder();
212 foreach (string wildcard in wildcards)
214 if (0 != sb.Length) sb.Append("|");
215 string regex = "^" + Regex.Escape(wildcard).Replace("\\*", ".*").Replace("\\?", ".") + "$";
216 sb.Append(regex);
219 return new Regex(sb.ToString());
222 public void ConfirmFilesSpecified()
224 if (Arguments.Length < 2)
226 Console.WriteLine("No files specified.");
227 Environment.Exit(0);
231 public abstract void Run ();