# <script>

[This is preliminary documentation and subject to change.]

Executes the code contained within the task.

##### Code

The <script> task 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 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.

### Parameters

Attribute | Type | Description | Required
--- | --- | --- | ---
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 CodeDomProvider. | True
mainclass | string | The name of the main class containing the static `ScriptMain` entry point. | False
prefix | string | The namespace prefix for any custom functions defined in the script. If ommitted the prefix will default to 'script' | False
failonerror | bool | Determines if task failure stops the build, or is just reported. The default is true. | False
if | bool | If true then the task will be executed; otherwise, skipped. The default is true. | False
unless | bool | Opposite of `if`. If false then the task will be executed; otherwise, skipped. The default is false. | False
verbose | bool | Determines whether the task should report detailed build log messages. The default is false. | False

### Nested Elements:

#### <references>

Any required references.

#### </references>

#### <imports>

The namespaces to import.

#### </imports>

#### <code>

The code to execute.

Represents an element of which the XML is processed by its parent task or type.

#### </code>

### 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()}'/>

### Requirements

Assembly: NAnt.DotNetTasks (0.91.3881.0)