无法以编程方式将文件添加到TFS源代码管理

时间:2014-10-24 17:41:19

标签: tfs tfs2012 tfs2013

我有以下示例活动,它尝试在构建过程中将新创建的文件添加到源代码管理中。我没有修改现有文件和检查修改的问题,但添加一个新文件让我适合。

这是一个简单的活动,它抓取Workspace,创建一个文件,然后失败(代码后面的消息)。

有人能看到这里的问题吗?

using System;
using System.Activities;
using System.IO;
using Medallion.Shell;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Build.Workflow.Activities;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace RQTfsActivities
{
    public sealed class AddFileToTfs : CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            // grab the build directory
            var buildDirectory = GetBuildDirectory(context);

            // create the file
            var sourceFile = Path.Combine(buildDirectory, "src", "FileToBeAdded.txt");
            File.WriteAllText(sourceFile, "filecontents");

            // throw if the previous failed somehow - it doesn't
            if (!File.Exists(sourceFile)) throw new Exception("File not written");

            // grab the workspace
            var workspace = GetWorkspace(context);

            // attempt to add the newly created file 
            var affectedCt = workspace.PendAdd(Path.GetDirectoryName(sourceFile));

            // output the affected count -- this is zero, but should be one!
            TrackingExtensions.TrackBuildMessage(context, string.Format("AffectedCt = {0}", affectedCt));

            // this throws an exception
            workspace.CheckIn(workspace.GetPendingChanges(), "Adding sample file to source control");
        }

        private string GetBuildDirectory(CodeActivityContext context)
        {
            var buildDetail = context.GetExtension<IBuildDetail>();
            var buildAgent = context.GetExtension<IBuildAgent>();
            return buildAgent.GetExpandedBuildDirectory(buildDetail.BuildDefinition);
        }

        private Workspace GetWorkspace(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>();
            return vcs.GetWorkspace(workspacePath);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

// attempt to add the newly created file 
var affectedCt = workspace.PendAdd(Path.GetDirectoryName(sourceFile));

您打算将此作为workspace.PendAdd(sourceFile)吗?