Search code examples
cakebuild

Check for empty file and quit cakebuild


I am attempting to write a check in my Cake build script to pull in a file from BuildParameters and check if the file contents are empty -- if contents are empty, throw an exception and quit the build.

I am attempting to use FileReadText from the FileHelpers namespace but for some reason I cannot get my build to recognize the file command. I am following the syntax and documentation found here: https://cakebuild.net/api/Cake.FileHelpers/FileHelperAliases/97F5679A

Here is the code I am trying in build.cake:

var fileReadText= FileReadText(Parameters.TestParameters.TestListFP);
var fileText= fileReadText.ThrowIfNullOrEmpty(nameof(fileReadText));

The argument Parameters.TestParameters.TestListFP is set in my Parameters.cake file as such:

TestListFP = context.File("C:\Some\Path\some_file_name.txt");

Using the above code, I see this error:

error CS0103: The name 'FileReadText' does not exist in the current context

Note that I do not have a ICakeContext in build.cake, just BuildParameters.

I tried to resolve the issue by adding using Cake.FileHelpers; at the top of my build.cake file, but then I see this error:

The type or namespace name 'FileHelpers' does not exist in the namespace 'Cake' (are you missing an assembly reference?)

The script works fine without my FileReadText code, so I know TestListFP is actually a valid file.

I think I am inherently misunderstanding how to use FileHelpers and FileReadText and I could not find any examples of usage in documentation or anywhere else. If anyone has guidance on how to use this method, or a better way to accomplish what I am trying to do, I would appreciate the help.


Solution

  • Have you added the #addin pre-processor directive, as mentioned here:

    https://github.com/cake-contrib/Cake.FileHelpers/#cakefilehelpers

    You can easily reference Cake.FileHelpers directly in your build script via a cake addin:

    #addin "Cake.FileHelpers"