MozconfigWrapper.psm1

function Get-MozconfigDir {
    if ($env:BUILDWITH_HOME) {
        return $env:BUILDWITH_HOME
    }
    return Join-Path $env:USERPROFILE ".mozconfigs"
}

function Get-SelectedMozconfig {
    return Get-Content (Join-Path (Get-MozconfigDir) ".active")
}

$GetMozconfigNameCompletions = [scriptblock]::Create({
    $WordToComplete = $args[2]
    $names = Where-Object -Property Name -InputObject (Get-Mozconfig) -Like $WordToComplete | Sort-Object
    $names | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_) }
})

<#
 .Synopsis
  Print the list of available mozconfigs
#>

function Get-Mozconfig {

    $files = Get-ChildItem -Path (Get-MozconfigDir) | Where-Object Name -NotMatch "^\..*"
    return $files.Name
}

<#
 .Synopsis
  Activate a mozconfig

 .Parameter Name
  Name of the mozconfig you want to use. Should have been previously created using mkmozconfig.
#>

function Select-Mozconfig {
    param([Parameter(Mandatory = $false)] [string] $Name)

    if (!$Name) {
        Write-Output (Get-SelectedMozconfig)
        return
    }

    $file = Join-Path (Get-MozconfigDir) $Name
    if (Test-Path $file -PathType Leaf) {
        Out-File -FilePath (Join-Path (Get-MozconfigDir) ".active") -InputObject $Name
        $env:MOZCONFIG = $file
    }
    else {
        Write-Error -Message "Mozconfig $Name doesn't exist" -Category InvalidArgument -RecommendedAction "Use mkmozconfig to create $name."
    }
}
Register-ArgumentCompleter -CommandName Select-Mozconfig -ParameterName Name -ScriptBlock $GetMozconfigNameCompletions

<#
 .Synopsis
  Open a mozconfig file for editing

 .Parameter Name
  Name of the mozconfig you want to edit. If omitted, uses the currently selected mozconfig.
#>

function Edit-Mozconfig {
    param([Parameter(Mandatory = $false)] [string] $Name)

    if (!$Name) {
        $Name = (Get-SelectedMozconfig)
    }
    $path = (Join-Path (Get-MozconfigDir) $Name)

    if (!$env:EDITOR) {
        Write-Error "No EDITOR environment variable set. The file that would have been opened was $path" -RecommendedAction "Set `$env:EDITOR` to the path to your preferred text editor."
        return
    }

    & $env:EDITOR $path
}
Register-ArgumentCompleter -CommandName Edit-Mozconfig -ParameterName Name -ScriptBlock $GetMozconfigNameCompletions

<#
 .Synopsis
  Create a new mozconfig and open it for editing

 .Parameter Name
  Name of the mozconfig you want to use. Should have been previously created using mkmozconfig.
#>

function Add-Mozconfig {
    param([Parameter(Mandatory = $true)] [string] $Name)

    $templatePath = (Join-Path (Get-MozconfigDir) ".template")
    if (!(Test-Path $templatePath -PathType Leaf)) {
        $defaultTemplate =
        @"
ac_add_options --enable-application=browser
ac_add_options --enable-debug
mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/objdirs/
"@

        Out-File -FilePath $templatePath -InputObject $defaultTemplate
    }

    $template = Get-Content $templatePath
    $text = @()

    foreach ($line in $template) {
        if ($line.Contains("MOZ_OBJDIR")) {
            $text += $line + $Name
        }
        else {
            $text += $line
        }
    }

    Out-File -FilePath (Join-Path (Get-MozconfigDir) $Name) -InputObject $text

    Edit-Mozconfig $Name
}

<#
 .Synopsis
  Delete a mozconfig

 .Parameter Name
  Name of the mozconfig you want to use. Should have been previously created using mkmozconfig.
#>

function Remove-Mozconfig {
    [CmdletBinding(SupportsShouldProcess)]
    param([Parameter(Mandatory = $true)] [string] $Name)

    $file = Join-Path (Get-MozconfigDir) $Name
    if ($PSCmdlet.ShouldProcess($file)) {
        Remove-Item $file
    }
}
Register-ArgumentCompleter -CommandName Remove-Mozconfig -ParameterName Name -ScriptBlock $GetMozconfigNameCompletions

# Set up the last active mozconfig upon loading the module
Select-Mozconfig -Name (Get-Content (Join-Path (Get-MozconfigDir) ".active"))

Set-Alias buildwith Select-Mozconfig
Set-Alias edmozconfig Edit-Mozconfig
Set-Alias mkmozconfig Add-Mozconfig
Set-Alias rmmozconfig Remove-Mozconfig
Set-Alias lsmozconfig Get-Mozconfig

Export-ModuleMember -Function Select-Mozconfig, Edit-Mozconfig, Add-Mozconfig, Remove-Mozconfig, Get-Mozconfig -Alias *