| 1 | #nullable enable |
| 2 | using System.Collections.ObjectModel; |
| 3 | using CascadeIDE.Contracts; |
| 4 | using CascadeIDE.Features.Workspace.Application; |
| 5 | using CascadeIDE.Models; |
| 6 | |
| 7 | namespace CascadeIDE.Features.Launch.Application; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Эвристика стартового <c>.csproj</c> перед F5, если в памяти путь пуст или файл не найден на диске |
| 11 | /// (<see cref="McpSolutionTree"/> · обозреватель решения). |
| 12 | /// </summary> |
| 13 | [ComputingUnit("startup-project-f5-infer")] |
| 14 | public static class StartupProjectDebugInferenceProjection |
| 15 | { |
| 16 | public static bool HasPersistedStartupPointingToExistingFile(string? startupCsprojFullPath) => |
| 17 | !string.IsNullOrEmpty(startupCsprojFullPath) && File.Exists(startupCsprojFullPath); |
| 18 | |
| 19 | /// <summary> |
| 20 | /// Подбирает полный канонический путь к управляемому проекту или <see langword="null"/>. |
| 21 | /// </summary> |
| 22 | public static string? TryInferCanonicalCsproj( |
| 23 | ObservableCollection<SolutionItem> solutionRoots, |
| 24 | string? currentFilePath, |
| 25 | string? selectedSolutionItemFullPath) |
| 26 | { |
| 27 | if (solutionRoots.Count == 0) |
| 28 | return null; |
| 29 | |
| 30 | var csprojs = McpSolutionTree.CollectDistinctManagedProjectPaths(solutionRoots); |
| 31 | var set = csprojs.ToHashSet(StringComparer.OrdinalIgnoreCase); |
| 32 | |
| 33 | if (csprojs.Count == 1) |
| 34 | return csprojs[0]; |
| 35 | |
| 36 | if (!string.IsNullOrEmpty(currentFilePath)) |
| 37 | { |
| 38 | try |
| 39 | { |
| 40 | var full = CanonicalFilePath.Normalize(currentFilePath); |
| 41 | if (File.Exists(full) && !McpSolutionTree.IsBuildArtifactPath(full)) |
| 42 | { |
| 43 | if (McpSolutionTree.MapFileToProject(solutionRoots).TryGetValue(full, out var treeProj) && |
| 44 | !string.IsNullOrEmpty(treeProj) && set.Contains(treeProj)) |
| 45 | return treeProj; |
| 46 | |
| 47 | var disk = McpSolutionTree.ResolveOwningProjectPath(full); |
| 48 | if (!string.IsNullOrEmpty(disk) && set.Contains(disk)) |
| 49 | return disk; |
| 50 | } |
| 51 | } |
| 52 | catch |
| 53 | { |
| 54 | // как в VM: тихо, идём к выбору из обозревателя |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | var sel = selectedSolutionItemFullPath; |
| 59 | if (!string.IsNullOrEmpty(sel) && |
| 60 | (sel.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) || |
| 61 | sel.EndsWith(".fsproj", StringComparison.OrdinalIgnoreCase)) && |
| 62 | File.Exists(sel)) |
| 63 | { |
| 64 | var p = CanonicalFilePath.Normalize(sel); |
| 65 | if (set.Contains(p)) |
| 66 | return p; |
| 67 | } |
| 68 | |
| 69 | return null; |
| 70 | } |
| 71 | } |
| 72 | |