ALCompiler.psm1

function Get-ALCompiler {
    param(
        [string]$path
    )
    Process
    {
        $url = "https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-dynamics-smb/vsextensions/al/6.6.425415/vspackage"
        $vsixpath = Join-Path -Path $path -ChildPath "al.vsix"
        $zippath = Join-Path -Path $path -ChildPath "al.zip"

        if(-not (Test-Path -Path $path))
        {
            $null = New-Item -Path $path -ItemType Directory
            Invoke-RestMethod $url -OutFile $vsixpath
            Rename-Item -Path $vsixpath -NewName $zippath
            Expand-Archive -Path $zippath -DestinationPath $path
        }
    }
}

function Invoke-ALCompiler {
    param (
        [string]$compilerpath,
        [string]$projectpath,
        [int]$buildnumber,
        [string]$outputpath
    )
    process
    {
        #Update build number
        $jsonpath = Join-Path -Path $projectpath -ChildPath "app.json"
        $oldjson = Get-Content -Path $jsonpath -raw
        $newjson = $oldjson | ConvertFrom-Json
        $oldversion = (New-Object Version -ArgumentList $newjson.version)
        $newversion = (New-Object Version -ArgumentList $oldversion.Major, $oldversion.Minor, $oldversion.Build, $buildnumber).ToString()
        $newjson.Version = $newversion
        $newjson | ConvertTo-Json -depth 32 | set-content -Path $jsonpath
        $outputfile = Join-Path -Path $outputpath -ChildPath "$($newjson.Publisher)_$($newjson.Name)_$($newversion).app";
        $cachepath = Join-Path -Path $projectpath -ChildPath ".alpackages";
        $compiler = Join-Path -Path $compilerpath -ChildPath "extension\bin\alc.exe"
        & $compiler /project:$projectpath /out:$outputfile /packagecachepath:$cachepath
        $oldjson | set-content -Path $jsonpath
    }
}