Skip to content

Allow defining a typed context to be used throughout a Cake script. #2008

Closed
@patriksvensson

Description

@patriksvensson

Allow defining a typed context data class to be used throughout a Cake script.

Example of usage:

Declare a context in your Cake script like this:

public class MyData
{
    public string MyProperty { get; set; }
}

And now you can use it like this:

#load "./MyData.cake"

/////////////////////////////////////////////////////
// SETUP / TEARDOWN
/////////////////////////////////////////////////////

Setup<MyData>(context, data => 
{
    return new MyData {
        MyProperty = "Foo"
    }
});

/////////////////////////////////////////////////////
// TASKS
/////////////////////////////////////////////////////

Task("First")
    .Does<MyData>((context, data) => 
{
    Information("MyProperty = {0}", data.MyProperty);
});

Task("Second")
    .IsDependentOn("First")
    .Does(context => 
{
    // We can add an alias to make it easier to get data.
    Information("MyProperty = {0}", 
        context.Data.Get<MyData>().MyProperty);
});

/////////////////////////////////////////////////////
// EXECUTE
/////////////////////////////////////////////////////

RunTarget("Second");

This would remove dependencies on global variables and shared mutable states that make larger Cake script difficult to maintain in some scenarios. Declaring a Setup<TContextData> should be required to use the context data in the script. If no Setup<TContextData> has been declared, an error should be thrown when the script compiles.

With this approach we could even allow multiple setup/teardown methods for different types of context data, but that is something for later.

I'm going to write a quick proof-of-concept for this, and we can discuss this more after support for .NET Standard 2.0 has been released.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions