Skip to content

Commit e6208fd

Browse files
authored
Auto-create PSResourceRepository.xml file if the file does not already exist. (#450)
1 parent b96b290 commit e6208fd

10 files changed

+41
-58
lines changed

src/code/FindPSResource.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ public sealed class FindPSResource : PSCmdlet
134134
protected override void BeginProcessing()
135135
{
136136
_source = new CancellationTokenSource();
137-
_cancellationToken = _source.Token;
137+
_cancellationToken = _source.Token;
138+
139+
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
140+
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
141+
RepositorySettings.CheckRepositoryStore();
138142
}
139143

140144
protected override void StopProcessing()

src/code/GetPSResourceRepository.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,7 @@ class GetPSResourceRepository : PSCmdlet
3939

4040
protected override void BeginProcessing()
4141
{
42-
try
43-
{
44-
WriteVerbose("Calling API to check repository store exists in non-corrupted state");
45-
RepositorySettings.CheckRepositoryStore();
46-
}
47-
catch (PSInvalidOperationException e)
48-
{
49-
ThrowTerminatingError(new ErrorRecord(
50-
new PSInvalidOperationException(e.Message),
51-
"RepositoryStoreException",
52-
ErrorCategory.ReadError,
53-
this));
54-
}
42+
RepositorySettings.CheckRepositoryStore();
5543
}
5644
protected override void ProcessRecord()
5745
{

src/code/InstallPSResource.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class InstallPSResource : PSCmdlet
103103

104104
protected override void BeginProcessing()
105105
{
106+
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
107+
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
108+
RepositorySettings.CheckRepositoryStore();
109+
106110
// validate that if a -Version param is passed in that it can be parsed into a NuGet version range.
107111
// An exact version will be formatted into a version range.
108112
if (ParameterSetName.Equals(NameParameterSet) && Version != null && !Utils.TryParseVersionOrVersionRange(Version, out _versionRange))

src/code/PublishPSResource.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,16 @@ public PSCredential ProxyCredential {
156156
private NuGetVersion _pkgVersion;
157157
private string _pkgName;
158158
private static char[] _PathSeparators = new [] { System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar };
159-
159+
160160
#endregion
161161

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

164170
protected override void ProcessRecord()
165171
{

src/code/RegisterPSResourceRepository.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,8 @@ protected override void BeginProcessing()
118118
ErrorCategory.NotImplemented,
119119
this));
120120
}
121-
122-
try
123-
{
124-
RepositorySettings.CheckRepositoryStore();
125-
}
126-
catch (PSInvalidOperationException e)
127-
{
128-
ThrowTerminatingError(new ErrorRecord(
129-
new PSInvalidOperationException(e.Message),
130-
"RepositoryStoreException",
131-
ErrorCategory.ReadError,
132-
this));
133-
}
121+
122+
RepositorySettings.CheckRepositoryStore();
134123
}
135124
protected override void ProcessRecord()
136125
{

src/code/RepositorySettings.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@ namespace Microsoft.PowerShell.PowerShellGet.UtilClasses
1919
/// </summary>
2020

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

31-
3235
/// <summary>
3336
/// Check if repository store xml file exists, if not then create
3437
/// </summary>
@@ -52,6 +55,10 @@ public static void CheckRepositoryStore()
5255
{
5356
throw new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Repository store creation failed with error: {0}.", e.Message));
5457
}
58+
59+
// Add PSGallery to the newly created store
60+
Uri psGalleryUri = new Uri(PSGalleryRepoURL);
61+
Add(PSGalleryRepoName, psGalleryUri, defaultPriority, defaultTrusted);
5562
}
5663

5764
// Open file (which should exist now), if cannot/is corrupted then throw error

src/code/SavePSResource.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public string Path
125125

126126
protected override void BeginProcessing()
127127
{
128+
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
129+
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
130+
RepositorySettings.CheckRepositoryStore();
131+
128132
// validate that if a -Version param is passed in that it can be parsed into a NuGet version range.
129133
// an exact version will be formatted into a version range.
130134
if (ParameterSetName.Equals("NameParameterSet") &&

src/code/SetPSResourceRepository.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,7 @@ public SwitchParameter Trusted
9595
#region Methods
9696
protected override void BeginProcessing()
9797
{
98-
try
99-
{
100-
WriteVerbose("Calling API to check repository store exists in non-corrupted state");
101-
RepositorySettings.CheckRepositoryStore();
102-
}
103-
catch (PSInvalidOperationException e)
104-
{
105-
ThrowTerminatingError(new ErrorRecord(
106-
new PSInvalidOperationException(e.Message),
107-
"RepositoryStoreException",
108-
ErrorCategory.ReadError,
109-
this));
110-
}
98+
RepositorySettings.CheckRepositoryStore();
11199
}
112100

113101
protected override void ProcessRecord()

src/code/UnregisterPSResourceRepository.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,7 @@ class UnregisterPSResourceRepository : PSCmdlet
3737

3838
protected override void BeginProcessing()
3939
{
40-
try
41-
{
42-
WriteVerbose("Calling API to check repository store exists in non-corrupted state");
43-
RepositorySettings.CheckRepositoryStore();
44-
}
45-
catch (PSInvalidOperationException e)
46-
{
47-
ThrowTerminatingError(new ErrorRecord(
48-
new PSInvalidOperationException(e.Message),
49-
"RepositoryStoreException",
50-
ErrorCategory.ReadError,
51-
this));
52-
}
40+
RepositorySettings.CheckRepositoryStore();
5341
}
5442
protected override void ProcessRecord()
5543
{

src/code/UpdatePSResource.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ public sealed class UpdatePSResource : PSCmdlet
103103
#region Override Methods
104104

105105
protected override void BeginProcessing()
106-
{
106+
{
107+
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
108+
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
109+
RepositorySettings.CheckRepositoryStore();
110+
107111
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, Scope);
108112
}
109113

@@ -166,6 +170,7 @@ protected override void ProcessRecord()
166170
}
167171

168172
#endregion
173+
169174
#region Private Methods
170175

171176
/// <Summary>

0 commit comments

Comments
 (0)