Skip to content

Commit fba448b

Browse files
thomhurstclaude
andauthored
refactor: Use XML parsing instead of string parsing for project dependencies (#1645) (#1713)
* refactor: Use XML parsing instead of string parsing for project dependencies Replace fragile string parsing with proper XML parsing using XDocument to extract ProjectReference elements from .csproj files. Benefits: - More robust and reliable parsing - Handles all formatting variations correctly - Platform-independent path handling with Path.GetFileName - Follows the same pattern used elsewhere in the codebase Fixes #1645 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: Dispose FileStream properly and use OfType<string> - Fix resource leak by wrapping FileStream in await using statement - Use .OfType<string>() instead of .Where(v => v != null) for better null handling and type safety 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: Use ProjectRootElement instead of XDocument for consistency Replace XDocument-based XML parsing with ProjectRootElement from Microsoft.Build.Construction, matching the existing pattern used in GenerateReadMeModule.cs. This provides a more idiomatic approach for parsing MSBuild project files. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d6158a3 commit fba448b

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

src/ModularPipelines.Build/Modules/FindProjectDependenciesModule.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Build.Construction;
12
using Microsoft.Extensions.Logging;
23
using ModularPipelines.Attributes;
34
using ModularPipelines.Context;
@@ -9,23 +10,23 @@ namespace ModularPipelines.Build.Modules;
910
[DependsOn<FindProjectsModule>]
1011
public class FindProjectDependenciesModule : Module<FindProjectDependenciesModule.ProjectDependencies>
1112
{
12-
public override async Task<ProjectDependencies?> ExecuteAsync(IModuleContext context, CancellationToken cancellationToken)
13+
public override Task<ProjectDependencies?> ExecuteAsync(IModuleContext context, CancellationToken cancellationToken)
1314
{
1415
var projects = context.GetModule<FindProjectsModule, IReadOnlyList<File>>();
1516

1617
var dependencies = new List<File>();
1718

1819
foreach (var file in projects.Value!)
1920
{
20-
await foreach (var line in file.ReadLinesAsync(cancellationToken))
21-
{
22-
if (!line.Contains("<ProjectReference"))
23-
{
24-
continue;
25-
}
21+
var projectRootElement = ProjectRootElement.Open(file)!;
2622

27-
var name = line.Split('\\').Last().Split('"').First();
23+
var projectReferences = projectRootElement.Items
24+
.Where(i => i.ItemType == "ProjectReference")
25+
.Select(i => i.Include);
2826

27+
foreach (var reference in projectReferences)
28+
{
29+
var name = Path.GetFileName(reference);
2930
var project = projects.Value!.FirstOrDefault(x => x.Name == name);
3031

3132
if (project != null)
@@ -39,7 +40,7 @@ public class FindProjectDependenciesModule : Module<FindProjectDependenciesModul
3940

4041
LogProjects(context, projectDependencies);
4142

42-
return projectDependencies;
43+
return Task.FromResult<ProjectDependencies?>(projectDependencies);
4344
}
4445

4546
private static void LogProjects(IModuleContext context, ProjectDependencies projectDependencies)

0 commit comments

Comments
 (0)