tasks/powerbi.tasks.ps1

$SkipPowerBi = $false
$SkipExtractPbiReports = $false
$SkipExportPbixData = $false
$GeneratePbixFile = $false

$PbiToolsVersion = "1.0.0-rc.1"
$PbiToolsDir = Join-Path $here "_tools"
$PbiReportsDir = $false
$PbiReportDataDirSuffix = "__data"
$PbixDir = Join-Path $here "_pbix"
$PbiWinToolPath = ""
$PbiCoreToolPath = ""

task InstallPbiTools -If { !$PbiWinToolPath -and !$PbiCoreToolPath } {

    if($IsWindows)
    {
        $pbit_url = "https://github.com/pbi-tools/pbi-tools/releases/download/$PbiToolsVersion/pbi-tools.$PbiToolsVersion.zip"
        $pbitcx64_url = "https://github.com/pbi-tools/pbi-tools/releases/download/$PbiToolsVersion/pbi-tools.core.$($PbiToolsVersion)_win-x64.zip"
        $pbit_dest = Join-Path $env:TEMP (Split-Path -Path $pbit_url -Leaf)
        $pbitcx64_dest = Join-Path $env:TEMP (Split-Path -Path $pbitcx64_url -Leaf)

        if (!(Test-Path -Path $pbit_dest))
        {
            Invoke-RestMethod -Uri $pbit_url -OutFile $pbit_dest
        }
        Expand-Archive $pbit_dest (Join-Path $PbiToolsDir "pbit") -Force

        if (!(Test-Path -Path $pbitcx64_dest))
        {
            Invoke-RestMethod -Uri $pbitcx64_url -OutFile $pbitcx64_dest
        }
        Expand-Archive $pbitcx64_dest (Join-Path $PbiToolsDir "pbitc") -Force

        # Store the path to the Windows-only CLI tool
        $script:pbiWinToolPath = Join-Path -Resolve $PbiToolsDir "pbit" "pbi-tools.exe"
        # Store the path to the cross-platform CLI tool
        $script:pbiCoreToolPath = Join-Path -Resolve $PbiToolsDir "pbitc" "pbi-tools.core.exe"
    }

    if($IsLinux)
    {
        $pbitcl64_url = "https://github.com/pbi-tools/pbi-tools/releases/download/$PbiToolsVersion/pbi-tools.core.$($PbiToolsVersion)_linux-x64.zip"
        $pbitcl64_dest = Join-Path ([IO.Path]::GetTempPath()) (Split-Path -Path $pbitcl64_url -Leaf)

        if (!(Test-Path -Path $pbitcl64_dest))
        {
            Invoke-RestMethod -Uri $pbitcl64_url -OutFile $pbitcl64_dest
        }
        Expand-Archive $pbitcl64_dest (Join-Path $PbiToolsDir "pbitl") -Force
        exec { chmod +x (Join-Path $PbiToolsDir "pbitl" "pbi-tools.core") }

        # Store the path to the cross-platform CLI tool
        $script:pbiCoreToolPath = Join-Path -Resolve $PbiToolsDir "pbitl" "pbi-tools.core"
    }
}

function getAllPbiProjects {
    # Files that begin with '.' are treated as hidden on Linux, but not on Windows
    Get-ChildItem -Path $PbiReportsDir -Filter ".pbixproj.json" -Recurse -Hidden:$IsLinux
}
function getAllPbixFiles {
    Get-ChildItem -Path $PbixDir -Filter *.pbix
}
task CompilePbiReports `
    -Partial `
    -If {!$SkipPowerBi } `
    -Inputs { getAllPbiProjects } `
    -Outputs {
        process { Join-Path $PbixDir "$(Split-Path (Split-Path -Parent $_) -Leaf).pbit" }
    } `
    -Jobs InstallPbiTools,{
        begin {
            $allSucceeded = $true
        }
        process {
            $pbiProjectDir = Split-Path -Parent $_
            $pbiProjectName = Split-Path -Leaf $pbiProjectDir
            try {
                # Remove any previous deployment artefact
                Get-ChildItem -Path (Join-Path $PbixDir "$pbiProjectName.pbit") -ErrorAction Ignore | Remove-Item -Force
    
                Write-Build Green "`nCompile report file from folder: $pbiProjectDir"
                exec { 
                    & $pbiCoreToolPath `
                            compile `
                            -folder $pbiProjectDir `
                            -outPath $PbixDir `
                            -format "$( $GeneratePbixFile ? 'pbix' : 'pbit')" `
                            -overwrite true
                }
            }
            catch {
                $allSucceeded = $false
                Write-Warning "Error whilst compiling the Power BI report from '$pbiProjectDir' - SKIPPING - Check logs for more details"
            }
        }
        end {
            if (!$allSucceeded) {
                throw "One or more Power BI report compilations failed - check logs for warning messages with more details"
            }
        }
    }

task ExtractPbiReports `
    -Partial `
    -If { $IsWindows -and !$SkipPowerBi -and !$SkipExtractPbiReports } `
    -Inputs { getAllPbixFiles } `
    -Outputs {  process { Join-Path $PbiReportsDir (Split-Path $_ -LeafBase) ".pbixproj.json" } } `
    -Jobs InstallPbiTools,{
        begin {
            $allSucceeded = $true
        }
        process {
            $reportName = Split-Path $_ -LeafBase
            try {
                $extractDir = Join-Path $PbiReportsDir $reportName
                # Remove any previous data exports (this should avoid orphaned files when tables are deleted etc.)
                if (!(Test-Path $extractDir)) { New-Item -ItemType Directory $extractDir | Out-Null }
    
                Write-Build Green "`nExtracting report file: $_ --> $extractDir"
                exec { & $pbiWinToolPath extract $_ -extractFolder $extractDir }
    
                try {
                    $exportDir = "{0}{1}" -f $reportName, $PbiReportDataDirSuffix
                    if (!$SkipExportPbixData) {
                        $exportPath = Join-Path $PbiReportsDir $exportDir
                        if (Test-Path $exportPath) {
                            Get-ChildItem -Path $exportPath -Filter *.csv -ErrorAction Ignore | Remove-Item -Force
                        }
        
                        Write-Build Green "Exporting report data: $_ --> $exportPath"
                        exec { & $pbiWinToolPath export-data -pbixPath $_ -outPath $exportPath }
                    }
                }
                catch {
                    $allSucceeded = $false
                    Write-Warning "Error whilst exporting Power BI data from '$_' - SKIPPING - Check logs for more details"
                }
            }
            catch {
                $allSucceeded = $false
                Write-Warning "Error whilst extracting the Power BI report '$_' - SKIPPING - Check logs for more details"
            }
        }
        end {
            if (!$allSucceeded) {
                throw "One or more Power BI reports failed to extract - check logs for warning messages with more details"
            }
        }
    }

task BuildPowerBiReports -If { !$SkipPowerBi -and $PbiReportsDir } ExtractPbiReports,CompilePbiReports