Module/VSCode/Initialize-Workspace.ps1

<#
.SYNOPSIS
Initialize the AL workspace
 
.DESCRIPTION
Initialize the AL workspace based on workspace-settings.json
 
.PARAMETER licensenFile
 Secure String containing URL to Business Central Licensefile
 
.PARAMETER licensenFile
 Secure String containing password to be used for Business Central in Docker Container
 
.EXAMPLE
 Initialize-Workspace -licenseFile (Get-BCSSecureString -InputString "http://licensen.flf") -password (Get-BCSSecureString -InputString "myPassword")
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
#>


function Initialize-BCSWorkspace {
    Param(
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyname = $true)]
        [SecureString] $licensenFile,

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyname = $true)]
        [SecureString] $password
    )

    Write-Host "Intitializeing AL Workspace" -ForegroundColor Green

    $currentPath = Get-Location;
    $workspaceSettings = Get-Content (Get-Item "$($currentPath)\workspace-settings.json").PSPath | ConvertFrom-Json;

    #region app.json
    Write-Host "Updating app.json" -ForegroundColor Green

    $appJsonPath = (Get-Item "$($currentPath)\..\App\app.json").PSPath;
    $appJson = Get-Content $appJsonPath | ConvertFrom-Json;

    $guid = New-Guid;
    $appJson.id = $guid;
    $appJson.Name = $workspaceSettings.appName;
    $appJson.idRanges = $workspaceSettings.appIdRanges;
    $appJson.version = "$($workspaceSettings.appVersion).0.0"
    $appJson.platform = "$($workspaceSettings.appVersion).0.0"
    $appJson.application = "$($workspaceSettings.appVersion).0.0"

    $appJson | ConvertTo-Json -depth 6 | Format-Json -Indentation 2 | Out-File $appJsonPath
    #endregion

    #region pte-qa-settings.json
    Write-Host "Updating CI pipeline settings" -ForegroundColor Green

    $qaSettingsJsonPath = (Get-Item "$($currentPath)\pte-qa-settings.json").PSPath;
    $qaSettingsJson = Get-Content $qaSettingsJsonPath | ConvertFrom-Json;

    $qaSettingsJson.Name = $workspaceSettings.repositoryName
    $qaSettingsJson.dependencies = $workspaceSettings.appDependencies;
    $qaSettingsJson.deployments[0].DeployToName = $workspaceSettings.deployments.qa.ServerName
    $qaSettingsJson.deployments[0].DeployToInstance = $workspaceSettings.deployments.qa.ServerInstanceName
    $qaSettingsJson.deployments[0].DeployToTenants = $workspaceSettings.deployments.qa.Tenants

    $userProfile = $qaSettingsJson.userProfiles | Where-Object -Property profile -EQ "$env:computername\$env:username"
    
    if (!$userProfile) { 
        $qaProfile = $qaSettingsJson.userProfiles
        $qaProfile.userProfiles[0].profile = "$env:computername\$env:username"
        $qaProfile.userProfiles[0].licenseFilePath = (Get-BCSSecureString -InputString $licensenFile -AsPlainText)
        $qaProfile.userProfiles[0].Password = (Get-BCSSecureString -InputString $password -AsPlainText)
            $qaSettingsJson.userProfiles += $qaProfile
    }

    $qaSettingsJson | ConvertTo-Json -depth 6 | Format-Json -Indentation 2 | Out-File $qaSettingsJsonPath
    #endregion

    #region pte-qa-current.yml

    $pteQaCurrentYmlPath = (Get-Item "$($currentPath)\pte-qa-current.yml").PSPath;
    $pteQaCurrentYml = Get-Content $pteQaCurrentYmlPath | ConvertFrom-Yaml -Ordered

    $pteQaCurrentYml.extends.parameters.appVersion = $workspaceSettings.appVersion

    $pteQaCurrentYml | ConvertTo-Yaml | Out-File $pteQaCurrentYmlPath
    #endregion

    #region pte-prod-current.yml
    Write-Host "Updating Release pipeline settings" -ForegroundColor Green

    $pteProdCurrentYmlPath = (Get-Item "$($currentPath)\pte-prod-current.yml").PSPath;
    $pteProdCurrentYml = Get-Content $pteProdCurrentYmlPath | ConvertFrom-Yaml -Ordered

    $selfPipeName = "$($workspaceSettings.repositoryName) - CI"
    $pteProdCurrentYml.resources.pipelines[0].source = $selfPipeName
    $pteProdCurrentYml.extends.parameters.appVersion = $workspaceSettings.appVersion

    $pteProdCurrentYml | ConvertTo-Yaml | Out-File $pteProdCurrentYmlPath

    Write-Host "Inititialize complete" -ForegroundColor Green
}

Export-ModuleMember -Function Initialize-BCSWorkspace