public/Initialize-TerraformCompleterGrammar.ps1

function Initialize-TerraformCompleterGrammar
{
    [CmdletBinding()]
    param(
        [Parameter()]
        $TerraformExecutable = "terraform",

        [Parameter()]
        [switch] $Force
    )

    $GrammarFile = Join-Path $script:TerraformCompleter.Paths.Grammar -ChildPath $script:TerraformCompleter.GrammarFileName

    if((Test-Path -Path $GrammarFile -PathType Leaf) -and -not $Force)
    {
        Write-Verbose "Grammar file '$GrammarFile' already exists. Use -Force to overwrite."
        return
    }

    if($null -eq (Get-Command $TerraformExecutable -ErrorAction SilentlyContinue))
    {
        throw "Terraform executable not found at '$TerraformExecutable'"
    }

    $Grammar = Get-TerraformCommandGrammar -Command $TerraformExecutable
    $Grammar.Version = &$TerraformExecutable version -json | ConvertFrom-Json

    $Grammar | ConvertTo-Json -Depth 100 | Set-Content -Path $GrammarFile -Force
}