5 // Joel Reed (joelwreed@gmail.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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 protected Driver driver
;
43 private static Regex excludes
;
45 public string[] Arguments
47 get { return arguments; }
52 int p
= (int) Environment
.OSVersion
.Platform
;
53 if (!((p
== 4) || (p
== 128))) runningOnUnix
= false;
54 excludes
= WildcardToRegex(Settings
.Current
.Get("File.Excludes"));
57 public static StringComparer PathComparer
60 if (!runningOnUnix
) return StringComparer
.CurrentCultureIgnoreCase
;
61 return StringComparer
.CurrentCulture
;
65 public Command(Driver driver
, string[] args
)
69 this.arguments
= RemainingArguments
;
72 public Workspace
GetWorkspaceFromServer()
74 string name
= OptionWorkspace
;
76 // if option passed use it
77 if (String
.IsNullOrEmpty(name
))
79 // guess based on current working directory
80 WorkspaceInfo info
= Workstation
.Current
.GetLocalWorkspaceInfo(Environment
.CurrentDirectory
);
81 if (info
!= null) name
= info
.Name
;
84 if (String
.IsNullOrEmpty(name
))
86 Console
.WriteLine("Unable to determine the workspace");
87 Console
.WriteLine(" hint: try adding /workspace:<name>");
91 return VersionControlServer
.GetWorkspace(name
, driver
.Username
);
94 public WorkspaceInfo
GetWorkspaceInfoFromCache()
96 string path
= Environment
.CurrentDirectory
;
97 if (Arguments
.Length
> 0)
99 path
= Path
.GetFullPath(Arguments
[0]);
102 WorkspaceInfo info
= Workstation
.Current
.GetLocalWorkspaceInfo(path
);
103 if (info
!= null) return info
;
105 if (String
.IsNullOrEmpty(OptionWorkspace
))
107 Console
.WriteLine("Unable to determine the workspace.");
108 Console
.WriteLine(" Path: " + path
);
112 string ownerName
= String
.Format("{0}\\{1}", driver
.Domain
, driver
.Username
).ToUpper();
113 info
= Workstation
.Current
.GetLocalWorkspaceInfo(driver
.VersionControlServer
,
114 OptionWorkspace
, ownerName
);
118 Console
.WriteLine("Unable to determine the workspace.");
119 Console
.WriteLine(" Workspace Option: " + OptionWorkspace
);
125 public Workspace
GetWorkspaceFromCache()
127 WorkspaceInfo info
= GetWorkspaceInfoFromCache();
132 Console
.WriteLine("Hints:");
133 Console
.WriteLine(" Try adding /workspace:<name>");
134 Console
.WriteLine(" Review command options prefixed with '/'. Invalid options are mistaken for paths.");
138 return VersionControlServer
.GetWorkspace(info
);
141 public VersionSpec
VersionFromString(string version
)
143 if (!String
.IsNullOrEmpty(version
))
144 return VersionSpec
.ParseSingleSpec(version
, driver
.Username
);
146 return VersionSpec
.Latest
;
149 static public string ChangeTypeToString(ChangeType change
)
151 string ctype
= "edit";
153 if ((change
& ChangeType
.Add
) == ChangeType
.Add
) ctype
= "add";
154 else if ((change
& ChangeType
.Delete
) == ChangeType
.Delete
) ctype
= "delete";
159 public int WindowWidth
162 // if output piped to a file, we don't want 0!
163 // this also throws on weird terminals on msclr
166 int w
= Console
.WindowWidth
;
167 if (w
!= 0) return w
- 1;
169 catch (IOException
) {}
174 public string OwnerFromString(string owner
)
176 if (String
.IsNullOrEmpty(owner
)) return driver
.Username
;
177 if (owner
== "*") return null;
181 public VersionControlServer VersionControlServer
183 get { return driver.VersionControlServer; }
186 public List
<string> UnVerifiedFullPaths(string[] args
)
188 List
<string> paths
= new List
<string>();
189 for (int i
= 0; i
< args
.Length
; i
++)
191 string fullPath
= Path
.GetFullPath(args
[i
]);
198 public List
<string> VerifiedFullPaths(string[] args
)
200 List
<string> paths
= UnVerifiedFullPaths(args
);
201 char[] wildcards
= { '*', '?' }
;
203 foreach (string path
in paths
)
205 // skip wildcarded paths
206 if (-1 != path
.IndexOfAny(wildcards
)) continue;
208 if (!File
.Exists(path
) && !Directory
.Exists(path
))
210 Console
.WriteLine("{0}: No such file or directory.", path
);
211 Environment
.Exit(-1);
218 public void DeleteReadOnlyFile(string fullName
)
220 File
.SetAttributes(fullName
, FileAttributes
.Normal
);
221 File
.Delete(fullName
);
224 static public Regex
WildcardToRegex(string exclusionList
)
226 string[] wildcards
= exclusionList
.Split(',');
227 StringBuilder sb
= new StringBuilder();
229 foreach (string wildcard
in wildcards
)
231 if (0 != sb
.Length
) sb
.Append("|");
232 string regex
= "^" + Regex
.Escape(wildcard
).Replace("\\*", ".*").Replace("\\?", ".") + "$";
236 return new Regex(sb
.ToString());
239 public bool IsExcludedFile(string file
)
241 return excludes
.IsMatch(file
);
244 public void ConfirmFilesSpecified()
246 if (Arguments
.Length
< 1)
248 Console
.WriteLine("No files specified.");
253 public string CanonicalPath(string p
)
256 // maybe this feature is only interesting to me?
257 //if (runningOnUnix) return p;
258 //return p.Replace('\\', '/');
261 public abstract void Run ();