在 运行 应用程序中获取 TFS ChangeSet

Get TFS ChangeSet in running application

为了这个我已经在网上搜索了大约 2 天。也许我没有使用正确的术语。我想做的是用当前可用的 TFS ChangeSet 变量编译我的项目。我没有使用 TFS Build。

我唯一尝试过但没有成功的方法是将 MSBuild Task 与 MSBuild Extension Pack to get the current ChangeSet(Not only the and put it into the assembly version(Revision). It fail because i dont use Build ... Here is the sample i have try

一起使用

这是我第一次使用这种 "Task" 类型的编程,我不知道我是否可以在不设置 TFS Builds 的情况下做到这一点。我无法设置构建,因为我使用外部 dll 和项​​目,希望设置起来似乎很复杂。 (我的下一步是检查更多关于 TFS 构建的信息)...

我在运行时需要这个值的原因是,我在错误报告功能中需要这个值。我希望使这个过程自动进行以避免错误。

(Visual studio 2013, TFS [VisualStudio.com], Asp.net MVC 5)

解法: 对 Giulio Vian 的坦克

这将获取 当前解决方案 的 TFS ChangeSet,而不是最新的,并将其写入文件 "TFS.cs"。 输出文件如下所示:

static class TFS { public static string ChangeSet { get { return "436"; }}} 

我已经在我的项目的根目录中创建了一个文件 "TFS.targets":

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- 
    add this line to Target to run this only on Release:
    Condition="'$(Configuration)' == 'Release'" -->
  <Target Name="BeforeBuild" > 

    <!-- Get information on the latest build -->
    <Message Importance="High" Text="Getting TFS Chanset"/>
    <Exec Command="&quot;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe&quot; info &quot;$(SolutionPath)&quot; | FIND &quot;Changeset&quot;" ConsoleToMSBuild="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="ChangesetLine" />
    </Exec>
    <PropertyGroup>
      <ChangesetNumber>$(ChangesetLine.Substring(13, $([MSBuild]::Subtract( $(ChangesetLine.IndexOf(';')) , 13 ))))</ChangesetNumber>
    </PropertyGroup>

    <Message Importance="High" Text="ChangeSet: '$(ChangesetNumber)'. Writing TFS.cs ..."/>
    <Exec Command="echo static class TFS { public static string ChangeSet { get { return &quot;$(ChangesetNumber)&quot;; }}} &gt; TFS.cs" Outputs="TFS.cs">
      <!--Add this file to the list to compile-->
      <!--<Output ItemName="Compile" TaskParameter="Outputs" />-->
    </Exec>
  </Target>
</Project>

然后我编辑了项目文件并在项目节点中添加了这一行:

<Import Project="$(MSBuildProjectDirectory)\TFS.targets"/>

(搜索 "Import Project" 你会在你的项目中找到一些)

在第一次构建时,该文件不会出现在您的项目中。所以包括它来使用它。 (显示所有文件)。

如果你想在版本中包含它,用this question but you will need to install MSBuildCommunityTasks

中的代码替换exec echo

另一个更新

经过一些测试,它似乎没有显示 directory/solution 的最后一个 ChangeSet。正确的命令是

tf history "c:\YourSolutionDir" /noprompt /recursive /stopafter:1

问题是它给你一个很难用 MsBuild 提取的响应。

我已经编写了一个控制台应用程序,它将 return 只有变更集:

static void Main(string[] args)
        {

            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe";
            p.StartInfo.Arguments = "history \"" + args[0] + "\" /noprompt /recursive /stopafter:1";
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            var lines = output.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
            var last = lines.Last();
            var s = last.Split(' ');
            Console.WriteLine(s[0]);

        }

TeamBuild 任务只能检索 TFS 构建的变更集。你说你还没有使用 TFS 构建,所以那行不通。

您可以使用 TFS API 从 TFS 中提取变更集。

老实说,如果您无论如何都要使用 TFS 构建系统,那将比仅使用 TFS 构建系统更困难。

您可以调用 tf 命令来获取有关 TFVC 文件和文件夹的信息。通过映射到工作区的本地目录中的 运行 tf info 命令,您可以获得大量有用的数据,例如 Local information: Local path : D:\Workspaces\xyz\mine.sln Server path: $/DefaultCollection/xyz/mine.sln Changeset : 6611 Change : none Type : file Server information: Server path : $/DefaultCollection/xyz/mine.sln Changeset : 6611 Deletion ID : 0 Lock : none Lock owner : Last modified: Saturday, March 14, 2015 09:38:44 Type : file File type : utf-8 Size : 1994

将命令嵌入 MSBuild 文件并不难,如以下代码片段所示 <Exec Command="tf info &quot;$(myChangesSetReferenceFile)&quot; | FIND &quot;Changeset&quot;" ConsoleToMSBuild="true"> <Output TaskParameter="ConsoleOutput" PropertyName="ChangesetLine" /> </Exec> <PropertyGroup> <ChangesetNumber>$(ChangesetLine.Substring(13, $([MSBuild]::Subtract( $(ChangesetLine.IndexOf(';')) , 13 ))))</ChangesetNumber> </PropertyGroup>

tf 命令随团队资源管理器一起安装。