2011-05-31 79 views
2

我需要向默认工作流模板添加自定义活动,以在构建过程中尽可能早的时候增加装配版本。如何在自定义工作流程活动中获取构建细节?

我想实现的是在我的自定义活动中创建和映射完全相同的工作区(在工作流中进一步创建),以便检出一个xml文件,增加内部保存的版本号,把它重新写回到xml文件中,然后检查xml文件。

我知道这个工作空间将在稍后的工作流中创建,但在构建过程中为时过晚试图实现,所以而不是移动任何活动或复制他们在我的自定义活动上方的位置(这应该是好的,因为此工作空间将被删除并稍后再次创建)

我认为我需要的细节是BuildDirectory,WorkspaceName和SourcesDirectory。任何人都可以告诉我如何实现创建工作空间或如何在代码中获取这些数据?

构建将在构建服务器上进行,我使用的是TFS 2010和c#。

在此先感谢

回答

3

我通过Ewald Hofman作为底漆沿袭了一系列博客文章,并创造了不退房,更新和签入一个GlobalAssemblyInfo文件的,我分析了当前版本的自定义活动从。我的任务插入到“更新放置位置”的顶部,该位置位于执行工作流的“获取构建”部分之后。我只需要使用IBuildDetail和一个文件掩码作为参数,您可以从中抽取出VersionControlServer以访问TFS。我的代码如下:

protected override string Execute(CodeActivityContext context) 
    { 
     // Obtain the runtime value of the input arguments. 
     string assemblyInfoFileMask = context.GetValue(AssemblyInfoFileMask); 
     IBuildDetail buildDetail = context.GetValue(BuildDetail); 

     var workspace = buildDetail.BuildDefinition.Workspace; 
     var versionControl = buildDetail.BuildServer.TeamProjectCollection.GetService<VersionControlServer>(); 

     Regex regex = new Regex(AttributeKey + VersionRegex); 

     // Iterate of the folder mappings in the workspace and find the AssemblyInfo files 
     // that match the mask. 
     foreach (var folder in workspace.Mappings) 
     { 
      string path = Path.Combine(folder.ServerItem, assemblyInfoFileMask); 
      context.TrackBuildMessage(string.Format("Checking for file: {0}", path)); 
      ItemSet itemSet = versionControl.GetItems(path, RecursionType.Full); 

      foreach (Item item in itemSet.Items) 
      { 
       context.TrackBuildMessage(string.Format("Download {0}", item.ServerItem)); 
       string localFile = Path.GetTempFileName(); 

       try 
       { 
        // Download the file and try to extract the version. 
        item.DownloadFile(localFile); 
        string text = File.ReadAllText(localFile); 
        Match match = regex.Match(text); 

        if (match.Success) 
        { 
         string versionNumber = match.Value.Substring(AttributeKey.Length + 2, match.Value.Length - AttributeKey.Length - 4); 
         Version version = new Version(versionNumber); 
         Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision); 

         context.TrackBuildMessage(string.Format("Version found {0}", newVersion)); 

         return newVersion.ToString(); 
        } 
       } 
       finally 
       { 
        File.Delete(localFile); 
       } 
      } 
     } 

     return null; 
    } 
+0

感谢您的帮助,它是需要的! – Vermin 2011-06-01 08:42:29

+0

你碰巧知道是否有任何方式访问BuildDetail而不用它作为参数?只是好奇。 – 2012-03-27 17:51:28

+0

我不知道有这样做的方法。 – 2012-04-03 12:05:54

相关问题