/// <summary> /// Scans a given <see cref="Type" /> for filters. /// </summary> /// <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(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(type.FullName, type.Assembly.Location); if (FilterBuilders[builder.FilterName] == null) { FilterBuilders.Add(builder); task.Log(Level.Debug, "Adding filter \"{0}\" from {1}:{2}.", builder.FilterName, builder.AssemblyFileName, 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; } }
/// <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; } }
public static void ScanDir(string path, Task task, bool failOnError) { // don't do anything if we don't have a valid directory path if (StringUtils.IsNullOrEmpty(path)) { return; } task.Log(Level.Info, "Scanning directory \"{0}\" for extension" + " assemblies.", path); // scan all dll's for tasks, types and functions DirectoryScanner scanner = new DirectoryScanner(); scanner.BaseDirectory = new DirectoryInfo(path); scanner.Includes.Add("*.dll"); foreach (string assemblyFile in scanner.FileNames) { try { TypeFactory.ScanAssembly(assemblyFile, task); } catch (Exception ex) { if (failOnError) { throw; } task.Log(Level.Error, "Failure scanning \"{0}\" for extensions: {1}", assemblyFile, ex.Message); } } }
public static void ScanDir(string path, Task task, bool failOnError) { // don't do anything if we don't have a valid directory path if (String.IsNullOrEmpty(path)) { return; } task.Log(Level.Info, "Scanning directory \"{0}\" for extension" + " assemblies.", path); // scan all dll's for tasks, types and functions DirectoryScanner scanner = new DirectoryScanner(); scanner.BaseDirectory = new DirectoryInfo(path); scanner.Includes.Add("*.dll"); foreach (string assemblyFile in scanner.FileNames) { try { TypeFactory.ScanAssembly(assemblyFile, task); } catch (Exception ex) { string msg = string.Format(CultureInfo.InvariantCulture, "Failure scanning \"{0}\" for extensions", assemblyFile); if (failOnError) { throw new BuildException(msg + ".", Location.UnknownLocation, ex); } task.Log(Level.Error, msg + ": " + assemblyFile, ex.Message); } } }
/// <summary> /// Scans a given <see cref="Type" /> for tasks. /// </summary> /// <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(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(type.Assembly, 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); foreach (WeakReference wr in _projects) { if (!wr.IsAlive) { task.Log(Level.Debug, "WeakReference for project is dead."); continue; } Project p = wr.Target as Project; if (p == null) { task.Log(Level.Debug, "WeakReference is not a" + " project! This should not be possible."); continue; } UpdateProjectWithBuilder(p, 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; } }
/// <summary> /// Scans a given <see cref="Type" /> for functions. /// </summary> /// <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 /// valid set of funtions; otherwise, <see langword="false" />. /// </returns> private static bool ScanTypeForFunctions(Type type, Task task) { try { FunctionSetAttribute functionSetAttribute = (FunctionSetAttribute) Attribute.GetCustomAttribute(type, typeof(FunctionSetAttribute)); if (functionSetAttribute == null) { // specified type does not represent a valid functionset return false; } bool acceptType = (type == typeof(ExpressionEvaluator)); if (type.IsSubclassOf(typeof(FunctionSetBase)) && !type.IsAbstract) { acceptType = true; } if (acceptType) { string prefix = functionSetAttribute.Prefix; if (prefix != null && prefix != String.Empty) { prefix += "::"; } else { task.Log(Level.Warning, "Ignoring functions in type \"{0}\":" + " no prefix was set.", type.AssemblyQualifiedName); // specified type does not represent a valid functionset return false; } // // add public static/instance methods // foreach (MethodInfo info in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { FunctionAttribute functionAttribute = (FunctionAttribute) Attribute.GetCustomAttribute(info, typeof(FunctionAttribute)); if (functionAttribute != null) RegisterFunction(prefix + functionAttribute.Name, info); } // specified type represents a valid functionset return true; } else { // specified type does not represent a valid functionset return false; } } catch { task.Log(Level.Error, "Failure scanning \"{0}\" for functions.", type.AssemblyQualifiedName); throw; } }
/// <summary> /// Scans a given <see cref="Type" /> for data type. /// </summary> /// <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(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(type.FullName, type.Assembly.Location); if (DataTypeBuilders[dtb.DataTypeName] == null) { logger.Debug(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("String_AddingDataType"), dtb.DataTypeName, dtb.AssemblyFileName, 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; } }
public static bool ScanAssembly(Assembly assembly, Task task) { task.Log(Level.Info, "Scanning assembly \"{0}\" for extensions.", assembly.GetName().Name); bool extensionAssembly = false; foreach (Type type in assembly.GetTypes()) { // // 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(type, task); if (!extensionFound) { extensionFound = ScanTypeForDataTypes(type, task); } if (!extensionFound) { extensionFound = ScanTypeForFunctions(type, task); } if (!extensionFound) { extensionFound = ScanTypeForFilters(type, task); } // if extension is found in type, then mark assembly as // extension assembly extensionAssembly = extensionAssembly || 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 (!extensionAssembly) { 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(extensionAssembly); }
/// <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; } }
/// <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; } }
void eventSource_WarningRaised(object sender, BuildWarningEventArgs e) { m_task.Log(Level.Warning, e.Message); }
/// <summary> /// Scans a given <see cref="Type" /> for tasks. /// </summary> /// <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(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, "Creating TaskBuilder for \"{0}\".", type.Name); TaskBuilder tb = new TaskBuilder(type.FullName, type.Assembly.Location); if (TaskBuilders[tb.TaskName] == null) { task.Log(Level.Debug, "Adding \"{0}\" from {1}:{2}.", tb.TaskName, tb.AssemblyFileName, tb.ClassName); TaskBuilders.Add(tb); foreach(WeakReference wr in _projects) { if (!wr.IsAlive) { task.Log(Level.Debug, "WeakReference for project is dead."); continue; } Project p = wr.Target as Project; if (p == null) { task.Log(Level.Debug, "WeakReference is not a" + " project! This should not be possible."); continue; } UpdateProjectWithBuilder(p, 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; } }
public static bool ScanAssembly(Assembly assembly, Task task) { task.Log(Level.Info, "Scanning assembly \"{0}\" for extensions.", assembly.GetName().Name); bool extensionAssembly = false; foreach (Type type in assembly.GetTypes()) { // // 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(type, task); if (!extensionFound) { extensionFound = ScanTypeForDataTypes(type, task); } if (!extensionFound) { extensionFound = ScanTypeForFunctions(type, task); } if (!extensionFound) { extensionFound = ScanTypeForFilters(type, task); } // if extension is found in type, then mark assembly as // extension assembly extensionAssembly = extensionAssembly || 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 (!extensionAssembly) { 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 extensionAssembly; }
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); }
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; }
private static void SetUpProperties(ArrayList attributeList, Task task, XmlNode xml, PropertyDictionary oldPropertyValues) { PropertyDictionary projectProperties = task.Project.Properties; StringBuilder logMessage = new StringBuilder(); foreach (MacroAttribute macroAttribute in attributeList) { string attributeName = macroAttribute.name; XmlAttribute xmlAttribute = xml.Attributes[attributeName]; string value = null; if (xmlAttribute != null) { value = projectProperties.ExpandProperties(xmlAttribute.Value, null); } else if (macroAttribute.defaultValue != null) { value = macroAttribute.defaultValue; } string localPropertyName = macroAttribute.LocalPropertyName; task.Log(Level.Debug, "Setting property " + localPropertyName + " to " + value); if (logMessage.Length > 0) logMessage.Append(", "); logMessage.Append(localPropertyName); logMessage.Append(" = '"); logMessage.Append(value); logMessage.Append("'"); if (projectProperties.Contains(localPropertyName)) { oldPropertyValues.Add(localPropertyName, projectProperties[localPropertyName]); projectProperties.Remove(localPropertyName); } if (value != null) projectProperties.Add(localPropertyName, value); } task.Log(Level.Info, logMessage.ToString()); }