Cake: Allow #load of a file to be optional

Created on 4 Aug 2017  路  18Comments  路  Source: cake-build/cake

Allow the load directive to point at an optional file that may or may not exist. Current behaviour is to throw an exception if the file is not found.

Desired so that we can use a common build.cake script across projects, some of which will have the optional file to load variables for use in the cake script

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

Current error in Cake v0.21.1

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

Feature

Most helpful comment

An alternative to optional could be if we also supported using pattern parameter enabling a globber as an alternative to path, then it could make sense to yield 0 files and be ok with that. Would also enable easier to load all cake files in a build folder i.e.

All 18 comments

Perhaps we can add an optional query string argument to indicate that the script is optional.

#load "local:?path=properties.cake&optional"

@cake-build/cake-team Any strong opinions against this?

Not totally against that, as long as we verbose log it's not found and loaded.

The workaround for now could be to use an environment variable and default to an empty script i.e.

#load "local:?path=%CUSTOM_PROPERTIES_CAKE%"

I like @devlead鈥檚 workaround more than introducing an optional parameter.

The problem as I see it, what happens if %CUSTOM_PROPERTIES_CAKE% isn't set?

Can鈥檛 the load directive check for what is essentially an empty string, and if that is the case, log the problem, and continue on without doing anything else?

Guess it depends on your bootstrapper, you could always set it there if not set.

Or you could construct it in a way do it falls back to a common file if not set.

I.e

#load "local:?path=bulid/%CUSTOM_PROPERTIES_CAKE%.cake"

Then you could have an empty build/.cake file in repo it would fallback on.

Or we can just support optional. Would be trivial in the load directive 馃槃

Then you explicitly state that this isn't a side effect.

An alternative to optional could be if we also supported using pattern parameter enabling a globber as an alternative to path, then it could make sense to yield 0 files and be ok with that. Would also enable easier to load all cake files in a build folder i.e.

I like @devlead鈥檚 pattern suggestion! That鈥檚 imo best solution? How about you?

While I like the idea of a globber pattern for loading multiple files, I think that is a separate issue.

The more I think about it, the more I think an explicit optional parameter is the only way to confirm that the intention is to omit loading that file. Anything else should really fail.

optional parameter sounds good to me. Also the pattern suggestion, but I would see this as a separate issue.

I'd also like support for #load with globber patterns! 馃槈

@gitfool Sounds like a good idea, but should perhaps be another issue/PR than this one. Should be a pretty easy fix if you want to create issue and/or PR: https://github.com/cake-build/cake/blob/develop/src/Cake.Core/Scripting/Processors/Loading/FileLoadDirectiveProvider.cs#L22

I believe this has been resolved as part of #2400 and can be closed.

#load "optional.cake"

If the file optional.cake does not exist a warning is displayed e.g. No scripts found at C:/project/optional.cake. and the build process continues.

Tested on both Cake 0.38.5 and 1.0.0-rc0001.

ps: On version 1.0.0-rc0001 the same warning appears twice. I suspect it's because of the auto bootstrapping (?).

ps: On version 1.0.0-rc0001 the same warning appears twice. I suspect it's because of the auto bootstrapping (?).

I would also assume this is because of bootstrapping. Can you create a separate issue for this? I don't know if there's an easy fix for it, but in a separate issue we can track it properly.

Closing this as resolved via #2400 in the v0.33.0 milestone.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johncrim picture johncrim  路  4Comments

pvwichen picture pvwichen  路  3Comments

RichardJFoster picture RichardJFoster  路  4Comments

Gargony picture Gargony  路  3Comments

vktr picture vktr  路  6Comments