Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally load .cake files

Tags:

cakebuild

I use CAKE 0.22.0.

Depending on the arguments passed into build.cake, I want to load different .cake files. E.g., if the parameter VisualStudioVersion has the value 2013, I want to load the vs2013dlls.cake file; if it has the value 2015, then I want to load the vs2015dlls.cake file; etc.

Looking at the CAKE page on preprocessor directives, I don't see any information on preprocessor keywords such as #if and #else.

I would prefer not to copy the contents of the other .cake files into build.cake, lest build.cake becomes much too bloated.

How can I load .cake files conditionally?

like image 672
M.Y. Babt Avatar asked Oct 09 '17 14:10

M.Y. Babt


1 Answers

Currently there's no support for conditionals when it comes to script loading the 0.23.0 will add if def support but pre processor directives are processed as same level/priority so won't help for your specific problem.

What you could do though is create a small bootstrapper cake script that pulls in the pieces needed for your specific scenarios.

Example using CakeExecuteExpression

var visualStudioVersion = Argument("VisualStudioVersion", "2017");
var statements = new List<string>();
var currentDir = MakeAbsolute(Directory("./"));

statements.Add("#load \"{0}/common.cake\"");
switch(visualStudioVersion)
{
    case "2013":
        statements.Add("#load \"{0}/vs2013.cake\"");
        break;
    case "2017":
        statements.Add("#load \"{0}/vs2017.cake\"");
        break;
    default:
    throw new Exception(string.Format("Unknown VisualStudioVersion: {0}", visualStudioVersion));
}

var expression = string.Format(
                    string.Join(
                        "\r\n",
                        statements
                        ),
                    currentDir
                    );

CakeExecuteExpression(
    expression
);

For above if argument VisualStudioVersion is set to 2017 or no argument specified then it'll load

  • common.cake
  • vs2017.cake

If argument VisualStudioVersion is set to 2013 then it'll load

  • common.cake
  • vs2013.cake

Example using CakeExecuteScript

Perhaps less complex is to just provide to different entry points i.e. have a build.cake file call either vs2013.cake or vs2017.cake depending on argument.

common.cake

Information("This will execute regardless version!") ;

vs2013.cake

#load "common.cake"
Information("Hello VS2013!");

vs2017.cake

#load "common.cake"
Information("Hello VS2017!");

build.cake

var visualStudioVersion = Argument("VisualStudioVersion", "2017");

switch(visualStudioVersion)
{
    case "2013":
        CakeExecuteScript("vs2013.cake");
        break;
    case "2017":
        CakeExecuteScript("vs2017.cake");
        break;
    default:
    throw new Exception(string.Format("Unknown VisualStudioVersion: {0}", visualStudioVersion));
}

2017 output

cake .\executescript.cake

Will output

This will execute regardless version!

Hello VS2017!

2013 output

cake .\executescript.cake --VisualStudioVersion=2013

will output

This will execute regardless version!

Hello VS2013!

like image 146
devlead Avatar answered Oct 19 '22 20:10

devlead