Skip to content

Commit

Permalink
DYN-3320: Add basic standard library support (#11383)
Browse files Browse the repository at this point in the history
* Add basic standard library support

* Add basic test

* Address comments

* Remove unneeded this references.
  • Loading branch information
sm6srw authored Jan 8, 2021
1 parent f78cf1b commit 7f7a95f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
33 changes: 21 additions & 12 deletions src/DynamoPackages/PackageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public string DefaultPackagesDirectory

private readonly List<string> packagesDirectoriesToVerifyCertificates = new List<string>();

// 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 })
{
Expand All @@ -87,10 +92,14 @@ public PackageLoader(IEnumerable<string> 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);
}

/// <summary>
Expand All @@ -104,7 +113,7 @@ public PackageLoader(IEnumerable<string> packagesDirectories, IEnumerable<string
if (packageDirectoriesToVerify == null)
throw new ArgumentNullException("packageDirectoriesToVerify");

this.packagesDirectoriesToVerifyCertificates.AddRange(packageDirectoriesToVerify);
packagesDirectoriesToVerifyCertificates.AddRange(packageDirectoriesToVerify);
}

private void OnPackageAdded(Package pkg)
Expand All @@ -125,19 +134,19 @@ private void OnPackageRemoved(Package pkg)

internal void Add(Package pkg)
{
if (!this.localPackages.Contains(pkg))
if (!localPackages.Contains(pkg))
{
this.localPackages.Add(pkg);
localPackages.Add(pkg);
pkg.MessageLogged += OnPackageMessageLogged;
OnPackageAdded(pkg);
}
}

internal void Remove(Package pkg)
{
if (this.localPackages.Contains(pkg))
if (localPackages.Contains(pkg))
{
this.localPackages.Remove(pkg);
localPackages.Remove(pkg);
pkg.MessageLogged -= OnPackageMessageLogged;
OnPackageRemoved(pkg);
}
Expand Down Expand Up @@ -178,7 +187,7 @@ private IEnumerable<CustomNodeInfo> OnRequestLoadCustomNodeDirectory(string dire
/// <param name="package"></param>
internal void TryLoadPackageIntoLibrary(Package package)
{
this.Add(package);
Add(package);

// Prevent duplicate loads
if (package.Loaded) return;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
Expand Down
18 changes: 17 additions & 1 deletion test/Libraries/PackageManagerTests/PackageLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 7f7a95f

Please sign in to comment.