NaverticAl transition from settings.ps1 to JSON

Hi all,

it is time for new blog post from me (after nearly one year, too much things going around in last year), because I added support of JSON settings file into NVRAppDevOps module and into NaverticAl VSCode extension. I will try to explain how it works and how you can transform your settings.ps1 to JSON settings.

How it worked

When using the module NVRAppDevOps, you need to pass parameters to the cmdlets directly, or you can use the pipeline to pass object with the parameters. This object could be generated e.g. by using cmdlet Read-ALConfiguration. This cmdlet first run file $Path\Scripts\Settings.ps1 and then takes different variables and compose the configuration object through using Get-ALConfiguration cmdlet. The variables could be set inside th Settings.ps1 script as needed. Example of the settings.ps1:

$ContainerName = 'BC'
$ImageName = 'bcinsider.azurecr.io/bcsandbox'
$LicenseFile = 'somepath\to\license.flf'
$AppJSON = Get-Content -Raw -Path (Join-Path $PSSCriptRoot '..\\MainApp\\app.json') | ConvertFrom-Json
$TestAppJSON = Get-Content -Raw -Path (Join-Path $PSSCriptRoot '..\\TestApp\\app.json') | ConvertFrom-Json
$AppFile = (Get-ChildItem -Path (Join-Path $PSSCriptRoot '..\\MainApp') -Filter '*.app' |
           Sort-Object LastWriteTime -Descending |
           Select-Object -First 1).Fullname
$TestAppFile = (Get-ChildItem -Path (Join-Path $PSSCriptRoot '..\\TestApp') -Filter '*.app' |
                 Sort-Object LastWriteTime -Descending |
                 Select-Object -First 1).Fullname
$RepoPath = (Join-Path $PSSCriptRoot '..')
$AppPath = (Join-Path $PSSCriptRoot '..\MainApp')
$TestAppPath = (Join-Path $PSSCriptRoot '..\TestApp')
$RAM = '5GB'

As you can see, it just set the variables and could use everything available in PowerShell.

Created configuration object was passed as input to all other cmdlets.

How it works now

The change is, that the settings.ps1 is called only if it exists and after that, Read-ALJsonConfiguration cmdlet is called. This cmdlet looks for all *.json files in the folder tree (or for SettingsFileName only if set), and if the Json have value "default.ContainerName", it is taken as Json settings for the module NVRAppDevOps. In this case, the Json is processed. Json file could look like:

{
    "default": {
        "ContainerName": "BC",
        "ImageName": "bcinsider.azurecr.io/bcsandbox",
        "LicenseFile" : "somepath\\to\\license.flf",
        "AppJSON" : "Get-Content -Raw -Path (Join-Path (Get-Location) '..\\MainApp\\app.json') | ConvertFrom-Json",
        "TestAppJSON" : "Get-Content -Raw -Path (Join-Path (Get-Location) '..\\TestApp\\app.json') | ConvertFrom-Json",
        "AppFile" : "(Get-ChildItem -Path (Join-Path (Get-Location) '..\\MainApp') -Filter '*.app' | Sort-Object LastWriteTime -Descending | Select-Object -First 1).Fullname",
        "TestAppFile" : "(Get-ChildItem -Path (Join-Path (Get-Location) '..\\TestApp') -Filter '*.app' | Sort-Object LastWriteTime -Descending | Select-Object -First 1).Fullname",
        "RepoPath" : "(Join-Path (Get-Location) '..')",
        "AppPath" : "(Join-Path (Get-Location) '..\\MainApp')",
        "TestAppPath" : "(Join-Path (Get-Location) '..\\TestApp')",
        "RAM" : "4GB"
    },
    "master": {
        "ContainerName": "BCMaster",
        "ImageName": "bcinsider.azurecr.io/bcsandbox-master"
    }
}

This example includes two profiles: default and master. Default profile have same values like the settings.ps1 and is showing you how you can transform the settings.ps1 to the Json setting file.

First, the default profile is processed each time, if another profile should be applied, the specific profile is processed afterwards. It means the values from default profile will be overwritten by values from the specific profile. Each value is processed and result is saved into variable with given name. It means the result is same like when running the original settings.ps1. Values in the Json are interpreted like:

1) If the value is existing file name, it is passed as is

2) if the value could be Invoked as PowerShell expression, it is invoked and result is passed

3) if it cannot be executed without error, it is expanded (variable names are replaced with values) and passed

Variables are then used in the Read-ALConfiguration cmdlet to create the configuration object.

Changes in NaverticAl

When you run NaverticAl: Show Configuration in both cases (having Scripts\Settings.ps1 and having Json settings file) you will get same result. You can change the SettingsFileName parameter through VSCode extension setting 

navertical.JSONConfigFile and which profile to use through navertical.JSONConfigProfile.

What is planned

I want to add support for switching between profiles into NaverticAl including setting correct server URL in .vscode\launch.json if needed. But we are not there yet.

As usual, you can file issues/questions/request on the appropriate Github repository.

Anonymous