3
3
4
4
using System . Collections . Immutable ;
5
5
using System . Text . Json ;
6
+ using System . Text . Json . Serialization ;
6
7
using System . Text . RegularExpressions ;
7
8
8
9
namespace Microsoft . DotNet . Cli . New . IntegrationTests
@@ -29,7 +30,7 @@ private static readonly (string ProjectTemplateName, string[] Languages, bool Ru
29
30
( "nunit-playwright" , new [ ] { Languages . CSharp } , false , false ) ,
30
31
] ;
31
32
32
- private static readonly string PackagesJsonPath = Path . Combine ( CodeBaseRoot , "test" , "component-governance " , "packages .json" ) ;
33
+ private static readonly string PackagesJsonPath = Path . Combine ( CodeBaseRoot , "test" , "TestPackages " , "cgmanifest .json" ) ;
33
34
34
35
public DotnetNewTestTemplatesTests ( ITestOutputHelper log ) : base ( log )
35
36
{
@@ -226,23 +227,32 @@ private void RecordPackages(string projectDirectory)
226
227
file . EndsWith ( ".fsproj" , StringComparison . OrdinalIgnoreCase ) ||
227
228
file . EndsWith ( ".vbproj" , StringComparison . OrdinalIgnoreCase ) ) ;
228
229
229
- Dictionary < string , string > packageVersions =
230
- [ ] ;
230
+ // Load existing component detection manifest or create new one
231
+ ComponentDetectionManifest manifest ;
231
232
232
- // Load existing package versions if file exists
233
233
if ( File . Exists ( PackagesJsonPath ) )
234
234
{
235
235
try
236
236
{
237
- packageVersions = JsonSerializer . Deserialize < Dictionary < string , string > > (
238
- File . ReadAllText ( PackagesJsonPath ) ) ??
239
- [ ] ;
237
+ string jsonContent = File . ReadAllText ( PackagesJsonPath ) ;
238
+ manifest = JsonSerializer . Deserialize < ComponentDetectionManifest > ( jsonContent ) ??
239
+ CreateNewManifest ( ) ;
240
240
}
241
241
catch ( Exception ex )
242
242
{
243
- _log . WriteLine ( $ "Warning: Could not parse existing packages.json: { ex . Message } ") ;
243
+ _log . WriteLine ( $ "Warning: Could not parse existing component detection manifest: { ex . Message } ") ;
244
+ // Don't create a new manifest when we can't parse the existing one
245
+ // This prevents overwriting the existing file with an empty manifest
246
+ return ;
244
247
}
245
248
}
249
+ else
250
+ {
251
+ manifest = CreateNewManifest ( ) ;
252
+ }
253
+
254
+ // Keep track of whether we added anything new
255
+ bool updatedManifest = false ;
246
256
247
257
// Extract package references from project files
248
258
foreach ( var projectFile in projectFiles )
@@ -271,24 +281,109 @@ private void RecordPackages(string projectDirectory)
271
281
version = match . Groups [ 3 ] . Value ;
272
282
}
273
283
274
- packageVersions [ packageId ] = version ;
284
+ // Find existing registration for this package or null if not found
285
+ var existingRegistration = manifest . Registrations ? . FirstOrDefault ( r =>
286
+ r . Component != null &&
287
+ r . Component . Nuget != null &&
288
+ string . Equals ( r . Component . Nuget . Name , packageId , StringComparison . OrdinalIgnoreCase ) ) ;
289
+
290
+ if ( existingRegistration == null )
291
+ {
292
+ // Add new package if it doesn't exist
293
+ manifest . Registrations ? . Add ( new Registration
294
+ {
295
+ Component = new Component
296
+ {
297
+ Type = "nuget" ,
298
+ Nuget = new NugetComponent
299
+ {
300
+ Name = packageId ,
301
+ Version = version
302
+ }
303
+ }
304
+ } ) ;
305
+ updatedManifest = true ;
306
+ }
307
+ else if ( existingRegistration . Component ? . Nuget ? . Version != version )
308
+ {
309
+ // Update version if it's different from the existing one
310
+ existingRegistration . Component ? . Nuget ? . Version = version;
311
+ updatedManifest = true;
312
+ }
275
313
}
276
314
}
277
315
278
- // Ensure directory exists
279
- if ( Path . GetDirectoryName ( PackagesJsonPath ) is string directoryPath )
316
+ // Only write the file if we actually added something new
317
+ if ( updatedManifest )
280
318
{
281
- Directory . CreateDirectory ( directoryPath ) ;
319
+ // Ensure directory exists
320
+ if ( Path . GetDirectoryName ( PackagesJsonPath ) is string directoryPath )
321
+ {
322
+ Directory . CreateDirectory ( directoryPath ) ;
323
+ }
324
+ else
325
+ {
326
+ _log . WriteLine ( $ "Warning: Could not determine directory path for '{ PackagesJsonPath } '.") ;
327
+ return ;
328
+ }
329
+
330
+ // Write updated manifest
331
+ File . WriteAllText (
332
+ PackagesJsonPath ,
333
+ JsonSerializer . Serialize ( manifest , new JsonSerializerOptions
334
+ {
335
+ WriteIndented = true ,
336
+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase
337
+ } ) ) ;
282
338
}
283
- else
339
+ }
340
+
341
+ private ComponentDetectionManifest CreateNewManifest ( )
342
+ {
343
+ return new ComponentDetectionManifest
284
344
{
285
- _log . WriteLine ( $ "Warning: Could not determine directory path for '{ PackagesJsonPath } '.") ;
286
- }
345
+ Schema = "https://json.schemastore.org/component-detection-manifest.json" ,
346
+ Version = 1 ,
347
+ Registrations =
348
+ [ ]
349
+ } ;
350
+ }
351
+
352
+ // Classes to model the component detection manifest
353
+ private class ComponentDetectionManifest
354
+ {
355
+ [ JsonPropertyName ( "$schema" ) ]
356
+ public string ? Schema { get ; set ; }
357
+
358
+ [ JsonPropertyName ( "version" ) ]
359
+ public int Version { get ; set ; }
360
+
361
+ [ JsonPropertyName ( "registrations" ) ]
362
+ public List < Registration > ? Registrations { get ; set ; }
363
+ }
364
+
365
+ private class Registration
366
+ {
367
+ [ JsonPropertyName ( "component" ) ]
368
+ public Component ? Component { get ; set ; }
369
+ }
370
+
371
+ private class Component
372
+ {
373
+ [ JsonPropertyName ( "type" ) ]
374
+ public string ? Type { get ; set ; }
375
+
376
+ [ JsonPropertyName ( "nuget" ) ]
377
+ public NugetComponent ? Nuget { get ; set ; }
378
+ }
379
+
380
+ private class NugetComponent
381
+ {
382
+ [ JsonPropertyName ( "name" ) ]
383
+ public string ? Name { get ; set ; }
287
384
288
- // Write updated packages.json
289
- File . WriteAllText (
290
- PackagesJsonPath ,
291
- JsonSerializer . Serialize ( packageVersions , new JsonSerializerOptions { WriteIndented = true } ) ) ;
385
+ [ JsonPropertyName ( "version" ) ]
386
+ public string ? Version { get ; set ; }
292
387
}
293
388
294
389
private static string GenerateTestProjectName ( )
0 commit comments