简体   繁体   中英

Get TFS ChangeSet in running application

I have searched over the net for about 2 day now for this. Maybe i dont use the right terms. What i wish to do is to compile my project with the current TFS ChangeSet varriable avaliable. I'm not using TFS Build.

The only thing i have try, without success, is to use MSBuild Task with 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

It's the first time i use this "Task" kind of programming and i dont know if i can do this without setuping TFS Builds. I can't setup Builds because i use external dlls and projects, wish seem to be complicated to setup. (My next step is to check more about TFS Builds) ...

The raison i need this value at runtime, it's because i need this value in our error report feature. I wish to make this process automatic to avoid errors.

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

Solution: Tanks to Giulio Vian

This get the TFS ChangeSet of the current solution , not the newest, and write it in a file "TFS.cs". the output file look like this:

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

i have created a file "TFS.targets" into the root of my project:

<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>

Then i have edited the project file and added this line in the Project node:

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

(Search for "Import Project" you will find some in your project)

on the first build, the file will not be in your project. So include it to use it. (Show all files).

if you wish to include it in the version, replace the exec echo by the code in this question but you will need to install MSBuildCommunityTasks

An other update

After some testing, it seem it dont show the last ChangeSet for the directory/solution. the correct command is

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

the problem with this is it give you a response who is hard to extract with MsBuild.

i have writed an console application who will return only the changeset:

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]);

        }

The TeamBuild task can only retrieve ChangeSets for a TFS build. You said you aren't using TFS builds yet so that won't work.

You could use the TFS API to pull changesets from TFS.

Honestly though that will be more difficult than just using the TFS build system if you were going to go to that anyways.

You can call the tf command to get information about TFVC files and folders. By running the tf info command in a local directory mapped to a workspace, you get lot of useful data, eg 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

Embedding the command in an MSBuild file is not hard as the following snippet shows <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> <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>

The tf command comes installed with Team Explorer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM