Skip to content

Auto-create PSResourceRepository.xml file if the file does not already exist. #450

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 3 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion src/code/FindPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ public sealed class FindPSResource : PSCmdlet
protected override void BeginProcessing()
{
_source = new CancellationTokenSource();
_cancellationToken = _source.Token;
_cancellationToken = _source.Token;

// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
RepositorySettings.CheckRepositoryStore();
}

protected override void StopProcessing()
Expand Down
14 changes: 1 addition & 13 deletions src/code/GetPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,7 @@ class GetPSResourceRepository : PSCmdlet

protected override void BeginProcessing()
{
try
{
WriteVerbose("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));
}
RepositorySettings.CheckRepositoryStore();
}
protected override void ProcessRecord()
{
Expand Down
4 changes: 4 additions & 0 deletions src/code/InstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class InstallPSResource : PSCmdlet

protected override void BeginProcessing()
{
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
RepositorySettings.CheckRepositoryStore();

// validate that if a -Version param is passed in that it can be parsed into a NuGet version range.
// An exact version will be formatted into a version range.
if (ParameterSetName.Equals(NameParameterSet) && Version != null && !Utils.TryParseVersionOrVersionRange(Version, out _versionRange))
Expand Down
8 changes: 7 additions & 1 deletion src/code/PublishPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,16 @@ public PSCredential ProxyCredential {
private NuGetVersion _pkgVersion;
private string _pkgName;
private static char[] _PathSeparators = new [] { System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar };

#endregion

#region Method overrides
protected override void BeginProcessing()
{
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
RepositorySettings.CheckRepositoryStore();
}

protected override void ProcessRecord()
{
Expand Down
15 changes: 2 additions & 13 deletions src/code/RegisterPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,8 @@ protected override void BeginProcessing()
ErrorCategory.NotImplemented,
this));
}

try
{
RepositorySettings.CheckRepositoryStore();
}
catch (PSInvalidOperationException e)
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException(e.Message),
"RepositoryStoreException",
ErrorCategory.ReadError,
this));
}

RepositorySettings.CheckRepositoryStore();
}
protected override void ProcessRecord()
{
Expand Down
13 changes: 10 additions & 3 deletions src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ namespace Microsoft.PowerShell.PowerShellGet.UtilClasses
/// </summary>

internal static class RepositorySettings
{
{
/// <summary>
/// File name for a user's repository store file is 'PSResourceRepository.xml'
/// The repository store file's location is currently only at '%LOCALAPPDATA%\PowerShellGet' for the user account.
/// </summary>
private static readonly string RepositoryFileName = "PSResourceRepository.xml";
private const string PSGalleryRepoName = "PSGallery";
private const string PSGalleryRepoURL = "https://www.powershellgallery.com/api/v2";
private const int defaultPriority = 50;
private const bool defaultTrusted = false;
private const string RepositoryFileName = "PSResourceRepository.xml";
private static readonly string RepositoryPath = Path.Combine(Environment.GetFolderPath(SpecialFolder.LocalApplicationData), "PowerShellGet");
private static readonly string FullRepositoryPath = Path.Combine(RepositoryPath, RepositoryFileName);


/// <summary>
/// Check if repository store xml file exists, if not then create
/// </summary>
Expand All @@ -52,6 +55,10 @@ public static void CheckRepositoryStore()
{
throw new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Repository store creation failed with error: {0}.", e.Message));
}

// Add PSGallery to the newly created store
Uri psGalleryUri = new Uri(PSGalleryRepoURL);
Add(PSGalleryRepoName, psGalleryUri, defaultPriority, defaultTrusted);
}

// Open file (which should exist now), if cannot/is corrupted then throw error
Expand Down
4 changes: 4 additions & 0 deletions src/code/SavePSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public string Path

protected override void BeginProcessing()
{
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
RepositorySettings.CheckRepositoryStore();

// validate that if a -Version param is passed in that it can be parsed into a NuGet version range.
// an exact version will be formatted into a version range.
if (ParameterSetName.Equals("NameParameterSet") &&
Expand Down
14 changes: 1 addition & 13 deletions src/code/SetPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,7 @@ public SwitchParameter Trusted
#region Methods
protected override void BeginProcessing()
{
try
{
WriteVerbose("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));
}
RepositorySettings.CheckRepositoryStore();
}

protected override void ProcessRecord()
Expand Down
14 changes: 1 addition & 13 deletions src/code/UnregisterPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,7 @@ class UnregisterPSResourceRepository : PSCmdlet

protected override void BeginProcessing()
{
try
{
WriteVerbose("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));
}
RepositorySettings.CheckRepositoryStore();
}
protected override void ProcessRecord()
{
Expand Down
7 changes: 6 additions & 1 deletion src/code/UpdatePSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ public sealed class UpdatePSResource : PSCmdlet
#region Override Methods

protected override void BeginProcessing()
{
{
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
RepositorySettings.CheckRepositoryStore();

_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, Scope);
}

Expand Down Expand Up @@ -166,6 +170,7 @@ protected override void ProcessRecord()
}

#endregion

#region Private Methods

/// <Summary>
Expand Down