@@ -133,6 +133,7 @@ public override FindResults FindName(string packageName, bool includePrerelease,
133
133
_cmdletPassedIn . WriteDebug ( "In ACRServerAPICalls::FindName()" ) ;
134
134
string accessToken = string . Empty ;
135
135
string tenantID = string . Empty ;
136
+ string packageNameLowercase = packageName . ToLower ( ) ;
136
137
137
138
// Need to set up secret management vault before hand
138
139
var repositoryCredentialInfo = Repository . CredentialInfo ;
@@ -167,7 +168,7 @@ public override FindResults FindName(string packageName, bool includePrerelease,
167
168
}
168
169
169
170
_cmdletPassedIn . WriteVerbose ( "Getting tags" ) ;
170
- var foundTags = FindAcrImageTags ( registry , packageName , "*" , acrAccessToken , out errRecord ) ;
171
+ var foundTags = FindAcrImageTags ( registry , packageNameLowercase , "*" , acrAccessToken , out errRecord ) ;
171
172
if ( errRecord != null || foundTags == null )
172
173
{
173
174
return new FindResults ( stringResponse : new string [ ] { } , hashtableResponse : emptyHashResponses , responseType : acrFindResponseType ) ;
@@ -193,20 +194,29 @@ public override FindResults FindName(string packageName, bool includePrerelease,
193
194
return new FindResults ( stringResponse : Utils . EmptyStrArray , hashtableResponse : emptyHashResponses , responseType : acrFindResponseType ) ;
194
195
}
195
196
196
- if ( NuGetVersion . TryParse ( pkgVersionElement . ToString ( ) , out NuGetVersion pkgVersion ) )
197
+ if ( ! NuGetVersion . TryParse ( pkgVersionElement . ToString ( ) , out NuGetVersion pkgVersion ) )
197
198
{
198
- _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' version parsed as '{ pkgVersion } '") ;
199
- if ( ! pkgVersion . IsPrerelease || includePrerelease )
200
- {
201
- // Versions are always in descending order i.e 5.0.0, 3.0.0, 1.0.0 so grabbing the first match suffices
202
- latestVersionResponse . Add ( GetACRMetadata ( registry , packageName , pkgVersion , acrAccessToken , out errRecord ) ) ;
203
- if ( errRecord != null )
204
- {
205
- return new FindResults ( stringResponse : new string [ ] { } , hashtableResponse : latestVersionResponse . ToArray ( ) , responseType : acrFindResponseType ) ;
206
- }
199
+ errRecord = new ErrorRecord (
200
+ new ArgumentException ( $ "Version { pkgVersionElement . ToString ( ) } to be parsed from metadata is not a valid NuGet version.") ,
201
+ "FindNameFailure" ,
202
+ ErrorCategory . InvalidArgument ,
203
+ this ) ;
207
204
208
- break ;
205
+ return new FindResults ( stringResponse : Utils . EmptyStrArray , hashtableResponse : emptyHashResponses , responseType : acrFindResponseType ) ;
206
+ }
207
+
208
+ _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' version parsed as '{ pkgVersion } '") ;
209
+ if ( ! pkgVersion . IsPrerelease || includePrerelease )
210
+ {
211
+ // TODO: ensure versions are in order, fix bug https://github.com/PowerShell/PSResourceGet/issues/1581
212
+ Hashtable metadata = GetACRMetadata ( registry , packageNameLowercase , pkgVersion , acrAccessToken , out errRecord ) ;
213
+ if ( errRecord != null || metadata . Count == 0 )
214
+ {
215
+ return new FindResults ( stringResponse : new string [ ] { } , hashtableResponse : emptyHashResponses , responseType : acrFindResponseType ) ;
209
216
}
217
+
218
+ latestVersionResponse . Add ( metadata ) ;
219
+ break ;
210
220
}
211
221
}
212
222
}
@@ -285,6 +295,7 @@ public override FindResults FindVersionGlobbing(string packageName, VersionRange
285
295
_cmdletPassedIn . WriteDebug ( "In ACRServerAPICalls::FindVersionGlobbing()" ) ;
286
296
string accessToken = string . Empty ;
287
297
string tenantID = string . Empty ;
298
+ string packageNameLowercase = packageName . ToLower ( ) ;
288
299
289
300
// Need to set up secret management vault beforehand
290
301
var repositoryCredentialInfo = Repository . CredentialInfo ;
@@ -318,7 +329,7 @@ public override FindResults FindVersionGlobbing(string packageName, VersionRange
318
329
}
319
330
320
331
_cmdletPassedIn . WriteVerbose ( "Getting tags" ) ;
321
- var foundTags = FindAcrImageTags ( registry , packageName , "*" , acrAccessToken , out errRecord ) ;
332
+ var foundTags = FindAcrImageTags ( registry , packageNameLowercase , "*" , acrAccessToken , out errRecord ) ;
322
333
if ( errRecord != null || foundTags == null )
323
334
{
324
335
return new FindResults ( stringResponse : new string [ ] { } , hashtableResponse : emptyHashResponses , responseType : acrFindResponseType ) ;
@@ -355,7 +366,7 @@ public override FindResults FindVersionGlobbing(string packageName, VersionRange
355
366
continue ;
356
367
}
357
368
358
- latestVersionResponse . Add ( GetACRMetadata ( registry , packageName , pkgVersion , acrAccessToken , out errRecord ) ) ;
369
+ latestVersionResponse . Add ( GetACRMetadata ( registry , packageNameLowercase , pkgVersion , acrAccessToken , out errRecord ) ) ;
359
370
if ( errRecord != null )
360
371
{
361
372
return new FindResults ( stringResponse : new string [ ] { } , hashtableResponse : latestVersionResponse . ToArray ( ) , responseType : acrFindResponseType ) ;
@@ -706,7 +717,7 @@ internal Hashtable GetACRMetadata(string registry, string packageName, NuGetVers
706
717
if ( exception != null )
707
718
{
708
719
errRecord = new ErrorRecord ( exception , "FindNameFailure" , ErrorCategory . InvalidResult , this ) ;
709
-
720
+
710
721
return requiredVersionResponse ;
711
722
}
712
723
@@ -727,14 +738,22 @@ internal Hashtable GetACRMetadata(string registry, string packageName, NuGetVers
727
738
return requiredVersionResponse ;
728
739
}
729
740
730
- if ( NuGetVersion . TryParse ( pkgVersionElement . ToString ( ) , out NuGetVersion pkgVersion ) )
741
+ if ( ! NuGetVersion . TryParse ( pkgVersionElement . ToString ( ) , out NuGetVersion pkgVersion ) )
731
742
{
732
- _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' version parsed as '{ pkgVersion } '") ;
743
+ errRecord = new ErrorRecord (
744
+ new ArgumentException ( $ "Version { pkgVersionElement . ToString ( ) } to be parsed from metadata is not a valid NuGet version.") ,
745
+ "FindNameFailure" ,
746
+ ErrorCategory . InvalidArgument ,
747
+ this ) ;
733
748
734
- if ( pkgVersion == requiredVersion )
735
- {
736
- requiredVersionResponse . Add ( metadataPkgName , metadata ) ;
737
- }
749
+ return requiredVersionResponse ;
750
+ }
751
+
752
+ _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' version parsed as '{ pkgVersion } '") ;
753
+
754
+ if ( pkgVersion == requiredVersion )
755
+ {
756
+ requiredVersionResponse . Add ( metadataPkgName , metadata ) ;
738
757
}
739
758
}
740
759
@@ -1102,7 +1121,7 @@ private static Collection<KeyValuePair<string, string>> GetDefaultHeaders(string
1102
1121
} ;
1103
1122
}
1104
1123
1105
- internal bool PushNupkgACR ( string psd1OrPs1File , string outputNupkgDir , string pkgName , NuGetVersion pkgVersion , PSRepositoryInfo repository , Hashtable parsedMetadataHash , out ErrorRecord errRecord )
1124
+ internal bool PushNupkgACR ( string psd1OrPs1File , string outputNupkgDir , string pkgName , NuGetVersion pkgVersion , PSRepositoryInfo repository , ResourceType resourceType , Hashtable parsedMetadataHash , out ErrorRecord errRecord )
1106
1125
{
1107
1126
errRecord = null ;
1108
1127
// Push the nupkg to the appropriate repository
@@ -1188,7 +1207,7 @@ internal bool PushNupkgACR(string psd1OrPs1File, string outputNupkgDir, string p
1188
1207
1189
1208
/* Create manifest layer */
1190
1209
_cmdletPassedIn . WriteVerbose ( "Create package version metadata as JSON string" ) ;
1191
- string jsonString = CreateMetadataContent ( psd1OrPs1File , parsedMetadataHash , out ErrorRecord metadataCreationError ) ;
1210
+ string jsonString = CreateMetadataContent ( psd1OrPs1File , resourceType , parsedMetadataHash , out ErrorRecord metadataCreationError ) ;
1192
1211
if ( metadataCreationError != null )
1193
1212
{
1194
1213
_cmdletPassedIn . ThrowTerminatingError ( metadataCreationError ) ;
@@ -1197,7 +1216,7 @@ internal bool PushNupkgACR(string psd1OrPs1File, string outputNupkgDir, string p
1197
1216
FileInfo nupkgFile = new FileInfo ( fullNupkgFile ) ;
1198
1217
var fileSize = nupkgFile . Length ;
1199
1218
var fileName = System . IO . Path . GetFileName ( fullNupkgFile ) ;
1200
- string fileContent = CreateJsonContent ( nupkgDigest , configDigest , fileSize , fileName , pkgName , jsonString ) ;
1219
+ string fileContent = CreateJsonContent ( nupkgDigest , configDigest , fileSize , fileName , pkgName , resourceType , jsonString ) ;
1201
1220
File . WriteAllText ( configFilePath , fileContent ) ;
1202
1221
1203
1222
_cmdletPassedIn . WriteVerbose ( "Create the manifest layer" ) ;
@@ -1207,6 +1226,7 @@ internal bool PushNupkgACR(string psd1OrPs1File, string outputNupkgDir, string p
1207
1226
{
1208
1227
return true ;
1209
1228
}
1229
+
1210
1230
return false ;
1211
1231
}
1212
1232
@@ -1216,6 +1236,7 @@ private string CreateJsonContent(
1216
1236
long nupkgFileSize ,
1217
1237
string fileName ,
1218
1238
string packageName ,
1239
+ ResourceType resourceType ,
1219
1240
string jsonString )
1220
1241
{
1221
1242
StringBuilder stringBuilder = new StringBuilder ( ) ;
@@ -1224,6 +1245,7 @@ private string CreateJsonContent(
1224
1245
1225
1246
jsonWriter . Formatting = Newtonsoft . Json . Formatting . Indented ;
1226
1247
1248
+ // start of manifest JSON object
1227
1249
jsonWriter . WriteStartObject ( ) ;
1228
1250
1229
1251
jsonWriter . WritePropertyName ( "schemaVersion" ) ;
@@ -1257,17 +1279,18 @@ private string CreateJsonContent(
1257
1279
jsonWriter . WriteValue ( fileName ) ;
1258
1280
jsonWriter . WritePropertyName ( "metadata" ) ;
1259
1281
jsonWriter . WriteValue ( jsonString ) ;
1260
- jsonWriter . WriteEndObject ( ) ;
1261
- jsonWriter . WriteEndObject ( ) ;
1282
+ jsonWriter . WritePropertyName ( "artifactType" ) ;
1283
+ jsonWriter . WriteValue ( resourceType . ToString ( ) ) ;
1284
+ jsonWriter . WriteEndObject ( ) ; // end of annotations object
1262
1285
1263
- jsonWriter . WriteEndArray ( ) ;
1264
- jsonWriter . WriteEndObject ( ) ;
1286
+ jsonWriter . WriteEndObject ( ) ; // end of 'layers' entry object
1287
+
1288
+ jsonWriter . WriteEndArray ( ) ; // end of 'layers' array
1289
+ jsonWriter . WriteEndObject ( ) ; // end of manifest JSON object
1265
1290
1266
1291
return stringWriter . ToString ( ) ;
1267
1292
}
1268
1293
1269
-
1270
-
1271
1294
// ACR method
1272
1295
private bool CreateDigest ( string fileName , out string digest , out ErrorRecord error )
1273
1296
{
@@ -1310,45 +1333,40 @@ private bool CreateDigest(string fileName, out string digest, out ErrorRecord er
1310
1333
return true ;
1311
1334
}
1312
1335
1313
- private string CreateMetadataContent ( string manifestFilePath , Hashtable parsedMetadata , out ErrorRecord metadataCreationError )
1336
+ private string CreateMetadataContent ( string manifestFilePath , ResourceType resourceType , Hashtable parsedMetadata , out ErrorRecord metadataCreationError )
1314
1337
{
1315
1338
metadataCreationError = null ;
1316
- Hashtable parsedMetadataHash = null ;
1317
1339
string jsonString = string . Empty ;
1318
1340
1319
- // A script will already have the metadata parsed into the parsedMetadatahash,
1320
- // a module will still need the module manifest to be parsed.
1321
1341
if ( parsedMetadata == null || parsedMetadata . Count == 0 )
1322
- {
1323
- // Use the parsed module manifest data as 'parsedMetadataHash' instead of the passed-in data.
1324
- if ( ! Utils . TryReadManifestFile (
1325
- manifestFilePath : manifestFilePath ,
1326
- manifestInfo : out parsedMetadataHash ,
1327
- error : out Exception manifestReadError ) )
1328
- {
1329
- metadataCreationError = new ErrorRecord (
1330
- manifestReadError ,
1331
- "ManifestFileReadParseForACRPublishError" ,
1332
- ErrorCategory . ReadError ,
1333
- _cmdletPassedIn ) ;
1334
-
1335
- return jsonString ;
1336
- }
1337
- }
1338
-
1339
- if ( parsedMetadataHash == null )
1340
1342
{
1341
1343
metadataCreationError = new ErrorRecord (
1342
- new InvalidOperationException ( "Error parsing package metadata into hashtable. ") ,
1343
- "PackageMetadataHashEmptyError " ,
1344
- ErrorCategory . InvalidData ,
1344
+ new ArgumentException ( "Hashtable created from .ps1 or .psd1 containing package metadata was null or empty ") ,
1345
+ "MetadataHashtableEmptyError " ,
1346
+ ErrorCategory . InvalidArgument ,
1345
1347
_cmdletPassedIn ) ;
1346
1348
1347
1349
return jsonString ;
1348
1350
}
1349
1351
1350
1352
_cmdletPassedIn . WriteVerbose ( "Serialize JSON into string." ) ;
1351
- jsonString = System . Text . Json . JsonSerializer . Serialize ( parsedMetadataHash ) ;
1353
+
1354
+ if ( parsedMetadata . ContainsKey ( "Version" ) && parsedMetadata [ "Version" ] is NuGetVersion pkgNuGetVersion )
1355
+ {
1356
+ // do not serialize NuGetVersion, this will populate more metadata than is needed and makes it harder to deserialize later
1357
+ parsedMetadata . Remove ( "Version" ) ;
1358
+ parsedMetadata [ "Version" ] = pkgNuGetVersion . ToString ( ) ;
1359
+ }
1360
+
1361
+ try
1362
+ {
1363
+ jsonString = System . Text . Json . JsonSerializer . Serialize ( parsedMetadata ) ;
1364
+ }
1365
+ catch ( Exception ex )
1366
+ {
1367
+ metadataCreationError = new ErrorRecord ( ex , "JsonSerializationError" , ErrorCategory . InvalidResult , _cmdletPassedIn ) ;
1368
+ return jsonString ;
1369
+ }
1352
1370
1353
1371
return jsonString ;
1354
1372
}
0 commit comments