plcsharp/PLCSharpWpf/VVMs/Workflows/ScriptReviewWindow.xaml.cs

86 lines
5.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using PLCSharp.Models;
using System.Threading;
using System.Windows;
namespace PLCSharp.VVMs.Workflows;
public partial class ScriptReviewWindow : Window
{
private ScriptReviewWindow(ProjectChange change, string root)
{
InitializeComponent();
if (change.Edit is { } edit)
{
string action = edit.Action switch { "create" => "新增", "update" => "修改", _ => "删除" };
Title = $"确认{(edit.UndoOf.HasValue ? "撤销 · " : "")}{action}工程对象";
WorkflowTitle.Text = $"{edit.Kind} · {change.Name} · {edit.Id}";
Location.Text = System.IO.Path.Combine(root, edit.Path);
CheckResult.Text = $"已编译工作流 {edit.Workflows.Count},工作流引用 {edit.WorkflowReferences.Count},配置引用 {edit.ConfigReferences.Count},待核对 {edit.ManualReferences.Count}";
var lines = new List<string>();
foreach (var hit in edit.WorkflowReferences) lines.Add($"工作流 {hit["Name"]} · {hit["path"]}:{hit["line"]} {hit["expression"]}");
foreach (var link in edit.ConfigReferences) lines.Add($"配置 {link["path"]} · {link["table"]}/{link["id"]} · {link["field"]}");
foreach (var hit in edit.ManualReferences) lines.Add($"待核对 [{hit["confidence"]}] {hit["path"]}:{hit["line"]} {hit["expression"]}");
if (edit.Action == "delete" && lines.Count > 0) lines.Add("确认删除将保留上列引用,请核对删除后的影响。");
foreach (var owned in edit.OwnedRows) lines.Add($"{(owned.Restore ? "恢复" : "随对象删除")} {owned.Kind} · {owned.Name} · {owned.Id}");
foreach (var path in edit.ChangedFiles) lines.Add($"文件 {path}");
foreach (var key in (edit.Before ?? edit.After).Select(k => k.Key))
if (!System.Text.Json.Nodes.JsonNode.DeepEquals(edit.Before?[key], edit.After?[key]))
lines.Add($"{key}:{edit.Before?[key]} → {edit.After?[key]}");
CodeDiff.Text = string.Join("\n", lines);
}
else if (change.Rename is { } rename)
{
var subject = rename.Kind switch { "VisionLocalVariables" => "视觉局部变量", "ControlMeasurements" => "控件测量项",
"InterpolationSegments" => "插补段", "InterpolationPoints" => "插补段内点", "Recipes" => "配方", "SystemVariables" => "系统变量", "CustomControls" => "主页控件",
"Matrices" => "轴矩阵", "RobotMatrices" => "机器人矩阵", "InterpolationGroups" => "插补组", "Cameras" => "相机", "ImageDatas" => "视觉图像数据", "LocalImageDatas" => "本地图像数据", "Cylinders" => "气缸", "Robots" => "机器人", "Connects" => "通信连接",
"DI" => "数字输入", "DQ" => "数字输出", "Axes" => "运动轴", "VisionFunctions" => "视觉功能", "Workflows" => "工作流",
"AxisPoints" or "RobotPoints" => "点位", _ => "变量" };
Title = $"确认{(rename.UndoOf.HasValue ? "撤销" : "")}{subject}重命名";
WorkflowTitle.Text = $"{rename.OldName} → {rename.NewName}";
Location.Text = System.IO.Path.Combine(root, change.Path);
CheckResult.Text = $"已通过编译的工作流:{rename.Scripts.Count},需人工核对的引用:{rename.ManualReferences.Count}";
CodeDiff.Text = string.Join("\n", rename.PointReferences.Select(p => $"configuration/points.json · {p.Table}/{p.Id} · {p.Field}:{rename.OldName} → {rename.NewName}")
.Concat(rename.CylinderReferences.Select(p => $"configuration/hardware.json · {p.Table}/{p.Id} · {p.Field}:{rename.OldName} → {rename.NewName}"))
.Concat(rename.VisionReferences.Select(e => $"configuration/vision.json · {e.FunctionId} · {(e.Index < 0 ? "功能" : $"步骤 {e.Index + 1}")} · {e.Key}:{rename.OldName} → {rename.NewName}"))
.Concat(rename.MatrixReferences.Select(m => $"{m.Table}/{m.Id} · {m.Field}:{rename.OldName} → {rename.NewName}"))
.Concat(rename.ManualReferences.Select(h =>
$"待核对 [{h["confidence"]}] {h["path"]}:{h["line"]} {h["expression"]}"))
.Concat(rename.Scripts.Select(s => $"\n{s.Path} · {s.Name}\n{s.Review.Diff}")));
}
else
{
WorkflowTitle.Text = $"工作流 · {change.Name}";
Location.Text = System.IO.Path.Combine(root, change.Path);
CheckResult.Text = string.Join("\n", new[] { "编译通过(仅检查,未运行)" }.Concat(
change.Review.Diagnostics.Select(d => $"{d.Code} ({d.Line},{d.Column}) {d.Message}")));
CodeDiff.Text = change.Review.Diff;
}
Loaded += (_, _) => CancelChange.Focus();
}
private void Approve(object sender, RoutedEventArgs e) => DialogResult = true;
public static bool Confirm(ProjectChange change, string root, Window owner)
{
var window = new ScriptReviewWindow(change, root);
if (owner != null) { window.Owner = owner; window.WindowStartupLocation = WindowStartupLocation.CenterOwner; }
return window.ShowDialog() == true;
}
public static bool ConfirmOffline(ProjectChange change, string root)
{
if (!Environment.UserInteractive) throw new InvalidOperationException("脚本保存需要交互式 Windows 桌面。");
var result = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var thread = new Thread(() =>
{
try { result.SetResult(Confirm(change, root, null)); }
catch (Exception ex) { result.SetException(ex); }
finally { System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown(); }
}) { IsBackground = true };
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return result.Task.GetAwaiter().GetResult();
}
}