Keeping your Cake scripts organised with #load

The subject of Cake Build is a matter on its own, I will target the #load directive in C# scripting in this post.

For those who haven’t heard about C# Scripting, I encourage your to add it to your skills list in 2018. C# scripting is a ‘language’ that is build on top of Roslyn (the compiler platform). It allows for many dynamic programming to occur and I will go into more detail in a later post.

For now, just image C# scripting as .csx files.

Since scripts can be completely isolated (no class or static void main required), you will be able to offload parts of the script (functions, variables, etc) into seperate files.

This is also the case for Cake build scripts. Cake already provides great support for managing environment information.

However, If you don’t like this approach because it does not always provide the transparency you’d hoped for, like me; you could provide Cake with different ‘configuration’ files.

One solution could be to set variables (baseUrl, usernames, passwords, etc …) in separate scripts and load them into Cake.

Imagine this config script for example:

This script sets up the environment for our local build scenario.

We can now leverage these variables in our script.

See line #3 there ? This loads in the variables from the external script, and any other globally declared objects for that matter.

Where this could really shine is in deployment, you could possibly deploy an environment-specific params.csx file and extract all the parameter parsing out of cake.

Like what you read? Give Maarten Merken a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.