diff --git a/src/DynamoCLI/CommandLineRunner.cs b/src/DynamoCLI/CommandLineRunner.cs index 2a4ba16a593..2d887a21472 100644 --- a/src/DynamoCLI/CommandLineRunner.cs +++ b/src/DynamoCLI/CommandLineRunner.cs @@ -44,6 +44,7 @@ private static XmlDocument RunCommandLineArgs(DynamoModel model, StartupUtils.Co { Console.WriteLine("geometryFilePath option is only available when running DynamoWPFCLI, not DynamoCLI"); } + model.HostAnalyticsInfo = cmdLineArgs.AnalyticsInfo; cmdLineArgs.ImportedPaths.ToList().ForEach(path => diff --git a/src/DynamoCLI/Program.cs b/src/DynamoCLI/Program.cs index 2fa6946bd5d..ff0f6bd03dd 100644 --- a/src/DynamoCLI/Program.cs +++ b/src/DynamoCLI/Program.cs @@ -21,11 +21,11 @@ static internal void Main(string[] args) DynamoModel model; if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath)) { - model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath); + model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath, cmdLineArgs.AnalyticsInfo); } else { - model = Dynamo.Applications.StartupUtils.MakeModel(true); + model = Dynamo.Applications.StartupUtils.MakeModel(true, string.Empty, cmdLineArgs.AnalyticsInfo); } var runner = new CommandLineRunner(model); runner.Run(cmdLineArgs); diff --git a/src/DynamoCoreWpf/ViewModels/Watch3D/DefaultWatch3DViewModel.cs b/src/DynamoCoreWpf/ViewModels/Watch3D/DefaultWatch3DViewModel.cs index bc185d13bff..9da3001aa5f 100644 --- a/src/DynamoCoreWpf/ViewModels/Watch3D/DefaultWatch3DViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Watch3D/DefaultWatch3DViewModel.cs @@ -195,7 +195,14 @@ internal WorkspaceViewModel CurrentSpaceViewModel { get { - return viewModel.Workspaces.FirstOrDefault(vm => vm.Model == dynamoModel.CurrentWorkspace); + if (viewModel == null) + { + return null; + } + else + { + return viewModel.Workspaces.FirstOrDefault(vm => vm.Model == dynamoModel.CurrentWorkspace); + } } } diff --git a/src/DynamoWPFCLI/CommandLineRunnerWPF.cs b/src/DynamoWPFCLI/CommandLineRunnerWPF.cs index da6f4c92c6c..8a932009412 100644 --- a/src/DynamoWPFCLI/CommandLineRunnerWPF.cs +++ b/src/DynamoWPFCLI/CommandLineRunnerWPF.cs @@ -40,13 +40,6 @@ private static XmlDocument RunCommandLineArgs(DynamoViewModel viewModel, Startup Console.WriteLine("commandFilePath option is only available when running DynamoSandbox, not DynamoWPFCLI"); } - viewModel.Model.HostAnalyticsInfo= cmdLineArgs.AnalyticsInfo; - - cmdLineArgs.ImportedPaths.ToList().ForEach(path => - { - ImportAssembly(viewModel.Model, path); - }); - viewModel.OpenCommand.Execute(new Tuple(cmdLineArgs.OpenFilePath, true)); Console.WriteLine("loaded file"); viewModel.Model.EvaluationCompleted += (o, args) => { evalComplete = true; }; diff --git a/src/DynamoWPFCLI/DynamoWPFCLI.csproj b/src/DynamoWPFCLI/DynamoWPFCLI.csproj index 70100f969ad..0a7b885ec57 100644 --- a/src/DynamoWPFCLI/DynamoWPFCLI.csproj +++ b/src/DynamoWPFCLI/DynamoWPFCLI.csproj @@ -13,6 +13,7 @@ ..\..\extern\prism\Microsoft.Practices.Prism.dll + diff --git a/src/DynamoWPFCLI/Program.cs b/src/DynamoWPFCLI/Program.cs index a3714a47778..81c259659e5 100644 --- a/src/DynamoWPFCLI/Program.cs +++ b/src/DynamoWPFCLI/Program.cs @@ -1,49 +1,50 @@ using System; +using System.Linq; +using System.Threading; using Dynamo.Applications; using Dynamo.Models; using Dynamo.ViewModels; using Dynamo.Wpf.ViewModels.Watch3D; +using static System.Windows.Threading.Dispatcher; namespace DynamoWPFCLI { internal class Program { [STAThread] - static internal void Main(string[] args) + internal static void Main(string[] args) { try { var cmdLineArgs = StartupUtils.CommandLineArguments.Parse(args); var locale = StartupUtils.SetLocale(cmdLineArgs); + if (cmdLineArgs.DisableAnalytics) { Dynamo.Logging.Analytics.DisableAnalytics = true; } - DynamoModel model; - if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath)) + if (cmdLineArgs.KeepAlive) { - model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath); + var thread = new Thread(() => RunKeepAlive(cmdLineArgs)); + + thread.Name = "DynamoModelKeepAlive"; + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + + Console.WriteLine("Starting DynamoWPFCLI in keepalive mode"); + Console.ReadLine(); + + ShutDown(); } else { - model = Dynamo.Applications.StartupUtils.MakeModel(true); + var viewModel = StartupDaynamo(cmdLineArgs); + + var runner = new CommandLineRunnerWPF(viewModel); + runner.Run(cmdLineArgs); } - var viewModel = DynamoViewModel.Start( - new DynamoViewModel.StartConfiguration() - { - DynamoModel = model, - Watch3DViewModel = new DefaultWatch3DViewModel(null, new Watch3DViewModelStartupParams(model)) - { - Active = false, - CanBeActivated = false - } - }); - - var runner = new CommandLineRunnerWPF(viewModel); - runner.Run(cmdLineArgs); - } catch (Exception e) { @@ -61,5 +62,93 @@ static internal void Main(string[] args) } } + /// + /// Start Dynamo Model and ViewModel per cmdLineArgs parameters. + /// + /// + /// + private static DynamoViewModel StartupDaynamo(StartupUtils.CommandLineArguments cmdLineArgs) + { + DynamoModel model; + if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath)) + { + model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath, cmdLineArgs.AnalyticsInfo); + } + else + { + model = Dynamo.Applications.StartupUtils.MakeModel(true, string.Empty, cmdLineArgs.AnalyticsInfo); + } + + model.ShutdownCompleted += (m) => { ShutDown(); }; + + var viewModel = DynamoViewModel.Start( + new DynamoViewModel.StartConfiguration() + { + DynamoModel = model, + Watch3DViewModel = new DefaultWatch3DViewModel(null, new Watch3DViewModelStartupParams(model)) + { + Active = false, + CanBeActivated = false + } + }); + + cmdLineArgs.ImportedPaths.ToList().ForEach(path => + { + ImportAssembly(model, path); + }); + + return viewModel; + } + + private static void RunKeepAlive(StartupUtils.CommandLineArguments cmdLineArgs) + { + try + { + StartupDaynamo(cmdLineArgs); + + Console.WriteLine("-----------------------------------------"); + Console.WriteLine("DynamoWPFCLI is running in keepalive mode"); + Console.WriteLine("Press Enter to shutdown..."); + + Run(); + } + catch + { + Console.WriteLine("Server is shutting down due to an error"); + } + } + + /// + /// Attempts to import an assembly as a node library from a given file path. + /// + /// + /// + private 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, true); + } + } + catch (Exception e) + { + Console.WriteLine($"exception while trying to load assembly {path}: {e}"); + } + } + + private static void ShutDown() + { + Environment.Exit(0); + } + } }