~ubuntu-branches/ubuntu/feisty/nant/feisty

« back to all changes in this revision

Viewing changes to src/NAnt.DotNet/Tasks/ScriptTask.cs

  • Committer: Bazaar Package Importer
  • Author(s): Dave Beckett
  • Date: 2006-06-12 23:30:36 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060612233036-a1uwh0949z0218ep
Tags: 0.84+0.85-rc4-1
* New upstream release
* Acknowledge NMU (Closes: #372588)
* Standards-Version 3.7.2
* Update to latest CLI policy package split.  Build-Depends-Indep: on
  cli-common-dev, libmono-winforms1.0-cil, libmono-winforms2.0-cil and
  mono-gmcs to get 1.0 and 2.0 packages
* Removed patches no longer needed:
  - 01-AssemblyInfoTask.cs.patch
  - 02-ScriptTask.cs.patch
  - 03-XmlResultFormatter.cs.patch
  - 04-SourceControl.patch
  - 05-ExceptionTest.cs

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
    ///   </code>
107
107
    /// </example>
108
108
    /// <example>
109
 
    ///   <para>Run Visual Basic.NET code that writes a message to the build log.</para>
 
109
    ///   <para>Use a custom namespace in C# to create a database</para>
 
110
    ///   <code>
 
111
    ///         &lt;script language=&quot;C#&quot; &gt;
 
112
    ///             &lt;references&gt;
 
113
    ///                 &lt;include name=&quot;System.Data.dll&quot; /&gt;
 
114
    ///             &lt;/references&gt;
 
115
    ///             &lt;imports&gt;
 
116
    ///                 &lt;import namespace=&quot;System.Data.SqlClient&quot; /&gt;
 
117
    ///             &lt;/imports&gt;
 
118
    ///             &lt;code&gt;
 
119
    ///               &lt;![CDATA[
 
120
    ///                 public static void ScriptMain(Project project) {
 
121
    ///                     string dbUserName = &quot;nant&quot;;
 
122
    ///                     string dbPassword = &quot;nant&quot;;
 
123
    ///                     string dbServer = &quot;(local)&quot;;
 
124
    ///                     string dbDatabaseName = &quot;NAntSample&quot;;
 
125
    ///                     string connectionString = String.Format(&quot;Server={0};uid={1};pwd={2};&quot;, dbServer, dbUserName, dbPassword);
 
126
    ///                     
 
127
    ///                     SqlConnection connection = new SqlConnection(connectionString);
 
128
    ///                     string createDbQuery = "CREATE DATABASE " + dbDatabaseName;
 
129
    ///                     SqlCommand createDatabaseCommand = new SqlCommand(createDbQuery);
 
130
    ///                     createDatabaseCommand.Connection = connection;
 
131
    ///                     
 
132
    ///                     connection.Open();
 
133
    ///                     
 
134
    ///                     try {
 
135
    ///                         createDatabaseCommand.ExecuteNonQuery();
 
136
    ///                         project.Log(Level.Info, &quot;Database added successfully: &quot; + dbDatabaseName);
 
137
    ///                     } catch (Exception e) {
 
138
    ///                         project.Log(Level.Error, e.ToString());
 
139
    ///                     } finally {
 
140
    ///                         connection.Close();
 
141
    ///                     }
 
142
    ///                 }
 
143
    ///               ]]&gt;
 
144
    ///             &lt;/code&gt;
 
145
    ///         &lt;/script&gt;
 
146
    ///   </code>
 
147
    /// </example>
 
148
    /// <example>
 
149
    ///   <para>
 
150
    ///   Run Visual Basic.NET code that writes a message to the build log.
 
151
    ///   </para>
110
152
    ///   <code>
111
153
    ///         &lt;script language=&quot;VB&quot;&gt;
112
154
    ///             &lt;code&gt;
157
199
    ///   </code>
158
200
    /// </example>
159
201
    /// <example>
160
 
    ///   <para>Define a custom function and call it using Boo (http://boo.codehaus.org/).</para>
 
202
    ///   <para>
 
203
    ///   Define a custom function and call it using <see href="http://boo.codehaus.org/">Boo</see>.
 
204
    ///   </para>
161
205
    ///   <code>
162
206
    ///         &lt;script language=&quot;Boo.CodeDom.BooCodeProvider, Boo.CodeDom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32c39770e9a21a67&quot;
163
207
    ///             failonerror=&quot;true&quot;&gt;
411
455
 
412
456
        private CompilerInfo CreateCompilerInfo(string language) {
413
457
            CodeDomProvider provider = null;
414
 
            LanguageId languageId;
415
458
 
416
459
            try {
417
460
                switch (language) {
418
461
                    case "vb":
419
462
                    case "VB":
420
463
                    case "VISUALBASIC":
421
 
                        languageId = LanguageId.VisualBasic;
422
464
                        provider = CreateCodeDomProvider(
423
465
                            "Microsoft.VisualBasic.VBCodeProvider",
424
466
                            "System, Culture=neutral");
426
468
                    case "c#":
427
469
                    case "C#":
428
470
                    case "CSHARP":
429
 
                        languageId = LanguageId.CSharp;
430
471
                        provider = CreateCodeDomProvider(
431
472
                            "Microsoft.CSharp.CSharpCodeProvider",
432
473
                            "System, Culture=neutral");
434
475
                    case "js":
435
476
                    case "JS":
436
477
                    case "JSCRIPT":
437
 
                        languageId = LanguageId.JScript;
438
478
                        provider = CreateCodeDomProvider(
439
479
                            "Microsoft.JScript.JScriptCodeProvider",
440
480
                            "Microsoft.JScript, Culture=neutral");
442
482
                    case "vjs":
443
483
                    case "VJS":
444
484
                    case "JSHARP":
445
 
                        languageId = LanguageId.JSharp;
446
485
                        provider = CreateCodeDomProvider(
447
486
                            "Microsoft.VJSharp.VJSharpCodeProvider",
448
487
                            "VJSharpCodeProvider, Culture=neutral");
450
489
                    default:
451
490
                        // if its not one of the above then it must be a fully 
452
491
                        // qualified provider class name
453
 
                        languageId = LanguageId.Other;
454
492
                        provider = CreateCodeDomProvider(language);
455
493
                        break;
456
494
                }
457
495
 
458
 
                return new CompilerInfo(languageId, provider);
 
496
                return new CompilerInfo(provider);
459
497
            } catch (Exception ex) {
460
498
                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
461
499
                    ResourceUtils.GetString("NA2036"), language), Location, ex);
493
531
 
494
532
        #endregion Private Static Methods
495
533
 
496
 
        internal enum LanguageId : int {
497
 
            CSharp      = 1,
498
 
            VisualBasic = 2,
499
 
            JScript     = 3,
500
 
            JSharp      = 4,
501
 
            Other       = 5
502
 
        }
503
 
 
504
534
        internal class CompilerInfo {
505
 
            private LanguageId _lang;
506
535
            public readonly ICodeCompiler Compiler;
507
536
            public readonly ICodeGenerator CodeGen;
508
537
 
509
 
            public CompilerInfo(LanguageId languageId, CodeDomProvider provider) {
510
 
                _lang = languageId;
511
 
 
 
538
            public CompilerInfo(CodeDomProvider provider) {
512
539
                Compiler = provider.CreateCompiler();
513
540
                CodeGen = provider.CreateGenerator();
514
541
            }
515
542
 
516
 
 
517
543
            public CodeCompileUnit GenerateCode(string typeName, string codeBody,
518
544
                                       StringCollection imports,
519
545
                                       string prefix) {
526
552
                // create constructor
527
553
                CodeConstructor constructMember = new CodeConstructor();
528
554
                constructMember.Attributes = MemberAttributes.Public;
529
 
                constructMember.Parameters.Add(new CodeParameterDeclarationExpression("Project", "project"));
530
 
                constructMember.Parameters.Add(new CodeParameterDeclarationExpression("PropertyDictionary", "propDict"));
 
555
                constructMember.Parameters.Add(new CodeParameterDeclarationExpression("NAnt.Core.Project", "project"));
 
556
                constructMember.Parameters.Add(new CodeParameterDeclarationExpression("NAnt.Core.PropertyDictionary", "propDict"));
531
557
                
532
558
                constructMember.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("project"));
533
559
                constructMember.BaseConstructorArgs.Add(new CodeVariableReferenceExpression ("propDict"));
561
587
                compileUnit.Namespaces.Add( nspace );
562
588
                nspace.Types.Add(typeDecl);
563
589
    
564
 
                
565
590
                return compileUnit;
566
591
            }
567
592
        }