Skip to content

Commit cb358bf

Browse files
committed
Add to PS, DSCv2, and GP
1 parent 08f115e commit cb358bf

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

src/AppInstallerSharedLib/GroupPolicy.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ namespace AppInstaller::Settings
219219
}
220220
}
221221
#endif
222-
// TrustLevel and Explicit are optional policy fields with default values.
222+
// TrustLevel, Explicit, and Priority are optional policy fields with default values.
223223
const std::string trustLevelName = "TrustLevel";
224224
if (sourceJson.isMember(trustLevelName) && sourceJson[trustLevelName].isArray())
225225
{
@@ -236,6 +236,12 @@ namespace AppInstaller::Settings
236236
source.Explicit = sourceJson[explicitName].asBool();
237237
}
238238

239+
const std::string priorityName = "Priority";
240+
if (sourceJson.isMember(priorityName) && sourceJson[priorityName].isInt())
241+
{
242+
source.Priority = sourceJson[priorityName].asInt();
243+
}
244+
239245
return source;
240246
}
241247
}

src/PowerShell/Microsoft.WinGet.Client.Cmdlets/Cmdlets/AddSourceCmdlet.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ public sealed class AddSourceCmdlet : PSCmdlet
6060
[Parameter(ValueFromPipelineByPropertyName = true)]
6161
public SwitchParameter Explicit { get; set; }
6262

63+
/// <summary>
64+
/// Gets or sets a value indicating the priority of the source. Higher values are sorted first.
65+
/// </summary>
66+
///
67+
[Parameter(ValueFromPipelineByPropertyName = true)]
68+
public int Priority { get; set; }
69+
6370
/// <summary>
6471
/// Adds source.
6572
/// </summary>
6673
protected override void ProcessRecord()
6774
{
6875
var command = new CliCommand(this);
69-
command.AddSource(this.Name, this.Argument, this.Type, this.ConvertPSSourceTrustLevelToString(this.TrustLevel), this.Explicit.ToBool());
76+
command.AddSource(this.Name, this.Argument, this.Type, this.ConvertPSSourceTrustLevelToString(this.TrustLevel), this.Explicit.ToBool(), this.Priority);
7077
}
7178

7279
private string ConvertPSSourceTrustLevelToString(PSSourceTrustLevel trustLevel) => trustLevel switch

src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/CliCommand.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public void GetSettings(bool asPlainText)
7272
/// <param name="type">Type of source.</param>
7373
/// <param name="trustLevel">Trust level of source.</param>
7474
/// <param name="isExplicit">Make source explicit.</param>
75-
public void AddSource(string name, string arg, string type, string trustLevel, bool isExplicit)
75+
/// <param name="priority">Set the priority if the source.</param>
76+
public void AddSource(string name, string arg, string type, string trustLevel, bool isExplicit, int priority)
7677
{
7778
Utilities.VerifyAdmin();
7879
string parameters = $"add --name \"{name}\" --arg \"{arg}\"";
@@ -92,6 +93,11 @@ public void AddSource(string name, string arg, string type, string trustLevel, b
9293
parameters += " --explicit";
9394
}
9495

96+
if (priority != 0)
97+
{
98+
parameters += $" --priority \"{priority}\"";
99+
}
100+
95101
_ = this.Run("source", parameters, 300000);
96102
}
97103

src/PowerShell/Microsoft.WinGet.Client.Engine/PSObjects/PSSourceResult.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal PSSourceResult(Management.Deployment.PackageCatalogReference catalogRef
2323
this.Type = info.Type;
2424
this.TrustLevel = info.TrustLevel.ToString();
2525
this.Explicit = info.Explicit;
26+
this.Priority = info.Priority;
2627
}
2728

2829
/// <summary>
@@ -49,5 +50,10 @@ internal PSSourceResult(Management.Deployment.PackageCatalogReference catalogRef
4950
/// Gets a value indicating whether the source must be explicitly specified for discovery.
5051
/// </summary>
5152
public bool Explicit { get; private set; }
53+
54+
/// <summary>
55+
/// Gets a value indicating the priority of the source. Higher values are sorted first.
56+
/// </summary>
57+
public int Priority { get; private set; }
5258
}
5359
}

src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ class WinGetSource
203203

204204
[DscProperty()]
205205
[nullable[bool]]$Explicit = $null
206+
207+
[DscProperty()]
208+
[nullable[int]]$Priority = $null
206209

207210
[DscProperty()]
208211
[WinGetEnsure]$Ensure = [WinGetEnsure]::Present
@@ -232,6 +235,7 @@ class WinGetSource
232235
$result.Type = $currentSource.Type
233236
$result.TrustLevel = $currentSource.TrustLevel
234237
$result.Explicit = $currentSource.Explicit
238+
$result.Priority = $currentSource.Priority
235239
}
236240
else
237241
{
@@ -314,6 +318,11 @@ class WinGetSource
314318
$hashArgs.Add("Explicit", $this.Explicit)
315319
}
316320

321+
if ($null -ne $this.Priority)
322+
{
323+
$hashArgs.Add("Priority", $this.Priority)
324+
}
325+
317326
Add-WinGetSource @hashArgs
318327
}
319328
}
@@ -352,6 +361,12 @@ class WinGetSource
352361
return $false
353362
}
354363

364+
if ($null -ne $this.Priority -and
365+
$this.Priority -ne $currentSource.Priority)
366+
{
367+
return $false
368+
}
369+
355370
return $true
356371
}
357372
}

src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Describe 'Reset-WinGetSource' {
188188
Describe 'Get|Add|Reset-WinGetSource' {
189189

190190
BeforeAll {
191-
Add-WinGetSource -Name 'TestSource' -Arg 'https://localhost:5001/TestKit/' -TrustLevel 'Trusted' -Explicit
191+
Add-WinGetSource -Name 'TestSource' -Arg 'https://localhost:5001/TestKit/' -TrustLevel 'Trusted' -Explicit -Priority 42
192192
}
193193

194194
It 'Get Test source' {
@@ -200,6 +200,7 @@ Describe 'Get|Add|Reset-WinGetSource' {
200200
$source.Type | Should -Be 'Microsoft.PreIndexed.Package'
201201
$source.TrustLevel | Should -Be 'Trusted'
202202
$source.Explicit | Should -Be $true
203+
$source.Priority | Should -Be 42
203204
}
204205

205206
It 'Get fake source' {

src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Describe 'WinGetSource' {
148148
}
149149

150150
It 'Set WinGet source' {
151-
InvokeWinGetDSC -Name WinGetSource -Method Set -Property @{ Name = $testSourceName; Argument = $testSourceArg; Type = $testSourceType; TrustLevel = 'Trusted'; Explicit = $true }
151+
InvokeWinGetDSC -Name WinGetSource -Method Set -Property @{ Name = $testSourceName; Argument = $testSourceArg; Type = $testSourceType; TrustLevel = 'Trusted'; Explicit = $true; Priority = 42 }
152152

153153
$result = InvokeWinGetDSC -Name WinGetSource -Method Test -Property @{ Name = $testSourceName; Argument = $testSourceArg; Type = $testSourceType }
154154
$result.InDesiredState | Should -Be $true
@@ -159,6 +159,7 @@ Describe 'WinGetSource' {
159159
$result.Argument | Should -Be $testSourceArg
160160
$result.TrustLevel | Should -Be 'Trusted'
161161
$result.Explicit | Should -Be $true
162+
$result.Priority | Should -Be 42
162163
}
163164
}
164165

0 commit comments

Comments
 (0)