-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFindProjectDependenciesModule.cs
More file actions
60 lines (47 loc) · 2.16 KB
/
FindProjectDependenciesModule.cs
File metadata and controls
60 lines (47 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Microsoft.Build.Construction;
using Microsoft.Extensions.Logging;
using ModularPipelines.Attributes;
using ModularPipelines.Context;
using ModularPipelines.Modules;
using File = ModularPipelines.FileSystem.File;
namespace ModularPipelines.Build.Modules;
[DependsOn<FindProjectsModule>]
public class FindProjectDependenciesModule : Module<FindProjectDependenciesModule.ProjectDependencies>
{
public override Task<ProjectDependencies?> ExecuteAsync(IModuleContext context, CancellationToken cancellationToken)
{
var projects = context.GetModule<FindProjectsModule, IReadOnlyList<File>>();
var dependencies = new List<File>();
foreach (var file in projects.Value!)
{
var projectRootElement = ProjectRootElement.Open(file)!;
var projectReferences = projectRootElement.Items
.Where(i => i.ItemType == "ProjectReference")
.Select(i => i.Include);
foreach (var reference in projectReferences)
{
var name = Path.GetFileName(reference);
var project = projects.Value!.FirstOrDefault(x => x.Name == name);
if (project != null)
{
dependencies.Add(project);
}
}
}
var projectDependencies = new ProjectDependencies(Dependencies: dependencies.Distinct().ToList(), Others: projects.Value!.Except(dependencies).Distinct().ToList());
LogProjects(context, projectDependencies);
return Task.FromResult<ProjectDependencies?>(projectDependencies);
}
private static void LogProjects(IModuleContext context, ProjectDependencies projectDependencies)
{
foreach (var project in projectDependencies.Dependencies)
{
context.Logger.LogInformation("Project {Project} is a Dependency of other projects", project);
}
foreach (var project in projectDependencies.Others)
{
context.Logger.LogInformation("Project {Project} is a NOT Dependency of other projects", project);
}
}
public record ProjectDependencies(IReadOnlyList<File> Dependencies, IReadOnlyList<File> Others);
}