So, I ran into an interesting problem today. I wanted to see if I could use HomeSite+ to compile and run c# programs. HS+ makes it easy to add custom buttons, and it is easy enough to bind a button to an external program. So the first thing I did was add a button to call cfc and pass in %CURRENTFILE% as the argument. However, this simply resulted in a DOS window that appeared for one brief second and then immidiately went away. Not sure of what to do, I decided to switch to a simple bat file:

csc %1
pause

I switched my button to call my bat file and this corrected the "Disappearing DOS" window issue, but it caused another problem. The literal string of the filename, in this case, c:\document and settings\etc, was being passed, and the spaces caused an issue with csc. So, I simply wrapped %1 with quotes. Tada - problem solved.

Or so I thought.

When csc runs, it compiles the file in the same directory that you run csc in. In this case, my bat file was at the root of C, which means the .exe from my c# file ended up in the root directory. Luckily the c# compiler supports an /out: directive that allows you to specify the full path and name of the output file.

However, I was passing in path\foo.cs, and I needed to end up with path\foo.exe. Once again, HS+ came to the rescue. I switched from calling my bat file directly to calling a piece of JavaScript code I created from within HS+. Here is the code:

function Main() {

   var fileName = Application.ActiveDocument.Filename;
   var fileNameEXE = fileName.substring(0,fileName.length-3) +".exe";
   var commandToRun = 'c:\\cs.bat"' + fileName + '""' + fileNameEXE + '"';

   //remove extension    with (Application) {
      ShellToApp(commandToRun);
   }

}

Basically, I get the current file name, remove the ".cs" and ".exe" to a new version of the file name. I then call my bat file and pass in the name of the file to compile as well as the name of the file to generate. cs.bat simply is:

@echo off
csc /out:%2 %1 /nologo
pause

This now compiled my code and paused so I could see any issues.

Next I wanted to see if I could run the current document. Once again I created a button, and as before, I made it call a JavaScript file:

function Main() {

   var fileName = Application.ActiveDocument.Filename;
   fileName = fileName.substring(0,fileName.length-3) +".exe";

   //remove extension    with (Application) {
      ShellToApp('c:\\csrun.bat"' + fileName + '"');
   }

}

As before, I take the current file and remove the .cs and add .exe. This time I call a bat file called csrun.bat, all it does is:

@echo off
call %1
pause

Which basically means, run the file I was given and pause.

So far so good. Now I can compile and run simple programs from HS+. I could also easily modify this to call another compiler, like javac. My next modification will be to make my "run" JS code prompt for input arguments to pass to the c sharp file. Enjoy.