diff --git a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 index fcbe96f023..1c8d6a57bf 100644 --- a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 +++ b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 @@ -85,6 +85,7 @@ 'WinGetSource' 'WinGetPackageManager' 'WinGetPackage' + 'WinGetConfigRoot' ) # List of all modules packaged with this module diff --git a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 index 96938c707a..a2fa47ee03 100644 --- a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 +++ b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 @@ -52,6 +52,12 @@ enum WinGetTrustLevel Trusted } +enum WinGetScope +{ + User + Machine +} + #endregion enums #region DscResources @@ -446,6 +452,78 @@ class WinGetPackageManager } } +[DSCResource()] +class WinGetConfigRoot +{ + [DscProperty(Key)] + [string]$WinGetConfigRoot = "WinGetConfigRoot" + + [DscProperty()] + [string]$Path = "" + + [DscProperty()] + [bool]$Exist = $false + + [DscProperty()] + [WinGetScope]$WinGetScope = [WinGetScope]::User + + [WinGetConfigRoot] Get() + { + $currentState = [WinGetConfigRoot]::new() + $wingetConfigRootValue = [System.Environment]::GetEnvironmentVariable($this.WinGetConfigRoot, $this.WinGetScope) + + if (-not [string]::IsNullOrEmpty($wingetConfigRootValue)) + { + $currentState.Exist = $true + $currentState.Path = $wingetConfigRootValue + } + + $currentState.WinGetScope = $this.WinGetScope + return $currentState + } + + [bool] Test() + { + $currentState = $this.Get() + if ($this.Exist -ne $currentState.Exist) + { + return $false + } + + if ($this.Path -ne $currentState.Path) + { + return $false + } + + if ($this.WinGetScope -ne $currentState.WinGetScope) + { + return $false + } + + if ($this.WinGetConfigRoot -ne $currentState.WinGetConfigRoot) + { + return $false + } + + return $true + } + + [void] Set() + { + if (-not $this.Test()) + { + if ($this.Exist) + { + [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, $this.Path, $this.WinGetScope) + } + else + { + [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, "", $this.WinGetScope) + } + } + } +} + [DSCResource()] class WinGetPackage { diff --git a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 index 0dc01f6dd8..b0796fe739 100644 --- a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 +++ b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 @@ -32,9 +32,9 @@ BeforeAll { Describe 'List available DSC resources'{ It 'Shows DSC Resources'{ - $expectedDSCResources = "WinGetAdminSettings", "WinGetPackage", "WinGetPackageManager", "WinGetSource", "WinGetUserSettings" + $expectedDSCResources = "WinGetAdminSettings", "WinGetPackage", "WinGetPackageManager", "WinGetSource", "WinGetUserSettings", "WinGetConfigRoot" $availableDSCResources = (Get-DscResource -Module Microsoft.WinGet.DSC).Name - $availableDSCResources.length | Should -Be 5 + $availableDSCResources.length | Should -Be 6 $availableDSCResources | Where-Object {$expectedDSCResources -notcontains $_} | Should -BeNullOrEmpty -ErrorAction Stop } } @@ -242,3 +242,36 @@ Describe 'WinGetPackageManager' { # TODO: Add test to verify Set method for WinGetPackageManager } + +Describe 'WinGetConfigRoot' { + It 'Sets and removes $WinGetConfigRoot by scope' -ForEach 'User','Machine' { + $get = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ WinGetScope = $_ } + $get.WinGetConfigRoot = "WinGetConfigRoot" + $get.Exist | Should -Be $false + $get.Path | Should -Be "" + $get.WinGetScope | Should -Be $_ + + $test = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $true; Path = 'C:\Foo\Bar'; WinGetScope = $_ } + $test.InDesiredState | Should -Be $false + + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $true; Path = 'C:\Foo\Bar'; WinGetScope = $_ } + + # Verify that $WinGetConfigRoot variable is set. + $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} + $result.Exist | Should -Be $true + $result.Path | Should -Be 'C:\Foo\Bar' + $result.WinGetScope | Should -Be $_ + + # Remove $WinGetConfigRoot variable. + $test2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $false; WinGetScope = $_ } + $test2.InDesiredState | Should -Be $false + + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $false; WinGetScope = $_ } + + $result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} + $result2.WinGetConfigRoot = "WinGetConfigRoot" + $result2.Exist | Should -Be $false + $result2.Path | Should -Be "" + $result2.WinGetScope | Should -Be $_ + } +}