show the difference between two versions for a specified path
[tfs.git] / tools / opentf / WorkfoldCommand.cs
blobaa0568fc2cbdf33577bec364b7c4905ea4961791
1 //
2 // WorkfoldCommand.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.Collections.Generic;
31 using System.IO;
32 using System.Text;
33 using Microsoft.TeamFoundation.Client;
34 using Microsoft.TeamFoundation.VersionControl.Common;
35 using Microsoft.TeamFoundation.VersionControl.Client;
36 using Mono.GetOptions;
37 using OpenTF.Common;
39 [Command("workfold", "Map and unmap server paths to local folders.", "<server path> [ <local path> ]")]
40 class WorkfoldCommand : Command
42 [Option("Unmap", "", "unmap")]
43 public bool OptionUnmap;
45 public WorkfoldCommand(Driver driver, string[] args): base(driver, args)
49 public void CurrentWorkfolders(Workspace workspace)
51 Console.WriteLine("=".PadRight(WindowWidth, '='));
52 Console.WriteLine("Workspace: " + workspace.Name);
53 Console.WriteLine("Server : " + Driver.GetServerUrl());
55 foreach (WorkingFolder folder in workspace.Folders)
57 Console.WriteLine(" " + folder.ServerItem + " " + folder.LocalItem);
61 public void MapWorkfolder(Workspace workspace, string serverItem, string localItem)
63 List<WorkingFolder> folders = new List<WorkingFolder>(workspace.Folders);
64 folders.Add(new WorkingFolder(serverItem, localItem));
65 workspace.Update(workspace.Name, workspace.Comment, folders.ToArray());
67 if ((Settings.Current.GetAsBool("Workfold.CreateLocal")) &&
68 (!Directory.Exists(localItem)))
69 Directory.CreateDirectory(localItem);
72 public void UnmapWorkfolder(Workspace workspace, string item)
74 if (! VersionControlPath.IsServerItem(item))
75 item = Path.GetFullPath(item);
77 List<WorkingFolder> folders = new List<WorkingFolder>(workspace.Folders);
78 string msg = String.Empty;
80 for (int i = 0; i < folders.Count; ++i)
82 WorkingFolder folder = folders[i];
84 if (item == folder.ServerItem || item == folder.LocalItem)
86 msg = String.Format("Removed: {0} => {1}",
87 folder.ServerItem, folder.LocalItem);
88 folders.RemoveAt(i);
89 break;
93 workspace.Update(workspace.Name, workspace.Comment, folders.ToArray());
94 if (!String.IsNullOrEmpty(msg))
95 Console.WriteLine(msg);
98 public override void Run()
100 Workspace workspace = GetWorkspaceFromServer();
102 if (Arguments.Length < 1)
103 CurrentWorkfolders(workspace);
104 else if (Arguments.Length < 2)
106 if (!OptionUnmap)
107 Console.WriteLine("No-op");
108 else
109 UnmapWorkfolder(workspace, Arguments[0]);
111 else if (Arguments.Length == 2)
112 MapWorkfolder(workspace, Arguments[0], Arguments[1]);