【问题标题】:Passing a TFS Workspace object to c# code from Powershell?将 TFS 工作区对象从 Powershell 传递给 c# 代码?
【发布时间】:2015-06-25 09:50:56
【问题描述】:

我目前正在创建 Powershell 脚本,以便在 TFS 2013 中创建更易于维护的构建过程。

此过程的一部分需要多次 SQL 比较才能使用更新脚本运行。

我正在尝试使用我们旧的自定义构建活动之一(来自 TF 2010),它看起来相当容易转换为 c# 代码文件以在 Powershell 中使用。 这是构建后/单元前测试步骤所必需的。

我想让升级可用于单元测试,其中一些是基于数据库的集成测试,以前直到下一个版本才升级。

问题是我的 C# 脚本需要 TFS 工作区,我不确定如何或在何处正确实例化它。 Build Activity 使用 TFS 代码模型,它以 InArguments 作为属性,并处理了所有这些。

我已将其更改为如下所示的命令运行,如下面的 C# 示例所示:

using System.IO;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Build.Workflow.Activities;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Activities;
using System;
using System.Collections.Generic;
using System.Linq; 

public class UpdateAndCheckinNewScripts
{
    Workspace workspace {get; set;}

    public UpdateAndCheckinNewScripts(Workspace workspace)
    {
         this.workspace = workspace; 
    }

    public void Execute(string versionToUpdate, string sourcesDirectory, string binariesDirectory)
    {
          //... do some stuff to scripts - ommitted...

          //Copy over previous script
           File.Copy(projectCreatefilePath, projectFinalPath, true);

          //Checkin Project Create Script
          workspace.CheckIn(workspace.GetPendingChanges(projectFinalPath), "Build Agent", "My checkin",  null, null, new PolicyOverrideInfo("Auto checkin", null),CheckinOptions.SuppressEvent);
    }

    private static void CheckinScript(string upgradeHolder,
            string filePath, string sourcesDirectory, string upgradeReplacementText, Workspace workspace)
        {
            string file = sourcesDirectory + @"\\yyy\xxx\Upgrade.csproj";

            //Checkout the project file <-- How do i create workspace to pass into  object? 
            workspace.PendEdit(file);

            workspace.PendAdd(filePath);
            // ... other code 
    }
}

如果可能,我想避免彻底重写此步骤,但对想法持开放态度。 我想摆脱在 TFS 2013 中自定义默认 Xaml 构建定义。 我从这个链接中获取一些建议作为在 Powershell 中使用 .net 程序集的起点:

http://ruudvanderlinden.com/2010/10/19/running-inline-c-with-custom-assemblies-in-powershell-2-0/

我想知道如何获取当前构建的 TFS 工作区对象,以及如何实例化。

我认为 PS 脚本可能看起来像这样:

# assume I've run SQL comparison, create upgrade script and backups as needed....

# assuming all assembly and custom references required will have been added, here...

$tfWorkspace = ??? #not sure here
Add-Type -TypeDefinition $Source #assume $Source is the above c# source.
$scriptingobject = New-Object UpdateAndCheckinNewScripts($tfWorkspace)
$scriptingobject.Execute($buildVersion,$sourceLocalFolder,$binDirectory)

#continue with custom script...

我认为我有两个选择:

  1. 以某种方式在 C# 代码中实例化工作区对象。

  2. 在 PowerShell 中实例化对象并传递给实例化对象 对象。

如果有人可以为我提供任何一个选项或任何其他想法,我将不胜感激。

【问题讨论】:

  • 好的 - 所以我最终解决了我的问题,有点学习曲线。请参阅下面的答案

标签: c# .net visual-studio-2010 powershell visual-studio-2013


【解决方案1】:

我最终使用了 powershell 脚本,正如 Microsoft 开发网站所提到的:https://msdn.microsoft.com/en-us/library/dn376353.aspx

我将它与 TFS Power 工具结合使用,并使用了 Add-TfsPendingChangeNew-TfsChangeset cmdlet,但我还使用了需要更多控制的 TFS API .Net 对象。

我不再使用我们最初的自定义构建活动,但我们确实自定义了构建定义,以允许在 TFS 2013 标准模板的更多阶段调用更多 Powershell 脚本。

我只能说,Powershell 非常强大,值得努力学习,它几乎完成了我们构建过程所需的一切。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多