Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define a reusable subroutine/function/method within a Cake script?

Tags:

cakebuild

I'm trying out Cake (C# Make). So far all the examples and documentation have the script file declaring all of its code inside delegates, like this:

Task("Clean")
    .Does(() =>
{
    // Delete a file.
    DeleteFile("./file.txt");

    // Clean a directory.
    CleanDirectory("./temp");
});

However, one of the reasons I'm interested in using Cake is the possibility of writing my build scripts in a similar way to how I write code, as the scripts use a C#-based DSL. Included in this possibility is the ability to separate code that I use into methods (or functions / subroutines, whatever terminology is appropriate) so I can separate concerns and reuse code. For example, I may want to run the same set of steps for a multiple SKUs.

While I realize that I could create my own separate DLL with Script Aliases, I would like to avoid having to recompile a separate project every time I want to change these bits of shared code when working on the build script. Is there a way to define, inline with the normal build.cake file, methods that can still run the Cake aliases (e.g., DeleteFile) and can themselves be called from my Cake tasks?

like image 670
Joe Sewell Avatar asked Dec 22 '22 23:12

Joe Sewell


1 Answers

Cake is C#, so you can create classes, methods, just like in regular C#

I.e. declare a class in a cake file

public class MyClass
{
    public void MyMethod()
    {

    }

    public static void MyStaticMethod()
    {

    }
}

and then use it a script like

var myClass = new MyClass();

// Call instance method
myClass.MyMethod();

//Call static method
MyClass.MyStaticMethod();

The Cake DSL is based on Roslyn scripting so there are some differences, code is essentially already in a type so you can declare a method without a class for reuse

public void MyMethod()
{

}

and then it can be called like a global methods

MyMethod();

A few gotchas, doing class will change scoping so you won't have access to aliases / context and global methods. You can get around this by i.e. passing ICakeContext as a parameter to class

public class MyClass
{
    ICakeContext Context { get; }
    public MyClass(ICakeContext context)
    {
        Context = context;
    }

    public void MyMethod()
    {
        Context.Information("Hello");
    }
}

then used like this

// pass reference to Cake context
var myClass = new MyClass(Context);

// Call instance method which uses an Cake alias.
myClass.MyMethod();

You can have extension methods, but these can't be in a class, example:

public static void MyMethod(this ICakeContext context, string message)
{
    context.Information(message);
}


Context.MyMethod("Hello");
like image 160
devlead Avatar answered May 05 '23 04:05

devlead