org.apache.tools.ant.taskdefs.optional.dotnet
Class CSharp

java.lang.Object
  |
  +--org.apache.tools.ant.Task
        |
        +--org.apache.tools.ant.taskdefs.MatchingTask
              |
              +--org.apache.tools.ant.taskdefs.optional.dotnet.CSharp

public class CSharp
extends MatchingTask

This task compiles CSharp source into executables or modules. The task will only work on win2K until other platforms support csc.exe or an equivalent. CSC.exe must be on the execute path too.

All parameters are optional: <csc/> should suffice to produce a debug build of all *.cs files. References to external files do require explicit enumeration, so are one of the first attributes to consider adding.

The task is a directory based task, so attributes like includes="*.cs" and excludes="broken.cs" can be used to control the files pulled in. By default, all *.cs files from the project folder down are included in the command. When this happens the output file -if not specified- is taken as the first file in the list, which may be somewhat hard to control. Specifying the output file with 'outfile' seems prudent.

TODO

  1. is incremental build still broken in beta-1?
  2. is Win32Icon broken?
  3. all the missing options

History
0.3 Beta 1 edition To avoid having to remember which assemblies to include, the task automatically refers to the main dotnet libraries in Beta1.
0.2 Slightly different Split command execution to a separate class;
0.1 "I can't believe it's so rudimentary" First pass; minimal builds only support;

Version:
0.3
Author:
Steve Loughran steve_l@iseran.com

Field Summary
protected  boolean _debug
          debug flag.
protected  java.io.File _docFile
          output XML documentation flag
protected  java.lang.String _extraOptions
          any extra command options?
protected  boolean _failOnError
          flag to control action on execution trouble
protected  boolean _includeDefaultReferences
          flag to enable automatic reference inclusion
protected  boolean _incremental
          incremental build flag
protected  java.lang.String _mainClass
          main class (or null for automatic choice)
protected  boolean _optimize
          optimise flag
protected  java.lang.String _outputFile
          output file.
protected  Path _referenceFiles
          using the path approach didnt work as it could not handle the implicit execution path.
protected  java.lang.String _references
          list of reference classes.
protected  java.lang.String _targetType
          type of target.
protected  boolean _unsafe
          enable unsafe code flag.
protected  java.io.File _win32icon
          icon for incorporation into apps
protected static java.lang.String csc_exe_name
          name of the executable.
protected static java.lang.String csc_file_ext
          what is the file extension we search on?
protected static java.lang.String csc_file_pattern
          derive the search pattern from the extension
protected static java.lang.String DEFAULT_REFERENCE_LIST
          C# is really dumb in how it handles inclusion.
 
Fields inherited from class org.apache.tools.ant.taskdefs.MatchingTask
fileset, useDefaultExcludes
 
Fields inherited from class org.apache.tools.ant.Task
description, location, project, target, taskName, taskType, wrapper
 
Constructor Summary
CSharp()
          constructor inits everything and set up the search pattern
 
Method Summary
 void Clear()
          reset all contents.
 void execute()
          do the work by building the command line and then calling it
protected  java.lang.String getAdditionalModulesParameter()
          get the argument or null for no argument needed
 boolean getDebug()
          query the debug flag
protected  java.lang.String getDebugParameter()
          get the debug switch argument
protected  java.lang.String getDefaultReferenceParameter()
          get default reference list
protected  java.lang.String getDefinitionsParameter()
          get the argument or null for no argument needed
protected  java.lang.String getDocFileParameter()
          get the argument or null for no argument needed
 java.lang.String getExtraOptions()
          Gets the ExtraOptions attribute
protected  java.lang.String getExtraOptionsParameter()
          get any extra options or null for no argument needed
 boolean getFailFailOnError()
          query fail on error flag
 boolean getIncludeDefaultReferences()
          query the optimise flag
protected  java.lang.String getIncludeDefaultReferencesParameter()
          get the include default references flag or null for no argument needed
 boolean getIncremental()
          query the incrementalflag
protected  java.lang.String getIncrementalParameter()
          get the incremental build argument
 java.lang.String getMainClass()
          Gets the MainClass attribute
protected  java.lang.String getMainClassParameter()
          get the /main argument or null for no argument needed
 boolean getOptimize()
          query the optimise flag
protected  java.lang.String getOptimizeParameter()
          get the optimise flag or null for no argument needed
protected  java.lang.String getOutputFileParameter()
          get the argument or null for no argument needed
protected  java.lang.String getReferenceFilesParameter()
          turn the path list into a list of files and a /references argument
protected  java.lang.String getReferencesParameter()
          get the reference string or null for no argument needed
 java.lang.String getTargetType()
          Gets the TargetType attribute
protected  java.lang.String getTargetTypeParameter()
          get the argument or null for no argument needed
 boolean getUnsafe()
          query the Unsafe attribute
protected  java.lang.String getUnsafeParameter()
          get the argument or null for no argument needed
 int getWarnLevel()
          query warn level
protected  java.lang.String getWarnLevelParameter()
          get the warn level switch
protected  java.lang.String getWin32IconParameter()
          get the argument or null for no argument needed
protected  boolean notEmpty(java.lang.String s)
          test for a string containing something useful
 void setAdditionalModules(java.lang.String params)
          Set the definitions
 void setDebug(boolean f)
          set the debug flag on or off
 void setDefinitions(java.lang.String params)
          Set the definitions
 void setDestDir(java.lang.String dirName)
          Set the destination dir to find the files to be compiled
 void setDocFile(java.lang.String f)
          file for generated XML documentation
 void setExtraOptions(java.lang.String extraOptions)
          Sets the ExtraOptions attribute
 void setFailOnError(boolean b)
          set fail on error flag
 void setIncludeDefaultReferences(boolean f)
          set the automatic reference inclusion flag on or off this flag controls the string of references and the /nostdlib option in CSC
 void setIncremental(boolean f)
          set the incremental compilation flag on or off
 void setMainClass(java.lang.String mainClass)
          Sets the MainClass attribute
 void setOptimize(boolean f)
          set the optimise flag on or off
 void setOutputFile(java.lang.String params)
          Set the definitions
 void setReferenceFiles(Path path)
          add another path to the reference file path list
 void setReferences(java.lang.String s)
          Set the reference list to be used for this compilation.
 void setSrcDir(java.lang.String srcDirName)
          Set the source dir to find the files to be compiled
 void setTargetType(java.lang.String targetType)
          define the target
 void setUnsafe(boolean unsafe)
          Sets the Unsafe attribute
 void setWarnLevel(int warnLevel)
          set warn level (no range checking)
 void setWin32Icon(java.lang.String fileName)
          Set the win32 icon
 
Methods inherited from class org.apache.tools.ant.taskdefs.MatchingTask
createExclude, createInclude, createPatternSet, getDirectoryScanner, setDefaultexcludes, setExcludes, setExcludesfile, setIncludes, setIncludesfile, XsetIgnore, XsetItems
 
Methods inherited from class org.apache.tools.ant.Task
getDescription, getLocation, getOwningTarget, getProject, getRuntimeConfigurableWrapper, getTaskName, init, log, log, maybeConfigure, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

csc_exe_name

protected static final java.lang.String csc_exe_name
name of the executable. the .exe suffix is deliberately not included in anticipation of the unix version

csc_file_ext

protected static final java.lang.String csc_file_ext
what is the file extension we search on?

csc_file_pattern

protected static final java.lang.String csc_file_pattern
derive the search pattern from the extension

_references

protected java.lang.String _references
list of reference classes. (pretty much a classpath equivalent)

_referenceFiles

protected Path _referenceFiles
using the path approach didnt work as it could not handle the implicit execution path. Perhaps that could be extracted from the runtime and then the path approach would be viable

DEFAULT_REFERENCE_LIST

protected static final java.lang.String DEFAULT_REFERENCE_LIST
C# is really dumb in how it handles inclusion. You have to list every 'assembly' -read DLL that is imported. So already you are making a platform assumption -shared libraries have a .dll extension and the poor developer has to know every library which is included why the compiler cant find classes on the path or in a directory, is a mystery. To reduce the need to be explicit, here is a long list of the core libraries used in Beta-1 of .NET ommitting the blatantly non portable (MS.win32.interop) and the .designer libraries. (ripping out Com was tempting) Casing is chosen to match that of the file system exactly so may work on a unix box too.

_includeDefaultReferences

protected boolean _includeDefaultReferences
flag to enable automatic reference inclusion

_optimize

protected boolean _optimize
optimise flag

_incremental

protected boolean _incremental
incremental build flag

_debug

protected boolean _debug
debug flag. Controls generation of debug information.

_docFile

protected java.io.File _docFile
output XML documentation flag

_unsafe

protected boolean _unsafe
enable unsafe code flag. Clearly set to false by default

_mainClass

protected java.lang.String _mainClass
main class (or null for automatic choice)

_extraOptions

protected java.lang.String _extraOptions
any extra command options?

_targetType

protected java.lang.String _targetType
type of target. Should be one of exe|library|module|winexe|(null) default is exe; the actual value (if not null) is fed to the command line.
See /target

_win32icon

protected java.io.File _win32icon
icon for incorporation into apps

_outputFile

protected java.lang.String _outputFile
output file. If not supplied this is derived from the source file

_failOnError

protected boolean _failOnError
flag to control action on execution trouble
Constructor Detail

CSharp

public CSharp()
constructor inits everything and set up the search pattern
Method Detail

setReferences

public void setReferences(java.lang.String s)
Set the reference list to be used for this compilation.
Parameters:
s - The new References value

getReferencesParameter

protected java.lang.String getReferencesParameter()
get the reference string or null for no argument needed
Returns:
The References Parameter to CSC

setReferenceFiles

public void setReferenceFiles(Path path)
add another path to the reference file path list
Parameters:
path - another path to append

getReferenceFilesParameter

protected java.lang.String getReferenceFilesParameter()
turn the path list into a list of files and a /references argument
Returns:
null or a string of references.

getDefaultReferenceParameter

protected java.lang.String getDefaultReferenceParameter()
get default reference list
Returns:
null or a string of references.

setIncludeDefaultReferences

public void setIncludeDefaultReferences(boolean f)
set the automatic reference inclusion flag on or off this flag controls the string of references and the /nostdlib option in CSC
Parameters:
f - on/off flag

getIncludeDefaultReferences

public boolean getIncludeDefaultReferences()
query the optimise flag
Returns:
true if optimise is turned on

getIncludeDefaultReferencesParameter

protected java.lang.String getIncludeDefaultReferencesParameter()
get the include default references flag or null for no argument needed
Returns:
The Parameter to CSC

setOptimize

public void setOptimize(boolean f)
set the optimise flag on or off
Parameters:
f - on/off flag

getOptimize

public boolean getOptimize()
query the optimise flag
Returns:
true if optimise is turned on

getOptimizeParameter

protected java.lang.String getOptimizeParameter()
get the optimise flag or null for no argument needed
Returns:
The Optimize Parameter to CSC

setIncremental

public void setIncremental(boolean f)
set the incremental compilation flag on or off
Parameters:
f - on/off flag

getIncremental

public boolean getIncremental()
query the incrementalflag
Returns:
true iff incremental compilation is turned on

getIncrementalParameter

protected java.lang.String getIncrementalParameter()
get the incremental build argument
Returns:
The Incremental Parameter to CSC

setDebug

public void setDebug(boolean f)
set the debug flag on or off
Parameters:
f - on/off flag

getDebug

public boolean getDebug()
query the debug flag
Returns:
true if debug is turned on

getDebugParameter

protected java.lang.String getDebugParameter()
get the debug switch argument
Returns:
The Debug Parameter to CSC

setDocFile

public void setDocFile(java.lang.String f)
file for generated XML documentation
Parameters:
f - output file

getDocFileParameter

protected java.lang.String getDocFileParameter()
get the argument or null for no argument needed
Returns:
The DocFile Parameter to CSC

setWarnLevel

public void setWarnLevel(int warnLevel)
set warn level (no range checking)
Parameters:
warnLevel - warn level -see .net docs for valid range (probably 0-4)

getWarnLevel

public int getWarnLevel()
query warn level
Returns:
current value

getWarnLevelParameter

protected java.lang.String getWarnLevelParameter()
get the warn level switch
Returns:
The WarnLevel Parameter to CSC

setUnsafe

public void setUnsafe(boolean unsafe)
Sets the Unsafe attribute
Parameters:
unsafe - The new Unsafe value

getUnsafe

public boolean getUnsafe()
query the Unsafe attribute
Returns:
The Unsafe value

getUnsafeParameter

protected java.lang.String getUnsafeParameter()
get the argument or null for no argument needed
Returns:
The Unsafe Parameter to CSC

setMainClass

public void setMainClass(java.lang.String mainClass)
Sets the MainClass attribute
Parameters:
mainClass - The new MainClass value

getMainClass

public java.lang.String getMainClass()
Gets the MainClass attribute
Returns:
The MainClass value

getMainClassParameter

protected java.lang.String getMainClassParameter()
get the /main argument or null for no argument needed
Returns:
The MainClass Parameter to CSC

setExtraOptions

public void setExtraOptions(java.lang.String extraOptions)
Sets the ExtraOptions attribute
Parameters:
extraOptions - The new ExtraOptions value

getExtraOptions

public java.lang.String getExtraOptions()
Gets the ExtraOptions attribute
Returns:
The ExtraOptions value

getExtraOptionsParameter

protected java.lang.String getExtraOptionsParameter()
get any extra options or null for no argument needed
Returns:
The ExtraOptions Parameter to CSC

setSrcDir

public void setSrcDir(java.lang.String srcDirName)
Set the source dir to find the files to be compiled
Parameters:
srcDirName - The new SrcDir value

setDestDir

public void setDestDir(java.lang.String dirName)
Set the destination dir to find the files to be compiled
Parameters:
dirName - The new DestDir value

setTargetType

public void setTargetType(java.lang.String targetType)
                   throws BuildException
define the target
Parameters:
targetType - The new TargetType value
Throws:
BuildException - if target is not one of exe|library|module|winexe

getTargetType

public java.lang.String getTargetType()
Gets the TargetType attribute
Returns:
The TargetType value

getTargetTypeParameter

protected java.lang.String getTargetTypeParameter()
get the argument or null for no argument needed
Returns:
The TargetType Parameter to CSC

setWin32Icon

public void setWin32Icon(java.lang.String fileName)
Set the win32 icon
Parameters:
fileName - path to the file. Can be relative, absolute, whatever.

getWin32IconParameter

protected java.lang.String getWin32IconParameter()
get the argument or null for no argument needed
Returns:
The Win32Icon Parameter to CSC

setDefinitions

public void setDefinitions(java.lang.String params)
Set the definitions
Parameters:
list - of definitions split by ; or , or even :

getDefinitionsParameter

protected java.lang.String getDefinitionsParameter()
get the argument or null for no argument needed
Returns:
The Definitions Parameter to CSC

setAdditionalModules

public void setAdditionalModules(java.lang.String params)
Set the definitions
Parameters:
list - of definitions split by ; or , or even :

getAdditionalModulesParameter

protected java.lang.String getAdditionalModulesParameter()
get the argument or null for no argument needed
Returns:
The AdditionalModules Parameter to CSC

setOutputFile

public void setOutputFile(java.lang.String params)
Set the definitions
Parameters:
list - of definitions split by ; or , or even :

getOutputFileParameter

protected java.lang.String getOutputFileParameter()
get the argument or null for no argument needed
Returns:
The OutputFile Parameter to CSC

setFailOnError

public void setFailOnError(boolean b)
set fail on error flag
Parameters:
b - The new FailOnError value

getFailFailOnError

public boolean getFailFailOnError()
query fail on error flag
Returns:
The FailFailOnError value

Clear

public void Clear()
reset all contents.

notEmpty

protected boolean notEmpty(java.lang.String s)
test for a string containing something useful
Parameters:
s - string in
Returns:
true if the argument is not null or empty

execute

public void execute()
             throws BuildException
do the work by building the command line and then calling it
Overrides:
execute in class Task
Following copied from class: org.apache.tools.ant.Task
Throws:
BuildException - if someting goes wrong with the build


Copyright ? 2000 Apache Software Foundation. All Rights Reserved.