如何在不涉及Visual Studio的情况下编译C#解决方案? [英] How can I compile a C# solution without involving Visual Studio?

查看:99
本文介绍了如何在不涉及Visual Studio的情况下编译C#解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完全不喜欢Visual Studio的人,宁愿使用我高度配置的gvim编辑代码,也不愿将其隔离到IDE中.

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.

因此,我的目标是寻找一种方法来编译Web应用程序的C#代码,而无需a肿的> 2gb怪物,而我只会偶尔使用它.

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.

当前,我可以使用以下批处理脚本编译现有代码: http://www.codeproject.com/Questions/495608/howplustopluspublishplussiteplususingplusaspnet_co

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

该问题与对项目文件和解决方案文件的需求有关.是否有办法隔离它们的生成方式(如果我可以获得Visual Studio使用的现有脚本,则可以在自动化脚本中使用远程计算机的副本)还是自己生成它们?

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?

或者,是否有一种方法可以绕过较小的项目和单个表单?不幸的是,我对aspnet_compiler的了解有限.

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

推荐答案

Microsoft.Build名称空间是您的朋友!

The Microsoft.Build namespace is your friend!

示例:制作一个csproj:

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

从解决方案中生成元项目"文件(.sln文件):

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

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

逛逛,逛逛啊哈-知道我有这种事.实际构建生成的metaproject/解决方案文件的项目的示例:

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

这篇关于如何在不涉及Visual Studio的情况下编译C#解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆