Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a instance of the <see cref="ExtensionBuilder" /> 
 /// class for an extension in a given <see cref="ExtensionAssembly" />.
 /// </summary>
 /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> in which the extension is found.</param>
 /// <exception cref="ArgumentNullException"><paramref name="extensionAssembly" /> is <see langword="null" />.</exception>
 internal ExtensionBuilder(ExtensionAssembly extensionAssembly)
 {
     if (extensionAssembly == null) {
         throw new ArgumentNullException("extensionAssembly");
     }
     _extensionAssembly = extensionAssembly;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of the <see cref="FilterBuilder" /> class
        /// for the specified <see cref="Filter" /> class in the specified
        /// <see cref="ExtensionAssembly" />.
        /// </summary>
        /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> containing the <see cref="Filter" />.</param>
        /// <param name="className">The class representing the <see cref="Filter" />.</param>
        internal FilterBuilder(ExtensionAssembly extensionAssembly, string className) : base (extensionAssembly) {
            _className = className;

            // get Element name from attribute
            ElementNameAttribute ElementNameAttribute = (ElementNameAttribute)
                Attribute.GetCustomAttribute(Assembly.GetType(ClassName),
                typeof(ElementNameAttribute));

            _filterName = ElementNameAttribute.Name;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Scans a given <see cref="Type" /> for plugins.
        /// </summary>
        /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> containing the <see cref="Type" /> to scan.</param>
        /// <param name="type">The <see cref="Type" /> to scan.</param>
        /// <param name="task">The <see cref="Task" /> which will be used to output messages to the build log.</param>
        /// <returns>
        /// <see langword="true" /> if <paramref name="type" /> represents a
        /// <see cref="IPlugin" />; otherwise, <see langword="false" />.
        /// </returns>
        public bool ScanTypeForPlugins(ExtensionAssembly extensionAssembly, Type type, Task task) {
            if (type.IsAbstract)
                return false;
            try {
                bool isplugin = typeof(IPlugin).IsAssignableFrom(type);
                if (!isplugin) {
                    return false;
                }

                PluginBuilder pb = new PluginBuilder(extensionAssembly, type);
                _pluginBuilders.Add(pb);
                return true;
            } catch {
                task.Log(Level.Error, "Failure scanning \"{0}\" for plugins.",
                    type.AssemblyQualifiedName);
                throw;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new instance of the <see cref="TaskBuilder" /> class
 /// for the specified <see cref="Task" /> class in the specified
 /// <see cref="ExtensionAssembly" />.
 /// </summary>
 /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> containing the <see cref="Task" />.</param>
 /// <param name="className">The class representing the <see cref="Task" />.</param>
 internal TaskBuilder(ExtensionAssembly extensionAssembly, string className)
     : base(extensionAssembly)
 {
     _className = className;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates an  <see cref="ExtensionAssembly" /> for the specified
        /// <see cref="Assembly" /> and caches it for future use.
        /// </summary>
        /// <remarks>
        /// If an <see cref="ExtensionAssembly" /> for the same assembly is
        /// available in the cache, then this cached instance is returned.
        /// </remarks>
        /// <param name="assembly">The <see cref="Assembly" /> for which to construct an <see cref="ExtensionAssembly" />.</param>
        /// <returns>
        /// The <see cref="ExtensionAssembly" /> for the specified <see cref="Assembly" />.
        /// </returns>
        public static ExtensionAssembly Create(Assembly assembly)
        {
            if (assembly == null)
                throw new ArgumentNullException ("assembly");

            string aname = assembly.FullName;
            ExtensionAssembly ext = _extensionAssemblies [aname]
                as ExtensionAssembly;
            if (ext == null) {
                ext = new ExtensionAssembly (assembly);
                _extensionAssemblies [aname] = assembly;
            }
            return ext;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Scans a given <see cref="Type" /> for tasks.
        /// </summary>
        /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> containing the <see cref="Type" /> to scan.</param>
        /// <param name="type">The <see cref="Type" /> to scan.</param>
        /// <param name="task">The <see cref="Task" /> which will be used to output messages to the build log.</param>
        /// <returns>
        /// <see langword="true" /> if <paramref name="type" /> represents a
        /// <see cref="Task" />; otherwise, <see langword="false" />.
        /// </returns>
        private static bool ScanTypeForTasks(ExtensionAssembly extensionAssembly, Type type, Task task)
        {
            try {
                TaskNameAttribute taskNameAttribute = (TaskNameAttribute)
                    Attribute.GetCustomAttribute(type, typeof(TaskNameAttribute));

                if (type.IsSubclassOf(typeof(Task)) && !type.IsAbstract && taskNameAttribute != null) {
                    task.Log(Level.Debug, string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("String_CreatingTaskBuilder"),
                        type.Name));

                    TaskBuilder tb = new TaskBuilder(extensionAssembly, type.FullName);
                    if (TaskBuilders[tb.TaskName] == null) {
                        task.Log(Level.Debug, string.Format(CultureInfo.InvariantCulture,
                            ResourceUtils.GetString("String_AddingTask"), tb.TaskName,
                            GetAssemblyLocation(tb.Assembly), tb.ClassName));

                        TaskBuilders.Add(tb);
                    }

                    // specified type represents a task
                    return true;
                } else {
                    // specified type does not represent valid task
                    return false;
                }
            } catch {
                task.Log(Level.Error, "Failure scanning \"{0}\" for tasks.",
                    type.AssemblyQualifiedName);
                throw;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Scans a given <see cref="Type" /> for filters.
        /// </summary>
        /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> containing the <see cref="Type" /> to scan.</param>
        /// <param name="type">The <see cref="Type" /> to scan.</param>
        /// <param name="task">The <see cref="Task" /> which will be used to output messages to the build log.</param>
        /// <returns>
        /// <see langword="true" /> if <paramref name="type" /> represents a
        /// <see cref="Filter" />; otherwise, <see langword="false" />.
        /// </returns>
        private static bool ScanTypeForFilters(ExtensionAssembly extensionAssembly, Type type, Task task)
        {
            try {
                ElementNameAttribute elementNameAttribute = (ElementNameAttribute)
                    Attribute.GetCustomAttribute(type, typeof(ElementNameAttribute));

                if (type.IsSubclassOf(typeof(Filter)) && !type.IsAbstract && elementNameAttribute != null) {
                    task.Log(Level.Debug, "Creating FilterBuilder for \"{0}\".",
                        type.Name);
                    FilterBuilder builder = new FilterBuilder(extensionAssembly, type.FullName);
                    if (FilterBuilders[builder.FilterName] == null) {
                        FilterBuilders.Add(builder);

                        task.Log(Level.Debug, "Adding filter \"{0}\" from {1}:{2}.",
                            builder.FilterName, GetAssemblyLocation(builder.Assembly),
                            builder.ClassName);
                    }

                    // specified type represents a filter
                    return true;
                }

                // specified type does not represent a valid filter
                return false;
            } catch {
                task.Log(Level.Error, "Failure scanning \"{0}\" for filters.",
                    type.AssemblyQualifiedName);
                throw;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Scans a given <see cref="Type" /> for data type.
        /// </summary>
        /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> containing the <see cref="Type" /> to scan.</param>
        /// <param name="type">The <see cref="Type" /> to scan.</param>
        /// <param name="task">The <see cref="Task" /> which will be used to output messages to the build log.</param>
        /// <returns>
        /// <see langword="true" /> if <paramref name="type" /> represents a
        /// data type; otherwise, <see langword="false" />.
        /// </returns>
        private static bool ScanTypeForDataTypes(ExtensionAssembly extensionAssembly, Type type, Task task)
        {
            try {
                ElementNameAttribute elementNameAttribute = (ElementNameAttribute)
                    Attribute.GetCustomAttribute(type, typeof(ElementNameAttribute));

                if (type.IsSubclassOf(typeof(DataTypeBase)) && !type.IsAbstract && elementNameAttribute != null) {
                    logger.Info(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("String_CreatingDataTypeBaseBuilder"), type.Name));
                    DataTypeBaseBuilder dtb = new DataTypeBaseBuilder(extensionAssembly, type.FullName);
                    if (DataTypeBuilders[dtb.DataTypeName] == null) {
                        logger.Debug(string.Format(CultureInfo.InvariantCulture,
                            ResourceUtils.GetString("String_AddingDataType"), dtb.DataTypeName,
                            GetAssemblyLocation(dtb.Assembly), dtb.ClassName));

                        DataTypeBuilders.Add(dtb);
                    }

                    // specified type represents a data type
                    return true;
                } else {
                    // specified type does not represent valid data type
                    return false;
                }
            } catch {
                task.Log(Level.Error, "Failure scanning \"{0}\" for data types.",
                    type.AssemblyQualifiedName);
                throw;
            }
        }
Ejemplo n.º 9
0
        public static bool ScanAssembly(Assembly assembly, Task task)
        {
            task.Log(Level.Verbose, "Scanning assembly \"{0}\" for extensions.",
                assembly.GetName().Name);

            foreach (Type type in assembly.GetExportedTypes()) {
                foreach (MethodInfo methodInfo in type.GetMethods()) {
                    if (methodInfo.IsStatic) {
                        task.Log(Level.Verbose, "Found method {0}.",
                            methodInfo.Name);
                    }
                }
            }

            bool isExtensionAssembly = false;

            ExtensionAssembly extensionAssembly = new ExtensionAssembly (
                assembly);

            Type[] types;

            try {
                types = assembly.GetTypes();
            }
            catch(ReflectionTypeLoadException ex) {
                if(ex.LoaderExceptions != null && ex.LoaderExceptions.Length > 0) {
                    throw ex.LoaderExceptions[0];
                }

                throw;
            }

            foreach (Type type in types) {
                //
                // each extension type is exclusive, meaning a given type
                // cannot be both a task and data type
                //
                // so it doesn't make sense to scan a type for, for example,
                // data types if the type has already been positively
                // identified as a task
                //

                bool extensionFound = ScanTypeForTasks(extensionAssembly,
                    type, task);

                if (!extensionFound) {
                    extensionFound = ScanTypeForDataTypes(extensionAssembly,
                        type, task);
                }

                if (!extensionFound) {
                    extensionFound = ScanTypeForFunctions(type, task);
                }

                if (!extensionFound) {
                    extensionFound = ScanTypeForFilters(extensionAssembly,
                        type, task);
                }

                if (!extensionFound) {
                    extensionFound = _pluginScanner.ScanTypeForPlugins(
                        extensionAssembly, type, task);
                }

                // if extension is found in type, then mark assembly as
                // extension assembly
                isExtensionAssembly = isExtensionAssembly || extensionFound;
            }

            // if no extension could be found at all, then we might be dealing
            // with an extension assembly that was built using an older version
            // of NAnt(.Core)
            if (!isExtensionAssembly) {
                AssemblyName coreAssemblyName = Assembly.GetExecutingAssembly().
                    GetName(false);

                foreach (AssemblyName assemblyName in assembly.GetReferencedAssemblies()) {
                    if (assemblyName.Name == coreAssemblyName.Name) {
                        // the given assembly references NAnt.Core, so check whether
                        // it doesn't reference an older version of NAnt.Core
                        if (assemblyName.Version != coreAssemblyName.Version) {
                            task.Log(Level.Warning, "Assembly \"{0}\" is built"
                                + " using version {1} of NAnt. If any problems"
                                + " arise, then try using a version that is built"
                                + " for NAnt version {2}.", assembly.GetName().Name,
                                assemblyName.Version, coreAssemblyName.Version);
                        }
                    }
                }
            }

            return isExtensionAssembly;
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Creates a new instance of the <see cref="DataTypeBaseBuilder" />
 /// class for the specified <see cref="DataTypeBase" /> class in the
 /// <see cref="ExtensionAssembly" /> specified.
 /// </summary>
 /// <param name="extensionAssembly">The <see cref="ExtensionAssembly" /> containing the <see cref="DataTypeBase" />.</param>
 /// <param name="className">The class representing the <see cref="DataTypeBase" />.</param>
 internal DataTypeBaseBuilder(ExtensionAssembly extensionAssembly, string className) : base (extensionAssembly) {
     _className = className;
 }
Ejemplo n.º 11
0
 internal PluginBuilder(ExtensionAssembly extensionAssembly, Type pluginType) : base (extensionAssembly) {
     _pluginType = pluginType;
 }
Ejemplo n.º 12
0
 internal PluginBuilder(ExtensionAssembly extensionAssembly, Type pluginType) : base(extensionAssembly)
 {
     _pluginType = pluginType;
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes a instance of the <see cref="ExtensionBuilder" />
 /// class for an extension in a given <see cref="Assembly" />.
 /// </summary>
 /// <param name="assembly">The <see cref="Assembly" /> in which the extension is found.</param>
 /// <exception cref="ArgumentNullException"><paramref name="assembly" /> is <see langword="null" />.</exception>
 protected ExtensionBuilder(Assembly assembly)
     : this(ExtensionAssembly.Create(assembly))
 {
 }