【问题标题】:How can I compile a C# solution without involving Visual Studio?如何在不涉及 Visual Studio 的情况下编译 C# 解决方案?
【发布时间】:2013-01-16 08:59:19
【问题描述】:

我是一个根本不特别喜欢 Visual Studio 的人,我更愿意使用我高度配置的 gvim 来编辑代码,而不是被隔离在 IDE 中。

因此,我的目标是找到一种方法来为 Web 应用程序编译 C# 代码,而不需要我只需要偶尔使用的臃肿的 >2gb 怪物。

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

问题在于需要项目文件和解决方案文件。有没有办法隔离这些是如何生成的(如果我可以获得 Visual Studio 使用的现有脚本,我可以在自动化脚本中使用远程机器的副本)或自己生成它们?

或者,对于较小的项目和单个表单,有没有办法绕过它们?不幸的是,我对 aspnet_compiler 的了解有限。

【问题讨论】:

  • 我相信您可以在不抨击 IDE 的情况下提出这个问题。 Visual Studio 做过什么伤害你的感情?
  • 对不起 x.x。我是 Linux 用户 - 我有点习惯于更小、更时尚的基于控制台的应用程序(不可否认,因为我在 Windows 上工作,所以我必须使用 gvim 而不是 vim)和一个可以安装超过 4gb 的 IDE吓到我了。
  • 也许你的意思是“让我不知所措”而不是“让我害怕”。
  • @LaniAlden 没问题 :)。顺便一提。我的手机有超过 4GB 的存储空间。时代变了。

标签: c# asp.net visual-studio-2010 batch-file aspnet-compiler


【解决方案1】:

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

【讨论】:

  • 非常感谢!这对帮助我大有帮助!
  • 没问题!这种类型的东西对于在构建服务器上动态生成和构建项目也是黄金。我已经写了上面的十几个变体...... :)
猜你喜欢
  • 2020-09-05
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多