Framework/Helpers/ConfigOverride.ps1

Set-StrictMode -Version Latest

class ConfigOverride
{
    hidden [string] $ConfigFileName;
    [PSObject] $ParsedFile;
    hidden [string[]] $ChangedProperties = @();
    
    ConfigOverride([string] $configFileName)
    {
        if([string]::IsNullOrWhiteSpace($configFileName))
        {
            throw [System.ArgumentException] ("The argument 'configFileName' is null or empty")
        }

        $this.ConfigFileName = $configFileName;
        $this.ParsedFile = [ConfigurationHelper]::LoadModuleJsonFile($configFileName);

        if(-not $this.ParsedFile)
        {
            throw [System.ArgumentException] ("The file '$configFileName' is empty")
        }
    }

    [bool] UpdatePropertyValue([string] $propertyName, [PSObject] $propertyValue)
    {
        if([string]::IsNullOrWhiteSpace($propertyName))
        {
            throw [System.ArgumentException] ("The argument 'propertyName' is null or empty")
        }

        #if(-not $propertyValue)
        #{
        # throw [System.ArgumentException] ("The argument 'propertyValue' is null or empty")
        #}

        if([Helpers]::CheckMember($this.ParsedFile, $propertyName, $false))
        {
            $this.ParsedFile.$propertyName = $propertyValue;
            $this.ChangedProperties += $propertyName;
            return $true;
        }

        return $false;
    }

    [void] WriteToFolder()
    {
        $this.WriteToFolder([Constants]::AzSdkAppFolderPath + "\Temp\PolicySetup");
    }

    [void] WriteToFolder([string] $folderName)
    {
        if([string]::IsNullOrWhiteSpace($folderName))
        {
            throw [System.ArgumentException] ("The argument 'folderName' is null or empty")
        }

        if (-not (Test-Path $folderName)) 
        {
            mkdir -Path $folderName -ErrorAction Stop | Out-Null
        }

        if (-not $folderName.EndsWith("\"))
        {
            $folderName += "\";
        }

        [Helpers]::ConvertToJsonCustom(($this.ParsedFile | Select-Object -Property $this.ChangedProperties)) | Out-File -Force -FilePath ($folderName + $this.ConfigFileName) -Encoding utf8
    }
}