public void CanInjectAndExtractWithClasses() { IInjectionContext injection = new InjectionClass(); injection.State = 1; IBuildContext context = new BuildContext(); context.SetContextObject(injection); TaskClass task = new TaskClass(2); Assert.IsNull(task.InjectedObject); ContextInjector.Inject(context, task); Assert.IsNotNull(task.InjectedObject); Assert.AreEqual(1, task.InjectedObject.State); ReturnCode result = task.Run(); Assert.AreEqual(ReturnCode.Success, result); ContextInjector.Extract(context, task); IInjectionContext modifiedInjection = context.GetContextObject <IInjectionContext>(); Assert.AreEqual(task.NewState, modifiedInjection.State); }
public void CanInjectAndExtractWithStructs() { IInjectionContext injection = new InjectionStruct(); injection.State = 1; IBuildContext context = new BuildContext(); context.SetContextObject(injection); TaskStruct task = new TaskStruct(2); Assert.IsNull(task.InjectedObject); // Still need to box / unbox the struct task IBuildTask boxed = task; ContextInjector.Inject(context, boxed); task = (TaskStruct)boxed; Assert.IsNotNull(task.InjectedObject); Assert.AreEqual(1, task.InjectedObject.State); ReturnCode result = task.Run(); Assert.AreEqual(ReturnCode.Success, result); ContextInjector.Extract(context, task); IInjectionContext modifiedInjection = context.GetContextObject <IInjectionContext>(); Assert.AreEqual(task.NewState, modifiedInjection.State); }
static void ExtractTestData(IBuildTask task, out CustomAssets customAssets, out TestContent content, out TestDependencyData dependencyData) { IBuildContext context = new BuildContext(); ContextInjector.Extract(context, task); customAssets = (CustomAssets)context.GetContextObject <ICustomAssets>(); content = (TestContent)context.GetContextObject <IBundleBuildContent>(); dependencyData = (TestDependencyData)context.GetContextObject <IDependencyData>(); }
/// <summary> /// Basic run implementation that takes a set of tasks, a context, and runs returning the build results. /// <seealso cref="IBuildTask"/>, <seealso cref="IBuildContext"/>, and <seealso cref="ReturnCode"/> /// </summary> /// <param name="pipeline">The set of build tasks to run.</param> /// <param name="context">The build context to use for this run.</param> /// <returns>Return code with status information about success or failure causes.</returns> public static ReturnCode Run(IList <IBuildTask> pipeline, IBuildContext context) { // Avoid throwing exceptions in here as we don't want them bubbling up to calling user code if (pipeline == null) { BuildLogger.LogException(new ArgumentNullException("pipeline")); return(ReturnCode.Exception); } // Avoid throwing exceptions in here as we don't want them bubbling up to calling user code if (context == null) { BuildLogger.LogException(new ArgumentNullException("context")); return(ReturnCode.Exception); } IProgressTracker tracker; if (context.TryGetContextObject(out tracker)) { tracker.TaskCount = pipeline.Count; } context.TryGetContextObject(out IBuildLogger logger); foreach (IBuildTask task in pipeline) { { try { if (!tracker.UpdateTaskUnchecked(task.GetType().Name.HumanReadable())) { return(ReturnCode.Canceled); } ContextInjector.Inject(context, task); ReturnCode result; using (logger.ScopedStep(LogLevel.Info, task.GetType().Name)) result = task.Run(); if (result < ReturnCode.Success) { return(result); } ContextInjector.Extract(context, task); } catch (Exception e) { BuildLogger.LogError("Build Task {0} failed with exception:\n{1}\n{2}", task.GetType().Name, e.Message, e.StackTrace); return(ReturnCode.Exception); } } } return(ReturnCode.Success); }
static ReturnCode RunTask <T>(params IContextObject[] args) where T : IBuildTask { IBuildContext context = new BuildContext(args); IBuildTask instance = Activator.CreateInstance <T>(); ContextInjector.Inject(context, instance); var result = instance.Run(); ContextInjector.Extract(context, instance); return(result); }
/// <summary> /// Basic run implementation that takes a set of tasks, a context, and runs returning the build results. /// <seealso cref="IBuildTask"/>, <seealso cref="IBuildContext"/>, and <seealso cref="ReturnCode"/> /// </summary> /// <param name="pipeline">The set of build tasks to run.</param> /// <param name="context">The build context to use for this run.</param> /// <returns>Return code with status information about success or failure causes.</returns> public static ReturnCode Run(IList <IBuildTask> pipeline, IBuildContext context) { // Avoid throwing exceptions in here as we don't want them bubbling up to calling user code if (pipeline == null) { BuildLogger.LogException(new ArgumentNullException("pipeline")); return(ReturnCode.Exception); } // Avoid throwing exceptions in here as we don't want them bubbling up to calling user code if (context == null) { BuildLogger.LogException(new ArgumentNullException("context")); return(ReturnCode.Exception); } IProgressTracker tracker; if (context.TryGetContextObject(out tracker)) { tracker.TaskCount = pipeline.Count; } foreach (IBuildTask task in pipeline) { try { if (!tracker.UpdateTaskUnchecked(task.GetType().Name.HumanReadable())) { return(ReturnCode.Canceled); } ContextInjector.Inject(context, task); var result = task.Run(); if (result < ReturnCode.Success) { return(result); } ContextInjector.Extract(context, task); } catch (Exception e) { BuildLogger.LogException(e); return(ReturnCode.Exception); } } return(ReturnCode.Success); }
static CalculateCustomDependencyData CreateDefaultBuildTask(List <CustomContent> customAssets, Dictionary <GUID, AssetLoadInfo> assetInfo = null) { var task = new CalculateCustomDependencyData(); var testParams = new TestBuildParameters(); var testContent = new TestContent(customAssets); if (assetInfo == null) { assetInfo = new Dictionary <GUID, AssetLoadInfo>(); } var testData = new TestDependencyData(assetInfo); IBuildContext context = new BuildContext(testParams, testContent, testData); ContextInjector.Inject(context, task); return(task); }
public void CanInjectIBuildContextAsInOnly() { IBuildContext context = new BuildContext(); TaskContext task = new TaskContext(); Assert.IsNull(task.InjectedContext); ContextInjector.Inject(context, task); Assert.IsNotNull(task.InjectedContext); Assert.AreEqual(context, task.InjectedContext); Assert.Throws <InvalidOperationException>(() => { ContextInjector.Extract(context, task); }); }