Skip to content

Commit

Permalink
add wingetconfigroot dsc
Browse files Browse the repository at this point in the history
  • Loading branch information
ryfu-msft committed Nov 19, 2024
1 parent 5645e0c commit 29af060
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
'WinGetSource'
'WinGetPackageManager'
'WinGetPackage'
'WinGetConfigRoot'
)

# List of all modules packaged with this module
Expand Down
65 changes: 65 additions & 0 deletions src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ enum WinGetTrustLevel
Trusted
}

enum Scope
{
User
Machine
}

#endregion enums

#region DscResources
Expand Down Expand Up @@ -446,6 +452,65 @@ class WinGetPackageManager
}
}

[DSCResource()]
class WinGetConfigRoot
{
# We need a key. Do not set.
[DscProperty(Key)]
[string]$SID

[DscProperty()]
[bool]$Exists

[DscProperty()]
[string]$Path

[DscProperty()]
[Scope]$Scope = [Scope]::User

hidden [string] $WinGetConfigRoot = 'WinGetConfigRoot'

[WinGetConfigRoot] Get()
{
$currentState = [WinGetConfigRoot]::new()
$currentState.SID = ''
$wingetConfigRootValue = [System.Environment]::GetEnvironmentVariable($this.WinGetConfigRoot, $this.Scope)

if ([string]::IsNullOrEmpty($wingetConfigRootValue))
{
$currentState.Exists = $false
}
else
{
$currentState.Exists = $true
$currentState.Path = $wingetConfigRootValue
}

return $currentState
}

[bool] Test()
{
$currentState = $this.Get()
return $this.Exists -eq $currentState.Exists
}

[void] Set()
{
if (-not $this.Test)
{
if ($this.Exists)
{
[System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, $this.Path, $this.Scope)
}
else
{
[System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, "", $this.Scope)
}
}
}
}

[DSCResource()]
class WinGetPackage
{
Expand Down
28 changes: 28 additions & 0 deletions src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,31 @@ Describe 'WinGetPackageManager' {

# TODO: Add test to verify Set method for WinGetPackageManager
}

Describe 'WinGetConfigRoot' {
It 'Get WinGetConfigRoot' {
$result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{}
$result.Exists | Should -Be $false
$result.Path | Should -Be $null
$result.Scope | Should -Be 'User'
}

It 'Test WinGetConfigRoot' {
$result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exists = $true; Path = 'C:\Foo\Bar' }
$result.InDesiredState | Should -Be $false
}

It 'Set WinGetConfigRoot' {
InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exists = $true; Path = 'C:\Foo\Bar' }

# Verify $winGetConfigRoot is set
$result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ Exists = $true; Path = 'C:\Foo\Bar' }
$result.Exists | Should -Be $true
$result.Path | Should -Be 'C:\Foo\Bar'
$result.Scope | Should -Be 'User'
}

AfterAll {
InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Ensure = 'Absent' }
}
}

0 comments on commit 29af060

Please sign in to comment.