Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Workspace object in new TeamFoundation 2013 Templates

Tags:

c#

tfs

tfsbuild

In new version of TeamFoundation 2013 default build templates, the Workspace variable is missing. It is needed as intput parameter for few key activities like ConvertWorkspaceItem. How do I get current workspace for TfvcTemplate.12.xaml templates? I've tried to use this msdn thread but it's not working for me (returns null workspace name). Any suggestions?

like image 835
ghord Avatar asked Jan 10 '14 13:01

ghord


3 Answers

There's a new activity in 2013 called GetLocalPath that replaces ConvertWorkspaceItem. The activity is under the Microsoft.TeamFoundation.Build.Activities.Core namespace in the Microsoft.TeamFoundation.Build.Activities assembly.

It uses the LocalPathProvider class that aggregates all workspaces used in the build and exposes path translation for all of them in one place. This basically removes the dependency of knowing the workspace in order to translate server paths to local paths and allows you to use as many workspaces as you want without worrying about breaking something down the line.

When MS takes something away, it's usually for a good reason. "hacking" is really not necessary.

like image 65
Tamir Daniely Avatar answered Nov 09 '22 02:11

Tamir Daniely


I went with a hack using internal classes from Microsoft.TeamFoundation.Build.Activities.dll (used by microsoft to create workspace name). You need to create custom activity with 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;
    }
}
like image 23
ghord Avatar answered Nov 09 '22 02:11

ghord


This answer might work better for some people. ghord's answer works well, and passes the Workspace back where it can be used in the XAML. However, for my purposes I only want the workspace in my custom TFS activities, 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 don't have to have an activity that just returns the workspace, and then have to pass the workspace into other TFS activities. I just get the workspace from within my own activity while it runs.

like image 34
John Livermore Avatar answered Nov 09 '22 03:11

John Livermore