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
;
37 abstract class Command
39 private static bool runningOnUnix
= true;
40 private string[] arguments
;
41 private CommandOptions options
= new CommandOptions();
42 private VersionControlServer vcs
;
43 protected Driver driver
;
45 public string[] Arguments
47 get { return arguments; }
50 public CommandOptions Options
52 get { return options; }
57 int p
= (int) Environment
.OSVersion
.Platform
;
58 if (!((p
== 4) || (p
== 128))) runningOnUnix
= false;
61 public static StringComparer PathComparer
64 if (!runningOnUnix
) return StringComparer
.CurrentCultureIgnoreCase
;
65 return StringComparer
.CurrentCulture
;
69 public Command(Driver driver
, string[] args
)
72 this.vcs
= driver
.VersionControlServer
;
73 options
.ProcessArgs(args
);
74 this.arguments
= Options
.RemainingArguments
;
77 public Workspace
GetWorkspaceFromServer()
79 string name
= Options
.Workspace
;
81 // if option passed use it
82 if (String
.IsNullOrEmpty(name
))
84 // guess based on current working directory
85 WorkspaceInfo info
= Workstation
.Current
.GetLocalWorkspaceInfo(Environment
.CurrentDirectory
);
86 if (info
!= null) name
= info
.Name
;
89 if (String
.IsNullOrEmpty(name
))
91 Console
.WriteLine("Unable to determine the workspace");
92 Console
.WriteLine(" hint: try adding /workspace:<name>");
96 return VersionControlServer
.GetWorkspace(name
, driver
.Username
);
99 public Workspace
GetWorkspaceFromCache()
101 string path
= Environment
.CurrentDirectory
;
102 if (Arguments
.Length
> 1)
104 path
= Path
.GetFullPath(Arguments
[1]);
107 WorkspaceInfo info
= Workstation
.Current
.GetLocalWorkspaceInfo(path
);
108 if (info
== null && (!String
.IsNullOrEmpty(Options
.Workspace
)))
110 string ownerName
= String
.Format("{0}\\{1}", driver
.Domain
, driver
.Username
).ToUpper();
111 info
= Workstation
.Current
.GetLocalWorkspaceInfo(vcs
, Options
.Workspace
,
117 Console
.WriteLine("Unable to determine the workspace.");
118 Console
.WriteLine(" Path: " + path
);
120 Console
.WriteLine("Hints:");
121 Console
.WriteLine(" Try adding /workspace:<name>");
122 Console
.WriteLine(" Review command options prefixed with '/'. Invalid options are mistaken for paths.");
126 return vcs
.GetWorkspace(info
);
129 public VersionSpec Version
132 if (!String
.IsNullOrEmpty(Options
.Version
))
133 return VersionSpec
.ParseSingleSpec(Options
.Version
, driver
.Username
);
134 return VersionSpec
.Latest
;
138 public int WindowWidth
141 // if output piped to a file, we don't want 0!
142 // this also throws on weird terminals on msclr
145 int w
= Console
.WindowWidth
;
146 if (w
!= 0) return w
- 1;
148 catch (IOException
) {}
153 public string Computer
156 if (String
.IsNullOrEmpty(Options
.Computer
)) return Environment
.MachineName
;
157 if (Options
.Computer
== "*") return null;
158 return Options
.Computer
;
165 if (String
.IsNullOrEmpty(Options
.Owner
)) return driver
.Username
;
166 if (Options
.Owner
== "*") return null;
167 return Options
.Owner
;
171 public VersionControlServer VersionControlServer
176 public List
<string> UnVerifiedFullPaths(string[] args
)
178 List
<string> paths
= new List
<string>();
179 for (int i
= 1; i
< args
.Length
; i
++)
181 string fullPath
= Path
.GetFullPath(args
[i
]);
188 public List
<string> VerifiedFullPaths(string[] args
)
190 List
<string> paths
= UnVerifiedFullPaths(args
);
191 foreach (string path
in paths
)
193 if (!File
.Exists(path
) && !Directory
.Exists(path
))
195 Console
.WriteLine("{0}: No such file or directory.", path
);
196 Environment
.Exit(-1);
203 public void DeleteReadOnlyFile(string fullName
)
205 File
.SetAttributes(fullName
, FileAttributes
.Normal
);
206 File
.Delete(fullName
);
209 public Regex
WildcardToRegex(string exclusionList
)
211 string[] wildcards
= exclusionList
.Split(',');
212 StringBuilder sb
= new StringBuilder();
214 foreach (string wildcard
in wildcards
)
216 if (0 != sb
.Length
) sb
.Append("|");
217 string regex
= "^" + Regex
.Escape(wildcard
).Replace("\\*", ".*").Replace("\\?", ".") + "$";
221 return new Regex(sb
.ToString());
224 public void ConfirmFilesSpecified()
226 if (Arguments
.Length
< 2)
228 Console
.WriteLine("No files specified.");
233 public abstract void Run ();