RegasmTask.cs :  » Build-Systems » NAntContrib » NAnt » Contrib » Tasks » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » NAntContrib 
NAntContrib » NAnt » Contrib » Tasks » RegasmTask.cs
//
// NAntContrib
// Copyright (C) 2001-2003 Gerry Shaw
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
//
// Ian MacLean (ian@maclean.ms)

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;

using NAnt.Core;
using NAnt.Core.Types;
using NAnt.Core.Tasks;
using NAnt.Core.Attributes;

namespace NAnt.Contrib.Tasks{
    /// <summary>
    /// Registers an assembly for use from COM clients.
    /// </summary>
    /// <remarks>
    ///   <para>
    ///   Refer to the <see href="ms-help://MS.VSCC/MS.MSDNVS/cptools/html/cpgrfassemblyregistrationtoolregasmexe.htm">Regasm</see> 
    ///   documentation for more information on the regasm tool.
    ///   </para>
    /// </remarks>
    /// <example>
    ///   <para>
    ///   Register a single assembly.
    ///   </para>
    ///   <code>
    ///     <![CDATA[
    /// <regasm assembly="myAssembly.dll" />
    ///     ]]>
    ///   </code>
    /// </example>
    /// <example>
    ///   <para>
    ///   Register an assembly while exporting a typelibrary.
    ///   </para>
    ///   <code>
    ///     <![CDATA[
    /// <regasm assembly="myAssembly.dll" typelib="myAssembly.tlb" />
    ///     ]]>
    ///   </code>
    /// </example>
    /// <example>
    ///   <para>
    ///   Register a set of assemblies at once.
    ///   </para>
    ///   <code>
    ///     <![CDATA[
    /// <regasm unregister="false" codebase="true">
    ///     <fileset>
    ///         <include name="**/*.dll" />
      ///       <exclude name="notanassembly.dll" />
    ///     </fileset>
    /// </regasm>
    ///     ]]>
    ///   </code>
    /// </example>
    [TaskName("regasm")]
    [ProgramLocation(LocationType.FrameworkDir)]
    public class RegasmTask : ExternalProgramBase {
        #region Private Instance Fields

        private FileInfo _assemblyFile;
        private FileInfo _regfile;
        private FileInfo _typelib;
        private bool _codebase;
        private bool _exporttypelib;
        private bool _unregister;
        private bool _registered;
        private FileSet _fileset = new FileSet();

        #endregion Private Instance Fields

        #region Public Instance Properties

       /// <summary>
       /// The name of the file to register. This is provided as an alternate 
       /// to using the task's fileset.
       /// </summary>
        [TaskAttribute("assembly")]
        public FileInfo AssemblyFile {
            get { return _assemblyFile; }
            set { _assemblyFile = value; }
        }

        /// <summary>
        /// Registry file to export to instead of entering the types directly 
        /// into the registry. If a fileset is used then the entries are all 
        /// collated into this file.
        /// </summary>
        [TaskAttribute("regfile")]
        public FileInfo RegistryFile {
            get { return _regfile; }
            set { _regfile = value; }
        }

        /// <summary>
        /// Set the code base registry setting.
        /// </summary>
        [TaskAttribute("codebase")]
        [BooleanValidator()]
        public bool CodeBase {
            get { return _codebase; }
            set { _codebase = value; }
        }

        /// <summary>
        /// Export a typelib and register it. The typelib will have the same 
        /// name as the source assembly unless the <see cref="TypeLib" /> 
        /// attribute is used.
        /// </summary>
        [TaskAttribute("exporttypelib")]
        [BooleanValidator()]
        public bool ExportTypelib {
            get { return _exporttypelib; }
            set { _exporttypelib = value; }
        }

        /// <summary>
        /// Only refer to already registered type libraries.
        /// </summary>
        [TaskAttribute("registered")]
        [BooleanValidator()]
        public bool Registered {
            get { return _registered; }
            set { _registered = value; }
        }
        
        /// <summary>
        /// Export the assembly to the specified type library and register it.
        /// This attribute is ignored when a fileset is specified.
        /// </summary>
        [TaskAttribute("typelib")]
        public FileInfo TypeLib {
            get { return _typelib; }
            set { _typelib = value; }
        }
        
        /// <summary>
        /// Unregister the assembly. The default is <see langword="false" />.
        /// </summary>
        [TaskAttribute("unregister")]
        [BooleanValidator()]
        public bool Unregister {
            get { return _unregister; }
            set { _unregister = value; }
        }
               
        /// <summary>
        /// The set of files to register.
        /// </summary>
        [BuildElement("fileset")]
        public FileSet RegasmFileSet {
            get { return _fileset; }
            set { _fileset = value; }
        }

        #endregion Public Instance Properties

        public override string ProgramArguments {
            get {
                string args = "";

                if (Unregister) {
                    args += " /unregister ";
                }
                if (ExportTypelib && TypeLib != null) {
                    args += string.Format(CultureInfo.InvariantCulture,
                        " /tlb:\"{0}\"", TypeLib.FullName);
                }
                if (CodeBase) {
                    args += " /codebase";
                }
                if (RegistryFile != null) {
                    args += string.Format(CultureInfo.InvariantCulture,
                        " /regfile:\"{0}\"", RegistryFile.FullName);
                }
                if (Registered) {
                     args += " /registered";
                }
                if (Verbose) {
                     args += " /verbose";
                } else {
                     args += " /silent";
                }
                args += " /nologo";

                args += " \"" + AssemblyFile.FullName + "\"";
                return args;
            }
        }

        protected override void ExecuteTask() {
            if (AssemblyFile != null) {
                Log(Level.Info, "{0} '{1}' for COM Interop", 
                    Unregister ? "UnRegistering" : "Registering", 
                    AssemblyFile.FullName);

                if (ExportTypelib && TypeLib == null) {
                    TypeLib = new FileInfo(AssemblyFile.FullName.Replace(
                        Path.GetExtension(AssemblyFile.FullName), ".tlb"));
                }
                base.ExecuteTask();
            } else { // Loop thru fileset 
                // gather the information needed to perform the operation
                StringCollection fileNames = RegasmFileSet.FileNames;

                // display build log message
                Log(Level.Info, "{0} {1} files for COM interop", 
                    Unregister ? "UnRegistering" : "Registering", 
                    fileNames.Count);
                
                FileInfo registryFile = RegistryFile;

                // perform operation
                foreach (string path in fileNames) {
                    AssemblyFile = new FileInfo(path);
                    if (RegistryFile != null) {
                        RegistryFile = new FileInfo(path.Replace(
                            Path.GetExtension(path), ".reg"));
                    }
                    if (ExportTypelib) {
                        TypeLib = new FileInfo(path.Replace(
                            Path.GetExtension(path), ".tlb"));
                    }
                    Log(Level.Verbose, "{0} '{1}' for COM Interop", 
                        Unregister ? "UnRegistering" : "Registering", 
                        path);
                    base.ExecuteTask();
                }

                // Collate registry files
                if (registryFile != null) {
                    try {
                        using (StreamWriter writer = new StreamWriter(registryFile.FullName)) {
                            foreach (string path in fileNames) {
                                string regFile = path.Replace(Path.GetExtension(path), ".reg");
                                StreamReader reader = new StreamReader(regFile);
                                string data = reader.ReadToEnd();
                                writer.Write(data);
                                // close reader
                                reader.Close();
                                // clean up the registry file once its read
                                File.Delete(regFile); 
                            }
                        }
                    } catch (Exception ex) {
                        throw new BuildException("Error collating .reg files.", 
                            Location, ex);
                    }
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.