Assets/postinst.ps1

#Requires -RunAsAdministrator

<#
.SYNOPSIS
Post-installation Script
 
.DESCRIPTION
This script will be executed at the end of installation.
Simply put this script in the `build/${TARGET}` folder where ${TARGET} is Windows/macOS/Linux and it will (should ?) be automatically run as the last step of installation process.
It use the same actions as in Debian policy @url https://www.debian.org/doc/debian-policy/ap-flowcharts.html
 
#>

[CmdletBinding()]
param (
    # Action to execute :: Configure
    [Parameter(ParameterSetName = 'Configure')]
    [switch]$Configure,

    # Action to execute :: Abort-Upgrade
    [Parameter(ParameterSetName = 'AbortUpgrade')]
    [switch]$AbortUpgrade,

    # Allow installation of PreRelease powershell modules
    [switch]$AllowPrerelease,

    # Install modules to given scope
    [ValidateSet('AllUsers', 'CurrentUser')]
    [string]$Scope = "AllUsers"
)

Start-Transcript -Path $PSScriptRoot\postinst.log
$BASENAME = Split-Path -Path $PSCommandPath -Leaf
$INSTDIR = (Resolve-Path "$PSScriptRoot\..").Path.TrimEnd('/\')
[string]$arg = ""
Foreach ($p in $PSBoundParameters.GetEnumerator()) {
    $arg += "-$($p.key) $($p.value) "
}
Write-Output "$BASENAME $arg"

# . "$PSScriptRoot\windows.inc.ps1"

$PROJECTNAME = "{{ Name }}"
$PROJECTPRERELEASE = "{{ preRelease }}"
$FQVERSION = "{{ fqVersion }}"
$AllowPrerelease = $AllowPrerelease -or $PROJECTPRERELEASE

switch ($PSCmdlet.ParameterSetName) {
    'Configure' {
        # if (!(Get-PackageProvider -Name NuGet)) { Install-PackageProvider -Name NuGet -Force -Confirm:$false }
        Write-Host "Configure Powershell Gallery"
        $PSGallery = Get-PSRepository -Name PSGallery
        if ([string]::IsNullOrEmpty($PSGallery)) {
            Register-PSRepository -Default -InstallationPolicy Trusted
        } else {
            Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
        }

        $modules = @()
        $modules += "PwSh.Fw.Core"
        # $modules += "<ADD HERE SOME MODULE NAME TO INSTALL>"
        foreach ($m in $modules) {
            Write-Output "Install module $m"
            $moduleIsInstalled = (Get-Module $m -ListAvailable).count -gt 0
            if ($moduleIsInstalled) {
                Update-Module $m -Force -Scope $Scope -AllowPrerelease:$AllowPrerelease -AcceptLicense
            } else {
                Install-Module $m -Force -Scope $Scope -AllowPrerelease:$AllowPrerelease -AcceptLicense -AllowClobber
            }
        }
    }
    'AbortUpgrade' {
        Write-Error "$_"
        Write-Error "Upgrade aborted."
    }
}
Stop-Transcript