2013-02-27 43 views
4

我正在使用MSBUILD API來使用服務構建解決方案。使用API​​跳過/排除MSBUILD中的項目類型

var pc = new ProjectCollection(); 
var buildProperties = new Dictionary<string, string> 
{ 
    {"Configuration", "Release"}, 
    {"Platform", "Any CPU"}, 
    {"OutputPath", _outputPath} 
}; 

var buildParameters = new BuildParameters(pc); 

var buildRequest = new BuildRequestData(_buildFile, buildProperties, null, new[] { "Clean", "Rebuild" }, null);    

var buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest); 

我希望能夠做的,是通過在排除項目類型或擴展名的列表。首先我想排除:

  • 數據庫項目
  • 的WinRT項目
  • 通用MSBUILD文件(沒有項目類型GUID)。

有什麼辦法可以通過傳遞一些參數到MSBUILD管理器來解決這個問題嗎?

回答

4

這不是相當你以後,但我只是碰巧與舊的一天的工作過程中的MSBuild API擺弄周圍,並推斷該代碼可能是有用的:

var basePath = "path-to-where-source-is"; 
var outputDir = "path-to-output"; 

// Setup some properties that'll apply to all projs 
var pc = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection; 
pc.SetGlobalProperty("Configuration", "Debug"); 
pc.SetGlobalProperty("Platform", "Any CPU"); 
pc.SetGlobalProperty("OutDir", outputDir); 

// Generate the metaproject that represents a given solution file 
var slnProjText = SolutionWrapperProject.Generate(
    Path.Combine(basePath, "NAME-OF-SOLUTION-FILE.sln"), 
    "4.0", 
    null); 

// It's now a nice (well, ugly) XML blob, so read it in 
using(var srdr = new StringReader(slnProjText)) 
using(var xrdr = XmlReader.Create(srdr)) 
{ 
    // Load the meta-project into the project collection   
    var slnProj = pc.LoadProject(xrdr, "4.0"); 

    // Slice and dice the projects in solution with LINQ to 
    // get a nice subset to work with 
    var solutionProjects = 
     from buildLevel in Enumerable.Range(0, 10) 
     let buildLevelType = "BuildLevel" + buildLevel 
     let buildLevelItems = slnProj.GetItems(buildLevelType) 
     from buildLevelItem in buildLevelItems 
     let include = buildLevelItem.EvaluatedInclude 
     where !include.Contains("Some thing I don't want to build") 
     select new 
     { 
      Include=include, 
      Project = pc.LoadProject(Path.Combine(basePath, include)) 
     }; 

    // For each of them, build em! 
    foreach (var projectPair in solutionProjects) 
    { 
     var project = projectPair.Project; 
     var include = projectPair.Include; 
     var outputPath = outputDir; 
     project.SetProperty("OutputPath", outputPath); 
     Console.WriteLine("Building project:" + project.DirectoryPath); 
     var buildOk = project.Build("Build"); 
     if(buildOk) 
     { 
      Console.WriteLine("Project build success!"); 
     } 
     else 
     { 
      throw new Exception("Build failed"); 
     } 
    } 
} 
+0

SolutionWrapperProject看起來是個人! – Doug 2013-02-28 00:53:03

+1

Yessir - 解決方案包裝器處理從解決方案語法(這是一個遺留問題)轉換到msbuild項目語法,這是更好(和適當的xml) – JerKimball 2013-02-28 00:53:19