| 1 | using CascadeIDE.Contracts; |
| 2 | using CascadeIDE.Features.Launch.DataAcquisition; |
| 3 | using CascadeIDE.Features.Workspace.Application; |
| 4 | using CascadeIDE.Models; |
| 5 | using CascadeIDE.Services; |
| 6 | |
| 7 | namespace CascadeIDE.Features.Launch.Application; |
| 8 | |
| 9 | /// <summary>Восстановление стартового <c>.csproj</c> после загрузки решения (store + единственный проект).</summary> |
| 10 | [ComputingUnit("startup project refresh after solution load")] |
| 11 | public static class StartupProjectRefreshProjection |
| 12 | { |
| 13 | public sealed record Result(string? CsprojFullPath, string ShortLabel); |
| 14 | |
| 15 | public static Result Empty => new(null, ""); |
| 16 | |
| 17 | public static Result ResolveAfterSolutionLoad( |
| 18 | string? solutionPath, |
| 19 | System.Collections.ObjectModel.ObservableCollection<SolutionItem> solutionRoots, |
| 20 | string solutionDirectory) |
| 21 | { |
| 22 | if (string.IsNullOrEmpty(solutionPath) || solutionRoots.Count == 0) |
| 23 | return Empty; |
| 24 | |
| 25 | var projects = McpSolutionTree.CollectProjectPaths(solutionRoots) |
| 26 | .Select(CanonicalFilePath.Normalize) |
| 27 | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| 28 | |
| 29 | if (StartupProjectStore.TryLoad(solutionPath, out var rel) && !string.IsNullOrEmpty(rel)) |
| 30 | { |
| 31 | var full = CanonicalFilePath.Normalize(Path.Combine(solutionDirectory, rel)); |
| 32 | if (LaunchProjectPathResolver.NormalizeExistingProjectFileFullPath(full) is not null |
| 33 | && projects.Contains(full)) |
| 34 | return new Result(full, Path.GetFileNameWithoutExtension(full)); |
| 35 | |
| 36 | StartupProjectStore.Clear(solutionPath); |
| 37 | } |
| 38 | |
| 39 | return TryDefaultSingleManagedProject(solutionPath, solutionDirectory, solutionRoots, projects); |
| 40 | } |
| 41 | |
| 42 | private static Result TryDefaultSingleManagedProject( |
| 43 | string solutionPath, |
| 44 | string solutionDirectory, |
| 45 | System.Collections.ObjectModel.ObservableCollection<SolutionItem> solutionRoots, |
| 46 | HashSet<string> projectPathSet) |
| 47 | { |
| 48 | var csprojs = McpSolutionTree.CollectDistinctManagedProjectPaths(solutionRoots); |
| 49 | if (csprojs.Count != 1) |
| 50 | return Empty; |
| 51 | |
| 52 | var only = csprojs[0]; |
| 53 | if (!projectPathSet.Contains(only)) |
| 54 | return Empty; |
| 55 | |
| 56 | if (!LaunchProjectRelativePath.TryGetRelativeToSolutionDirectory(solutionDirectory, only, out var rel, out _)) |
| 57 | return Empty; |
| 58 | |
| 59 | try |
| 60 | { |
| 61 | StartupProjectStore.Save(solutionPath, rel); |
| 62 | } |
| 63 | catch |
| 64 | { |
| 65 | // сохранение опционально — в памяти стартовый проект всё равно будет |
| 66 | } |
| 67 | |
| 68 | return new Result(only, Path.GetFileNameWithoutExtension(only)); |
| 69 | } |
| 70 | } |
| 71 | |