Skip to content

Commit

Permalink
Fix Reset-WinGetSource behavior (#4732)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryfu-msft authored Aug 21, 2024
1 parent 7723804 commit 83b2db4
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Assert-WinGetPackageManager [-Version <String>] [<CommonParameters>]
### IntegrityLatestSet

```
Assert-WinGetPackageManager [-Latest] [-IncludePreRelease] [<CommonParameters>]
Assert-WinGetPackageManager [-Latest] [-IncludePrerelease] [<CommonParameters>]
```

## DESCRIPTION
Expand Down
40 changes: 36 additions & 4 deletions src/PowerShell/Help/Microsoft.WinGet.Client/Reset-WinGetSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ title: Reset-WinGetSource
# Reset-WinGetSource

## SYNOPSIS
Resets default WinGet sources.
Resets WinGet sources.

## SYNTAX

### DefaultSet (Default)

```
Reset-WinGetSource -Name <String> [<CommonParameters>]
```

### OptionalSet

```
Reset-WinGetSource -All [<CommonParameters>]
```

## DESCRIPTION

Resets the current WinGet sources to the default source configurations. This command must be
executed with administrator permissions.
Resets a named WinGet source by removing the source configuration. You can reset all configured sources and add the default source configurations using the **All** switch parameter.
This command must be executed with administrator permissions.

## EXAMPLES

Expand All @@ -33,15 +41,39 @@ Reset-WinGetSource -Name msstore

This example resets the configured source named 'msstore' by removing it.

### Example 2: Reset all sources

```powershell
Reset-WinGetSource -All
```

This example resets all configured sources and adds the default sources.

## PARAMETERS

### -All

Reset all sources and add the default sources.

```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (OptionalSet)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```
### -Name
The name of the source.
```yaml
Type: System.String
Parameter Sets: (All)
Parameter Sets: (DefaultSet)
Aliases:

Required: True
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="AssertWinGetPackageManagerCmdlet.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand Down Expand Up @@ -29,7 +29,7 @@ protected override void ProcessRecord()
var command = new WinGetPackageManagerCommand(this);
if (this.ParameterSetName == Constants.IntegrityLatestSet)
{
command.AssertUsingLatest(this.IncludePreRelease.ToBool());
command.AssertUsingLatest(this.IncludePrerelease.ToBool());
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="WinGetPackageManagerCmdlet.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand Down Expand Up @@ -36,6 +36,6 @@ public abstract class WinGetPackageManagerCmdlet : PSCmdlet
[Parameter(
ParameterSetName = Constants.IntegrityLatestSet,
ValueFromPipelineByPropertyName = true)]
public SwitchParameter IncludePreRelease { get; set; }
public SwitchParameter IncludePrerelease { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="RemoveSourceCmdlet.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand All @@ -20,6 +20,7 @@ public sealed class RemoveSourceCmdlet : PSCmdlet
/// Gets or sets the name of the source to remove.
/// </summary>
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void ProcessRecord()
this.command = new WinGetPackageManagerCommand(this);
if (this.ParameterSetName == Constants.IntegrityLatestSet)
{
this.command.RepairUsingLatest(this.IncludePreRelease.ToBool(), this.AllUsers.ToBool(), this.Force.ToBool());
this.command.RepairUsingLatest(this.IncludePrerelease.ToBool(), this.AllUsers.ToBool(), this.Force.ToBool());
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="ResetSourceCmdlet.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand All @@ -13,25 +13,41 @@ namespace Microsoft.WinGet.Client.Cmdlets.Cmdlets
/// <summary>
/// Resets a source. Requires admin.
/// </summary>
[Cmdlet(VerbsCommon.Reset, Constants.WinGetNouns.Source)]
[Cmdlet(VerbsCommon.Reset, Constants.WinGetNouns.Source, DefaultParameterSetName = Constants.DefaultSet)]
public sealed class ResetSourceCmdlet : PSCmdlet
{
/// <summary>
/// Gets or sets the name of the source to reset.
/// </summary>
[Parameter(
Position = 0,
Mandatory = true,
ParameterSetName = Constants.DefaultSet,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
public string Name { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to reset all sources.
/// </summary>
[Parameter(ParameterSetName = Constants.OptionalSet, ValueFromPipelineByPropertyName = true)]
public SwitchParameter All { get; set; }

/// <summary>
/// Resets source.
/// </summary>
protected override void ProcessRecord()
{
var command = new CliCommand(this);
command.ResetSource(this.Name);

if (!string.IsNullOrEmpty(this.Name))
{
command.ResetSourceByName(this.Name);
}
else if (this.All)
{
command.ResetAllSources();
}
}
}
}
14 changes: 12 additions & 2 deletions src/PowerShell/Microsoft.WinGet.Client.Cmdlets/Common/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="Constants.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand Down Expand Up @@ -30,7 +30,17 @@ internal static class Constants
/// This parameter set indicates that a package was not provided via a parameter or the pipeline and it
/// needs to be found by searching a package source.
/// </summary>
public const string FoundSet = "FoundSet";
public const string FoundSet = "FoundSet";

/// <summary>
/// This parameter set indicates the default parameters associated with a cmdlet.
/// </summary>
public const string DefaultSet = "DefaultSet";

/// <summary>
/// This parameter set indicates the optional parameters associated with a cmdlet.
/// </summary>
public const string OptionalSet = "OptionalSet";

/// <summary>
/// Parameter set for an specific version parameter.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- If these target frameworks are updated, make sure to also update the .psd1 and .nuspec files.-->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="CliCommand.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand Down Expand Up @@ -94,15 +94,24 @@ public void RemoveSource(string name)
}

/// <summary>
/// Resets source.
/// Resets a source.
/// </summary>
/// <param name="name">Name of source.</param>
public void ResetSource(string name)
public void ResetSourceByName(string name)
{
Utilities.VerifyAdmin();
_ = this.Run("source", $"reset --name {name} --force");
}

/// <summary>
/// Resets all sources and adds the defaults.
/// </summary>
public void ResetAllSources()
{
Utilities.VerifyAdmin();
_ = this.Run("source", $"reset --force");
}

private WinGetCLICommandResult Run(string command, string parameters, int timeOut = 60000)
{
var wingetCliWrapper = new WingetCLIWrapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="GitHubClient.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand Down Expand Up @@ -43,24 +43,24 @@ public async Task<Release> GetReleaseAsync(string releaseTag)
/// <summary>
/// Gets the latest released and waits.
/// </summary>
/// <param name="includePreRelease">Include prerelease.</param>
/// <param name="includePrerelease">Include prerelease.</param>
/// <returns>Latest version.</returns>
public async Task<string> GetLatestReleaseTagNameAsync(bool includePreRelease)
public async Task<string> GetLatestReleaseTagNameAsync(bool includePrerelease)
{
return (await this.GetLatestReleaseAsync(includePreRelease)).TagName;
return (await this.GetLatestReleaseAsync(includePrerelease)).TagName;
}

/// <summary>
/// Gets the latest released version.
/// </summary>
/// <param name="includePreRelease">Include prerelease.</param>
/// <param name="includePrerelease">Include prerelease.</param>
/// <returns>Latest version.</returns>
public async Task<Release> GetLatestReleaseAsync(bool includePreRelease)
public async Task<Release> GetLatestReleaseAsync(bool includePrerelease)
{
Release release;

// GetLatest doesn't respect prerelease or gives an option to get it.
if (includePreRelease)
if (includePrerelease)
{
// GetAll orders by newest and includes pre releases.
release = (await this.gitHubClient.Repository.Release.GetAll(this.owner, this.repo))[0];
Expand Down
4 changes: 2 additions & 2 deletions src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ class WinGetPackageManager
} elseif ($this.UseLatestPreRelease)
{
$hashArgs.Add("Latest", $true)
$hashArgs.Add("IncludePreRelease", $true)
$hashArgs.Add("IncludePrerelease", $true)
} elseif (-not [string]::IsNullOrWhiteSpace($this.Version))
{
$hashArgs.Add("Version", $this.Version)
Expand Down Expand Up @@ -398,7 +398,7 @@ class WinGetPackageManager
} elseif ($this.UseLatestPreRelease)
{
$hashArgs.Add("Latest", $true)
$hashArgs.Add("IncludePreRelease", $true)
$hashArgs.Add("IncludePrerelease", $true)
} elseif (-not [string]::IsNullOrWhiteSpace($this.Version))
{
$hashArgs.Add("Version", $this.Version)
Expand Down
15 changes: 15 additions & 0 deletions src/PowerShell/tests/Microsoft.WinGet.Client.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ Describe 'Get-WinGetVersion' {
}
}

Describe 'Reset-WinGetSource' {
BeforeAll {
AddTestSource
}

# Requires admin
It 'Resets all sources' {
Reset-WinGetSource -All
}

It 'Test source should be removed' {
{ Get-WinGetSource -Name 'TestSource' } | Should -Throw
}
}

Describe 'Get|Add|Reset-WinGetSource' {

BeforeAll {
Expand Down

0 comments on commit 83b2db4

Please sign in to comment.