Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/Cake.Frosting.Example/TaskChains.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Core.Diagnostics;
using Cake.Frosting;
using Cake.Frosting.TaskChains;

public static class ChainedTasksProgram
{
public static int MainDemo(string[] args)
{
return new CakeHost()
.UseContext<BuildContext>()
.UseChainedTaskConfigurator<AppTaskChainProvider>()
.Run(args);
}
}

/// <summary>
/// Tasks chain provider.
/// </summary>
public class AppTaskChainProvider : ITaskChainProvider
{
public TaskChainItem GetChain()
{
return Chain
.Task<Task1>()
.Group("Do something", _ =>
{
// Reference the task type.
_.Task<Task2>()
// Reference the task by name ([TaskName("Task3: do something")]).
.Task("Task3: do something");
})
// Task chain inline groups example.
// .Group("Do something else", _ =>
// {
// _.Task<SomeTask>()
// .Group("Internal group", _ =>
// {
// _.Task<OtherTask>();
// })
// .Task("Task name");
// })
// The last task in the chain which the Default task will be dependent on to execute all the chain.
.Task<FinalTask>();
}
}

[TaskName("Task1 chained")]
public sealed class Task1 : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.Log.Information("Task1");
}
}

public sealed class Task2 : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.Log.Information("Task2");
}
}

[TaskName("Task3: do something")]
public sealed class Task3 : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.Log.Information("Task3");
}
}

public sealed class FinalTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.Log.Information("FinalTask");
}
}

[TaskName("Default")]
public class ChainedDefaultTask : FrostingTask
{
}
3 changes: 0 additions & 3 deletions src/Cake.Frosting/Cake.Frosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
<ProjectReference Include="..\Cake.NuGet\Cake.NuGet.csproj" />
<ProjectReference Include="..\Cake.DotNetTool.Module\Cake.DotNetTool.Module.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Internal\Imported\" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
3 changes: 3 additions & 0 deletions src/Cake.Frosting/CakeHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Cake.Core.Modules;
using Cake.DotNetTool.Module;
using Cake.Frosting.Internal;
using Cake.Frosting.TaskChains;
using Cake.NuGet;
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli;
Expand Down Expand Up @@ -116,6 +117,8 @@ private ServiceCollection CreateServiceCollection()
services.AddSingleton<VersionFeature>();
services.AddSingleton<InfoFeature>();

services.AddSingleton<ITaskConfigurator, DefaultTaskConfigurator>();

services.AddSingleton<FrostingRunner>();
services.AddSingleton<FrostingDryRunner>();
services.AddSingleton<FrostingTreeRunner>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cake.Cli;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting.TaskChains;

namespace Cake.Frosting.Internal
{
Expand All @@ -14,9 +15,10 @@ internal sealed class FrostingDescriptionRunner : FrostingEngine<DescriptionScri
public FrostingDescriptionRunner(DescriptionScriptHost host,
ICakeEngine engine, IFrostingContext context, ICakeLog log,
IEnumerable<IFrostingTask> tasks,
ITaskConfigurator taskConfigurator,
IFrostingSetup setup = null, IFrostingTeardown teardown = null,
IFrostingTaskSetup taskSetup = null, IFrostingTaskTeardown taskTeardown = null)
: base(host, engine, context, log, tasks, setup, teardown, taskSetup, taskTeardown)
: base(host, engine, context, log, tasks, taskConfigurator, setup, teardown, taskSetup, taskTeardown)
{
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Cake.Frosting/Internal/Engines/FrostingDryRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cake.Cli;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting.TaskChains;

namespace Cake.Frosting.Internal
{
Expand All @@ -14,9 +15,10 @@ internal sealed class FrostingDryRunner : FrostingEngine<DryRunScriptHost<IFrost
public FrostingDryRunner(DryRunScriptHost<IFrostingContext> host,
ICakeEngine engine, IFrostingContext context, ICakeLog log,
IEnumerable<IFrostingTask> tasks,
ITaskConfigurator taskConfigurator,
IFrostingSetup setup = null, IFrostingTeardown teardown = null,
IFrostingTaskSetup taskSetup = null, IFrostingTaskTeardown taskTeardown = null)
: base(host, engine, context, log, tasks, setup, teardown, taskSetup, taskTeardown)
: base(host, engine, context, log, tasks, taskConfigurator, setup, teardown, taskSetup, taskTeardown)
{
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Cake.Frosting/Internal/Engines/FrostingRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cake.Cli;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting.TaskChains;

namespace Cake.Frosting.Internal
{
Expand All @@ -14,9 +15,10 @@ internal sealed class FrostingRunner : FrostingEngine<BuildScriptHost<IFrostingC
public FrostingRunner(BuildScriptHost<IFrostingContext> host,
ICakeEngine engine, IFrostingContext context, ICakeLog log,
IEnumerable<IFrostingTask> tasks,
ITaskConfigurator taskConfigurator,
IFrostingSetup setup = null, IFrostingTeardown teardown = null,
IFrostingTaskSetup taskSetup = null, IFrostingTaskTeardown taskTeardown = null)
: base(host, engine, context, log, tasks, setup, teardown, taskSetup, taskTeardown)
: base(host, engine, context, log, tasks, taskConfigurator, setup, teardown, taskSetup, taskTeardown)
{
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Cake.Frosting/Internal/Engines/FrostingTreeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cake.Cli;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting.TaskChains;

namespace Cake.Frosting.Internal
{
Expand All @@ -14,9 +15,10 @@ internal sealed class FrostingTreeRunner : FrostingEngine<TreeScriptHost>
public FrostingTreeRunner(TreeScriptHost host,
ICakeEngine engine, IFrostingContext context, ICakeLog log,
IEnumerable<IFrostingTask> tasks,
ITaskConfigurator taskConfigurator,
IFrostingSetup setup = null, IFrostingTeardown teardown = null,
IFrostingTaskSetup taskSetup = null, IFrostingTaskTeardown taskTeardown = null)
: base(host, engine, context, log, tasks, setup, teardown, taskSetup, taskTeardown)
: base(host, engine, context, log, tasks, taskConfigurator, setup, teardown, taskSetup, taskTeardown)
{
}
}
Expand Down
93 changes: 20 additions & 73 deletions src/Cake.Frosting/Internal/FrostingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cake.Common.Diagnostics;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.Scripting;
using Cake.Frosting.TaskChains;

namespace Cake.Frosting.Internal
{
Expand All @@ -28,13 +32,15 @@ internal abstract class FrostingEngine<THost> : IFrostingEngine
private readonly IFrostingTaskTeardown _taskTeardown;
private readonly THost _host;
private readonly ICakeEngine _engine;
private readonly ITaskConfigurator _taskConfigurator;

public ExecutionSettings Settings => _host.Settings;

protected FrostingEngine(
THost host,
ICakeEngine engine, IFrostingContext context, ICakeLog log,
IEnumerable<IFrostingTask> tasks,
ITaskConfigurator taskConfigurator,
IFrostingSetup setup = null,
IFrostingTeardown teardown = null,
IFrostingTaskSetup taskSetup = null,
Expand All @@ -49,6 +55,7 @@ protected FrostingEngine(
_taskSetup = taskSetup;
_taskTeardown = taskTeardown;
_tasks = new List<IFrostingTask>(tasks ?? Array.Empty<IFrostingTask>());
_taskConfigurator = taskConfigurator;
}

public CakeReport Run(IEnumerable<string> targets)
Expand Down Expand Up @@ -92,86 +99,26 @@ private void ConfigureLifetime()

private void ConfigureTasks()
{
if (_tasks == null)
if (_tasks != null)
{
return;
}

foreach (var task in _tasks)
{
var name = task.GetTaskName();
_log.Debug("Registering task: {0}", name);

// Get the task's context type.
if (!task.HasCompatibleContext(_context))
foreach (var task in _tasks)
{
const string format = "Task cannot be used since the context isn't convertible to {0}.";
_log.Warning(format, task.GetContextType().FullName);
}
else
{
// Register task with the Cake engine.
var cakeTask = _engine.RegisterTask(name);
var name = task.GetTaskName();
_log.Debug("Registering task: {0}", name);

var description = task.GetTaskDescription();
if (!string.IsNullOrWhiteSpace(description))
// Get the task's context type.
if (!task.HasCompatibleContext(_context))
{
cakeTask.Description(description);
const string format = "Task cannot be used since the context isn't convertible to {0}.";
_log.Warning(format, task.GetContextType().FullName);
continue;
}

// Is the run method overridden?
if (task.IsRunOverridden(_context))
{
cakeTask.Does(task.RunAsync);
}

// Is the criteria method overridden?
if (task.IsShouldRunOverridden(_context))
{
cakeTask.WithCriteria(task.ShouldRun, task.SkippedMessage);
}

// Continue on error?
if (task.IsContinueOnError())
{
cakeTask.ContinueOnError();
}

// Is the on error method overridden?
if (task.IsOnErrorOverridden(_context))
{
cakeTask.OnError(exception => task.OnError(exception, _context));
}

// Is the finally method overridden?
if (task.IsFinallyOverridden(_context))
{
cakeTask.Finally(() => task.Finally(_context));
}

// Add dependencies
foreach (var dependency in task.GetDependencies())
{
var dependencyName = dependency.GetTaskName();
if (!typeof(IFrostingTask).IsAssignableFrom(dependency.Task))
{
throw new FrostingException($"The dependency '{dependencyName}' is not a valid task.");
}

cakeTask.IsDependentOn(dependencyName);
}

// Add reverse dependencies
foreach (var dependee in task.GetReverseDependencies())
{
var dependeeName = dependee.GetTaskName();
if (!typeof(IFrostingTask).IsAssignableFrom(dependee.Task))
{
throw new FrostingException($"The reverse dependency '{dependeeName}' is not a valid task.");
}
// Register task with the Cake engine.
var cakeTask = _engine.RegisterTask(name);

cakeTask.IsDependeeOf(dependeeName);
}
// Configure the task.
_taskConfigurator.Configure(task, cakeTask);
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/Cake.Frosting/TaskChains/CakeHostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Extensions.DependencyInjection;

namespace Cake.Frosting.TaskChains
{
/// <summary>
/// Extension methods for task chains.
/// </summary>
public static class CakeHostExtensions
{
/// <summary>
/// Registers the task configurator that will configure the build task upon startup.
/// </summary>
/// <typeparam name="T">Task configurator type.</typeparam>
/// <param name="host">Cake host.</param>
/// <returns>Cake host.</returns>
public static CakeHost UseTaskConfigurator<T>(this CakeHost host) where T : class, ITaskConfigurator
{
host.ConfigureServices(services => { services.AddSingleton<ITaskConfigurator, T>(); });
return host;
}

/// <summary>
/// Registers the chained task configurator that will configure the build task chains upon startup.
/// </summary>
/// <typeparam name="T">Task configurator type.</typeparam>
/// <param name="host">Cake host.</param>
/// <returns>Cake host.</returns>
public static CakeHost UseChainedTaskConfigurator<T>(this CakeHost host) where T : class, ITaskChainProvider
{
host.ConfigureServices(services =>
{
services.AddSingleton<ITaskChainProvider, T>();
services.AddSingleton<ITaskConfigurator, ChainedTaskConfigurator>();
});
return host;
}
}
}
Loading