2013-10-08 28 views
3

我有幾個MSBuild任務在生成過程中生成新文件。自定義目標在生成後將這些文件添加到編譯項目組。因此,如果我的目標運行,構建將會工作,但是我想使用MSBuild的增量構建功能,並且如果這些文件自上次構建以來尚未修改,則跳過這些目標。但是如果構建跳過目標,那麼生成的文件將不會被包含。將MSBuild生成的文件添加到Visual Studio解決方案資源管理器中

所以,我想讓生成將文件添加到解決方案資源管理器。我意識到我可以手動添加這些文件,並且這可能是我必須去的路線,但我真的希望以編程方式添加生成的文件。

我目前有一個名爲Custom.targets的文件。它被包含在每個項目中並注入新的目標。我已經嘗試在項目中包含* .cs,但這並不奏效。

+0

您是否在討論將「條目」添加到csproj文件?還是.sln文件?或者是什麼? – granadaCoder

+1

這些將是csproj文件中的條目。 –

+0

您可以在這裏按照答案(我留給自己的問題):http://stackoverflow.com/questions/13867117/how-to-add-a-linked-file-to-a-csproj-file-with -msbuild-3-5-framework我使用了一個Msbuild(xmlupdate)任務來通過msbuild代碼添加一個「鏈接文件」。 – granadaCoder

回答

0

我最終做了類似granadaCoder的事情。我只是決定在自定義任務內部而不是在xml中完成它。我確保項目中包含3個文件,FilePath,BinPath和HooksPath。如果他們都在那裏,沒有任何反應。如果缺少它,則將其添加到ItemGroup並保存該文檔。在構建過程中不能保存文檔。所以它需要在保存後再次運行構建。

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Xml.Linq; 
using Microsoft.Build.Framework; 
using Microsoft.Build.Utilities; 

namespace BuildTasks 
{ 
    public class AddFilesToProject : Task 
    { 
     [Required] 
     public string ProjectPath { get; set; } 

     [Required] 
     public string FilePath { get; set; } 

     [Required] 
     public string BinPath { get; set; } 

     [Required] 
     public string HooksPath { get; set; } 

     [Required] 
     public string ProjectDir { get; set; } 


     /// <summary> 
     /// When overridden in a derived class, executes the task. 
     /// </summary> 
     /// <returns> 
     /// true if the task successfully executed; otherwise, false. 
     /// </returns> 
     public override bool Execute() 
     { 

      try 
      { 
       var binRelative = BinPath.Replace(ProjectDir + "\\", ""); 
       var hooksRelative = HooksPath.Replace(ProjectDir + "\\", ""); 
       var fileRelative = FilePath.Replace(ProjectDir + "\\", ""); 
       XDocument document = XDocument.Load(ProjectPath); 
       XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003"; 

       bool foundBin = false; 
       bool foundHooks = false; 
       bool foundFile = false; 
       XElement itemGroup = null; 
       foreach (XElement el in document.Descendants(ns + "ItemGroup")) 
       { 
        foreach (XElement item in el.Descendants(ns + "Compile")) 
        { 
         itemGroup = el; 
         if (item.Attribute("Include").Value.Contains(binRelative)) 
         { 
          foundBin = true; 
          Log.LogMessage(MessageImportance.Low, "FoundBin: {0}", foundBin); 
         } 
         else if (item.Attribute("Include").Value.Contains(hooksRelative)) 
         { 
          foundHooks = true; 
          Log.LogMessage(MessageImportance.Low, "FoundHooks: {0}", foundHooks); 
         } 
         else if (item.Attribute("Include").Value.Contains(fileRelative)) 
         { 
          foundFile = true; 
          Log.LogMessage(MessageImportance.Low, "FoundFile: {0}", foundFile); 
         } 
        } 
       } 

       if (!foundBin) 
       { 
        XElement item = new XElement(ns + "Compile"); 
        item.SetAttributeValue("Include", binRelative); 
        if (itemGroup != null) itemGroup.Add(item); 
       } 
       if (!foundHooks) 
       { 
        XElement item = new XElement(ns + "Compile"); 
        item.SetAttributeValue("Include", hooksRelative); 
        if (itemGroup != null) itemGroup.Add(item); 
       } 
       if (!foundFile) 
       { 
        XElement item = new XElement(ns + "Compile"); 
        item.SetAttributeValue("Include", fileRelative); 
        if (itemGroup != null) itemGroup.Add(item);      
       } 
       if (!foundBin || !foundHooks || !foundFile) 
       { 
        document.Save(ProjectPath); 
       } 
      } 
      catch (Exception e) 
      { 
       Log.LogErrorFromException(e); 
      } 

      return !Log.HasLoggedErrors; 
     } 
    } 
} 
相關問題