2011-05-31 75 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

相關問題