[Journal - Visual Studio to Command Line]

Visual Studio to Command Line

Saturday, September 17, 2005

Suppose you have a Visual Studio 2002 or 2003 project, but not Visual Studio, and need to build the project. It's easy to invoke the compiler on the command line for C# or VB.NET sources - you can simply pass wildcards for the sources, and a few options.

What's really nasty is embedding resources, such as icons. No wildcards are accepted here. Furthermore, Visual Studio assigns namespace-qualified names for embedded icons (for C#: project default namespace plus resource relative path; for VB.NET: project root namespace plus resource name). So our command line is going to grow quite a bit.

What are we to do? First, let's use a response file, where we can list all the options line-by-line:

/res:Images\GoBack.ico,Gregor.Core.Images.GoBack.ico
/res:Images\GoForward.ico,Gregor.Core.Images.GoForward.ico

We'll invoke the compiler like this:

csc.exe @BuildResponse.txt *.cs

If there are many embedded resources, it makes sense to generate the response file from the project file. Here's a .NET Console user function, runnable in WebEdit.NET, that gens command line options in a response file manner for embedded icons in C# project files:

createIconResourceSwitches(sCSharpProjectFile, sNamespace)
{
    sb = new StringBuilder();
    xd = new XmlDocument();
    xd.Load(sCSharpProjectFile);
    sXPath = "/VisualStudioProject/CSHARP/Files/Include/File/attribute::RelPath";
    hits = xd.SelectNodes(sXPath);
    foreach(hit in hits){
        if(hit.Value.EndsWith(".ico")){
            // syntax: /res:<file-name>[,<identifier>]
            sb.Append("/res:");
            sb.Append(hit.Value);
            sb.Append(",");
            sb.Append(sNamespace);
            sb.Append(".");
            sb.Append(hit.Value.Replace("\\", "."));
            sb.Append(Environment.NewLine);
        }
    }
    sb.ToString();
}

On Editing User Functions

By now it's well-know how to compile a .NET Console user function in WebEdit.NET. Open a new editor window, edit the code, and run the following command in the Console tool window:

Vars.Eval(Vars.Text)

Sometimes, you have have a user function that you want to edit, but you haven't saved the source (note that there is an optional persistance mechanism for interpreter code). Then, you can easily retrieve the source into an editor window, like this:

Vars.PasteToNewDocument(createIconResourceSwitches.GetCodeSection(true))

The new PasteToNewDocument, as well as AppendToDocument functions of the Vars module make line-oriented, command-based editing even easier. They both take variable arguments of type string. This means string arrays are also accepted (many Vars properties return such arrays, like TextLines or TextSelectionColumns).