Skip to content

Commit

Permalink
restore behavior of extension loadNodeLibrary method (DynamoDS#10187)
Browse files Browse the repository at this point in the history
* raise event when importing zt node via LoadNodeLibrary method to restore behavior

* Update DynamoModel.cs

review comments

* test passes with this change-
do not add duplicate search entires

* update behavior of custom node test - no longer entries with the same guid as seperate entries
add API test
improve equality and hashcode

* add new method to extensionLibraryLoader
use this to revert to old behavior for API users while keeping package manager behavior unchanged
update tests to use new method

* review comments
  • Loading branch information
mjkkirschner authored Dec 5, 2019
1 parent 8806926 commit 0605400
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/DynamoCLI/CommandLineRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected static void ImportAssembly(DynamoModel model, string path)
{
Console.WriteLine($"attempting to import assembly {path}");
var assembly = System.Reflection.Assembly.LoadFile(path);
model.LoadNodeLibrary(assembly);
model.LoadNodeLibrary(assembly,true);
}
}
catch (Exception e)
Expand Down
18 changes: 16 additions & 2 deletions src/DynamoCore/Extensions/ExtensionLibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,28 @@ internal ExtensionLibraryLoader(DynamoModel model)
}

/// <summary>
/// Loads the node library.
/// Loads a ZeroTouch or NodeModel based node into the VM and search.
/// To guarantee the node is correctly added to the LibraryUI this method should not
/// be called while LibraryExtension is loading.
/// </summary>
/// <param name="library">The library.</param>
public void LoadNodeLibrary(Assembly library)
{
model.LoadNodeLibrary(library);
model.LoadNodeLibrary(library,false);
}

//TODO add to ILibraryLoader in 3.0 OR refactor package/zeroTouch import code path.
/// <summary>
/// Loads a zeroTouch or explicit NodeModel based node into the VM.
/// Does not add zeroTouch libraries to Search. Currently only used by package manager extension.
/// </summary>
/// <param name="library">The library.</param>
internal void LoadLibraryAndSuppressZTSearchImport(Assembly library)
{
model.LoadNodeLibrary(library, true);
}

//TODO add to ILibraryLoader in 3.0
/// <summary>
/// Loads packages for import into VM and for node search.
/// </summary>
Expand Down
20 changes: 18 additions & 2 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,11 +1252,27 @@ private void InitializeNodeLibrary(IPreferences preferences)
CustomNodeManager.AddUninitializedCustomNodesInPath(pathManager.CommonDefinitions, IsTestMode);
}

internal void LoadNodeLibrary(Assembly assem)
/// <summary>
/// Imports a node library (zero touch or nodeModel) into the VM.
/// Does not necessarily add those imported functions to search.
/// </summary>
/// <param name="assem">The assembly to load which contains the types to import.</param>
/// <param name="suppressZeroTouchLibraryLoad">If True, zero touch types will not be added to search.
/// This is used by packageManager extension to defer adding ZT libraries to search until all libraries are loaded.
/// </param>
internal void LoadNodeLibrary(Assembly assem, bool suppressZeroTouchLibraryLoad = true)
{
if (!NodeModelAssemblyLoader.ContainsNodeModelSubType(assem))
{
LibraryServices.LoadNodeLibrary(assem.Location, false);
if (suppressZeroTouchLibraryLoad)
{
LibraryServices.LoadNodeLibrary(assem.Location,false);
}
else
{
LibraryServices.ImportLibrary(assem.Location);
}

return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DynamoPackages/PackageManagerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void Startup(StartupParams startupParams)
PackageLoader.MessageLogged += OnMessageLogged;
PackageLoader.PackgeLoaded += OnPackageLoaded;
PackageLoader.PackageRemoved += OnPackageRemoved;
RequestLoadNodeLibraryHandler = startupParams.LibraryLoader.LoadNodeLibrary;
RequestLoadNodeLibraryHandler = (startupParams.LibraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;
//TODO: Add LoadPackages to ILibraryLoader interface in 3.0
LoadPackagesHandler = (startupParams.LibraryLoader as ExtensionLibraryLoader).LoadPackages;
customNodeManager = (startupParams.CustomNodeManager as Core.CustomNodeManager);
Expand Down
19 changes: 19 additions & 0 deletions test/DynamoCoreTests/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Dynamo.Extensions;
using Dynamo.Logging;
using Dynamo.Models;
Expand All @@ -14,6 +15,7 @@ namespace Dynamo.Tests
class ExtensionTests
{
string extensionsPath;
string testpkgPath;
Mock<IExtension> extMock;
DynamoModel model;
ICommandExecutive executive;
Expand All @@ -24,6 +26,7 @@ class ExtensionTests
public void Init()
{
extensionsPath = Path.Combine(Directory.GetCurrentDirectory(), "extensions");
testpkgPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\test\pkgs"));
extMock = new Mock<IExtension>();
extMock.Setup(ext => ext.Ready(It.IsAny<ReadyParams>())).Callback((ReadyParams r) => ExtensionReadyCallback(r));
cmdExecutionState = -1;
Expand Down Expand Up @@ -234,6 +237,22 @@ public void RegisterSameObjectAsManyServices()
logsource = sm.Service<ILogSource>();
Assert.IsNull(logsource); //service is unregistered and not available any more
}

[Test]
public void ExtensionLoader_LoadNodeLibraryAddsZTNodesToSearch()
{
var assemPath = Path.Combine(testpkgPath, "Dynamo Samples", "bin", "SampleLibraryZeroTouch.dll");
var assembly = Assembly.LoadFrom(assemPath);
var libraryLoader = new ExtensionLibraryLoader(model);
libraryLoader.LoadNodeLibrary(assembly);

var entries = model.SearchModel.SearchEntries.ToList();
var nodesInLib = entries.Where(x => x.Assembly.Contains("SampleLibraryZeroTouch")).Select(y => y.FullName).ToList();
Assert.AreEqual(12, nodesInLib.Count());
Assert.IsTrue(entries.Count(x => x.FullName == "SampleLibraryZeroTouch.Examples.TransformableExample.TransformObject") == 1);

}

}

class ThrowExceptionCommand : DynamoModel.RecordableCommand
Expand Down
7 changes: 6 additions & 1 deletion test/DynamoCoreTests/SearchModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ public void CanRefactorCustomNodeWhilePreservingDuplicates()
const string descr = "TheCat";
const string path = @"C:\turtle\graphics.dyn";
var guid1 = Guid.NewGuid();
var guid2 = Guid.NewGuid();

//all properties are the same except guid.
var dummyInfo1 = new CustomNodeInfo(guid1, nodeName, catName, descr, path);
var dummyInfo2 = new CustomNodeInfo(guid2, nodeName, catName, descr, path);

var dummySearch1 = new CustomNodeSearchElement(null, dummyInfo1);
var dummySearch2 = new CustomNodeSearchElement(null, dummyInfo1);
var dummySearch2 = new CustomNodeSearchElement(null, dummyInfo2);

search.Add(dummySearch1);
search.Add(dummySearch2);
Expand Down
18 changes: 9 additions & 9 deletions test/Libraries/PackageManagerTests/PackageLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void LoadPackagesReturnsAllValidPackagesInValidDirectory()
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;

loader.LoadAll(new LoadPackageParams
{
Expand Down Expand Up @@ -157,7 +157,7 @@ public void LoadingPackageDoesNotAffectLoadedSearchEntries()
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;

loader.LoadAll(new LoadPackageParams
{
Expand Down Expand Up @@ -189,7 +189,7 @@ public void LoadingCustomNodeFromPackageSetsNodeInfoPackageInfoCorrectly()
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;

// This test needs the "isTestMode" flag to be turned off as an exception to be able
// to test duplicate custom node def loading.
Expand All @@ -216,7 +216,7 @@ public void PlacingCustomNodeInstanceFromPackageRetainsCorrectPackageInfoState()
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;

var packageDirectory = Path.Combine(TestDirectory, "pkgs", "EvenOdd");
var package1 = Package.FromDirectory(packageDirectory, CurrentDynamoModel.Logger);
Expand Down Expand Up @@ -245,7 +245,7 @@ public void LoadingConflictingCustomNodePackageDoesNotGetLoaded()
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;

// This test needs the "isTestMode" flag to be turned off as an exception to be able
// to test duplicate custom node def loading.
Expand Down Expand Up @@ -276,9 +276,9 @@ public void LoadingConflictingCustomNodePackage_AfterPlacingNode_DoesNotGetLoade
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;



var packageDirectory = Path.Combine(TestDirectory, "pkgs", "EvenOdd");
var packageDirectory2 = Path.Combine(TestDirectory, "pkgs", "EvenOdd2");
var package1 = Package.FromDirectory(packageDirectory,CurrentDynamoModel.Logger);
Expand Down Expand Up @@ -319,7 +319,7 @@ public void CreatingConflictingCustomNodeWithPackage_WillOverwriteCustomNodeInPa
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;

var packageDirectory = Path.Combine(TestDirectory, "pkgs", "EvenOdd");
var package1 = Package.FromDirectory(packageDirectory, CurrentDynamoModel.Logger);
Expand Down Expand Up @@ -381,7 +381,7 @@ public void LoadingAPackageThatConflictsWithLooseLoadedCustomNodeWillOverwriteLo
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel);

loader.PackagesLoaded += libraryLoader.LoadPackages;
loader.RequestLoadNodeLibrary += libraryLoader.LoadNodeLibrary;
loader.RequestLoadNodeLibrary += (libraryLoader as ExtensionLibraryLoader).LoadLibraryAndSuppressZTSearchImport;

var packageDirectory = Path.Combine(TestDirectory, "pkgs", "EvenOdd");
var package1 = Package.FromDirectory(packageDirectory, CurrentDynamoModel.Logger);
Expand Down

0 comments on commit 0605400

Please sign in to comment.