Can the load directive in CAKE be used on an optional file?
Asked Answered
S

3

7

In a cake script, when using the load directive (#load) can this be optional? If the referenced file does not exist I don't want the CAKE script to return an exception.

The documentation references the use of a '?' to make it attempt to load, but even with this I get exceptions if the file does not exist.

#load "local:?path=properties.cake";

But on exection I get error:

Analyzing build script...
Error: Could not find script 'C:/projects/my-project/properties.cake'.

The CAKE script is common across many projects, only some of which would have the additional file hence the question.

Thanks

Sprocket answered 2/8, 2017 at 15:8 Comment(0)
R
5

There currently isn't a way of optionally load a script, but feel free to create an issue at https://github.com/cake-build/cake for this.

The ? is not indicating an attempt to load, it's simply a query string separator. The #load preprocessor value is an URI with optional fallback to a path for backwards compatibility with older versions of Cake.

Rank answered 2/8, 2017 at 15:29 Comment(0)
G
0

You can solve this by provisioning a blank/default properties.cake in build.ps1 script, like this.

if (!(Test-Path "properties.cake")) {
    Write-Host "First run! Generating properties.cake..."
    Copy-Item "properties.cake.default" -Destination ".\properties.cake"
}

# Build Cake arguments
$cakeArguments = @("$Script");
(...)

Then you need to commit a properties.cake.default file with your cake script!

Glob answered 10/9, 2019 at 2:41 Comment(0)
U
0

This is now supported at least since Cake version 0.38.5. The #load directive will only display a warning if the .cake file being loaded does not exist.

Screenshot of Cake displaying a warning when the file does not exist

Unceasing answered 4/2, 2021 at 2:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.