From 7f7a95f7fcd81e3d787ab7c70f72820c4ea3b811 Mon Sep 17 00:00:00 2001 From: Jorgen Dahl Date: Fri, 8 Jan 2021 10:24:23 -0500 Subject: [PATCH] DYN-3320: Add basic standard library support (#11383) * Add basic standard library support * Add basic test * Address comments * Remove unneeded this references. --- src/DynamoPackages/PackageLoader.cs | 33 ++++++++++++------- .../PackageManagerTests/PackageLoaderTests.cs | 18 +++++++++- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/DynamoPackages/PackageLoader.cs b/src/DynamoPackages/PackageLoader.cs index 54a4dedb257..eac5ab32d3d 100644 --- a/src/DynamoPackages/PackageLoader.cs +++ b/src/DynamoPackages/PackageLoader.cs @@ -76,6 +76,11 @@ public string DefaultPackagesDirectory private readonly List packagesDirectoriesToVerifyCertificates = new List(); + // The standard library directory is located in the same directory as the DynamoPackages.dll + internal string StandardLibraryDirectory => + Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(GetType()).Location), + @"Standard Library", @"Packages"); + public PackageLoader(string overridePackageDirectory) : this(new[] { overridePackageDirectory }) { @@ -87,10 +92,14 @@ public PackageLoader(IEnumerable packagesDirectories) throw new ArgumentNullException("packagesDirectories"); this.packagesDirectories.AddRange(packagesDirectories); + this.packagesDirectories.Add(StandardLibraryDirectory); + var error = PathHelper.CreateFolderIfNotExist(DefaultPackagesDirectory); if (error != null) Log(error); + + packagesDirectoriesToVerifyCertificates.Add(StandardLibraryDirectory); } /// @@ -104,7 +113,7 @@ public PackageLoader(IEnumerable packagesDirectories, IEnumerable OnRequestLoadCustomNodeDirectory(string dire /// internal void TryLoadPackageIntoLibrary(Package package) { - this.Add(package); + Add(package); // Prevent duplicate loads if (package.Loaded) return; @@ -216,11 +225,11 @@ internal void TryLoadPackageIntoLibrary(Package package) { RequestAddExtension?.Invoke(extension); } - this.requestedExtensions.Add(extension); + requestedExtensions.Add(extension); } package.Loaded = true; - this.PackgeLoaded?.Invoke(package); + PackgeLoaded?.Invoke(package); } catch (CustomNodePackageLoadException e) { @@ -316,9 +325,9 @@ public void LoadCustomNodesAndPackages(LoadPackageParams loadPackageParams, Cust foreach (var path in loadPackageParams.Preferences.CustomPackageFolders) { customNodeManager.AddUninitializedCustomNodesInPath(path, false, false); - if (!this.packagesDirectories.Contains(path)) + if (!packagesDirectories.Contains(path)) { - this.packagesDirectories.Add(path); + packagesDirectories.Add(path); } } LoadAll(loadPackageParams); @@ -351,7 +360,7 @@ private void ScanPackageDirectories(string root, IPreferences preferences) return; } - this.Log(string.Format(Resources.InvalidPackageFolderWarning, root)); + Log(string.Format(Resources.InvalidPackageFolderWarning, root)); return; } @@ -415,7 +424,7 @@ public Package ScanPackageDirectory(string directory, bool checkCertificates) // prevent duplicates if (LocalPackages.All(pkg => pkg.Name != discoveredPkg.Name)) { - this.Add(discoveredPkg); + Add(discoveredPkg); return discoveredPkg; // success } throw new LibraryLoadFailedException(directory, String.Format(Properties.Resources.DulicatedPackage, discoveredPkg.Name, discoveredPkg.RootDirectory)); diff --git a/test/Libraries/PackageManagerTests/PackageLoaderTests.cs b/test/Libraries/PackageManagerTests/PackageLoaderTests.cs index 3504937b768..beee97e7782 100644 --- a/test/Libraries/PackageManagerTests/PackageLoaderTests.cs +++ b/test/Libraries/PackageManagerTests/PackageLoaderTests.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using Dynamo.Engine; +using System.Reflection; using Dynamo.Extensions; using Dynamo.Graph.Nodes; using Dynamo.Graph.Nodes.CustomNodes; @@ -642,6 +642,22 @@ public void ScanPackageDirectoryWithCheckingCertificatesEnabledWillLoadPackageWi // } //} + [Test] + public void HasValidStandardLibraryPath() + { + // Arrange + var loader = new PackageLoader(new[] { PackagesDirectory }, new[] { PackagesDirectorySigned }); + var directory = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(loader.GetType()).Location), + @"Standard Library", @"Packages"); + + // Act + var standardDirectory = loader.StandardLibraryDirectory; + + // Assert + Assert.IsNotNullOrEmpty(standardDirectory); + Assert.AreEqual(standardDirectory, directory); + } + [Test] public void IsUnderPackageControlIsCorrectForValidFunctionDefinition() {