2013-05-11 60 views
0

是否可以從MsBuild定製任務中知道哪個解決方案正在構建項目,或者哪些其他項目也參與構建?自定義MsBuild任務:哪些解決方案/其他項目正在參與構建?

編輯:

試圖闡明上下文一點。

比方說,我有以下設置:

Company 
+- LibA 
    +- LibA.csproj 
+- LibB 
    +- LibB.csproj 
+- App1 
    +- App1.sln : App1.csproj, LibA.csproj, LibB.csproj 
    +- App1.csproj 
+- App2 
    +- App2.sln : App2.csproj, LibA.csproj 
    +- App2.csproj 

所以你可以看到兩個App1和App2的溶液中使用力霸,包括它。然而LibB只存在於一個解決方案中。

現在我們假設LibA和LibB之間存在某種關係,並且此關係是通過LibA/LibA.csproj中的自定義MsBuild任務來處理的。然而,要做到這一點,自定義任務需要知道LibB是否參與了當前的構建,或者是否存在於當前的解決方案中。請記住,這是兩個解決方案中使用的相同的csproj文件。

我不介意自動或通過添加元數據到.sln文件。

有沒有辦法做到這一點?

+0

能否請您對您的問題展開?也許增加一個樣品或元樣? – oleksii 2013-05-11 10:53:31

+1

你可以在LibB添加自定義屬性並在LibA中使用條件來檢查該屬性的存在 – Nicodemeus 2013-05-14 01:34:27

回答

1

您可以解析csprojs的.sln(由於其不是xml),但可以解析csproj以獲取引用和依賴關係。

下面是一些示例代碼(可能會變成你的自定義任務。

string fileName = @"C:\MyFolder\MyProjectFile.csproj"; 

    XDocument xDoc = XDocument.Load(fileName); 

    XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); 

    //References "By DLL (file)" 
    var list1 = from list in xDoc.Descendants(ns + "ItemGroup") 
       from item in list.Elements(ns + "Reference") 
       /* where item.Element(ns + "HintPath") != null */ 
      select new 
       { 
        CsProjFileName = fileName, 
        ReferenceInclude = item.Attribute("Include").Value, 
        RefType = (item.Element(ns + "HintPath") == null) ? "CompiledDLLInGac" : "CompiledDLL", 
        HintPath = (item.Element(ns + "HintPath") == null) ? string.Empty : item.Element(ns + "HintPath").Value 
       }; 


    foreach (var v in list1) 
    { 
     Console.WriteLine(v.ToString()); 
    } 


    //References "By Project" 
    var list2 = from list in xDoc.Descendants(ns + "ItemGroup") 
       from item in list.Elements(ns + "ProjectReference") 
       where 
       item.Element(ns + "Project") != null 
       select new 
       { 
        CsProjFileName = fileName, 
        ReferenceInclude = item.Attribute("Include").Value, 
        RefType = "ProjectReference", 
        ProjectGuid = item.Element(ns + "Project").Value 
       }; 


    foreach (var v in list2) 
    { 
     Console.WriteLine(v.ToString()); 
    } 
+0

將環境變量「MSBuildEmitSolution」設置爲「1」將導致MSBUILD發出SolutionName.sln.metaproj文件,MSBUILD將該.sln轉換爲項目格式,允許您像解析任何XML文件一樣解析它:P – Nicodemeus 2013-05-14 01:31:34

+0

尼斯提示尼克..謝謝。 – granadaCoder 2013-05-14 05:47:12

相關問題