aportal.psm1

$ErrorActionPreference = "Stop"
#Requires -Version 5.1
#Requires -Modules PowerShellGet
#Requires -Modules SharePointPnPPowerShellOnline

$moduleName = "SharePointPnPPowerShellOnline"

if (!(Get-command -Module $moduleName).count -gt 0)
{
    Install-Module $moduleName -Force -SkipPublisherCheck -RequiredVersion 3.13.1909.0
}
Write-Output "The modules for $moduleVersion have been installed and can now be used."
function Set-APortalWeb
{
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
    Param(
        [Parameter(Mandatory=$true)]
        [string] $Url,
        [bool] $WithStarter,
        [bool] $Global
    )
    if ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
        # Critical code
    }

    $packagesAddress = "https://shuishan-tech.coding.net/p/APortal/d/APortal/git/raw/master/packages/";
    if ($Global) {
        $packagesAddress = "https://raw.githubusercontent.com/shuishan-tech/aportal/master/packages/";
    }
    Connect-PnPOnline $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000

    Get-PnPPackage -Name "web" -Address $packagesAddress
    Get-PnPPackage -Name "assets" -Address $packagesAddress

    Install-PnPPackage -Name "web"
    Install-PnPPackage -Name "assets"

}

function Get-PnPPackage
{
    [CmdletBinding(ConfirmImpact='Medium')]
    Param(
        [Parameter(Mandatory=$true)]
        [string] $Name,
        [string] $Address
    )
    Write-Progress -Activity ("Getting APortal PnP package ({0})." -f $Name) -Status "Getting current version."
    # Get current version.
    $versionFilePath = "$($Address)$($Name)/version.json"
    $versionFilePathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\version.json"

    New-Item -Path $versionFilePathLocal -ItemType File -Force | Out-Null
    
    Invoke-RestMethod -Uri $versionFilePath -OutFile $versionFilePathLocal
    $versionObj = (Get-Content $versionFilePathLocal) | ConvertFrom-Json


    $pnpFilePath = "$($Address)$($Name)/$($versionObj.currentVersion)/aportal.$($Name).pnp"
    $pnpFilePathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\$($versionObj.currentVersion)\aportal.$($Name).pnp"

    $existsLocalFile = Test-Path $pnpFilePathLocal -PathType Leaf

    if (!$existsLocalFile) {
        New-Item -Path $pnpFilePathLocal -ItemType File -Force | Out-Null
        # Download current version file and save it.
        Write-Progress -Activity ("Getting APortal PnP package ({0})." -f $Name) -Status ("Downloading current version ({0})." -f $versionObj.currentVersion)
        Invoke-WebRequest -Uri $pnpFilePath -OutFile $pnpFilePathLocal
    }
}

function Install-PnPPackage
{
    
    [CmdletBinding(ConfirmImpact='Medium')]
    Param(
        [Parameter(Mandatory=$true)]
        [string] $Name
    )
    Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "Getting current version."
    $versionFilePathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\version.json"
    if (Test-Path $versionFilePathLocal) {
        $versionObj = (Get-Content $versionFilePathLocal) | ConvertFrom-Json
        $filePath = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\$($versionObj.currentVersion)\aportal.$($Name).pnp"
        Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "Package name: $($versionObj.currentVersion)\aportal.$($Name).pnp"
        Apply-PnPProvisioningTemplate -Path $filePath
    } else {
        Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "The current version file is not exist, please run command `Get-APortalPackages` and try again."
    }
}

Export-ModuleMember -Function Set-APortalWeb, Get-APortalPackages