|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: Core.Apps.Development |
| 4 | + * FILE: DependencyExplorer.cs |
| 5 | + * PURPOSE: Maps project-to-project and project-to-library dependencies. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.IO; |
| 11 | +using System.Linq; |
| 12 | +using System.Text; |
| 13 | +using System.Xml.Linq; |
| 14 | +using Weaver; |
| 15 | +using Weaver.Interfaces; |
| 16 | +using Weaver.Messages; |
| 17 | +using Weaver.Registry; |
| 18 | + |
| 19 | +namespace Core.Apps.Development |
| 20 | +{ |
| 21 | + public sealed class DependencyExplorer : ICommand, IRegistryProducer |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// The variables |
| 25 | + /// </summary> |
| 26 | + private readonly IVariableRegistry _variables; |
| 27 | + |
| 28 | + /// <inheritdoc /> |
| 29 | + public string CurrentRegistryKey => "project_map"; |
| 30 | + |
| 31 | + /// <inheritdoc /> |
| 32 | + public EnumTypes DataType => EnumTypes.Wobject; |
| 33 | + |
| 34 | + /// <inheritdoc /> |
| 35 | + public IVariableRegistry Variables => _variables; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Initializes a new instance of the <see cref="DependencyExplorer"/> class. |
| 39 | + /// </summary> |
| 40 | + /// <param name="variables">The variables.</param> |
| 41 | + public DependencyExplorer(IVariableRegistry variables) |
| 42 | + { |
| 43 | + _variables = variables; |
| 44 | + } |
| 45 | + |
| 46 | + /// <inheritdoc /> |
| 47 | + public string Name => "depexplore"; |
| 48 | + |
| 49 | + /// <inheritdoc /> |
| 50 | + public string Namespace => "Development"; |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + public int ParameterCount => 1; |
| 54 | + |
| 55 | + /// <inheritdoc /> |
| 56 | + public string Description => "Explores project dependencies and maps them."; |
| 57 | + |
| 58 | + /// <inheritdoc /> |
| 59 | + public CommandSignature Signature => new(Namespace, Name, ParameterCount); |
| 60 | + |
| 61 | + /// <inheritdoc /> |
| 62 | + public CommandResult Execute(params string[] args) |
| 63 | + { |
| 64 | + if (args.Length < 1) return CommandResult.Fail("Usage: depexplore <root_folder>"); |
| 65 | + |
| 66 | + var rootPath = args[0]; |
| 67 | + if (!Directory.Exists(rootPath)) return CommandResult.Fail("Folder not found."); |
| 68 | + |
| 69 | + var projectMap = new Dictionary<string, VmValue>(); |
| 70 | + var sb = new StringBuilder(); |
| 71 | + |
| 72 | + // 1. Find all .csproj files |
| 73 | + var projects = Directory.EnumerateFiles(rootPath, "*.csproj", SearchOption.AllDirectories); |
| 74 | + |
| 75 | + foreach (var projFile in projects) |
| 76 | + { |
| 77 | + var projName = Path.GetFileNameWithoutExtension(projFile); |
| 78 | + var doc = XDocument.Load(projFile); |
| 79 | + |
| 80 | + // 2. Extract NuGet Packages |
| 81 | + var packages = doc.Descendants("PackageReference") |
| 82 | + .Select(x => x.Attribute("Include")?.Value) |
| 83 | + .Where(v => v != null) |
| 84 | + .ToList(); |
| 85 | + |
| 86 | + // 3. Extract Project References |
| 87 | + var refs = doc.Descendants("ProjectReference") |
| 88 | + .Select(x => Path.GetFileNameWithoutExtension(x.Attribute("Include")?.Value ?? "")) |
| 89 | + .Where(v => !string.IsNullOrEmpty(v)) |
| 90 | + .ToList(); |
| 91 | + |
| 92 | + // 4. Create a Sub-Object for this project |
| 93 | + var details = new Dictionary<string, VmValue> |
| 94 | + { |
| 95 | + { "packages", VmValue.FromString(string.Join(", ", packages)) }, |
| 96 | + { "references", VmValue.FromString(string.Join(", ", refs)) } |
| 97 | + }; |
| 98 | + |
| 99 | + projectMap[projName] = VmValue.FromObject(); // In your registry, this links to the dict |
| 100 | + |
| 101 | + sb.AppendLine($"Project: {projName}"); |
| 102 | + sb.AppendLine($" - Libraries: {string.Join(", ", packages)}"); |
| 103 | + sb.AppendLine($" - Projects: {string.Join(", ", refs)}"); |
| 104 | + sb.AppendLine(); |
| 105 | + } |
| 106 | + |
| 107 | + // 5. Store in the Registry for WPF or Scripts to use |
| 108 | + _variables.SetObject(CurrentRegistryKey, projectMap); |
| 109 | + |
| 110 | + return CommandResult.Ok(sb.ToString(), CurrentRegistryKey, EnumTypes.Wobject); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments