Last active
October 5, 2020 06:07
-
-
Save devlead/9f3be4970caede0498fd7de7c4bd4f23 to your computer and use it in GitHub Desktop.
Fluent Tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Tt]ools/ | |
[Aa]rtifacts/ | |
[Oo]bj/ | |
[Bb]in/ | |
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#load "FluentTasks.cake" | |
FluentSetup( | |
context => { | |
var buildDate = DateTime.UtcNow; | |
var version = FormattableString.Invariant( | |
$"{buildDate.Year}.{buildDate.Month}.{buildDate.Day}.{(int)((buildDate - buildDate.Date).TotalMinutes * 10)}" | |
); | |
return new { | |
BuildDate = buildDate, | |
Version = version, | |
BuildSystem = context.BuildSystem(), | |
ArtifactPath = context.Directory("./artifacts"), | |
Project = context.File("./HelloWorld.csproj"), | |
MSBuildSettings = new DotNetCoreMSBuildSettings() | |
.WithProperty("Version", version) | |
}; | |
} | |
) | |
.Then(clean => clean.Context.CleanDirectory(clean.Data.ArtifactPath)) | |
.Then(restore => restore.Context.DotNetCoreRestore( | |
restore.Data.Project, | |
new DotNetCoreRestoreSettings { | |
MSBuildSettings = restore.Data.MSBuildSettings | |
} | |
)) | |
.Then(build => build.Context.DotNetCoreBuild( | |
build.Data.Project, | |
new DotNetCoreBuildSettings { | |
NoRestore = true, | |
MSBuildSettings = build.Data.MSBuildSettings | |
} | |
)) | |
.Then(pack => pack.Context.DotNetCorePack( | |
pack.Data.Project, | |
new DotNetCorePackSettings { | |
NoRestore = true, | |
NoBuild = true, | |
OutputDirectory = pack.Data.ArtifactPath, | |
MSBuildSettings = pack.Data.MSBuildSettings | |
} | |
)) | |
.Then(push => push.Context.Information("Pushing version {0}", push.Data.Version)) | |
.If(push => !push.Data.BuildSystem.IsLocalBuild && !push.Data.BuildSystem.IsPullRequest) | |
.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public delegate void SetupDataHandler<TData>(Func<ISetupContext, TData> action) where TData : class; | |
public delegate CakeTaskBuilder TaskHandler(string name); | |
public delegate CakeReport RunTargetHandler(string target); | |
public delegate void DoesHandler<TData>((ICakeContext Context, TData Data) action); | |
public IFluentTasks<TData> FluentSetup<TData>(Func<ISetupContext, TData> action) where TData : class | |
{ | |
Setup(action); | |
return new FluentTasks<TData>(Setup, Task, RunTarget, Context); | |
} | |
public interface IFluentTasks<TData> | |
where TData : class | |
{ | |
SetupDataHandler<TData> SetupData { get; } | |
TaskHandler Task { get; } | |
RunTargetHandler RunTarget { get; } | |
string Target { get; } | |
CakeTaskBuilder CurrentTask { get; } | |
} | |
public class FluentTasks<TData> : IFluentTasks<TData> | |
where TData : class | |
{ | |
public SetupDataHandler<TData> SetupData { get; } | |
public TaskHandler Task { get; } | |
public RunTargetHandler RunTarget { get; } | |
public string Target { get; set; } | |
public CakeTaskBuilder CurrentTask { get; private set; } | |
public CakeTaskBuilder DefaultTask { get; } | |
public FluentTasks(SetupDataHandler<TData> setupData, TaskHandler task, RunTargetHandler runTarget, ICakeContext context) | |
{ | |
SetupData = setupData; | |
Task = name => { | |
var newTask = task(name); | |
DefaultTask.IsDependentOn(newTask); | |
CurrentTask?.IsDependeeOf(newTask.Task.Name); | |
CurrentTask = newTask; | |
return newTask; | |
}; | |
RunTarget = runTarget; | |
CurrentTask = null; | |
DefaultTask = task("Default"); | |
Target = context.Argument("target", DefaultTask.Task.Name); | |
} | |
} | |
public static IFluentTasks<TData> Then<TData>( | |
this IFluentTasks<TData> fluentTasks, | |
string name, | |
DoesHandler<TData> action | |
) where TData : class | |
{ | |
fluentTasks | |
.Task(name) | |
.Does<TData>((context, data) => action((context, data))); | |
return fluentTasks; | |
} | |
public static IFluentTasks<TData> Then<TData>( | |
this IFluentTasks<TData> fluentTasks, | |
DoesHandler<TData> action | |
) where TData : class | |
{ | |
var name = System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase( | |
string.Join( | |
" ", | |
action.Method.GetParameters().Select(name => name.Name) | |
) | |
); | |
fluentTasks | |
.Task(name) | |
.Does<TData>((context, data) => action((context, data))); | |
return fluentTasks; | |
} | |
public static CakeReport Run<TData>( | |
this IFluentTasks<TData> fluentTasks, | |
string target = null | |
) where TData : class | |
=> fluentTasks.RunTarget(fluentTasks.Target ?? "Default"); | |
public static IFluentTasks<TData> If<TData>( | |
this IFluentTasks<TData> fluentTasks, | |
Func<(ICakeContext Context, TData Data), bool> criteria | |
) where TData : class | |
{ | |
fluentTasks | |
.CurrentTask | |
.WithCriteria<TData>((context, data) => criteria((context, data))); | |
return fluentTasks; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFramework>net5.0</TargetFramework> | |
</PropertyGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment