Configuration.ps1



Class ConfigurationData
{
    # properties
    [psobject]$configData = @{}

    # Default constructor
    ConfigurationData() {}

    [hashtable] AsHash() {
        return Convert-ObjectToHashtable($this.configData)
    }

    [void] Add([string]$name, [string]$value) {
        LogIt "Adding $name to config" $False
        Add-Member -InputObject $this.configData -NotePropertyName $name -NotePropertyValue $value -Force
    }

    [bool] Contains([string]$name) {
        return $this.configData.psobject.Properties.name -contains $name
    }

    [string] Get([string]$name) {
        return $this.configData.$name
    }
    
    [string] GetFrom([string]$group, [string]$name) {
        return $this.configData.$group.$name
    }

    [string] GetDefault([string]$name, [string]$defaultValue) {
        if (Contains($name)) {
            return $this.configData.$name
        }
        return $defaultValue
    }

    [void] Load([string]$ConfigJsonFile) {
        if (Test-Path -Path $ConfigJsonFile)
        {
            LogIt "Loading config from $ConfigJsonFile"
            $this.configData = Get-Content -Raw -Path $ConfigJsonFile | ConvertFrom-Json
            LogIt "Config: $($this.configData)" $False
        }
        else
        {
            throw "$ConfigJsonFile not found"
        }
    }
}


Function New-Configuration() {
    return [ConfigurationData]::New()
}

Function Copy-ExampleConfigFiles([string]$path) {
    Copy-Item "$PSScriptRoot\Assets\*Example.json" $path -Force 
}