0

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 2
    I'm sure you could have asked this question without bashing the IDE. What did Visual Studio ever do to hurt your feelings? – Reactgular Jan 16 '13 at 00:27
  • Sorry x.x. I'm a Linux user - I'm a bit used to smaller, sleeker console-based apps (admittedly since I'm on Windows at work I have to use gvim instead of vim) and an IDE that can be over 4gb installed kind of horrifies me. –  Jan 16 '13 at 00:28
  • 1
    Have a look at http://monodevelop.com/ – oleksii Jan 16 '13 at 00:30
  • Perhaps you meant "overwhelmed me" than "horrified me". – makerofthings7 Jan 16 '13 at 00:30
  • @LaniAlden no problem :). By the way. My phone has more then 4gb of storage. Times have changed. – Reactgular Jan 16 '13 at 00:35
  • Time have changed, but still. In terms of monodevelop, it's a more acceptable alternative (not great since it doesn't solve my 'wanting to use my own IDE thanks' problem), but... We're not allowed to install anything on these machines =/. I may just pop in a liveCD and change the admin password though because seriously. I can get OIT to install Visual Studio, or I can get this working. –  Jan 16 '13 at 00:39
  • @LaniAlden I sympathize, although they'd have to pry my VS from my cold, dead, etc, etc...I just edited the below to include a "how to build" example. Have fun. :) – JerKimball Jan 16 '13 at 01:00
  • I have used many IDE's - Eclipse, NetBeans, MonoDevelop, Visual Studio (2005,2008, 2010, and 2012) and personally prefer VS 2010 over all of them. I use monodevelop for IOS and it's a good IDE but would prefer to use VS for .Net development. If you can get them to install it - do it and learn it. It only makes you better. :) – tsells Jan 16 '13 at 02:41
  • I have used many IDE's - Eclipse, NetBeans, MonoDevelop, Visual Studio (2010 and 2012). I don't develop for iOS. You should get your boss to install VIM on your machine - do it and learn it. It only makes you better. :) –  Jan 16 '13 at 07:39

1 Answers1

7

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");
    }
}
JerKimball
  • 16,584
  • 3
  • 43
  • 55
  • Thanks so much! This goes a long way to helping me out! –  Jan 16 '13 at 01:13
  • No problem! This type of stuff is also gold for generating-and-building projects dynamically on build servers; I've written what seems like a dozen variants of the above... :) – JerKimball Jan 16 '13 at 01:16