-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathPackageDocumentationManager.cs
204 lines (178 loc) · 7.67 KB
/
PackageDocumentationManager.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Dynamo.Interfaces;
using Dynamo.Logging;
namespace Dynamo.DocumentationBrowser
{
/// <summary>
/// Creates a lookup for node annotation markdown files associated with a package.
/// Also creates file watchers for the package node annotation paths so new files can be hot reloaded in Dynamo.
/// </summary>
public class PackageDocumentationManager : IDisposable
{
/// <summary>
/// Dictionary to lookup node documentation markdown files with the node namespace.
/// key: node namespace - value: file path to markdown file.
/// </summary>
private Dictionary<string, string> nodeDocumentationFileLookup = new Dictionary<string, string>();
/// <summary>
/// Dictionary to keep track of each FileSystemWatcher created for the package node annotation directory path
/// key: directory path - value: FileSystemWatcher
/// </summary>
private Dictionary<string, FileSystemWatcher> markdownFileWatchers = new Dictionary<string, FileSystemWatcher>();
private const string VALID_DOC_FILEEXTENSION = "*.md";
private const string FALLBACK_DOC_DIRECTORY_NAME = "fallback_docs";
private static PackageDocumentationManager instance;
//these fields should only be directly set by tests.
internal DirectoryInfo dynamoCoreFallbackDocPath;
internal DirectoryInfo hostDynamoFallbackDocPath;
/// <summary>
/// PackageDocManager singleton instance.
/// </summary>
public static PackageDocumentationManager Instance
{
get
{
if (instance is null) { instance = new PackageDocumentationManager(); }
return instance;
}
}
internal Action<ILogMessage> MessageLogged;
/// <summary>
/// Uses the path manager to set the fallback_doc path for DynamoCore and any Host running
/// </summary>
/// <param name="pathManager"></param>
internal void AddDynamoPaths(IPathManager pathManager)
{
if (pathManager is null)
{
return;
}
if (!string.IsNullOrEmpty(pathManager.DynamoCoreDirectory))
{
var coreDir = new DirectoryInfo(Path.Combine(pathManager.DynamoCoreDirectory, FALLBACK_DOC_DIRECTORY_NAME));
dynamoCoreFallbackDocPath = coreDir.Exists ? coreDir : null;
}
if (!string.IsNullOrEmpty(pathManager.HostApplicationDirectory))
{
var hostDir = new DirectoryInfo(Path.Combine(pathManager.HostApplicationDirectory, FALLBACK_DOC_DIRECTORY_NAME));
hostDynamoFallbackDocPath = hostDir.Exists ? hostDir : null;
}
}
private PackageDocumentationManager() { }
/// <summary>
/// Retrieves the markdown node documentation file associated with the input node namespace
/// </summary>
/// <param name="nodeNamespace">Namespace of the node to lookup documentation for</param>
/// <returns></returns>
public string GetAnnotationDoc(string nodeNamespace, string packageName)
{
if(nodeDocumentationFileLookup
.TryGetValue(Path.Combine(packageName, nodeNamespace),
out string output))
{
return output;
}
FileInfo matchingDoc = null;
if (hostDynamoFallbackDocPath != null)
{
matchingDoc = hostDynamoFallbackDocPath.GetFiles($"{nodeNamespace}.md").FirstOrDefault();
if (matchingDoc != null)
{
return matchingDoc.FullName;
}
}
if (dynamoCoreFallbackDocPath != null)
{
matchingDoc = dynamoCoreFallbackDocPath.GetFiles($"{nodeNamespace}.md").FirstOrDefault();
}
return matchingDoc is null ? string.Empty : matchingDoc.FullName;
}
/// <summary>
/// Checks if the nodeNamespace has a documentation markdown associated.
/// </summary>
/// <param name="nodeNamespace"></param>
/// <returns></returns>
public bool ContainsAnnotationDoc(string nodeNamespace)
{
return nodeDocumentationFileLookup.ContainsKey(nodeNamespace);
}
/// <summary>
/// Add package node annotation markdown files to the node annotation lookup.
/// Note this only works for Markdown files.
/// </summary>
/// <param name="package"></param>
internal void AddPackageDocumentation(string packageDocumentationPath, string packageName)
{
if (string.IsNullOrWhiteSpace(packageDocumentationPath))
return;
var directoryInfo = new DirectoryInfo(packageDocumentationPath);
if (!directoryInfo.Exists)
return;
MonitorDirectory(directoryInfo);
var files = directoryInfo.GetFiles(VALID_DOC_FILEEXTENSION, SearchOption.AllDirectories);
TrackDocumentationFiles(files, packageName);
}
private void MonitorDirectory(DirectoryInfo directoryInfo)
{
if (markdownFileWatchers.ContainsKey(directoryInfo.FullName))
return;
var watcher = new FileSystemWatcher(directoryInfo.FullName, VALID_DOC_FILEEXTENSION) { EnableRaisingEvents = true };
watcher.Renamed += OnFileRenamed;
watcher.Deleted += OnFileDeleted;
watcher.Created += OnFileCreated;
markdownFileWatchers.Add(watcher.Path, watcher);
}
private void TrackDocumentationFiles(FileInfo[] files, string packageName)
{
foreach (var file in files)
{
try
{
nodeDocumentationFileLookup.Add(Path.Combine(packageName,Path.GetFileNameWithoutExtension(file.Name)), file.FullName);
}
catch (Exception e)
{
LogWarning(e.Message, WarningLevel.Error);
}
}
}
private void OnFileCreated(object sender, FileSystemEventArgs e)
{
nodeDocumentationFileLookup.Add(Path.GetFileNameWithoutExtension(e.Name), e.FullPath);
}
private void OnFileDeleted(object sender, FileSystemEventArgs e)
{
var fileName = Path.GetFileNameWithoutExtension(e.Name);
if (nodeDocumentationFileLookup.ContainsKey(fileName))
{
nodeDocumentationFileLookup.Remove(fileName);
}
}
private void OnFileRenamed(object sender, RenamedEventArgs e)
{
var oldFileName = Path.GetFileNameWithoutExtension(e.OldName);
if (nodeDocumentationFileLookup.ContainsKey(oldFileName))
{
nodeDocumentationFileLookup.Remove(oldFileName);
}
var newFileName = Path.GetFileNameWithoutExtension(e.Name);
if (!nodeDocumentationFileLookup.ContainsKey(newFileName))
{
nodeDocumentationFileLookup.Add(newFileName, e.FullPath);
}
}
private void LogWarning(string msg, WarningLevel level) => this.MessageLogged?.Invoke(LogMessage.Warning(msg, level));
public void Dispose()
{
foreach (var watcher in markdownFileWatchers.Values)
{
watcher.Renamed -= OnFileRenamed;
watcher.Deleted -= OnFileDeleted;
watcher.Created -= OnFileCreated;
}
}
}
}