Search code examples
c#dockerhome-directorycakebuild

How to get user directory in a cake build


I need the user directory path (absolute) in my cake script, to mount the nuget cache of the build agent to the docker container, that builds the application. How do I get them?

Directory("~")

doesn't work.


Solution

  • Typically, on both Windows and *nix, the user home folder is stored in an environment variable. In which case, you should be able to do something like this:

    Task("Default")
    .Does(() =>
    {
        if(IsRunningOnWindows()) {
            Information(EnvironmentVariable("HOMEPATH"));
        } 
        else 
        {
            Information(EnvironmentVariable("HOME"));
        }
    });
    

    Assuming the above contains what you want, you could then pass the result to Directory().

    UPDATE:

    Try the following:

    Task("Default")
        .Does(() =>
    {
        if(IsRunningOnWindows()) {
            Information("{0}{1}", EnvironmentVariable("HOMEDRIVE"), EnvironmentVariable("HOMEPATH"));
        } else {
            Information(EnvironmentVariable("HOME"));
        }
    });