scripts/misc/Update-OsVersion.ps1

# This will update Osiris 0.1 to 0.2 non-destructively.
# Please be aware modules need to be updated as well to work with the new format.

Function Update-OsVersionEnvironment {
    Param(
        [string]$Environment = 's1',
        [alias('Dir', 'Path')]
        [string] $ProjectDirectory = (Get-Location | Select-Object -ExpandProperty Path).ToString(),
        [string] $ResourceGroupLocation = 'eastus2'
    )

    if (-not (Test-Path "$ProjectDirectory\env\$ResourceGroupLocation\$Environment\modules")) {
        New-Item -ItemType Directory "$ProjectDirectory\env\$ResourceGroupLocation\$Environment\modules"
    }

    # Copy all module json files to seperate folders as modules.

    $modfiles = Get-ChildItem -File -Path "$ProjectDirectory\env\$ResourceGroupLocation\$Environment\modules"

    foreach ($mod in $modfiles) {
        New-Item -ItemType Directory "$ProjectDirectory\env\$ResourceGroupLocation\$Environment\modules\$($mod.Name -replace '.json','')"
        Copy-Item $mod.FullName -Destination "$ProjectDirectory\env\$ResourceGroupLocation\$Environment\modules\$($mod.Name -replace '.json','')\module.json"
    }

    $modulefiles = Get-ChildItem -File -Recurse -Path "$ProjectDirectory\env\$ResourceGroupLocation\$Environment\modules"

    foreach ($module in $modulefiles) {
        $moduleJson = Get-Content $module.FullName -Raw | ConvertFrom-Json
        $moduleJson.parameters.DeploymentInfo.value | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name | ForEach-Object {
            $moduleJson.parameters | Add-Member -MemberType NoteProperty -Name $_ -Value (New-Object -TypeName pscustomobject) -Force
            $moduleJson.parameters.$_ | Add-Member -MemberType NoteProperty -Name value -Value $moduleJson.parameters.DeploymentInfo.value.$_ -Force
        }
        $moduleJson.parameters = $moduleJson.parameters | Select-Object -Exclude DeploymentInfo
        $moduleJson | ConvertTo-Json -Depth 100 | Out-File $module.FullName -Force
    }
}

Function Update-OsVersionModules {

    $modfiles = Get-ChildItem -File -Recurse "$ProjectDirectory\modules" | Where-Object { $_.Name -notmatch 'readme|example|metadata' }

    $ErrorActionPreference = 'Continue'
    foreach ($mod in $modfiles) {
        $modtext = Get-Content $mod.FullName -Raw
        $examplepath = $mod.DirectoryName + "\example.parameters.json"

        if (Test-Path $examplepath) {
            $exampleJson = Get-Content $examplepath -Raw | ConvertFrom-Json
            $exampleJson.parameters.DeploymentInfo.value | Get-Member -MemberType NoteProperty -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name | ForEach-Object {
                $exampleJson.parameters | Add-Member -MemberType NoteProperty -Name $_ -Value (New-Object -TypeName pscustomobject) -Force
                $exampleJson.parameters.$_ | Add-Member -MemberType NoteProperty -Name value -Value $exampleJson.parameters.DeploymentInfo.value.$_ -Force
            }
            $exampleJson.parameters = $exampleJson.parameters | Select-Object -Exclude DeploymentInfo
            $exampleparams = $exampleJson.parameters | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
            $exampleJson | ConvertTo-Json -Depth 100 | Out-File $examplepath -Force
        }

        $matches = $modtext | Select-String -Pattern "parameters\('DeploymentInfo'\)\.[\w-]+" | Sort-Object -Unique

        foreach ($match in $matches.Matches.Value) {
            $matchparts = $match.Split(".") | Select-Object -Last 1
            $replacement = "parameters('$($matchparts)')"
            Write-Output $replacement
            $modtext = $modtext.Replace($match, $replacement)
        }

        $modJson = $modtext | ConvertFrom-Json

        foreach ($param in $matches.Matches.Value) {
            $matchparts = $param.Split(".") | Select-Object -Last 1
            $modJson.parameters | Add-Member -MemberType NoteProperty -Name $matchparts -Value (New-Object -TypeName pscustomobject)

            if ($exampleparams -contains $matchparts) {
                $paramtype = Switch -Wildcard ($exampleJson.parameters.$matchparts.value.GetType().Name) {
                    "String" { "string" }
                    "String\[\]" { "array" }
                    "Int*" { "int" }
                    "Boolean" { "bool" }
                    "PSCustomObject" { "object" }
                }
                $modJson.parameters.$matchparts | Add-Member -MemberType NoteProperty -Name "type" -Value $paramtype
            }
            else {
                $modJson.parameters.$matchparts | Add-Member -MemberType NoteProperty -Name "type" -Value "object"
                Write-Verbose "Module $mod's example file param $param was not found, defaulting to Object for param type."
            }
        }

        $modJson.parameters = $modJson.parameters | Select-Object -ExcludeProperty DeploymentInfo
        $modJson | ConvertTo-Json -Depth 100 | Out-File $mod.FullName -Force
    }
}