From 2ded67560cabdb3e765cddb0364f771e598cf2e7 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Sat, 27 Aug 2022 09:55:46 +0100 Subject: [PATCH] Show warning dialog only if upgrade was performed --- Bonsai.Editor/EditorForm.cs | 41 ++++++--------- .../Properties/Resources.Designer.cs | 20 ++++---- Bonsai.Editor/Properties/Resources.resx | 12 ++--- Bonsai.Editor/UpgradeHelper.cs | 51 ++++++++++++++----- 4 files changed, 71 insertions(+), 53 deletions(-) diff --git a/Bonsai.Editor/EditorForm.cs b/Bonsai.Editor/EditorForm.cs index 8496e5913..63062fd80 100644 --- a/Bonsai.Editor/EditorForm.cs +++ b/Bonsai.Editor/EditorForm.cs @@ -774,34 +774,19 @@ WorkflowBuilder LoadWorkflow(string fileName, out SemanticVersion version) } } - WorkflowBuilder UpdateWorkflow(WorkflowBuilder workflowBuilder, SemanticVersion version) + WorkflowBuilder PrepareWorkflow(WorkflowBuilder workflowBuilder, SemanticVersion version, out bool upgraded) { + upgraded = false; var workflow = workflowBuilder.Workflow; - if (version == null || UpgradeHelper.IsDeprecated(version)) + try { upgraded = UpgradeHelper.TryUpgradeWorkflow(workflow, version, out workflow); } + catch (WorkflowBuildException) { MessageBox.Show( this, - Resources.UpdateWorkflow_Warning, - Resources.UpdateWorkflow_Warning_Caption, + Resources.UpgradeWorkflow_Error, + Resources.UpgradeWorkflow_Warning_Caption, MessageBoxButtons.OK, - MessageBoxIcon.Warning, - MessageBoxDefaultButton.Button1); - - try - { - UpgradeHelper.UpgradeEnumerableUnfoldingRules(workflowBuilder, version); - workflow = UpgradeHelper.UpgradeBuilderNodes(workflow, version); - } - catch (WorkflowBuildException) - { - MessageBox.Show( - this, - Resources.UpdateWorkflow_Error, - Resources.UpdateWorkflow_Warning_Caption, - MessageBoxButtons.OK, - MessageBoxIcon.Warning, - MessageBoxDefaultButton.Button1); - } + MessageBoxIcon.Warning); } workflowBuilder = new WorkflowBuilder(workflow.ToInspectableGraph()); @@ -858,7 +843,7 @@ bool OpenWorkflow(string fileName, bool setWorkingDirectory) } } - workflowBuilder = UpdateWorkflow(workflowBuilder, workflowVersion); + workflowBuilder = PrepareWorkflow(workflowBuilder, workflowVersion, out bool upgraded); editorControl.VisualizerLayout = null; editorControl.Workflow = workflowBuilder.Workflow; editorSite.ValidateWorkflow(); @@ -875,8 +860,14 @@ bool OpenWorkflow(string fileName, bool setWorkingDirectory) saveWorkflowDialog.FileName = fileName; ResetProjectStatus(); - if (UpgradeHelper.IsDeprecated(workflowVersion)) + if (upgraded) { + MessageBox.Show( + this, + Resources.UpgradeWorkflow_Warning, + Resources.UpgradeWorkflow_Warning_Caption, + MessageBoxButtons.OK, + MessageBoxIcon.Warning); saveWorkflowDialog.FileName = null; version++; } @@ -2355,7 +2346,7 @@ public DirectoryInfo EnsureExtensionsDirectory() public WorkflowBuilder LoadWorkflow(string fileName) { var workflow = siteForm.LoadWorkflow(fileName, out SemanticVersion version); - return siteForm.UpdateWorkflow(workflow, version); + return siteForm.PrepareWorkflow(workflow, version, out _); } public void OpenWorkflow(string fileName) diff --git a/Bonsai.Editor/Properties/Resources.Designer.cs b/Bonsai.Editor/Properties/Resources.Designer.cs index 7d533524d..456119dfb 100644 --- a/Bonsai.Editor/Properties/Resources.Designer.cs +++ b/Bonsai.Editor/Properties/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace Bonsai.Editor.Properties { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -531,29 +531,29 @@ internal static string UnsavedWorkflow_Question_Caption { } /// - /// Looks up a localized string similar to There was an error while updating the workflow. Please check if there are any missing packages that need to be installed before trying to upgrade again.. + /// Looks up a localized string similar to There was an error while upgrading the workflow. Please check if there are any missing packages that need to be installed before trying to upgrade again.. /// - internal static string UpdateWorkflow_Error { + internal static string UpgradeWorkflow_Error { get { - return ResourceManager.GetString("UpdateWorkflow_Error", resourceCulture); + return ResourceManager.GetString("UpgradeWorkflow_Error", resourceCulture); } } /// - /// Looks up a localized string similar to This workflow file has been created with an older version of Bonsai. Further modifications will be incompatible with previous versions.. + /// Looks up a localized string similar to This workflow file has been upgraded to the latest version of Bonsai. Further modifications will be incompatible with previous versions.. /// - internal static string UpdateWorkflow_Warning { + internal static string UpgradeWorkflow_Warning { get { - return ResourceManager.GetString("UpdateWorkflow_Warning", resourceCulture); + return ResourceManager.GetString("UpgradeWorkflow_Warning", resourceCulture); } } /// - /// Looks up a localized string similar to Update Workflow. + /// Looks up a localized string similar to Workflow Upgraded. /// - internal static string UpdateWorkflow_Warning_Caption { + internal static string UpgradeWorkflow_Warning_Caption { get { - return ResourceManager.GetString("UpdateWorkflow_Warning_Caption", resourceCulture); + return ResourceManager.GetString("UpgradeWorkflow_Warning_Caption", resourceCulture); } } diff --git a/Bonsai.Editor/Properties/Resources.resx b/Bonsai.Editor/Properties/Resources.resx index 591f44367..11eec4a10 100644 --- a/Bonsai.Editor/Properties/Resources.resx +++ b/Bonsai.Editor/Properties/Resources.resx @@ -206,11 +206,11 @@ Copyright (c) .NET Foundation and Contributors Save Error - - This workflow file has been created with an older version of Bonsai. Further modifications will be incompatible with previous versions. + + This workflow file has been upgraded to the latest version of Bonsai. Further modifications will be incompatible with previous versions. - - Update Workflow + + Workflow Upgraded There was an error saving the visualizer layout: @@ -228,8 +228,8 @@ Copyright (c) .NET Foundation and Contributors Package updates are available - - There was an error while updating the workflow. Please check if there are any missing packages that need to be installed before trying to upgrade again. + + There was an error while upgrading the workflow. Please check if there are any missing packages that need to be installed before trying to upgrade again. ..\Bonsai.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/Bonsai.Editor/UpgradeHelper.cs b/Bonsai.Editor/UpgradeHelper.cs index 11ab8e989..baccebc5e 100644 --- a/Bonsai.Editor/UpgradeHelper.cs +++ b/Bonsai.Editor/UpgradeHelper.cs @@ -33,7 +33,7 @@ static string RemoveMemberSelectorPrefix(string selector) return string.Join(ExpressionHelper.ArgumentSeparator, memberNames); } - internal static bool IsDeprecated(SemanticVersion version) + static bool IsDeprecated(SemanticVersion version) { return version < DeprecationTarget; } @@ -55,11 +55,25 @@ static void GetArgumentCount(ExpressionBuilderGraph workflow, Dictionary(); GetArgumentCount(workflow, argumentCount); - return workflow.Convert(builder => + ExpressionBuilder UpgradeBuilder(ExpressionBuilder builder) { #pragma warning disable CS0612 // Type or member is obsolete if (builder is SourceBuilder sourceBuilder) @@ -156,6 +170,7 @@ internal static ExpressionBuilderGraph UpgradeBuilderNodes(ExpressionBuilderGrap if (workflowElement is ExpressionBuilder builderElement && version < RemoveMemberSelectorPrefixVersion) { + upgraded = true; if (builderElement is PropertyMappingBuilder mappingBuilder) { foreach (var mapping in mappingBuilder.PropertyMappings) @@ -195,19 +210,31 @@ internal static ExpressionBuilderGraph UpgradeBuilderNodes(ExpressionBuilderGrap } return builder; + }; + + upgradedWorkflow = workflow.Convert(builder => + { + var upgradedBuilder = UpgradeBuilder(builder); + if (upgradedBuilder != builder) upgraded = true; + return upgradedBuilder; }); + return upgraded; } - internal static void UpgradeEnumerableUnfoldingRules(WorkflowBuilder workflowBuilder, SemanticVersion version) + static bool TryUpgradeEnumerableUnfoldingRules(ExpressionBuilderGraph workflow, SemanticVersion version) { + var upgraded = false; if (version < EnumerableUnfoldingVersion) { - var upgradeTargets = GetEnumerableUpgradeTargets(workflowBuilder.Workflow).ToList(); + var upgradeTargets = GetEnumerableUpgradeTargets(workflow).ToList(); foreach (var upgradeTarget in upgradeTargets) { - UpgradeEnumerableInputDependency(workflowBuilder, upgradeTarget); + UpgradeEnumerableInputDependency(workflow, upgradeTarget); + upgraded = true; } } + + return upgraded; } struct WorkflowInputDependency @@ -243,14 +270,14 @@ static IEnumerable GetEnumerableUpgradeTargets(Expressi } } - static void UpgradeEnumerableInputDependency(WorkflowBuilder builder, WorkflowInputDependency inputDependency) + static void UpgradeEnumerableInputDependency(ExpressionBuilderGraph workflow, WorkflowInputDependency inputDependency) { - var dependency = builder.Workflow.Build(inputDependency.Dependency); + var dependency = workflow.Build(inputDependency.Dependency); var sourceType = dependency.Type.GetGenericArguments()[0]; if (ExpressionHelper.IsEnumerableType(sourceType) && sourceType != typeof(string)) { - var workflow = inputDependency.Target; - var inputNode = workflow.FirstOrDefault(node => + var targetWorkflow = inputDependency.Target; + var inputNode = targetWorkflow.FirstOrDefault(node => { return ExpressionBuilder.Unwrap(node.Value) is WorkflowInputBuilder inputBuilder && inputBuilder.Index == 0; }); @@ -262,14 +289,14 @@ static void UpgradeEnumerableInputDependency(WorkflowBuilder builder, WorkflowIn Combinator = new Reactive.Merge() }; - var mergeNode = workflow.Add(mergeBuilder); + var mergeNode = targetWorkflow.Add(mergeBuilder); foreach (var successor in inputNode.Successors) { mergeNode.Successors.Add(successor); } inputNode.Successors.Clear(); - workflow.AddEdge(inputNode, mergeNode, new ExpressionBuilderArgument()); + targetWorkflow.AddEdge(inputNode, mergeNode, new ExpressionBuilderArgument()); } } }