| | | 1 | | using pva.Helpers.Extensions; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using pva.SuperV.Engine.FieldFormatters; |
| | | 4 | | using pva.SuperV.Engine.HistoryStorage; |
| | | 5 | | using System.Collections.Concurrent; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | |
| | | 8 | | namespace pva.SuperV.Engine |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// SuperV Project class. It contains all the information required (<see cref="Class"/>, <see cref="Instance"/>, pro |
| | | 12 | | /// </summary> |
| | | 13 | | public abstract class Project : IDisposable |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the projects path where all SuperV stuff is stored (generated assemblies, project definitions and snaps |
| | | 17 | | /// </summary> |
| | | 18 | | /// <value> |
| | | 19 | | /// The projects path. |
| | | 20 | | /// </value> |
| | 15819 | 21 | | public static string ProjectsPath { get; } = Path.Combine(Path.GetTempPath(), "pva.SuperV"); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets or sets the current project. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <value> |
| | | 27 | | /// The current project. |
| | | 28 | | /// </value> |
| | 0 | 29 | | public static Project? CurrentProject { get; set; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The name of project. Use <see cref="Name"/> to access its content. |
| | | 33 | | /// </summary> |
| | | 34 | | private string? _name; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets the name of the project. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <value> |
| | | 40 | | /// The project name. |
| | | 41 | | /// </value> |
| | | 42 | | public string? Name |
| | | 43 | | { |
| | 3704 | 44 | | get => _name; |
| | | 45 | | set |
| | 415 | 46 | | { |
| | 415 | 47 | | IdentifierValidation.ValidateIdentifier("project", value); |
| | 412 | 48 | | _name = value; |
| | 412 | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets or sets the description. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <value> |
| | | 56 | | /// The description. |
| | | 57 | | /// </value> |
| | 367 | 58 | | public string? Description { get; set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets or sets the version. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <value> |
| | | 64 | | /// The version. |
| | | 65 | | /// </value> |
| | 2068 | 66 | | public int Version { get; set; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the classes of project. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <value> |
| | | 72 | | /// The classes. |
| | | 73 | | /// </value> |
| | 6265 | 74 | | public Dictionary<string, Class> Classes { get; init; } = new(StringComparer.OrdinalIgnoreCase); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the field formatters. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <value> |
| | | 80 | | /// The field formatters. |
| | | 81 | | /// </value> |
| | 1903 | 82 | | public Dictionary<string, FieldFormatter> FieldFormatters { get; init; } = new(StringComparer.OrdinalIgnoreCase) |
| | | 83 | | |
| | | 84 | | private IHistoryStorageEngine? historyStorageEngine; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// The history storage engin connection string. |
| | | 88 | | /// </summary> |
| | 683 | 89 | | public string? HistoryStorageEngineConnectionString { get; set; } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets the history repositories. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <value> |
| | | 95 | | /// The history repositories. |
| | | 96 | | /// </value> |
| | | 97 | | [JsonIgnore] |
| | | 98 | | public IHistoryStorageEngine? HistoryStorageEngine |
| | | 99 | | { |
| | 591 | 100 | | get => historyStorageEngine; |
| | | 101 | | set |
| | 359 | 102 | | { |
| | 359 | 103 | | historyStorageEngine = value; |
| | 360 | 104 | | HistoryRepositories.Values.ForEach(historyRepository => historyRepository.HistoryStorageEngine = history |
| | 359 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the history repositories. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <value> |
| | | 112 | | /// The history repositories. |
| | | 113 | | /// </value> |
| | 2020 | 114 | | public Dictionary<string, HistoryRepository> HistoryRepositories { get; init; } = new(StringComparer.OrdinalIgno |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// List of projects in use. |
| | | 118 | | /// </summary> |
| | 1762 | 119 | | public static ConcurrentDictionary<string, Project> Projects { get; } = new(StringComparer.OrdinalIgnoreCase); |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Creates an empty <see cref="WipProject"/>. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="projectName">Name of the project.</param> |
| | | 125 | | /// <returns>The created <see cref="WipProject"/></returns> |
| | | 126 | | public static WipProject CreateProject(string projectName) |
| | 92 | 127 | | { |
| | 92 | 128 | | return CreateProject(projectName, null); |
| | 89 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Creates an empty <see cref="WipProject"/>. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="projectName">Name of the project.</param> |
| | | 135 | | /// <param name="historyStorageEngineConnectionString">History storage connection string.</param> |
| | | 136 | | /// <returns>The created <see cref="WipProject"/></returns> |
| | | 137 | | public static WipProject CreateProject(string projectName, string? historyStorageEngineConnectionString) |
| | 239 | 138 | | { |
| | 239 | 139 | | WipProject project = new(projectName); |
| | 236 | 140 | | AddProjectToCollection(project); |
| | 236 | 141 | | if (string.IsNullOrEmpty(historyStorageEngineConnectionString)) |
| | 91 | 142 | | { |
| | 91 | 143 | | return project; |
| | | 144 | | } |
| | | 145 | | |
| | 145 | 146 | | project.HistoryStorageEngineConnectionString = historyStorageEngineConnectionString; |
| | 145 | 147 | | project.HistoryStorageEngine = HistoryStorageEngineFactory.CreateHistoryStorageEngine(historyStorageEngineCo |
| | 145 | 148 | | return project; |
| | 236 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Creates a <see cref="WipProject"/> from a <see cref="RunnableProject"/> for modification. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="runnableProject">The runnable project from which to create the new <see cref="WipProject"/>.</p |
| | | 155 | | /// <returns>The new <see cref="WipProject"/></returns> |
| | | 156 | | public static WipProject CreateProject(RunnableProject runnableProject) |
| | 36 | 157 | | { |
| | 36 | 158 | | WipProject wipProject = new(runnableProject); |
| | 36 | 159 | | AddProjectToCollection(wipProject); |
| | 36 | 160 | | return wipProject; |
| | 36 | 161 | | } |
| | | 162 | | |
| | | 163 | | internal static void AddProjectToCollection(Project project) |
| | 410 | 164 | | { |
| | 410 | 165 | | if (Projects.TryGetValue(project.GetId(), out Project? previousProject)) |
| | 47 | 166 | | { |
| | 47 | 167 | | previousProject?.Unload(); |
| | 47 | 168 | | } |
| | 410 | 169 | | Projects[project.GetId()] = project; |
| | 410 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Builds the specified <see cref="WipProject"/>. |
| | | 174 | | /// </summary> |
| | | 175 | | /// <param name="wipProject">The WIP project.</param> |
| | | 176 | | /// <returns>a <see cref="RunnableProject"/></returns> |
| | | 177 | | public static async Task<RunnableProject> BuildAsync(WipProject wipProject) |
| | 126 | 178 | | { |
| | 126 | 179 | | RunnableProject runnableProject = await ProjectBuilder.BuildAsync(wipProject); |
| | 124 | 180 | | AddProjectToCollection(runnableProject); |
| | 124 | 181 | | return runnableProject; |
| | 124 | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Gets a class for a name. |
| | | 186 | | /// </summary> |
| | | 187 | | /// <param name="className">Name of the class.</param> |
| | | 188 | | /// <returns>Found class</returns> |
| | | 189 | | /// <exception cref="pva.SuperV.Engine.Exceptions.UnknownClassException"></exception> |
| | | 190 | | public Class GetClass(string className) |
| | 3587 | 191 | | { |
| | 3587 | 192 | | if (Classes.TryGetValue(className, out Class? value)) |
| | 3585 | 193 | | { |
| | 3585 | 194 | | return value; |
| | | 195 | | } |
| | | 196 | | |
| | 2 | 197 | | throw new UnknownEntityException("Class", className); |
| | 3585 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Finds a class for a name. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="className">Name of the class.</param> |
| | | 204 | | /// <returns><see cref="Class"/> if found or null otherwise.</returns> |
| | | 205 | | public Class? FindClass(string className) |
| | 2 | 206 | | { |
| | | 207 | | try |
| | 2 | 208 | | { |
| | 2 | 209 | | return GetClass(className); |
| | | 210 | | } |
| | 3 | 211 | | catch { return null; } |
| | 2 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Gets a formatter for name. |
| | | 216 | | /// </summary> |
| | | 217 | | /// <param name="formatterName">Name of the formatter.</param> |
| | | 218 | | /// <returns><see cref="FieldFormatter"/></returns> |
| | | 219 | | /// <exception cref="pva.SuperV.Engine.Exceptions.UnknownFormatterException"></exception> |
| | | 220 | | public FieldFormatter? GetFormatter(string? formatterName) |
| | 308 | 221 | | { |
| | 308 | 222 | | if (String.IsNullOrEmpty(formatterName)) |
| | 4 | 223 | | { |
| | 4 | 224 | | return null; |
| | | 225 | | } |
| | 304 | 226 | | if (FieldFormatters.TryGetValue(formatterName, out FieldFormatter? value)) |
| | 299 | 227 | | { |
| | 299 | 228 | | return value; |
| | | 229 | | } |
| | | 230 | | |
| | 5 | 231 | | throw new UnknownEntityException("Field formatter", formatterName); |
| | 303 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Finds a formatter for name. |
| | | 236 | | /// </summary> |
| | | 237 | | /// <param name="formatterName">Name of the formatter.</param> |
| | | 238 | | /// <returns><see cref="FieldFormatter"/> if found or null otherwise.</returns> |
| | | 239 | | public FieldFormatter? FindFormatter(string formatterName) |
| | 2 | 240 | | { |
| | | 241 | | try |
| | 2 | 242 | | { |
| | 2 | 243 | | return GetFormatter(formatterName); |
| | | 244 | | } |
| | 3 | 245 | | catch { return null; } |
| | 2 | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Gets the name of the generated assembly file for project. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <returns>Assembly file name</returns> |
| | | 252 | | public string GetAssemblyFileName() |
| | 654 | 253 | | { |
| | 654 | 254 | | if (!Directory.Exists(ProjectsPath)) |
| | 1 | 255 | | { |
| | 1 | 256 | | Directory.CreateDirectory(ProjectsPath); |
| | 1 | 257 | | } |
| | 654 | 258 | | return Path.Combine(ProjectsPath, $"{Name}-V{Version}.dll"); |
| | 654 | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Gets the project highest version from generated assemblies of project. |
| | | 263 | | /// </summary> |
| | | 264 | | /// <param name="projectName">Name of the project.</param> |
| | | 265 | | /// <returns>The highest version or 0 if first version.</returns> |
| | | 266 | | protected static int GetProjectHighestVersion(string projectName) |
| | 272 | 267 | | { |
| | 272 | 268 | | return Directory.Exists(ProjectsPath) |
| | 272 | 269 | | ? Directory.EnumerateFiles(ProjectsPath, $"{projectName}-V*.dll") |
| | 272 | 270 | | .Select(fileName => |
| | 13938 | 271 | | Convert.ToInt32(fileName |
| | 13938 | 272 | | .Replace(ProjectsPath, "") |
| | 13938 | 273 | | .Replace(Path.DirectorySeparatorChar.ToString(), "") |
| | 13938 | 274 | | .Replace($"{projectName}-V", "") |
| | 13938 | 275 | | .Replace(".dll", ""))) |
| | 272 | 276 | | .Order() |
| | 272 | 277 | | .LastOrDefault() |
| | 272 | 278 | | : 0; |
| | 272 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Gets the next version for project. |
| | | 283 | | /// </summary> |
| | | 284 | | /// <returns>Next project version</returns> |
| | | 285 | | protected int GetNextVersion() |
| | 272 | 286 | | { |
| | 272 | 287 | | return GetProjectHighestVersion(Name!) + 1; |
| | 272 | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Unloads the project. |
| | | 292 | | /// </summary> |
| | | 293 | | public static void Unload(Project project) |
| | 13 | 294 | | { |
| | 13 | 295 | | WeakReference? projectAssemblyLoaderWeakRef = null; |
| | 13 | 296 | | if (project is RunnableProject runnableProject) |
| | 13 | 297 | | { |
| | 13 | 298 | | projectAssemblyLoaderWeakRef = runnableProject.ProjectAssemblyLoaderWeakRef; |
| | 13 | 299 | | } |
| | 13 | 300 | | project.Dispose(); |
| | 13 | 301 | | if (projectAssemblyLoaderWeakRef is not null) |
| | 13 | 302 | | { |
| | 13 | 303 | | CallGcCleanup(projectAssemblyLoaderWeakRef); |
| | 13 | 304 | | } |
| | 13 | 305 | | } |
| | | 306 | | |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Unloads the project. |
| | | 310 | | /// </summary> |
| | | 311 | | public virtual void Unload() |
| | 408 | 312 | | { |
| | 408 | 313 | | FieldFormatters.Clear(); |
| | 408 | 314 | | Classes.Clear(); |
| | 408 | 315 | | Projects.Remove(GetId(), out _); |
| | 408 | 316 | | } |
| | | 317 | | |
| | | 318 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Code Smell", "S1215:\"GC.Collect\" should not be call |
| | | 319 | | Justification = "Need to call GC to remove references to assembly")] |
| | | 320 | | public static void CallGcCleanup(WeakReference palWeakRef) |
| | 13 | 321 | | { |
| | 286 | 322 | | for (int i = 0; palWeakRef.IsAlive && i < 10; i++) |
| | 130 | 323 | | { |
| | 130 | 324 | | GC.Collect(); |
| | 130 | 325 | | GC.WaitForPendingFinalizers(); |
| | 130 | 326 | | } |
| | 13 | 327 | | } |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 331 | | /// </summary> |
| | | 332 | | public void Dispose() |
| | 360 | 333 | | { |
| | 360 | 334 | | Dispose(true); |
| | 360 | 335 | | GC.SuppressFinalize(this); |
| | 360 | 336 | | } |
| | | 337 | | |
| | | 338 | | /// <summary> |
| | | 339 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 340 | | /// </summary> |
| | | 341 | | /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release |
| | | 342 | | protected virtual void Dispose(bool disposing) |
| | 360 | 343 | | { |
| | 360 | 344 | | Unload(); |
| | 360 | 345 | | } |
| | | 346 | | |
| | | 347 | | public abstract string GetId(); |
| | | 348 | | } |
| | | 349 | | } |