Get-FmgBuildDirectory

Filed under: PowerShell

In Fmg-Build, Full Metal Gnome Build Module, there is a function called Get-FmgBuildDirectory, its sole purpose is to get the build directory for the environment.

It can be explicitly set by calling Set-FmgBuildDirectory. The Fmg-Build module attempts to use CI specific environment variables. If none of those are found, it defaults to $Env:FMG_BUILD_DIRECTORY which is set by the module to the directory where the build config is read from.

It handles VSTS, App Veyor, Travis CI, Team City, and local builds. It’s core to the configuration in Fmg-Build as it used as the root directory for relative paths specified in the config. This way, users could start a path with ~/ or ./ and Fmg-Build will resolve it.

I’m posting it in case others will find it useful.

$fmgBuildDirectory = $null 
$fmgBuildDirectoryGenerator = $null;


function Get-FmgBuildDirectory() {
    Param()

    $var = Get-Variable -Name "fmgBuildDirectory" -Scope Script 
    if($var.Value) {
        return $var.Value;
    }

    if($Env:FMG_BUILD_DIRECTORY) {
        Set-FmgBuildDirectory $Env:FMG_BUILD_DIRECTORY
        return $Env:FMG_BUILD_DIRECTORY
    }

    if($Env:SYSTEM_DEFAULTWORKINGDIRECTORY) {
        Set-FmgBuildDirectory $Env:SYSTEM_DEFAULTWORKINGDIRECTORY
        return $Env:SYSTEM_DEFAULTWORKINGDIRECTORY
    }

    if($Env:APPVEYOR_BUILD_FOLDER) {
        Set-FmgBuildDirectory $Env:APPVEYOR_BUILD_FOLDER 
        return $Env:APPVEYOR_BUILD_FOLDER
    }

    if($Env:TRAVIS_BUILD_DIR) {
        Set-FmgBuildDirectory $Env:TRAVIS_BUILD_DIR
        return $Env:TRAVIS_BUILD_DIR
    }

    if($Env:WORKSPACE) {
        Set-FmgBuildDirectory $Env:WORKSPACE
        return $Env:WORKSPACE
    }

    return $Null;
}

function Test-FmgBuildDirectory() {
    $dir = Get-BuildDirectory
    if($dir -eq $null) {
        return $false;
    }

    return Test-Path $dir
}

function Set-FmgBuildDirectory() {
    Param(
        [Parameter(Position = 1)]
        [string] $Path
    )

    Set-Variable -Name "fmgBuildDirectory" -Value $Path -Scope Script
}
Nerdy Mishka