How to get a Workspace object in new TeamFoundation 2013 templates

In the new version of build templates, TeamFoundation 2013 does not have the Workspace variable by default. It is needed as an intput parameter for several key actions, such as ConvertWorkspaceItem . How to get current workspace for TfvcTemplate.12.xaml templates? I tried using this msdn stream , but it does not work for me (returns null workspace name). Any suggestions?

+7
c # tfs tfsbuild tfs2013
source share
4 answers

I went with a hack using the inner classes from Microsoft.TeamFoundation.Build.Activities.dll (used to create a workspace name for Microsoft). You need to create user activity using the following code:

 public sealed class GetDefaultWorkspace : BaseActivity<Workspace> { public override Activity CreateBody() { var type = typeof(TfGetSources).Assembly.GetType("Microsoft.TeamFoundation.Build.Activities.TeamFoundation.TfGetSources+GetDefaultWorkspaceName"); var activity = (CodeActivity<string>)Activator.CreateInstance(type); var sequence = new Sequence(); var workspaceName = new Variable<string>(); sequence.Variables.Add(workspaceName); sequence.Activities.Add(activity); activity.Result = (OutArgument<string>) workspaceName; sequence.Activities.Add(new GetWorkspace { Name = workspaceName, Result = new LambdaReference<Workspace>(ctx => Result.Get(ctx)) }); return sequence; } } 
+2
source share

In 2013, a new activity called GetLocalPath that replaces ConvertWorkspaceItem. The action is under the Microsoft.TeamFoundation.Build.Activities.Core namespace in the Microsoft.TeamFoundation.Build.Activities assembly.

It uses the LocalPathProvider class, which aggregates all the workspaces used in the assembly, and provides a path translation for all of them in one place. This basically eliminates the dependency on workspace knowledge to translate server paths to local paths and allows you to use as many workspaces as you want without worrying about breaking something on the line.

When MS takes something, this is usually for a good reason. "hacking" is not really necessary.

+6
source share

This answer may work better for some people. Ghord's answer works well and passes the workspace back where it can be used in XAML. However, for my purposes, I only want the workspace in my TFS user actions, so I ended up with this alternative ...

 public sealed class CustomActivity : CodeActivity { protected override void Execute(CodeActivityContext context) { // get workspace var buildDetail = context.GetExtension<IBuildDetail>(); var buildAgent = context.GetExtension<IBuildAgent>(); var buildDirectory = buildAgent.GetExpandedBuildDirectory(buildDetail.BuildDefinition); var workspacePath = Path.Combine(buildDirectory, "src"); var wsInfo = Workstation.Current.GetLocalWorkspaceInfo(workspacePath); var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(wsInfo.ServerUri); tfs.Connect(ConnectOptions.None); var vcs = tfs.GetService<VersionControlServer>(); // finally can get to the workspace here var workspace = vcs.GetWorkspace(workspacePath); } } 

Using this method, I should not have activity that simply returns the workspace, and then has to transfer the workspace to other TFS actions. I just get the workspace from my own activity while it works.

+2
source share

I believe that the method used here will use an already loaded workspace. Keep in mind that this approach will only work in the "Run on agent" sequence after the "Initialize Environment" and before the ResetEnvironment at the end of the Try Compile, Test, Publish statement. In addition, the workflow will not know the source directory.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/420ba073-bdf5-4ab4-88da-c84561d1a1ba/creating-dynamic-working-folder-in-tfs2013-defaulttemplate?forum=tfsbuild

0
source share

All Articles