diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index ba3016de3..1e576ad29 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -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() diff --git a/src/code/GetPSResourceRepository.cs b/src/code/GetPSResourceRepository.cs index 9eb57b530..310549f69 100644 --- a/src/code/GetPSResourceRepository.cs +++ b/src/code/GetPSResourceRepository.cs @@ -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() { diff --git a/src/code/InstallPSResource.cs b/src/code/InstallPSResource.cs index d546e6b4a..78e7ecd62 100644 --- a/src/code/InstallPSResource.cs +++ b/src/code/InstallPSResource.cs @@ -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)) diff --git a/src/code/PublishPSResource.cs b/src/code/PublishPSResource.cs index 3a41ac047..5684bfc08 100644 --- a/src/code/PublishPSResource.cs +++ b/src/code/PublishPSResource.cs @@ -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() { diff --git a/src/code/RegisterPSResourceRepository.cs b/src/code/RegisterPSResourceRepository.cs index 4ede96ae3..be8173488 100644 --- a/src/code/RegisterPSResourceRepository.cs +++ b/src/code/RegisterPSResourceRepository.cs @@ -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() { diff --git a/src/code/RepositorySettings.cs b/src/code/RepositorySettings.cs index 99911a39e..529e56186 100644 --- a/src/code/RepositorySettings.cs +++ b/src/code/RepositorySettings.cs @@ -19,16 +19,19 @@ namespace Microsoft.PowerShell.PowerShellGet.UtilClasses /// internal static class RepositorySettings - { + { /// /// 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. /// - 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); - /// /// Check if repository store xml file exists, if not then create /// @@ -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 diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index ecc20fe44..d4af1d151 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -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") && diff --git a/src/code/SetPSResourceRepository.cs b/src/code/SetPSResourceRepository.cs index d57588a32..284a27e7e 100644 --- a/src/code/SetPSResourceRepository.cs +++ b/src/code/SetPSResourceRepository.cs @@ -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() diff --git a/src/code/UnregisterPSResourceRepository.cs b/src/code/UnregisterPSResourceRepository.cs index 6257b77f0..10a15896f 100644 --- a/src/code/UnregisterPSResourceRepository.cs +++ b/src/code/UnregisterPSResourceRepository.cs @@ -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() { diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index de6bacac7..0cf0a8fe5 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -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); } @@ -166,6 +170,7 @@ protected override void ProcessRecord() } #endregion + #region Private Methods ///