2 // WorkspacesCommand.cs
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.
30 using System
.Collections
.Generic
;
33 using Microsoft
.TeamFoundation
.Client
;
34 using Microsoft
.TeamFoundation
.VersionControl
.Client
;
35 using Mono
.GetOptions
;
37 [Command("workspaces", "List workspaces in server repository.")]
38 class WorkspacesCommand
: Command
40 [Option("Computer name", "M", "computer")]
41 public string OptionComputer
;
43 [Option("Format \"brief\" or \"detailed\".", "F", "format")]
44 public string OptionFormat
= "";
46 [Option("Owner name", "O", "owner")]
47 public string OptionOwner
;
49 public WorkspacesCommand(Driver driver
, string[] args
): base(driver
, args
)
53 public void BriefOutput(Workspace
[] workspaces
)
55 int maxName
= 9, maxComputer
= 8, maxOwner
= 5;
56 foreach (Workspace workspace
in workspaces
)
58 if (workspace
.Name
.Length
> maxName
) maxName
= workspace
.Name
.Length
;
59 if (workspace
.Computer
.Length
> maxComputer
) maxComputer
= workspace
.Computer
.Length
;
61 // domain is stripped on output
62 int ownerNameLen
= workspace
.OwnerName
.Length
;
63 int slash
= workspace
.OwnerName
.IndexOf('\\');
64 if (-1 != slash
) ownerNameLen
= workspace
.OwnerName
.Length
- slash
;
66 if (ownerNameLen
> maxOwner
)
68 maxOwner
= ownerNameLen
;
72 int maxComment
= WindowWidth
- maxName
- maxComputer
- maxOwner
- 3;
73 if (maxComment
< 0) maxComment
= 0;
75 string line
= String
.Format("{0} {1} {2} {3}",
76 "Workspace".PadRight(maxName
),
77 "Owner".PadRight(maxOwner
),
78 "Computer".PadRight(maxComputer
),
80 Console
.WriteLine(line
);
82 line
= String
.Format("{0} {1} {2} {3}",
83 "-".PadRight(maxName
, '-'),
84 "-".PadRight(maxOwner
, '-'),
85 "-".PadRight(maxComputer
, '-'),
86 "-".PadRight(maxComment
, '-'));
87 Console
.WriteLine(line
);
89 foreach (Workspace workspace
in workspaces
)
92 if (workspace
.Comment
.Length
> maxComment
)
93 comment
= workspace
.Comment
.Remove(maxComment
);
95 comment
= workspace
.Comment
;
97 // domain is stripped on output
98 string ownerName
= workspace
.OwnerName
;
99 int slash
= workspace
.OwnerName
.IndexOf('\\');
102 ownerName
= workspace
.OwnerName
.Substring(slash
+1);
105 line
= String
.Format("{0} {1} {2} {3}",
106 workspace
.Name
.PadRight(maxName
),
107 ownerName
.PadRight(maxOwner
),
108 workspace
.Computer
.PadRight(maxComputer
),
110 Console
.WriteLine(line
);
114 public void DetailedOutput(Workspace
[] workspaces
)
118 foreach (Workspace workspace
in workspaces
)
120 if (!first
) Console
.WriteLine();
123 Console
.WriteLine("=".PadRight(WindowWidth
, '='));
124 Console
.WriteLine("Workspace: " + workspace
.Name
);
125 Console
.WriteLine("Owner : " + workspace
.OwnerName
);
126 Console
.WriteLine("Computer : " + workspace
.Computer
);
127 Console
.WriteLine("Comment : " + workspace
.Comment
);
128 Console
.WriteLine("Server : " + driver
.ServerUrl
);
131 Console
.WriteLine("Working folders:");
133 foreach (WorkingFolder folder
in workspace
.Folders
)
135 Console
.WriteLine(" " + folder
.ServerItem
+ " " + folder
.LocalItem
);
140 public string Computer
143 if (String
.IsNullOrEmpty(OptionComputer
)) return Environment
.MachineName
;
144 if (OptionComputer
== "*") return null;
145 return OptionComputer
;
149 public override void Run()
151 Workspace
[] workspaces
= VersionControlServer
.QueryWorkspaces(null, OwnerFromString(OptionOwner
), Computer
);
153 if (workspaces
.Length
== 0)
155 Console
.WriteLine("No workspace matching {0} on computer {1} found in Team Foundation Server.",
156 OwnerFromString(OptionOwner
), Computer
);
157 Environment
.Exit(-1);
160 bool detailed
= OptionFormat
.Equals("detailed", StringComparison
.InvariantCultureIgnoreCase
);
161 if (detailed
) DetailedOutput(workspaces
);
162 else BriefOutput(workspaces
);