Summary
- Assembly
- NAnt
.DotNet .dll - Namespace
- NAnt
.DotNet .Tasks - Interfaces
- Base Types
Syntax
[TaskName("script")]
public class ScriptTask : Task, IConditional
Examples
Run C# code that writes a message to the build log.
<script language="C#">
<code>
<![CDATA[
public static void ScriptMain(Project project) {
project.Log(Level.Info, "Hello World from a script task using C#");
}
]]>
</code>
</script>
Define a custom function and call it using C#.
<script language="C#" prefix="test" >
<code>
<![CDATA[
[Function("test-func")]
public static string Testfunc( ) {
return "some result !!!!!!!!";
}
]]>
</code>
</script>
<echo message='${test::test-func()}'/>
Use a custom namespace in C# to create a database
<script language="C#" >
<references>
<include name="System.Data.dll" />
</references>
<imports>
<import namespace="System.Data.SqlClient" />
</imports>
<code>
<![CDATA[
public static void ScriptMain(Project project) {
string dbUserName = "nant";
string dbPassword = "nant";
string dbServer = "(local)";
string dbDatabaseName = "NAntSample";
string connectionString = String.Format("Server={0};uid={1};pwd={2};", dbServer, dbUserName, dbPassword);
SqlConnection connection = new SqlConnection(connectionString);
string createDbQuery = "CREATE DATABASE " + dbDatabaseName;
SqlCommand createDatabaseCommand = new SqlCommand(createDbQuery);
createDatabaseCommand.Connection = connection;
connection.Open();
try {
createDatabaseCommand.ExecuteNonQuery();
project.Log(Level.Info, "Database added successfully: " + dbDatabaseName);
} catch (Exception e) {
project.Log(Level.Error, e.ToString());
} finally {
connection.Close();
}
}
]]>
</code>
</script>
Run Visual Basic.NET code that writes a message to the build log.
<script language="VB">
<code>
<![CDATA[
Public Shared Sub ScriptMain(project As Project)
project.Log(Level.Info, "Hello World from a script task using Visual Basic.NET")
End Sub
]]>
</code>
</script>
Define a custom task and call it using C#.
<script language="C#" prefix="test" >
<code>
<![CDATA[
[TaskName("usertask")]
public class TestTask : Task {
#region Private Instance Fields
private string _message;
#endregion Private Instance Fields
#region Public Instance Properties
[TaskAttribute("message", Required=true)]
public string FileName {
get { return _message; }
set { _message = value; }
}
#endregion Public Instance Properties
#region Override implementation of Task
protected override void ExecuteTask() {
Log(Level.Info, _message.ToUpper());
}
#endregion Override implementation of Task
}
]]>
</code>
</script>
<usertask message='Hello from UserTask'/>
Define a custom function and call it using Boo.
<script language="Boo.CodeDom.BooCodeProvider, Boo.CodeDom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32c39770e9a21a67"
failonerror="true">
<code>
<![CDATA[
[Function("test-func")]
def MyFunc():
return "Hello from Boo !!!!!!"
]]>
</code>
</script>
<echo message='${script::test-func()}'/>
Remarks
Code
The NAnt.DotNet.Tasks.ScriptTask
must contain a single code
element, which in turn contains the script code.
This code can include extensions such as functions, or tasks. Once the script task has executed those extensions will be available for use in the buildfile.
If no extensions have been defined, a static entry point named
ScriptMain
- which must have a single NAnt.Core.Project
argument - is required.
Namespaces
The following namespaces are imported by default:
- System
- System.Collections
- System.IO
- System.Text
- NAnt.Core
- NAnt.Core.Attributes
Assembly References
The assembly references that are specified will be used to compile the script, and will be loaded into the NAnt appdomain.
By default, only the NAnt.Core
and mscorlib
assemblies
are referenced.
Attributes
Type | Description |
---|---|
TaskNameAttribute | Indicates that class should be treated as a task. |
Properties
Name | Value | Summary |
---|---|---|
Code | RawXml |
The code to execute.
|
CustomXmlProcessing | bool |
Gets a value indicating whether the element is performing additional
processing using the
NAnt.Core.Element.XmlNode that was used to
initialize the element.
Inherited from Element
|
FailOnError | bool |
Determines if task failure stops the build, or is just reported.
The default is
true .
Inherited from Task
|
IfDefined | bool |
If
true then the task will be executed; otherwise,
skipped. The default is true .
Inherited from Task
|
Imports | Namespace |
The namespaces to import.
|
Language | string |
The language of the script block. Possible values are "VB", "vb", "VISUALBASIC", "C#", "c#", "CSHARP".
"JS", "js", "JSCRIPT" "VJS", "vjs", "JSHARP" or a fully-qualified name for a class implementing
System.CodeDom.Compiler.CodeDomProvider .
|
Location | Location |
Gets or sets the location in the build file where the element is
defined.
Inherited from Element
|
LogPrefix | string |
The prefix used when sending messages to the log.
Inherited from Task
|
MainClass | string |
The name of the main class containing the static
ScriptMain
entry point.
|
Name | string |
The name of the task.
Inherited from Task
|
NamespaceManager | XmlNamespaceManager |
Gets or sets the
System.Xml.XmlNamespaceManager .
Inherited from Element
|
Parent | Object |
Gets or sets the parent of the element.
Inherited from Element
|
Prefix | string |
The namespace prefix for any custom functions defined in the script.
If omitted the prefix will default to 'script'
|
Project | Project |
Gets or sets the
NAnt.Core.Element.Project to which this element belongs.
Inherited from Element
|
Properties | PropertyDictionary |
Gets the properties local to this
NAnt.Core.Element and the
NAnt.Core.Element.Project .
Inherited from Element
|
References | AssemblyFileSet |
Any required references.
|
Threshold | Level |
Gets or sets the log threshold for this
NAnt.Core.Task . By
default the threshold of a task is NAnt.Core.Level.Debug ,
causing no messages to be filtered in the task itself.
Inherited from Task
|
UnlessDefined | bool |
Opposite of
NAnt.Core.Task.IfDefined . If false
then the task will be executed; otherwise, skipped. The default is
false .
Inherited from Task
|
Verbose | bool |
Determines whether the task should report detailed build log messages.
The default is
false .
Inherited from Task
|
XmlNode | XmlNode |
Gets or sets the XML node of the element.
Inherited from Element
|
Methods
Name | Value | Summary |
---|---|---|
CopyTo |
void |
Copies all instance data of the
NAnt.Core.Element to a given
NAnt.Core.Element .
Inherited from Element
|
Execute |
void |
Executes the task unless it is skipped.
Inherited from Task
|
ExecuteTask |
void |
Executes the script block.
|
Get |
XmlNode |
Locates the XML node for the specified attribute in either the
configuration section of the extension assembly or the.project.
Inherited from Task
|
Get |
XmlNode |
Inherited from Element
|
GetLocation |
Location |
Retrieves the location in the build file where the element is
defined.
Inherited from Element
|
Initialize |
void |
Initializes the task.
|
Initialize |
void |
Performs default initialization.
Inherited from Element
|
Initialize |
Element |
Initializes the build element.
Inherited from Element
static
|
InitializeElement |
void |
Derived classes should override to this method to provide extra
initialization and validation not covered by the base class.
Inherited from Element
|
InitializeTask |
void |
Initializes the task.
Inherited from Task
|
Initialize |
void |
Initializes the configuration of the task using configuration
settings retrieved from the NAnt configuration file.
Inherited from Task
|
InitializeXml |
void |
Initializes all build attributes and child elements.
Inherited from Element
|
IsLogEnabledFor |
bool |
Determines whether build output is enabled for the given
NAnt.Core.Level .
Inherited from Task
|
Log |
void |
Logs a message with the given priority.
Inherited from Task
|
Log |
void |
Logs a formatted message with the given priority.
Inherited from Task
|