Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli import arg #9366

Merged
merged 5 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/DynamoApplications/StartupUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public static CommandLineArguments Parse(string[] args)

// Generate geometry json file
var geometryFilePath = string.Empty;
// dll paths we'll import before running a graph
var importPaths = new List<string>() ;

bool showHelp = false;
var optionsSet = new OptionSet().Add("o=|O=", "OpenFilePath, Instruct Dynamo to open headless and run a dyn file at this path", o => openfilepath = o)
Expand All @@ -97,7 +99,9 @@ public static CommandLineArguments Parse(string[] args)
.Add("x|X", "When used in combination with the 'O' flag, opens a .dyn file from the specified path and converts it to .json." +
"File will have the .json extension and be located in the same directory as the original file.", x => convertFile = x != null)
.Add("h|H|help", "Get some help", h => showHelp = h != null)
.Add("g=|G=|geometry", "Geometry, Instruct Dynamo to output geometry from all evaluations to a json file at this path", g => geometryFilePath = g);
.Add("g=|G=|geometry", "Geometry, Instruct Dynamo to output geometry from all evaluations to a json file at this path", g => geometryFilePath = g)
.Add("i=|I=|import", "Import, Instruct Dynamo to import an assembly as a node library. This argument should be a filepath to a single .dll" +
" - if you wish to import multiple dlls - use this flag multiple times: -i 'assembly1.dll' -i 'assembly2.dll' ", i=>importPaths.Add(i));

optionsSet.Parse(args);

Expand All @@ -118,7 +122,8 @@ public static CommandLineArguments Parse(string[] args)
OpenFilePath = openfilepath,
Verbose = verbose,
ConvertFile = convertFile,
GeometryFilePath = geometryFilePath
GeometryFilePath = geometryFilePath,
ImportedPaths = importPaths
};
}

Expand All @@ -134,6 +139,7 @@ private static void ShowHelp(OptionSet opSet)
public string Verbose { get; set; }
public bool ConvertFile { get; set; }
public string GeometryFilePath { get; set; }
public IEnumerable<String> ImportedPaths { get; set; }
}

public static void PreloadShapeManager(ref string geometryFactoryPath, ref string preloaderLocation)
Expand Down
33 changes: 33 additions & 0 deletions src/DynamoCLI/CommandLineRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ private static XmlDocument RunCommandLineArgs(DynamoModel model, StartupUtils.Co
Console.WriteLine("geometryFilePath option is only available when running DynamoWPFCLI, not DynamoCLI");
}

cmdLineArgs.ImportedPaths.ToList().ForEach(path =>
{
ImportAssembly(model, path);

});

model.OpenFileFromPath(cmdLineArgs.OpenFilePath, true);
Console.WriteLine("loaded file");
model.EvaluationCompleted += (o, args) => { evalComplete = true; };
Expand Down Expand Up @@ -79,6 +85,33 @@ private static XmlDocument RunCommandLineArgs(DynamoModel model, StartupUtils.Co
return doc;
}

/// <summary>
/// Attempts to import an assembly as a node library from a given file path.
/// </summary>
/// <param name="model"></param>
/// <param name="path"></param>
protected static void ImportAssembly(DynamoModel model, string path)
{
try
{
var filePath = new System.IO.FileInfo(path);
if (!filePath.Exists)
{
Console.WriteLine($"could not find requested import library at path{path}");
}
else
{
Console.WriteLine($"attempting to import assembly {path}");
var assembly = System.Reflection.Assembly.LoadFile(path);
model.LoadNodeLibrary(assembly);
}
}
catch (Exception e)
{
Console.WriteLine($"exception while trying to load assembly {path}: {e}");
}
}

protected static string GetStringRepOfCollection(ProtoCore.Mirror.MirrorData value)
{
var items = string.Join(",",
Expand Down
6 changes: 6 additions & 0 deletions src/DynamoWPFCLI/CommandLineRunnerWPF.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Xml;
using Dynamo.Applications;
Expand Down Expand Up @@ -39,6 +40,11 @@ private static XmlDocument RunCommandLineArgs(DynamoViewModel viewModel, Startup
Console.WriteLine("commandFilePath option is only available when running DynamoSandbox, not DynamoWPFCLI");
}

cmdLineArgs.ImportedPaths.ToList().ForEach(path =>
{
ImportAssembly(viewModel.Model, path);
});

viewModel.OpenCommand.Execute(new Tuple<string, bool>(cmdLineArgs.OpenFilePath, true));
Console.WriteLine("loaded file");
viewModel.Model.EvaluationCompleted += (o, args) => { evalComplete = true; };
Expand Down
71 changes: 69 additions & 2 deletions test/Libraries/CommandLineTests/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Dynamo.Wpf.ViewModels.Watch3D;
using System.Reflection;

namespace Dynamo.Tests
{
internal class CommandLineTests : DynamoModelTestBase
{
protected override void GetLibrariesToPreload(List<string> libraries)
{
libraries.Add("VMDataBridge.dll");
libraries.Add("DesignScriptBuiltin.dll");
libraries.Add("DSCoreNodes.dll");
libraries.Add("FunctionObject.ds");
libraries.Add("BuiltIn.ds");
libraries.Add("ProtoGeometry.dll");

base.GetLibrariesToPreload(libraries);
}

Expand Down Expand Up @@ -52,12 +60,14 @@ protected static StartupUtils.CommandLineArguments CommandstringToArgs(string co
return args;
}

//Only for use during testing. Windows or Posix will handle this when invoking commands.
protected static string[] CommandStringToStringArray(string commandString)
{
//convert string to commandlineargs,
var m = Regex.Matches(commandString, "([^\"]\\S*|\".+?\")\\s*");
var list = m.Cast<Match>().Select(match => match.Value).ToList();

var list = m.Cast<Match>().Select(match => match.Value);
//trim off leading and trailing "'s - this is handy when paths are passed as args with quotes around them.
list = list.Select(arg => arg.Trim(new[] { '"' })).ToList();
//strip trailing whitespace
var argarray = list.Select(x => x.Trim()).ToArray();
return argarray.ToArray();
Expand All @@ -78,6 +88,63 @@ public void CanOpenAndRunDynamoModelWithCommandLineRunner()
AssertPreviewValue("4c5889ac-7b91-4fb5-aaad-a2128b533279", 4.0);
}

[Test]
public void ImportingAnAssemblyDoesNotEffectCustomNodePaths()
{
//load a graph which requires first loading FFITarget.dll
var openpath = Path.Combine(TestDirectory, @"core\commandLine\FFITarget.dyn");
var importPath = Path.Combine(ExecutingDirectory, "FFITarget.dll");

var runner = new DynamoCLI.CommandLineRunner(this.CurrentDynamoModel);
string commandstring = $"/o {openpath} /i {importPath}";

runner.Run(CommandstringToArgs(commandstring));
//assert that the FFITarget dummy point node is created with correct properties.
AssertPreviewValue("fba565f89c8948e290bbb423177fa7bb", 2.0);
AssertPreviewValue("5a1fae3e13ce4ccfba737ec75057907b", 2.0);

//the custom package paths should not be effected by running the CLI.
this.CurrentDynamoModel.PreferenceSettings.CustomPackageFolders.ForEach(packagePath =>
{
Assert.IsFalse(packagePath.Contains(importPath) || packagePath == importPath);
});

}

[Test]
public void CanImportDependencyBeforeRunningGraph()
{
//load a graph which requires first loading FFITarget.dll
var openpath = Path.Combine(TestDirectory, @"core\commandLine\FFITarget.dyn");
var importPath = Path.Combine(ExecutingDirectory, "FFITarget.dll");

var runner = new DynamoCLI.CommandLineRunner(this.CurrentDynamoModel);
string commandstring = $"/o {openpath} /i {importPath}";

runner.Run(CommandstringToArgs(commandstring));
//assert that the FFITarget dummy point node is created with correct properties.
AssertPreviewValue("fba565f89c8948e290bbb423177fa7bb", 2.0);
AssertPreviewValue("5a1fae3e13ce4ccfba737ec75057907b", 2.0);

}

[Test]
public void CanImportMultipleDepsBeforeRunningGraph()
{
//load a graph which requires first loading FFITarget.dll and SampleLibraryZeroTouch.dll
var openpath = Path.Combine(TestDirectory, @"core\commandLine\multipleDeps.dyn");
var importPath1 = Path.Combine(ExecutingDirectory, "FFITarget.dll");
var importPath2 = Path.Combine(TestDirectory,"pkgs", "Dynamo Samples","bin" ,"SampleLibraryZeroTouch.dll");

var runner = new DynamoCLI.CommandLineRunner(this.CurrentDynamoModel);
string commandstring = $"/o {openpath} /i {importPath1} /i \"{importPath2}\"";

runner.Run(CommandstringToArgs(commandstring));
//assert that this node from the samples ZT dll produces a correct result.
AssertPreviewValue("04c1531865e34ecb80a265c7822450aa", "Point(X = 4.000, Y = 2.000, Z = 2.000)" );

}

[Test]
public void CanOpenAndRunFileWihtListsCorrectlyToOutputFileFromDynamoCLIexe()
{
Expand Down
Loading