Skip to content

Commit

Permalink
Show warning dialog only if upgrade was performed
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Aug 27, 2022
1 parent d6defce commit 2ded675
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 53 deletions.
41 changes: 16 additions & 25 deletions Bonsai.Editor/EditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -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++;
}
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions Bonsai.Editor/Properties/Resources.Designer.cs

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

12 changes: 6 additions & 6 deletions Bonsai.Editor/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ Copyright (c) .NET Foundation and Contributors</value>
<data name="SaveElement_Error_Caption" xml:space="preserve">
<value>Save Error</value>
</data>
<data name="UpdateWorkflow_Warning" xml:space="preserve">
<value>This workflow file has been created with an older version of Bonsai. Further modifications will be incompatible with previous versions.</value>
<data name="UpgradeWorkflow_Warning" xml:space="preserve">
<value>This workflow file has been upgraded to the latest version of Bonsai. Further modifications will be incompatible with previous versions.</value>
</data>
<data name="UpdateWorkflow_Warning_Caption" xml:space="preserve">
<value>Update Workflow</value>
<data name="UpgradeWorkflow_Warning_Caption" xml:space="preserve">
<value>Workflow Upgraded</value>
</data>
<data name="SaveLayout_Error" xml:space="preserve">
<value>There was an error saving the visualizer layout:
Expand All @@ -228,8 +228,8 @@ Copyright (c) .NET Foundation and Contributors</value>
<data name="PackageUpdatesAvailable_Notification" xml:space="preserve">
<value>Package updates are available</value>
</data>
<data name="UpdateWorkflow_Error" xml:space="preserve">
<value>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.</value>
<data name="UpgradeWorkflow_Error" xml:space="preserve">
<value>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.</value>
</data>
<data name="Icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Bonsai.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
Expand Down
51 changes: 39 additions & 12 deletions Bonsai.Editor/UpgradeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -55,11 +55,25 @@ static void GetArgumentCount(ExpressionBuilderGraph workflow, Dictionary<Express
}
}

internal static ExpressionBuilderGraph UpgradeBuilderNodes(ExpressionBuilderGraph workflow, SemanticVersion version)
internal static bool TryUpgradeWorkflow(ExpressionBuilderGraph workflow, SemanticVersion version, out ExpressionBuilderGraph upgradedWorkflow)
{
var upgraded = false;
upgradedWorkflow = workflow;
if (version == null || IsDeprecated(version))
{
upgraded |= TryUpgradeEnumerableUnfoldingRules(workflow, version);
upgraded |= TryUpgradeBuilderNodes(workflow, version, out upgradedWorkflow);
}

return upgraded;
}

static bool TryUpgradeBuilderNodes(ExpressionBuilderGraph workflow, SemanticVersion version, out ExpressionBuilderGraph upgradedWorkflow)
{
var upgraded = false;
var argumentCount = new Dictionary<ExpressionBuilder, int>();
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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -243,14 +270,14 @@ static IEnumerable<WorkflowInputDependency> 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;
});
Expand All @@ -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());
}
}
}
Expand Down

0 comments on commit 2ded675

Please sign in to comment.