Skip to content

Commit

Permalink
DYN-4034 : open file from json content (#12671)
Browse files Browse the repository at this point in the history
* open file from json content

* adress feedback 1

* openfilefromjson command and test

* adress feedback

Co-authored-by: Bzz <[email protected]>
  • Loading branch information
BogdanZavu and zavub authored Mar 11, 2022
1 parent 46e3fd3 commit d5bc36b
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 41 deletions.
24 changes: 22 additions & 2 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,18 @@ public void ForceRun()

#region save/load

/// <summary>
/// Opens a Dynamo workspace from a Json string.
/// </summary>
/// <param name="fileContents">Json file content</param>
/// <param name="forceManualExecutionMode">Set this to true to discard
/// execution mode specified in the file and set manual mode</param>
public void OpenFileFromJson(string fileContents, bool forceManualExecutionMode = false)
{
OpenJsonFileFromPath(fileContents, "", forceManualExecutionMode);
return;
}

/// <summary>
/// Opens a Dynamo workspace from a path to a file on disk.
/// </summary>
Expand Down Expand Up @@ -1852,7 +1864,10 @@ private bool OpenJsonFile(
bool forceManualExecutionMode,
out WorkspaceModel workspace)
{
CustomNodeManager.AddUninitializedCustomNodesInPath(Path.GetDirectoryName(filePath), IsTestMode);
if (!string.IsNullOrEmpty(filePath))
{
CustomNodeManager.AddUninitializedCustomNodesInPath(Path.GetDirectoryName(filePath), IsTestMode);
}

var currentHomeSpace = Workspaces.OfType<HomeWorkspaceModel>().FirstOrDefault();
currentHomeSpace.UndefineCBNFunctionDefinitions();
Expand All @@ -1870,7 +1885,7 @@ private bool OpenJsonFile(
CustomNodeManager,
this.LinterManager);

workspace.FileName = filePath;
workspace.FileName = string.IsNullOrEmpty(filePath) ? "" : filePath;
workspace.ScaleFactor = dynamoPreferences.ScaleFactor;

// NOTE: This is to handle the case of opening a JSON file that does not have a version string
Expand All @@ -1887,6 +1902,11 @@ private bool OpenJsonFile(

homeWorkspace.ReCompileCodeBlockNodesForFunctionDefinitions();

if (string.IsNullOrEmpty(workspace.FileName))
{
workspace.HasUnsavedChanges = true;
}

RunType runType;
if (!homeWorkspace.HasRunWithoutCrash || !Enum.TryParse(dynamoPreferences.RunType, false, out runType) || forceManualExecutionMode)
runType = RunType.Manual;
Expand Down
7 changes: 7 additions & 0 deletions src/DynamoCore/Models/DynamoModelCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ protected virtual void OpenFileImpl(OpenFileCommand command)
//ClipBoard.Clear();
}

protected virtual void OpenFileFromJsonImpl(OpenFileFromJsonCommand command)
{
string fileContents = command.FileContents;
bool forceManualMode = command.ForceManualExecutionMode;
OpenFileFromJson(fileContents, forceManualMode);
}

private void RunCancelImpl(RunCancelCommand command)
{
var model = CurrentWorkspace as HomeWorkspaceModel;
Expand Down
122 changes: 101 additions & 21 deletions src/DynamoCore/Models/RecordableCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,67 +141,70 @@ internal static RecordableCommand Deserialize(XmlElement element)

switch (element.Name)
{
case "OpenFileCommand":
case nameof(OpenFileCommand):
command = OpenFileCommand.DeserializeCore(element);
break;
case "PausePlaybackCommand":
case nameof(OpenFileFromJsonCommand):
command = OpenFileFromJsonCommand.DeserializeCore(element);
break;
case nameof(PausePlaybackCommand):
command = PausePlaybackCommand.DeserializeCore(element);
break;
case "RunCancelCommand":
case nameof(RunCancelCommand):
command = RunCancelCommand.DeserializeCore(element);
break;
case "CreateNodeCommand":
case nameof(CreateNodeCommand):
command = CreateNodeCommand.DeserializeCore(element);
break;
case "SelectModelCommand":
case nameof(SelectModelCommand):
command = SelectModelCommand.DeserializeCore(element);
break;
case "CreateNoteCommand":
case nameof(CreateNoteCommand):
command = CreateNoteCommand.DeserializeCore(element);
break;
case "SelectInRegionCommand":
case nameof(SelectInRegionCommand):
command = SelectInRegionCommand.DeserializeCore(element);
break;
case "DragSelectionCommand":
case nameof(DragSelectionCommand):
command = DragSelectionCommand.DeserializeCore(element);
break;
case "MakeConnectionCommand":
case nameof(MakeConnectionCommand):
command = MakeConnectionCommand.DeserializeCore(element);
break;
case "DeleteModelCommand":
case nameof(DeleteModelCommand):
command = DeleteModelCommand.DeserializeCore(element);
break;
case "UndoRedoCommand":
case nameof(UndoRedoCommand):
command = UndoRedoCommand.DeserializeCore(element);
break;
case "ModelEventCommand":
case nameof(ModelEventCommand):
command = ModelEventCommand.DeserializeCore(element);
break;
case "UpdateModelValueCommand":
case nameof(UpdateModelValueCommand):
command = UpdateModelValueCommand.DeserializeCore(element);
break;
case "ConvertNodesToCodeCommand":
case nameof(ConvertNodesToCodeCommand):
command = ConvertNodesToCodeCommand.DeserializeCore(element);
break;
case "CreateCustomNodeCommand":
case nameof(CreateCustomNodeCommand):
command = CreateCustomNodeCommand.DeserializeCore(element);
break;
case "SwitchTabCommand":
case nameof(SwitchTabCommand):
command = SwitchTabCommand.DeserializeCore(element);
break;
case "CreateAnnotationCommand":
case nameof(CreateAnnotationCommand):
command = CreateAnnotationCommand.DeserializeCore(element);
break;
case "UngroupModelCommand":
case nameof(UngroupModelCommand):
command = UngroupModelCommand.DeserializeCore(element);
break;
case "AddPresetCommand":
case nameof(AddPresetCommand):
command = AddPresetCommand.DeserializeCore(element);
break;
case "ApplyPresetCommand":
case nameof(ApplyPresetCommand):
command = ApplyPresetCommand.DeserializeCore(element);
break;
case "CreateAndConnectNodeCommand":
case nameof(CreateAndConnectNodeCommand):
command = CreateAndConnectNodeCommand.DeserializeCore(element);
break;
}
Expand Down Expand Up @@ -546,6 +549,83 @@ internal override void TrackAnalytics()
#endregion
}

/// <summary>
/// A command to open a file from Json content.
/// </summary>
[DataContract]
public class OpenFileFromJsonCommand : RecordableCommand
{
#region Public Class Methods

/// <summary>
///
/// </summary>
/// <param name="fileContents">The Json content of a file.</param>
/// <param name="forceManualExecutionMode">Should the file be opened in manual execution mode?</param>
public OpenFileFromJsonCommand(string fileContents, bool forceManualExecutionMode = false)
{
FileContents = fileContents;
ForceManualExecutionMode = forceManualExecutionMode;
}

internal static OpenFileFromJsonCommand DeserializeCore(XmlElement element)
{
XmlElementHelper helper = new XmlElementHelper(element);
string xmlFileContents = helper.ReadString("XmlFileContents");
return new OpenFileFromJsonCommand(xmlFileContents);
}

#endregion

#region Public Command Properties

[DataMember]
internal string FileContents { get; private set; }
internal bool ForceManualExecutionMode { get; private set; }
private DynamoModel dynamoModel;

#endregion

#region Protected Overridable Methods

protected override void ExecuteCore(DynamoModel dynamoModel)
{
this.dynamoModel = dynamoModel;
dynamoModel.OpenFileFromJsonImpl(this);
}


protected override void SerializeCore(XmlElement element)
{
var helper = new XmlElementHelper(element);
helper.SetAttribute("XmlFileContents", FileContents);
}


internal override void TrackAnalytics()
{
// Log file open action and the number of nodes in the opened workspace
Dynamo.Logging.Analytics.TrackFileOperationEvent(
"In memory json file",
Logging.Actions.Open,
dynamoModel.CurrentWorkspace.Nodes.Count());

// If there are unresolved nodes in the opened workspace, log the node names and count
var unresolvedNodes = dynamoModel.CurrentWorkspace.Nodes.OfType<DummyNode>();
if (unresolvedNodes != null && unresolvedNodes.Any())
{
Dynamo.Logging.Analytics.TrackEvent(
Logging.Actions.Unresolved,
Logging.Categories.NodeOperations,
unresolvedNodes.Select(n => string.Format("{0}:{1}", n.LegacyAssembly, n.LegacyFullName))
.Aggregate((x, y) => string.Format("{0}, {1}", x, y)),
unresolvedNodes.Count());
}
}

#endregion
}

/// <summary>
/// A command used to execute or cancel execution.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/DynamoCore/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/DynamoCore/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ This package likely contains an assembly that is blocked. You will need to load
<data name="LibraryLoadFailureMessageBoxTitle" xml:space="preserve">
<value>Library load failure</value>
</data>
<data name="FileLoadFailureMessageBoxTitle" xml:space="preserve">
<value>File load failure</value>
</data>
<data name="GroupStyleDefaultActions" xml:space="preserve">
<value>Actions</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions src/DynamoCore/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,9 @@ This package likely contains an assembly that is blocked. You will need to load
<data name="LibraryLoadFailureMessageBoxTitle" xml:space="preserve">
<value>Library load failure</value>
</data>
<data name="FileLoadFailureMessageBoxTitle" xml:space="preserve">
<value>File load failure</value>
</data>
<data name="GroupStyleDefaultActions" xml:space="preserve">
<value>Actions</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/DynamoCore/Scheduler/DynamoSchedulerInternals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public bool HasPendingTasks
}
}
}

#endregion

#region Private Class Helper Methods
Expand Down
2 changes: 2 additions & 0 deletions src/DynamoCoreWpf/Commands/DynamoCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void OnModelCommandCompleted(DynamoModel.RecordableCommand command)
case "CreateCustomNodeCommand":
case "AddPresetCommand":
case "ApplyPresetCommand":
case "OpenFileFromJsonCommand":
// for this commands there is no need
// to do anything after execution
break;
Expand Down Expand Up @@ -167,6 +168,7 @@ void OnModelCommandStarting(DynamoModel.RecordableCommand command)
break;

case "OpenFileCommand":
case "OpenFileFromJsonCommand":
case "RunCancelCommand":
case "ForceRunCancelCommand":
case "CreateNodeCommand":
Expand Down
Loading

0 comments on commit d5bc36b

Please sign in to comment.