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 Workspace
GetWorkspaceFromCache()
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 && (!String
.IsNullOrEmpty(OptionWorkspace
)))
105 string ownerName
= String
.Format("{0}\\{1}", driver
.Domain
, driver
.Username
).ToUpper();
106 info
= Workstation
.Current
.GetLocalWorkspaceInfo(driver
.VersionControlServer
,
107 OptionWorkspace
, ownerName
);
112 Console
.WriteLine("Unable to determine the workspace.");
113 Console
.WriteLine(" Path: " + path
);
115 Console
.WriteLine("Hints:");
116 Console
.WriteLine(" Try adding /workspace:<name>");
117 Console
.WriteLine(" Review command options prefixed with '/'. Invalid options are mistaken for paths.");
121 return VersionControlServer
.GetWorkspace(info
);
124 public VersionSpec
VersionFromString(string version
)
126 if (!String
.IsNullOrEmpty(version
))
127 return VersionSpec
.ParseSingleSpec(version
, driver
.Username
);
129 return VersionSpec
.Latest
;
132 public string ChangeTypeToString(ChangeType change
)
134 string ctype
= "edit";
136 if ((change
& ChangeType
.Add
) == ChangeType
.Add
) ctype
= "add";
137 else if ((change
& ChangeType
.Delete
) == ChangeType
.Delete
) ctype
= "delete";
142 public int WindowWidth
145 // if output piped to a file, we don't want 0!
146 // this also throws on weird terminals on msclr
149 int w
= Console
.WindowWidth
;
150 if (w
!= 0) return w
- 1;
152 catch (IOException
) {}
157 public string OwnerFromString(string owner
)
159 if (String
.IsNullOrEmpty(owner
)) return driver
.Username
;
160 if (owner
== "*") return null;
164 public VersionControlServer VersionControlServer
166 get { return driver.VersionControlServer; }
169 public List
<string> UnVerifiedFullPaths(string[] args
)
171 List
<string> paths
= new List
<string>();
172 for (int i
= 1; i
< args
.Length
; i
++)
174 string fullPath
= Path
.GetFullPath(args
[i
]);
181 public List
<string> VerifiedFullPaths(string[] args
)
183 List
<string> paths
= UnVerifiedFullPaths(args
);
184 foreach (string path
in paths
)
186 if (!File
.Exists(path
) && !Directory
.Exists(path
))
188 Console
.WriteLine("{0}: No such file or directory.", path
);
189 Environment
.Exit(-1);
196 public void DeleteReadOnlyFile(string fullName
)
198 File
.SetAttributes(fullName
, FileAttributes
.Normal
);
199 File
.Delete(fullName
);
202 static public Regex
WildcardToRegex(string exclusionList
)
204 string[] wildcards
= exclusionList
.Split(',');
205 StringBuilder sb
= new StringBuilder();
207 foreach (string wildcard
in wildcards
)
209 if (0 != sb
.Length
) sb
.Append("|");
210 string regex
= "^" + Regex
.Escape(wildcard
).Replace("\\*", ".*").Replace("\\?", ".") + "$";
214 return new Regex(sb
.ToString());
217 public bool IsExcludedFile(string file
)
219 return excludes
.IsMatch(file
);
222 public void ConfirmFilesSpecified()
224 if (Arguments
.Length
< 1)
226 Console
.WriteLine("No files specified.");
231 public void WritePath(string p
)
233 // make this an option soon
234 if (!runningOnUnix
) p
= p
.Replace('\\', '/');
235 Console
.WriteLine(p
);
238 public abstract void Run ();