|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

I receive every month or so, a free magazine from VSJ. The articles in the magazine focus on "visual" languages such as Java, VB.NET, etc. Along with the articles, there is an "Outlook" section that discusses some recent developments or something similar, and also book reviews as well as the usual advertising.
In the Feb 5th edition, there was an article entitled "Script it with C#" (you can read the original here). This article is a .NET 2.0 version inspired by the original VSJ article but duly rewritten from the ground up.
I contemplated changing the existing article to support the .NET 2.0 Framework back when it was still in Beta1, however when I tried this, it ran into a plethora of errors, and due to the time constraints, I dropped it. This evening, I decided that it would be nice if it did work under .NET 2.0 (just because of creating support for generics :) ) and so I did it.
Included inside the zip file are the source files (or should I say file) along with a VC# Express project to compile it with. However, because it's just a single file, you should be able to compile it quite easily using csc.exe.
There are two routines inside the class, Main and RunScript.
Main, as usual, takes care of loading the program. In this case, all it does is check that a file was passed as the first argument, and then runs it passing the remaining arguments to the script. I decided to leave the first argument in the array so that the script has an easy way of determining where on the hard drive it is located. I.e. arg[0]=scripfile.csx, arg[1...]=....
private static void Main(string[] args)
{
// If no arguments then display error
if ((args.Length == 0) || (!File.Exists(args[0])))
{
MessageBox.Show("A script file must be provided" +
" on the command-line.", "No Script File",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// Run the script (we'll leave the first argument so
// that the script knows where it is :)
CSScriptCompiler.RunScript(args[0], args);
}
}
RunScript takes care of all the hard work. Basically, the following steps occur:
#using, #import, or #target directive, then it is skipped, and these are explained below.
using statements at the beginning of the source file and reference the necessary assemblies.
Main routine to encapsulate the script.
Main was declared with or without the command-line arguments option and invoke the appropriate method. This is analogous to the C# using statement, it simply imports a namespace so that you can use shorthand versions of the class names. I decided to prefix the statement with a # because it is a bit simpler to avoid clashes with the actual C# statement.
A possibility that crossed my mind was to use regular expressions to increase the robustness of parsing, however, I decided to opt out because it was easier and quicker to accomplish.
I never got on well with Dino's system of using a separate file to reference the DLLs, the task is made much easier if all the code is contained within one source file and that's why I introduced the #import directive. This basically references an assembly so that you can use it in the script; the filename can be relative, absolute, or if it's on the paths searched by csc, then it can just be a filename.
This is like the C #include statement. All that really happens is this line is replaced with the contents of filename. You can of course have includes within includes within includes (insert recursion loop here :) ).
Note: Remember to take this into account when debugging errors. I have not thought of an elegant solution which will keep track of the source files or line numbers.
Along with the source / demo project is an installer created with NSIS which will register the .csx extension for the compiler so that when you run a script file, it automatically executes css.exe, the C# script compiler. It also associates an icon with the extension to identify the file-type.
This installer will be compiled when you build the solution (if you have NSIS on your machine and installed in the default path).
Here is a list of the main features of the application:
Main routine.
If anybody comes up with any cool or useful scripts that could be included with the source, then send me an email through the CP website and if they're good enough I'll include them in the zip.
#include statement.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Jan 2006 Editor: Rinish Biju |
Copyright 2006 by Ed.Poore Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |