2013-05-11 61 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

相关问题