/// <summary> /// Abstract out the creation of the new AssemblyTaskFactory with default task, and /// with some basic validation. /// </summary> private void SetupTaskFactory(IDictionary <string, string> factoryParameters, bool explicitlyLaunchTaskHost) { _taskFactory = new AssemblyTaskFactory(); _loadInfo = AssemblyLoadInfo.Create(null, Assembly.GetAssembly(typeof(TaskToTestFactories)).Location); _loadedType = _taskFactory.InitializeFactory(_loadInfo, "TaskToTestFactories", new Dictionary <string, TaskPropertyInfo>(), string.Empty, factoryParameters, explicitlyLaunchTaskHost, null, ElementLocation.Create("NONE"), String.Empty); Assert.True(_loadedType.Assembly.Equals(_loadInfo)); // "Expected the AssemblyLoadInfo to be equal" }
public void NoTypeNamePicksFirstType() { Type forwardingLoggerType = typeof(Microsoft.Build.Logging.ConfigurableForwardingLogger); string forwardingLoggerAssemblyLocation = forwardingLoggerType.Assembly.Location; Func <Type, object, bool> forwardingLoggerfilter = IsForwardingLoggerClass; Type firstPublicType = FirstPublicDesiredType(forwardingLoggerfilter, forwardingLoggerAssemblyLocation); TypeLoader loader = new TypeLoader(forwardingLoggerfilter); LoadedType loadedType = loader.Load(String.Empty, AssemblyLoadInfo.Create(null, forwardingLoggerAssemblyLocation)); Assert.NotNull(loadedType); Assert.True(loadedType.Assembly.AssemblyLocation.Equals(forwardingLoggerAssemblyLocation, StringComparison.OrdinalIgnoreCase)); Assert.True(loadedType.Type.Equals(firstPublicType)); Type fileLoggerType = typeof(Microsoft.Build.Logging.FileLogger); string fileLoggerAssemblyLocation = forwardingLoggerType.Assembly.Location; Func <Type, object, bool> fileLoggerfilter = IsLoggerClass; firstPublicType = FirstPublicDesiredType(fileLoggerfilter, fileLoggerAssemblyLocation); loader = new TypeLoader(fileLoggerfilter); loadedType = loader.Load(String.Empty, AssemblyLoadInfo.Create(null, fileLoggerAssemblyLocation)); Assert.NotNull(loadedType); Assert.True(loadedType.Assembly.AssemblyLocation.Equals(fileLoggerAssemblyLocation, StringComparison.OrdinalIgnoreCase)); Assert.True(loadedType.Type.Equals(firstPublicType)); }
public void NoTypeNamePicksFirstType() { Type forwardingLoggerType = typeof(net.r_eg.IeXod.Logging.ConfigurableForwardingLogger); string forwardingLoggerAssemblyLocation = forwardingLoggerType.Assembly.Location; Func <Type, object, bool> forwardingLoggerfilter = IsForwardingLoggerClass; Type firstPublicType = FirstPublicDesiredType(forwardingLoggerfilter, forwardingLoggerAssemblyLocation); TypeLoader loader = new TypeLoader(forwardingLoggerfilter); LoadedType loadedType = loader.Load(String.Empty, AssemblyLoadInfo.Create(null, forwardingLoggerAssemblyLocation)); Assert.NotNull(loadedType); Assert.Equal(forwardingLoggerAssemblyLocation, loadedType.Assembly.AssemblyLocation); Assert.Equal(firstPublicType, loadedType.Type); Type fileLoggerType = typeof(net.r_eg.IeXod.Logging.FileLogger); string fileLoggerAssemblyLocation = forwardingLoggerType.Assembly.Location; Func <Type, object, bool> fileLoggerfilter = IsLoggerClass; firstPublicType = FirstPublicDesiredType(fileLoggerfilter, fileLoggerAssemblyLocation); loader = new TypeLoader(fileLoggerfilter); loadedType = loader.Load(String.Empty, AssemblyLoadInfo.Create(null, fileLoggerAssemblyLocation)); Assert.NotNull(loadedType); Assert.Equal(fileLoggerAssemblyLocation, loadedType.Assembly.AssemblyLocation); Assert.Equal(firstPublicType, loadedType.Type); }
/// <summary> /// Converts the path to the logger assembly to a full path /// </summary> internal void ConvertPathsToFullPaths() { if (_loggerAssembly.AssemblyFile != null) { _loggerAssembly = AssemblyLoadInfo.Create(_loggerAssembly.AssemblyName, Path.GetFullPath(_loggerAssembly.AssemblyFile)); } }
internal void CreateFromStream(BinaryReader reader) { #region LoggerClassName if (reader.ReadByte() == 0) { _loggerClassName = null; } else { _loggerClassName = reader.ReadString(); } #endregion #region LoggerSwitchParameters if (reader.ReadByte() == 0) { _loggerSwitchParameters = null; } else { _loggerSwitchParameters = reader.ReadString(); } #endregion #region LoggerAssembly if (reader.ReadByte() == 0) { _loggerAssembly = null; } else { string assemblyName = null; string assemblyFile = null; if (reader.ReadByte() != 0) { assemblyFile = reader.ReadString(); } if (reader.ReadByte() != 0) { assemblyName = reader.ReadString(); } _loggerAssembly = AssemblyLoadInfo.Create(assemblyName, assemblyFile); } #endregion _verbosity = (LoggerVerbosity)reader.ReadInt32(); _loggerId = reader.ReadInt32(); }
public void Regress640476PartialName() { string forwardingLoggerLocation = typeof(Microsoft.Build.Logging.ConfigurableForwardingLogger).Assembly.Location; TypeLoader loader = new TypeLoader(IsForwardingLoggerClass); LoadedType loadedType = loader.Load("ConfigurableForwardingLogger", AssemblyLoadInfo.Create(null, forwardingLoggerLocation)); Assert.NotNull(loadedType); Assert.True(loadedType.Assembly.AssemblyLocation.Equals(forwardingLoggerLocation, StringComparison.OrdinalIgnoreCase)); string fileLoggerLocation = typeof(Microsoft.Build.Logging.FileLogger).Assembly.Location; loader = new TypeLoader(IsLoggerClass); loadedType = loader.Load("FileLogger", AssemblyLoadInfo.Create(null, fileLoggerLocation)); Assert.NotNull(loadedType); Assert.True(loadedType.Assembly.AssemblyLocation.Equals(fileLoggerLocation, StringComparison.OrdinalIgnoreCase)); }
public void Regress640476PartialName() { string forwardingLoggerLocation = typeof(net.r_eg.IeXod.Logging.ConfigurableForwardingLogger).Assembly.Location; TypeLoader loader = new TypeLoader(IsForwardingLoggerClass); LoadedType loadedType = loader.Load("ConfigurableForwardingLogger", AssemblyLoadInfo.Create(null, forwardingLoggerLocation)); Assert.NotNull(loadedType); Assert.Equal(forwardingLoggerLocation, loadedType.Assembly.AssemblyLocation); string fileLoggerLocation = typeof(net.r_eg.IeXod.Logging.FileLogger).Assembly.Location; loader = new TypeLoader(IsLoggerClass); loadedType = loader.Load("FileLogger", AssemblyLoadInfo.Create(null, fileLoggerLocation)); Assert.NotNull(loadedType); Assert.Equal(fileLoggerLocation, loadedType.Assembly.AssemblyLocation); }
public void Regress640476FullyQualifiedName() { Type forwardingLoggerType = typeof(Microsoft.Build.Logging.ConfigurableForwardingLogger); string forwardingLoggerLocation = forwardingLoggerType.Assembly.Location; TypeLoader loader = new TypeLoader(IsForwardingLoggerClass); LoadedType loadedType = loader.Load(forwardingLoggerType.FullName, AssemblyLoadInfo.Create(null, forwardingLoggerLocation)); Assert.NotNull(loadedType); Assert.Equal(forwardingLoggerLocation, loadedType.Assembly.AssemblyLocation); Type fileLoggerType = typeof(Microsoft.Build.Logging.FileLogger); string fileLoggerLocation = fileLoggerType.Assembly.Location; loader = new TypeLoader(IsLoggerClass); loadedType = loader.Load(fileLoggerType.FullName, AssemblyLoadInfo.Create(null, fileLoggerLocation)); Assert.NotNull(loadedType); Assert.Equal(fileLoggerLocation, loadedType.Assembly.AssemblyLocation); }
/// <summary> /// Creates a logger description from given data /// </summary> public LoggerDescription ( string loggerClassName, string loggerAssemblyName, string loggerAssemblyFile, string loggerSwitchParameters, LoggerVerbosity verbosity ) { _loggerClassName = loggerClassName; if (loggerAssemblyFile != null && !Path.IsPathRooted(loggerAssemblyFile)) { loggerAssemblyFile = FileUtilities.NormalizePath(loggerAssemblyFile); } _loggerAssembly = AssemblyLoadInfo.Create(loggerAssemblyName, loggerAssemblyFile); _loggerSwitchParameters = loggerSwitchParameters; _verbosity = verbosity; }
/// <summary> /// This is responsible for invoking Execute on the Task /// Any method calling ExecuteTask must remember to call CleanupTask /// </summary> /// <remarks> /// We also allow the Task to have a reference to the BuildEngine by design /// at ITask.BuildEngine /// </remarks> /// <param name="oopTaskHostNode">The OutOfProcTaskHostNode as the BuildEngine</param> /// <param name="taskName">The name of the task to be executed</param> /// <param name="taskLocation">The path of the task binary</param> /// <param name="taskFile">The path to the project file in which the task invocation is located.</param> /// <param name="taskLine">The line in the project file where the task invocation is located.</param> /// <param name="taskColumn">The column in the project file where the task invocation is located.</param> /// <param name="appDomainSetup">The AppDomainSetup that we want to use to launch our AppDomainIsolated tasks</param> /// <param name="taskParams">Parameters that will be passed to the task when created</param> /// <returns>Task completion result showing success, failure or if there was a crash</returns> internal OutOfProcTaskHostTaskResult ExecuteTask ( IBuildEngine oopTaskHostNode, string taskName, string taskLocation, string taskFile, int taskLine, int taskColumn, #if FEATURE_APPDOMAIN AppDomainSetup appDomainSetup, #endif IDictionary <string, TaskParameter> taskParams ) { buildEngine = oopTaskHostNode; this.taskName = taskName; #if FEATURE_APPDOMAIN _taskAppDomain = null; #endif wrappedTask = null; LoadedType taskType = null; try { TypeLoader typeLoader = new TypeLoader(TaskLoader.IsTaskClass); taskType = typeLoader.Load(taskName, AssemblyLoadInfo.Create(null, taskLocation)); } catch (Exception e) { if (ExceptionHandling.IsCriticalException(e)) { throw; } Exception exceptionToReturn = e; // If it's a TargetInvocationException, we only care about the contents of the inner exception, // so just save that instead. if (e is TargetInvocationException) { exceptionToReturn = e.InnerException; } return(new OutOfProcTaskHostTaskResult ( TaskCompleteType.CrashedDuringInitialization, exceptionToReturn, "TaskInstantiationFailureError", new string[] { taskName, taskLocation, String.Empty } )); } OutOfProcTaskHostTaskResult taskResult; if (taskType.HasSTAThreadAttribute()) { #if FEATURE_APARTMENT_STATE taskResult = InstantiateAndExecuteTaskInSTAThread(oopTaskHostNode, taskType, taskName, taskLocation, taskFile, taskLine, taskColumn, #if FEATURE_APPDOMAIN appDomainSetup, #endif taskParams); #else return(new OutOfProcTaskHostTaskResult ( TaskCompleteType.CrashedDuringInitialization, null, "TaskInstantiationFailureNotSupported", new string[] { taskName, taskLocation, typeof(RunInSTAAttribute).FullName } )); #endif } else { taskResult = InstantiateAndExecuteTask(oopTaskHostNode, taskType, taskName, taskLocation, taskFile, taskLine, taskColumn, #if FEATURE_APPDOMAIN appDomainSetup, #endif taskParams); } return(taskResult); }
private static int RunTarget() { StaticTarget target; var ser = new DataContractJsonSerializer(typeof(StaticTarget)); using (var memoryStream = new MemoryStream()) { using (var writer = new StreamWriter(memoryStream, Encoding.Default, 1024 * 4, true)) { string s; while ((s = Console.ReadLine()) != null) { writer.WriteLine(s); } } memoryStream.Position = 0; target = (StaticTarget)ser.ReadObject(memoryStream); } foreach (StaticTarget.Task staticTask in target.Tasks) { Type type; if (staticTask.AssemblyFile != null) { AssemblyName an = AssemblyName.GetAssemblyName(staticTask.AssemblyFile); if (an == null) { Console.WriteLine("Caouldn't get assembly name for assembly file: " + staticTask.AssemblyFile); return(1); } Assembly a = Assembly.Load(an); if (a == null) { Console.WriteLine("Couldn't loaded assembly for assembly: " + an.FullName + " from file: " + staticTask.AssemblyFile); return(1); } type = a.GetType(staticTask.Name.Split(',')[0]); if (type == null) { Console.WriteLine("Couldn't create type for string: " + staticTask.Name.Split(',')[0] + " assembly file: " + (staticTask.AssemblyFile ?? "null") + " and assembly name: " + (staticTask.AssemblyName ?? "null")); Console.WriteLine("Types in the assembly:\n" + string.Join(",\n", a.GetTypes().Select(availableType => availableType.FullName))); return(1); } } else { type = Type.GetType(staticTask.Name); if (type == null) { Console.WriteLine("Couldn't create type for string: " + staticTask.Name + " assembly file: " + (staticTask.AssemblyFile ?? "null") + " and assembly name: " + (staticTask.AssemblyName ?? "null")); return(1); } } var assemblyLoadInfo = AssemblyLoadInfo.Create(assemblyFile: staticTask.AssemblyFile, assemblyName: staticTask.AssemblyName); if (assemblyLoadInfo == null) { Console.WriteLine("Couldn't create type for assembly file: " + (staticTask.AssemblyFile ?? "null") + " and name: " + (staticTask.AssemblyName ?? "null")); return(1); } LoadedType loadedType = new LoadedType(type, assemblyLoadInfo, null); TaskPropertyInfo[] taskProperties = AssemblyTaskFactory.GetTaskParameters(loadedType); ITask task = TaskLoader.CreateTask(loadedType, staticTask.Name, staticTask.AssemblyName, 0, 0, null #if FEATURE_APPDOMAIN , null #endif , false #if FEATURE_APPDOMAIN , out var appDomain #endif ); foreach (var parameter in staticTask.Parameters) { var taskPropertyInfo = taskProperties.FirstOrDefault(property => property.Name == parameter.Key); if (taskPropertyInfo == null) { Console.Error.WriteLine("Could not find property: \"" + parameter.Key + "\" for task + \"" + staticTask.Name + "\""); return(1); } StaticTarget.Task.ParameterType parameterType = parameter.Value.ParameterType; switch (parameterType) { case StaticTarget.Task.ParameterType.Primitive: object value = GetTypedValue(parameter.Value.Primitive.Type, parameter.Value.Primitive.Value); TaskFactoryWrapper.SetPropertyValue(task, taskPropertyInfo, value); break; case StaticTarget.Task.ParameterType.Primitives: var values = GetTypedArrayValue(parameter.Value.Primitives.Type, parameter.Value.Primitives.Values); TaskFactoryWrapper.SetPropertyValue(task, taskPropertyInfo, values); break; case StaticTarget.Task.ParameterType.TaskItem: TaskFactoryWrapper.SetPropertyValue(task, taskPropertyInfo, new TaskItem(parameter.Value.TaskItem.ItemSpec, parameter.Value.TaskItem.Metadata)); break; case StaticTarget.Task.ParameterType.TaskItems: ITaskItem[] taskItems = parameter.Value.TaskItems.Select(taskItem => new TaskItem(taskItem.ItemSpec, taskItem.Metadata)).ToArray(); TaskFactoryWrapper.SetPropertyValue(task, taskPropertyInfo, taskItems); break; } } task.BuildEngine = new SimpleBuildEngine(); try { // MSBuild ignores this return value :( task.Execute(); } catch (Exception e) { Console.Error.WriteLine("TASK ERROR: " + e); return(1); } } return(0); }