|
1 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.IO;
|
3 | 4 | using System.Threading.Tasks;
|
4 | 5 | using Avalonia.Threading;
|
@@ -30,17 +31,26 @@ public override Task<bool> Sure()
|
30 | 31 | // If it is too fast, the panel will dispear very quickly, the we'll have a bad experience.
|
31 | 32 | Task.Delay(500).Wait();
|
32 | 33 |
|
| 34 | + var rootDir = new DirectoryInfo(RootDir); |
33 | 35 | var founded = new List<string>();
|
34 |
| - GetUnmanagedRepositories(new DirectoryInfo(RootDir), founded, new EnumerationOptions() |
| 36 | + GetUnmanagedRepositories(rootDir, founded, new EnumerationOptions() |
35 | 37 | {
|
36 | 38 | AttributesToSkip = FileAttributes.Hidden | FileAttributes.System,
|
37 | 39 | IgnoreInaccessible = true,
|
38 | 40 | });
|
39 | 41 |
|
40 | 42 | Dispatcher.UIThread.Invoke(() =>
|
41 | 43 | {
|
| 44 | + var normalizedRoot = rootDir.FullName.Replace("\\", "/"); |
| 45 | + var prefixLen = normalizedRoot.EndsWith('/') ? normalizedRoot.Length : normalizedRoot.Length + 1; |
| 46 | + |
42 | 47 | foreach (var f in founded)
|
43 |
| - Preference.Instance.FindOrAddNodeByRepositoryPath(f, null, false); |
| 48 | + { |
| 49 | + var relative = Path.GetDirectoryName(f).Substring(prefixLen); |
| 50 | + var group = FindOrCreateGroupRecursive(Preference.Instance.RepositoryNodes, relative); |
| 51 | + Preference.Instance.FindOrAddNodeByRepositoryPath(f, group, false); |
| 52 | + } |
| 53 | + |
44 | 54 | Welcome.Instance.Refresh();
|
45 | 55 | });
|
46 | 56 |
|
@@ -89,6 +99,45 @@ private void GetUnmanagedRepositories(DirectoryInfo dir, List<string> outs, Enum
|
89 | 99 | }
|
90 | 100 | }
|
91 | 101 |
|
| 102 | + private RepositoryNode FindOrCreateGroupRecursive(List<RepositoryNode> collection, string path) |
| 103 | + { |
| 104 | + var idx = path.IndexOf('/'); |
| 105 | + if (idx < 0) |
| 106 | + return FindOrCreateGroup(collection, path); |
| 107 | + |
| 108 | + var name = path.Substring(0, idx); |
| 109 | + var tail = path.Substring(idx + 1); |
| 110 | + var parent = FindOrCreateGroup(collection, name); |
| 111 | + return FindOrCreateGroupRecursive(parent.SubNodes, tail); |
| 112 | + } |
| 113 | + |
| 114 | + private RepositoryNode FindOrCreateGroup(List<RepositoryNode> collection, string name) |
| 115 | + { |
| 116 | + foreach (var node in collection) |
| 117 | + { |
| 118 | + if (node.Name.Equals(name, StringComparison.Ordinal)) |
| 119 | + return node; |
| 120 | + } |
| 121 | + |
| 122 | + var added = new RepositoryNode() |
| 123 | + { |
| 124 | + Id = Guid.NewGuid().ToString(), |
| 125 | + Name = name, |
| 126 | + IsRepository = false, |
| 127 | + IsExpanded = true, |
| 128 | + }; |
| 129 | + collection.Add(added); |
| 130 | + collection.Sort((l, r) => |
| 131 | + { |
| 132 | + if (l.IsRepository != r.IsRepository) |
| 133 | + return l.IsRepository ? 1 : -1; |
| 134 | + |
| 135 | + return string.Compare(l.Name, r.Name, StringComparison.Ordinal); |
| 136 | + }); |
| 137 | + |
| 138 | + return added; |
| 139 | + } |
| 140 | + |
92 | 141 | private HashSet<string> _managed = new HashSet<string>();
|
93 | 142 | }
|
94 | 143 | }
|
0 commit comments