命名空间是你的Microsoft.Build
朋友!
示例:制作一个 csproj:
var pathToLibs = @"..\..\..\libs\";
Engine engine=new Engine();
engine.BinPath=RuntimeEnvironment.GetRuntimeDirectory();
Project project=engine.CreateNewProject();
project.AddNewImport(@"$(MSBuildBinPath)\Microsoft.CSharp.targets", null);
BuildPropertyGroup props=project.AddNewPropertyGroup(false);
props.AddNewProperty("AssemblyName", "myassembly");
props.AddNewProperty("OutputType", "Library");
BuildItemGroup items=project.AddNewItemGroup();
var asmRef = items.AddNewItem("Reference", "SomLibFile");
asmRef.SetMetadata("HintPath", Path.Combine(pathToLibs, @"SomeLibFile.dll"));
items.AddNewItem("Compile", "somefile.cs");
project.Save(@"c:\temp\myproject.csproj");
从解决方案(.sln 文件)生成“元项目”文件:
var foo = SolutionWrapperProject.Generate(
@"path.to.my.sln",
"4.0",
null);
翻找,翻找啊哈——我知道我有这个;实际构建生成的元项目/解决方案文件的项目的示例:
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("UnitTests") || include.Contains("Unit Tests"))
select new { Include=include, Project = pc.LoadProject(Path.Combine(basePath, include))};
foreach (var projectPair in solutionProjects)
{
var project = projectPair.Project;
string.Format("OutDir={0}", project.ExpandString("$(OutDir)")).Dump();
string.Format("OutputPath={0}", project.ExpandString("$(OutputPath)")).Dump();
continue;
var include = projectPair.Include;
var outputPath = outputDir;
project.SetProperty("OutputPath", outputPath);
var projectLogger = new BasicFileLogger();
var tempProjectLogPath = Path.GetTempFileName();
projectLogger.Parameters = tempProjectLogPath ;
Console.WriteLine("Building project:" + project.DirectoryPath);
var buildOk = project.Build("Build", new[]{projectLogger});
if(buildOk)
{
Console.WriteLine("Project build success!");
}
else
{
var logDump = File.ReadAllText(tempProjectLogPath);
logDump.Dump();
throw new Exception("Build failed");
}
}