show.verstring.in.statusbar
[tfs.git] / tools / wit / Command.cs
blob5795cf369ce8cfea96560a0a939b19e3fb88126b
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;
36 using Microsoft.TeamFoundation.VersionControl.Common;
38 abstract class Command : CommandOptions
40 private static bool runningOnUnix = true;
41 private string[] arguments;
42 private Driver driver;
43 private static Regex excludes;
45 public Driver Driver
47 get { return driver; }
50 public string[] Arguments
52 get { return arguments; }
55 static Command()
57 int p = (int) Environment.OSVersion.Platform;
58 if (!((p == 4) || (p == 128))) runningOnUnix = false;
59 excludes = WildcardToRegex(Settings.Current.Get("File.Excludes"));
62 public static StringComparer PathComparer
64 get {
65 if (!runningOnUnix) return StringComparer.CurrentCultureIgnoreCase;
66 return StringComparer.CurrentCulture;
70 public Command(Driver driver, string[] args)
72 this.driver = driver;
73 ProcessArgs(args);
74 this.arguments = RemainingArguments;
77 public int WindowWidth
79 get {
80 // if output piped to a file, we don't want 0!
81 // this also throws on weird terminals on msclr
82 try
84 int w = Console.WindowWidth;
85 if (w != 0) return w - 1;
87 catch (IOException) {}
88 return 143;
92 public string OwnerFromString(string owner)
94 if (String.IsNullOrEmpty(owner)) return Driver.Username;
95 if (owner == "*") return null;
96 return owner;
99 public List<string> UnVerifiedFullPaths(string[] args)
101 List<string> paths = new List<string>();
102 for (int i = 0; i < args.Length; i++)
104 string fullPath = Path.GetFullPath(args[i]);
105 paths.Add(fullPath);
108 return paths;
111 public List<string> VerifiedFullPaths(string[] args)
113 List<string> paths = UnVerifiedFullPaths(args);
114 char[] wildcards = { '*', '?' };
116 foreach (string path in paths)
118 // skip wildcarded paths
119 if (-1 != path.IndexOfAny(wildcards)) continue;
121 if (!File.Exists(path) && !Directory.Exists(path))
123 Console.WriteLine("{0}: No such file or directory.", path);
124 Environment.Exit(-1);
128 return paths;
131 public void DeleteReadOnlyFile(string fullName)
133 File.SetAttributes(fullName, FileAttributes.Normal);
134 File.Delete(fullName);
137 static public Regex WildcardToRegex(string exclusionList)
139 string[] wildcards = exclusionList.Split(',');
140 StringBuilder sb = new StringBuilder();
142 foreach (string wildcard in wildcards)
144 if (0 != sb.Length) sb.Append("|");
145 string regex = "^" + Regex.Escape(wildcard).Replace("\\*", ".*").Replace("\\?", ".") + "$";
146 sb.Append(regex);
149 return new Regex(sb.ToString());
152 public bool IsExcludedFile(string file)
154 return excludes.IsMatch(file);
157 public void ConfirmFilesSpecified()
159 if (Arguments.Length < 1)
161 Console.WriteLine("No files specified.");
162 Environment.Exit(0);
166 public string CanonicalPath(string p)
168 return p;
169 // maybe this feature is only interesting to me?
170 //if (runningOnUnix) return p;
171 //return p.Replace('\\', '/');
174 public abstract void Run ();