How can I compile a C# solution without involving Visual Studio?

天涯浪子 提交于 2019-12-08 13:26:43

问题


I'm someone who does not particularly like Visual Studio at all and would much, much rather use my highly configured gvim to edit code than be fenced into an IDE.

As such, I'm aiming for a way to compile C# code for web applications without the need of a bloated >2gb monstrosity that I will only ever need to use occasionally.

Currently, I can compile existing code with the following batch script: http://www.codeproject.com/Questions/495608/howplustopluspublishplussiteplususingplusaspnet_co

The issue comes with the need for a project file and a solution file. Is there a way to isolate how these are generated (If I can get an existing script Visual Studio uses, I can use a remote machine's copy in an automated script) or generate them yourself?

Or, alternatively, is there a way to bypass them for smaller projects and individual forms? Unfortunately, my knowledge of aspnet_compiler is limited.


回答1:


The Microsoft.Build namespace is your friend!

Example: making a 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");

Generating a "metaproject" file from a solution (.sln file):

var foo = SolutionWrapperProject.Generate(
   @"path.to.my.sln", 
    "4.0", 
    null);

rummage, rummage Aha - knew I had this around; an example of actually building projects of a generated metaproject/solution file:

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");
    }
}


来源:https://stackoverflow.com/questions/14349419/how-can-i-compile-a-c-sharp-solution-without-involving-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!