Skip to content

Commit 3c21adb

Browse files
Remove cgmanifest.json and save packages dynamically (#49538)
1 parent 501fcef commit 3c21adb

File tree

2 files changed

+87
-96
lines changed

2 files changed

+87
-96
lines changed

eng/cgmanifest.json

Lines changed: 0 additions & 96 deletions
This file was deleted.

test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Immutable;
5+
using System.Text.Json;
6+
using System.Text.RegularExpressions;
57

68
namespace Microsoft.DotNet.Cli.New.IntegrationTests
79
{
@@ -27,6 +29,8 @@ private static readonly (string ProjectTemplateName, string[] Languages, bool Ru
2729
("nunit-playwright", new[] { Languages.CSharp }, false, false),
2830
];
2931

32+
private static readonly string PackagesJsonPath = Path.Combine(CodeBaseRoot, "test", "component-governance", "packages.json");
33+
3034
public DotnetNewTestTemplatesTests(ITestOutputHelper log) : base(log)
3135
{
3236
_log = log;
@@ -119,8 +123,12 @@ public void ItemTemplate_CanBeInstalledAndTestArePassing(string targetFramework,
119123
// Therefore, in total we would have 2.
120124
result.StdOut.Should().MatchRegex(@"Passed:\s*2");
121125

126+
// After executing dotnet new and before cleaning up
127+
RecordPackages(outputDirectory);
128+
122129
Directory.Delete(outputDirectory, true);
123130
Directory.Delete(workingDirectory, true);
131+
124132
}
125133

126134
[Theory]
@@ -152,6 +160,9 @@ public void ProjectTemplate_CanBeInstalledAndTestsArePassing(string targetFramew
152160
result.StdOut.Should().MatchRegex(@"Passed:\s*1");
153161
}
154162

163+
// After executing dotnet new and before cleaning up
164+
RecordPackages(outputDirectory);
165+
155166
Directory.Delete(outputDirectory, true);
156167
Directory.Delete(workingDirectory, true);
157168
}
@@ -191,6 +202,9 @@ public void MSTestAndPlaywrightProjectTemplate_WithCoverageToolAndTestRunner_Can
191202
result.StdOut.Should().MatchRegex(@"Passed:\s*1");
192203
}
193204

205+
// After executing dotnet new and before cleaning up
206+
RecordPackages(outputDirectory);
207+
194208
Directory.Delete(outputDirectory, true);
195209
Directory.Delete(workingDirectory, true);
196210
}
@@ -204,6 +218,79 @@ private void AddItemToFsproj(string itemName, string outputDirectory, string pro
204218
File.WriteAllLines(fsproj, lines);
205219
}
206220

221+
private void RecordPackages(string projectDirectory)
222+
{
223+
// Get all project files with a single directory search, then filter to specific types
224+
var projectFiles = Directory.GetFiles(projectDirectory, "*.*proj")
225+
.Where(file => file.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) ||
226+
file.EndsWith(".fsproj", StringComparison.OrdinalIgnoreCase) ||
227+
file.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase));
228+
229+
Dictionary<string, string> packageVersions =
230+
[];
231+
232+
// Load existing package versions if file exists
233+
if (File.Exists(PackagesJsonPath))
234+
{
235+
try
236+
{
237+
packageVersions = JsonSerializer.Deserialize<Dictionary<string, string>>(
238+
File.ReadAllText(PackagesJsonPath)) ??
239+
[];
240+
}
241+
catch (Exception ex)
242+
{
243+
_log.WriteLine($"Warning: Could not parse existing packages.json: {ex.Message}");
244+
}
245+
}
246+
247+
// Extract package references from project files
248+
foreach (var projectFile in projectFiles)
249+
{
250+
string content = File.ReadAllText(projectFile);
251+
var packageRefMatches = Regex.Matches(
252+
content,
253+
@"<PackageReference\s+(?:Include=""([^""]+)""\s+Version=""([^""]+)""|Version=""([^""]+)""\s+Include=""([^""]+)"")",
254+
RegexOptions.IgnoreCase);
255+
256+
foreach (Match match in packageRefMatches)
257+
{
258+
string packageId;
259+
string version;
260+
261+
if (!string.IsNullOrEmpty(match.Groups[1].Value))
262+
{
263+
// Include first, then Version
264+
packageId = match.Groups[1].Value;
265+
version = match.Groups[2].Value;
266+
}
267+
else
268+
{
269+
// Version first, then Include
270+
packageId = match.Groups[4].Value;
271+
version = match.Groups[3].Value;
272+
}
273+
274+
packageVersions[packageId] = version;
275+
}
276+
}
277+
278+
// Ensure directory exists
279+
if (Path.GetDirectoryName(PackagesJsonPath) is string directoryPath)
280+
{
281+
Directory.CreateDirectory(directoryPath);
282+
}
283+
else
284+
{
285+
_log.WriteLine($"Warning: Could not determine directory path for '{PackagesJsonPath}'.");
286+
}
287+
288+
// Write updated packages.json
289+
File.WriteAllText(
290+
PackagesJsonPath,
291+
JsonSerializer.Serialize(packageVersions, new JsonSerializerOptions { WriteIndented = true }));
292+
}
293+
207294
private static string GenerateTestProjectName()
208295
{
209296
// Avoiding VB errors because root namespace must not start with number or contain dashes

0 commit comments

Comments
 (0)