Skip to content

Refactoring *-PSResourceRepository cmdlets (Register, Get, Unregister) #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 95 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
1ea4062
added new RegisterPSResourceRepository.cs with parameters so far
anamnavi Feb 10, 2021
9d8b76a
add PSRepositoryItemInfo
anamnavi Feb 12, 2021
d6664c8
added passthru, need to complete implementation
anamnavi Feb 17, 2021
ff1f1ad
Merge branch 'development' of https://github.com/anamnavi/PowerShellG…
anamnavi Feb 18, 2021
3686f79
added code and tests for REgister-PSResourceRepository
anamnavi Feb 23, 2021
130ca29
add code for Get-PSResourceRepository and move to srcNew
anamnavi Feb 24, 2021
af6dc6b
add files to srcNew
anamnavi Feb 24, 2021
c6763e0
moved cmdlets and added Set-PSResourceRepository cmdlet
anamnavi Mar 1, 2021
027c507
pulled and merged conflict
anamnavi Mar 1, 2021
a6635de
removed set, get, unregister resource repository cmdlets to put in la…
anamnavi Mar 1, 2021
ccb1bc5
rename Register-PSResourceRepository cmdlet and references to previou…
anamnavi Mar 1, 2021
23576d3
added comemnts where needed
anamnavi Mar 2, 2021
38d175e
revert changes accidentally made
anamnavi Mar 2, 2021
59cef3c
add Get-PSResourceRepository, Unregister-PSResourceRepository so test…
anamnavi Mar 2, 2021
fb4f768
reset changes to settings.json
anamnavi Mar 2, 2021
5ce05cb
revert unintended changes to srcOld folder
anamnavi Mar 2, 2021
38cc792
revert unintended changes to testOld
anamnavi Mar 2, 2021
f1d95a6
revert unintended changes to PowerShellGet.psd1 script
anamnavi Mar 2, 2021
ca37182
add EOL to some files that were missing it
anamnavi Mar 2, 2021
d1b9e8c
remove commented out code from PSGetTestUtils.cs
anamnavi Mar 2, 2021
25d2a39
changed using statements so that namespace and class name can have sa…
anamnavi Mar 2, 2021
ba9f86b
fix reference in ArgumentCompleter.cs
anamnavi Mar 2, 2021
304d43d
fix some codacy warnings
anamnavi Mar 2, 2021
541f9c6
make RepositorySettings.cs a static class and update rest of classes …
anamnavi Mar 2, 2021
1e20a25
remove not so helpful comments
anamnavi Mar 2, 2021
fec86a9
explicitly type variables that were previously implicitly typed as var
anamnavi Mar 2, 2021
1640970
remove unintended writeDebug message
anamnavi Mar 2, 2021
be6fcf8
use yield to return IEnumerable for ArgumentCompleter and remove priv…
anamnavi Mar 4, 2021
0a22362
remove unncessary parameter set names from cmdlets which don't have m…
anamnavi Mar 4, 2021
8089a5e
store parameter set names as const fields
anamnavi Mar 4, 2021
5ebd820
remove unused RemotingCapability field from Cmdlets
anamnavi Mar 4, 2021
36af73c
RepositorySettings and PSRepositoryItem now belong to UtilClasses nam…
anamnavi Mar 5, 2021
90263ef
replace private fields with Properties, use switch case for parameter…
anamnavi Mar 8, 2021
d995345
Remove comments
anamnavi Mar 8, 2021
99430f0
create helper method to search if repo with name already exists
anamnavi Mar 8, 2021
22fcded
change error handling to be non terminating where needed, and continu…
anamnavi Mar 9, 2021
6ad811b
remove some unneccessary comments
anamnavi Mar 9, 2021
d36132e
refactor so there's a helper method calling the Add() API
anamnavi Mar 9, 2021
7fc92e8
call Read() to find existing repo by name in Add()
anamnavi Mar 10, 2021
0116920
make commented description of Add() clearer
anamnavi Mar 10, 2021
bed1f79
refactor FindRepositoryXML()
anamnavi Mar 10, 2021
4cd0df2
throw error if non implemented parameters are attempted to be used
anamnavi Mar 10, 2021
c969ffc
refactor CheckRepositoryStore() and how/where it throws errors
anamnavi Mar 11, 2021
7e49a78
wrap RepositorySettings file handling code with try/catch
anamnavi Mar 11, 2021
3a3d241
minor fix
anamnavi Mar 11, 2021
fc3d6f3
remove unneccessary using statements
anamnavi Mar 11, 2021
dacc37b
Delete UnregisterPSResourceRepository.Tests.ps1
anamnavi Mar 11, 2021
72c93ff
include error specific tests for Register-PSResourceRepository
anamnavi Mar 16, 2021
021a660
change Repositories parameter type from List<Hashtable> to Hashtable[]
anamnavi Mar 16, 2021
431de34
add tests for Unregister and refactor RepositorySettings to handle Un…
anamnavi Mar 16, 2021
5a9f14d
add out errorMsgs to RepositorySettings Read() and refactor GetPSReso…
anamnavi Mar 17, 2021
a10b55e
fix ArgumentCompleter issue
anamnavi Mar 17, 2021
2eae87d
remove uneccesary comments from RepositorySettings
anamnavi Mar 18, 2021
f52d048
rename FindRepositoryElement helper method
anamnavi Mar 18, 2021
7768345
refactor FindRepositoryElement code to just return and be more readable
anamnavi Mar 18, 2021
618254b
styling with if keywords
anamnavi Mar 18, 2021
c77d4d6
fix indentation and bracket placement according to PowerShell style s…
anamnavi Mar 18, 2021
ce14eb7
added Debug statements to register
anamnavi Mar 22, 2021
3754c86
added try catch to Get and Unregister, changed out string[] errorMsgs…
anamnavi Mar 22, 2021
9f21b6e
fix Debug statements and remove unneccessary try catch
anamnavi Mar 23, 2021
2e25cd0
remove shouldSupportProcess from Get
anamnavi Mar 23, 2021
68e9835
put check for repo store in BeginProcessing() and change default swit…
anamnavi Mar 23, 2021
77abf42
check if repo name is whitespace
anamnavi Mar 23, 2021
8b713e2
correct error message typo
anamnavi Mar 23, 2021
ca29828
remove unncessary comment
anamnavi Mar 23, 2021
37f74f0
fix typo in tests, rename errorMsgs to errorList
anamnavi Mar 23, 2021
7027be2
make PSRepositoryItem sealed class, add ShouldProcess() support to Re…
anamnavi Mar 24, 2021
c1b4a7b
add ShouldSupportProcess = true to Unregister
anamnavi Mar 24, 2021
5818f5e
don't write error if names matching wildcard aren't found
anamnavi Mar 24, 2021
2f03ed9
not set errorList to null as not needed
anamnavi Mar 24, 2021
683871b
add powershell helper to add quotes to ArgumentCompleter to deal with…
anamnavi Mar 26, 2021
d9b2d1a
tests use now
anamnavi Mar 26, 2021
6266309
changed Get and Unregister tests to use FullyQualifiedErrorId
anamnavi Mar 26, 2021
3fb3f43
remove setters from PSRepositoryItem
anamnavi Mar 26, 2021
af640d6
remove ValueByPipeline tag from Proxy, ProxyCredential and add onto R…
anamnavi Mar 26, 2021
ee7f257
use ErrorId for Unregister tests
anamnavi Mar 26, 2021
4bc88fc
all tests now rely on FullyQualifiedErrorId
anamnavi Mar 26, 2021
ac96b9d
make shouldsupport() target action message more intuitive
anamnavi Mar 26, 2021
d9fbed1
fix target name formatting for ShouldProcess()
anamnavi Mar 26, 2021
fd039f5
add newline after brackets and #endregion per powershell style guidel…
anamnavi Mar 29, 2021
73d29dd
add newline in Utils.cs
anamnavi Mar 29, 2021
e75ff37
trim trailing and leading whitespaces from Name and throw error if Na…
anamnavi Mar 29, 2021
d698084
add test for throwing error if Name is whitespace for register
anamnavi Mar 29, 2021
e6b423e
rename Get and Unregister to remove hype in cmdlet name
anamnavi Mar 29, 2021
bfde4fa
fix codacy warning
anamnavi Mar 29, 2021
73d8528
fixed some naming conventions in test to fix codacy warnings
anamnavi Mar 29, 2021
028272f
renamed PSRepositoryItem -> PSRepositoryInfo to match Amber's output …
anamnavi Mar 29, 2021
4d6fb9c
Update Name parameter description
anamnavi Mar 30, 2021
37607ad
fix styling issues
anamnavi Mar 30, 2021
968d084
change Repository settings fields to private
anamnavi Mar 30, 2021
da96b87
change testcases syntax to Pester v4 syntax
anamnavi Apr 1, 2021
e64464f
change temp variable name, update PSResourceRepository.xml file comment
anamnavi Apr 5, 2021
c678157
add newline to end of Utils.cs
anamnavi Apr 5, 2021
adbfa16
organize using directive statements alphabetically
anamnavi Apr 5, 2021
d6da310
change error type for checkRepositoryStore error thrown
anamnavi Apr 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/code/ArgumentCompleter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.ComponentModel;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.PowerShell.PowerShellGet.UtilClasses;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Language;

internal class RepositoryNameCompleter : IArgumentCompleter
{
public IEnumerable<CompletionResult> CompleteArgument(
string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
{
List<PSRepositoryInfo> listOfRepositories = RepositorySettings.Read(null, out string[] _);

wordToComplete = Utils.TrimQuotes(wordToComplete);
var wordToCompletePattern = WildcardPattern.Get(
pattern: string.IsNullOrWhiteSpace(wordToComplete) ? "*" : wordToComplete + "*",
options: WildcardOptions.IgnoreCase);

foreach (PSRepositoryInfo repo in listOfRepositories)
{
string repoName = repo.Name;
if (wordToCompletePattern.IsMatch(repoName))
{
yield return new CompletionResult(Utils.QuoteName(repoName));
}
}
}
}
80 changes: 80 additions & 0 deletions src/code/GetPSResourceRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.PowerShell.PowerShellGet.UtilClasses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace Microsoft.PowerShell.PowerShellGet.Cmdlets
{
/// <summary>
/// The Get-PSResourceRepository cmdlet replaces the Get-PSRepository cmdlet from V2.
/// It searches for the PowerShell module repositories that are registered for the current user.
/// By default it will return all registered repositories, or if the -Name parameter argument is specified then it wil return the repository with that name.
/// It returns PSRepositoryInfo objects which describe each resource item found.
/// </summary>

[Cmdlet(VerbsCommon.Get,
"PSResourceRepository",
HelpUri = "<add>")]
public sealed
class GetPSResourceRepository : PSCmdlet
{
#region Parameters

/// <summary>
/// Specifies the name(s) of a registered repository to find.
/// Supports wild card characters.
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[ArgumentCompleter(typeof(RepositoryNameCompleter))]
[ValidateNotNullOrEmpty]
public string[] Name { get; set; } = new string[0];

#endregion

#region Methods

protected override void BeginProcessing()
{
try
{
WriteDebug("Calling API to check repository store exists in non-corrupted state");
RepositorySettings.CheckRepositoryStore();
}
catch (PSInvalidOperationException e)
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException(e.Message),
"RepositoryStoreException",
ErrorCategory.ReadError,
this));
}
}
protected override void ProcessRecord()
{
string nameArrayAsString = (Name == null || !Name.Any() || string.Equals(Name[0], "*") || Name[0] == null) ? "all" : string.Join(", ", Name);
WriteDebug(String.Format("reading repository: {0}. Calling Read() API now", nameArrayAsString));
List<PSRepositoryInfo> items = RepositorySettings.Read(Name, out string[] errorList);

// handle non-terminating errors
foreach (string error in errorList)
{
WriteError(new ErrorRecord(
new PSInvalidOperationException(error),
"ErrorGettingSpecifiedRepo",
ErrorCategory.InvalidOperation,
this));
}

foreach (PSRepositoryInfo repo in items)
{
WriteObject(repo);
}
}

#endregion
}
}
50 changes: 50 additions & 0 deletions src/code/PSRepositoryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Management.Automation;

namespace Microsoft.PowerShell.PowerShellGet.UtilClasses
{
/// <summary>
/// This class contains information for a repository item.
/// </summary>
public sealed class PSRepositoryInfo
{
#region Constructor

public PSRepositoryInfo(string name, Uri url, int priority, bool trusted)
{
Name = name;
Url = url;
Priority = priority;
Trusted = trusted;
}

#endregion

#region Properties

/// <summary>
/// the Name of the repository
/// </summary>
public string Name { get; }

/// <summary>
/// the Url for the repository
/// </summary>
public Uri Url { get; }

/// <summary>
/// whether the repository is trusted
public bool Trusted { get; }

/// <summary>
/// the priority of the repository
/// </summary>
[ValidateRange(0, 50)]
public int Priority { get; }

#endregion
}
}
Loading