using System; using System.Activities; using System.IO; using Microsoft.TeamFoundation.Client; using Microsoft.TeamFoundation.VersionControl.Client; namespace BuildProcess.Activities { public sealed class ReplicateFiles : CodeActivity { //array of file names to replicate [RequiredArgument] public InArgument ReplicationFiles { get; set; } //array of directories to replicate to [RequiredArgument] public InArgument ReplicationFolders { get; set; } //Source location to replicate from [RequiredArgument] public InArgument SourceLocation { get; set; } [RequiredArgument] public InArgument CollectionName { get; set; } protected override void Execute(CodeActivityContext context) { string sourceLocation = context.GetValue(this.SourceLocation); string collectionName = context.GetValue(this.CollectionName); string[] replicationFolders = context.GetValue(this.ReplicationFolders); string[] replicationFiles = context.GetValue(this.ReplicationFiles); string tfsName = "http://tfs2010:8080/tfs/" + collectionName; TeamFoundationServer tfs = new TeamFoundationServer(tfsName); VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer)); int workSpaceCounter = 0; //for each folder to replicate to, make workspace, get latest, check out dlls foreach (string folder in replicationFolders) { string workspacePath = @"C:\temp\TFSReplication_" + timestamp; Workspace ws = vcs.CreateWorkspace("TFSReplication_" + timestamp, vcs.AuthenticatedUser); ws.Map(folder, workspacePath); ws.Get(); foreach (string file in replicationFiles) { string sourceFilePath = Path.Combine(sourceLocation, file); string repFilePath = Path.Combine(workspacePath, file); //check if file exists in TFS, if not: add file, if so: check out if (!ws.VersionControlServer.ServerItemExists(repFilePath, ItemType.File)) { //copy file then add to TFS File.Copy(sourceFilePath, repFilePath); ws.PendAdd(repFilePath); } else { //check file out, then overwrite ws.PendEdit(repFilePath); File.Delete(repFilePath); //I had some issues with overwriting at one point, this line may not be needed File.Copy(sourceFilePath, repFilePath, true); } } //Check in the file without firing off any Continuous Integration builds ws.CheckIn(ws.GetPendingChanges(), "Build Agent", "***NO_CI***", null, null, new PolicyOverrideInfo("Auto checkin", null), CheckinOptions.SuppressEvent); ws.Delete(); //delete temp workspace. } } } }