From 757176d2144bd143f5b31f43182071601750472c Mon Sep 17 00:00:00 2001 From: Craig Long Date: Tue, 9 Oct 2018 08:45:42 -0400 Subject: [PATCH 1/4] Swith ToJson to public methods --- src/DynamoCore/Graph/Workspaces/SerializationExtensions.cs | 2 +- src/DynamoCoreWpf/ViewModels/Core/SerializationExtensions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DynamoCore/Graph/Workspaces/SerializationExtensions.cs b/src/DynamoCore/Graph/Workspaces/SerializationExtensions.cs index 4aeb4073d91..ee9bd143063 100644 --- a/src/DynamoCore/Graph/Workspaces/SerializationExtensions.cs +++ b/src/DynamoCore/Graph/Workspaces/SerializationExtensions.cs @@ -16,7 +16,7 @@ public static class SerializationExtensions /// Save a Workspace to json. /// /// A string representing the serialized WorkspaceModel. - internal static string ToJson(this WorkspaceModel workspace, EngineController engine) + public static string ToJson(this WorkspaceModel workspace, EngineController engine) { var logger = engine != null ? engine.AsLogger() : null; diff --git a/src/DynamoCoreWpf/ViewModels/Core/SerializationExtensions.cs b/src/DynamoCoreWpf/ViewModels/Core/SerializationExtensions.cs index 112b1d1ce88..ad9ed7398d8 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/SerializationExtensions.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/SerializationExtensions.cs @@ -17,7 +17,7 @@ public static class SerializationExtensions /// /// /// A JSON string representing the WorkspaceViewModel - internal static string ToJson(this WorkspaceViewModel viewModel) + public static string ToJson(this WorkspaceViewModel viewModel) { var settings = new JsonSerializerSettings { From 3ae7af38da93954b732c45a9fafd37405c875d20 Mon Sep 17 00:00:00 2001 From: Craig Long Date: Tue, 9 Oct 2018 08:47:12 -0400 Subject: [PATCH 2/4] Switch OpenJsonFileFromPath to public and make path optional --- src/DynamoCore/Models/DynamoModel.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/DynamoCore/Models/DynamoModel.cs b/src/DynamoCore/Models/DynamoModel.cs index ef1177a5e65..42d3d66b38a 100644 --- a/src/DynamoCore/Models/DynamoModel.cs +++ b/src/DynamoCore/Models/DynamoModel.cs @@ -1444,7 +1444,7 @@ public void OpenFileFromPath(string filePath, bool forceManualExecutionMode = fa if (DynamoUtilities.PathHelper.isValidJson(filePath, out fileContents, out ex)) { - OpenJsonFileFromPath(fileContents, filePath, forceManualExecutionMode); + OpenJsonFileFromPath(fileContents, forceManualExecutionMode, filePath); return; } else @@ -1486,11 +1486,11 @@ static private DynamoPreferencesData DynamoPreferencesDataFromJson(string json) /// Opens a Dynamo workspace from a path to an JSON file on disk. /// /// Json file contents - /// Path to file /// Set this to true to discard + /// Optional Path to file on disk. This is used to register dyf files in the same directory /// execution mode specified in the file and set manual mode /// True if workspace was opened successfully - private bool OpenJsonFileFromPath(string fileContents, string filePath, bool forceManualExecutionMode) + public bool OpenJsonFileFromPath(string fileContents, bool forceManualExecutionMode, string filePath = null) { try { From 8a30e9c8a05bc29768179ca7d7635366a2a0197a Mon Sep 17 00:00:00 2001 From: Craig Long Date: Tue, 9 Oct 2018 08:49:59 -0400 Subject: [PATCH 3/4] Provide mechanism on DynamoViewModel to retreive full json model --- .../ViewModels/Core/DynamoViewModel.cs | 26 +++++++++++++++++++ .../ViewModels/Core/WorkspaceViewModel.cs | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs index add649e1b6a..da84eee26aa 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs @@ -35,6 +35,7 @@ using System.Windows; using System.Windows.Forms; using System.Windows.Threading; +using Newtonsoft.Json.Linq; using ISelectable = Dynamo.Selection.ISelectable; namespace Dynamo.ViewModels @@ -1482,6 +1483,31 @@ internal bool ShowSaveDialogIfNeededAndSave(WorkspaceModel workspace) return false; } + /// + /// Provides the combined model and view json data as is found in a Dynamo file. + /// + /// + public string GetCurrentWorkspaceModelAndViewJson() + { + try + { + // Serialize the workspace. + var json = CurrentSpaceViewModel.Model.ToJson(Model.EngineController); + var json_parsed = JObject.Parse(json); + + // Add the View data to the json. + var jo = CurrentSpaceViewModel.AddViewBlockToJSON(json_parsed); + + return jo.ToString(); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message + " : " + ex.StackTrace); + throw (ex); + } + } + + internal bool CanVisibilityBeToggled(object parameters) { return true; diff --git a/src/DynamoCoreWpf/ViewModels/Core/WorkspaceViewModel.cs b/src/DynamoCoreWpf/ViewModels/Core/WorkspaceViewModel.cs index 94c34b7e7e8..705a0094cf7 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/WorkspaceViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/WorkspaceViewModel.cs @@ -576,7 +576,7 @@ internal void Save(string filePath, bool isBackup = false, EngineController engi /// This function appends view block to the model json /// /// Workspace Model data in JSON format - private JObject AddViewBlockToJSON(JObject modelData) + internal JObject AddViewBlockToJSON(JObject modelData) { var token = JToken.Parse(this.ToJson()); modelData.Add("View", token); From eb71da545e88ddf90b8e00d41b46ab53c5b6c6b3 Mon Sep 17 00:00:00 2001 From: Craig Long Date: Fri, 12 Oct 2018 14:13:34 -0400 Subject: [PATCH 4/4] Add test for new json function --- test/DynamoCoreWpfTests/WorkspaceSaving.cs | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/DynamoCoreWpfTests/WorkspaceSaving.cs b/test/DynamoCoreWpfTests/WorkspaceSaving.cs index ac4fa36a0f0..c84871592ac 100644 --- a/test/DynamoCoreWpfTests/WorkspaceSaving.cs +++ b/test/DynamoCoreWpfTests/WorkspaceSaving.cs @@ -17,6 +17,7 @@ using Dynamo.Graph.Connectors; using Dynamo.Graph.Nodes; using Dynamo.Wpf; +using Newtonsoft.Json.Linq; namespace Dynamo.Tests { @@ -50,6 +51,62 @@ public void HomeWorkspaceCanSaveAsNewFile() Assert.IsTrue(File.Exists(newPath)); } + [Test] + [Category("UnitTests")] + public void EnsureGetCurrentWorkspaceModelAndViewJsonReturnsPropperJson() + { + //openFile + string openPath = Path.Combine(TestDirectory, @"core\isInput_isOutput\IsInput.dyn"); + ViewModel.OpenCommand.Execute(openPath); + + //Get file json contents + var fileContents = File.ReadAllText(openPath); + var fileObj = Newtonsoft.Json.Linq.JToken.Parse(fileContents); + + //Get json from DynamoViewModel + var json = ViewModel.GetCurrentWorkspaceModelAndViewJson(); + var jsonObj = Newtonsoft.Json.Linq.JToken.Parse(json); + + //Check file json and json provided are the same + //Exclude "View" property as "View.Dynamo.Version" value may vary + foreach (var token in fileObj.Children()) + { + var prop = token as JProperty; + + if (prop.Name != "View") + { + Assert.AreEqual(prop.Value, jsonObj[prop.Name]); + } + } + + //Check file "View" property vs json "View" provided + //Exclude "Dynamo" property as "Dynamo.Version" value may vary + foreach (var token in fileObj["View"].Children()) + { + var prop = token as JProperty; + + if (prop.Name != "Dynamo") + { + Assert.AreEqual(prop.Value, jsonObj["View"][prop.Name]); + } + } + + //Check file "Dynamo" property vs json "Dynamo" provided + //Exclude "Version" property as "Version" value may vary + foreach (var token in fileObj["View"]["Dynamo"].Children()) + { + var prop = token as JProperty; + + if (prop.Name != "Version") + { + Assert.AreEqual(prop.Value, jsonObj["View"]["Dynamo"][prop.Name]); + } + } + + //Verify "Version" poperty included + Assert.NotNull(jsonObj["View"]["Dynamo"]["Version"]); + } + [Test] [Category("UnitTests")] public void CleanWorkbenchClearsUndoStack()