178 lines
13 KiB
C#
178 lines
13 KiB
C#
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.Batch is { } batch) ShowBatch(batch);
|
||
else if (change.Edit is { } edit)
|
||
{
|
||
string action = edit.Action switch { "create" => "新增", "update" => "修改", _ => "删除" };
|
||
Title = $"确认{(edit.UndoOf.HasValue ? "撤销 · " : "")}{action}工程对象";
|
||
WorkflowTitle.Text = $"{action}{KindName(edit.Kind)}「{change.Name}」";
|
||
SaveChange.Content = edit.UndoOf.HasValue ? "确认撤销" : $"确认{action}";
|
||
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);
|
||
Location.Visibility = Visibility.Collapsed;
|
||
DetailsToggle.Visibility = Visibility.Visible;
|
||
ToggleDetails(this, null);
|
||
CheckResult.Text = edit.Workflows.Count == 0 ? "本次未发现需要编译的工作流。" : $"{edit.Workflows.Count} 个相关工作流通过编译,尚未运行。";
|
||
var summary = new List<string>();
|
||
if (edit.UndoOf.HasValue) summary.Add($"这将撤销之前的操作,{WorkflowTitle.Text}。");
|
||
if (edit.Action == "update")
|
||
foreach (var field in edit.After.Where(p => !System.Text.Json.Nodes.JsonNode.DeepEquals(edit.Before[p.Key], p.Value)))
|
||
{
|
||
var label = field.Key switch { "Comment" => "备注", "Code" => "工作流脚本", "ControlName" => "绑定控件", "AutomaticExecution" => "自动运行", "CycleDelayTime" => "循环间隔", _ => field.Key };
|
||
string Brief(System.Text.Json.Nodes.JsonNode value) => value == null ? "(空)" : value.ToString();
|
||
var from = Brief(edit.Before[field.Key]);
|
||
var to = Brief(field.Value);
|
||
summary.Add(from.Length + to.Length < 100 ? $"{label}由「{from}」改为「{to}」。" : $"{label}已修改,完整内容可在字段差异中查看。");
|
||
}
|
||
foreach (var owned in edit.OwnedRows) summary.Add($"{(owned.Restore ? "一并恢复" : "一并删除")}{KindName(owned.Kind)}「{owned.Name}」。");
|
||
foreach (var hit in edit.WorkflowReferences) summary.Add($"工作流「{hit["Name"]}」第 {hit["line"]} 行引用此对象。");
|
||
foreach (var reference in edit.ConfigReferences) summary.Add($"{KindName((string)reference["table"])}配置中的 {reference["field"]} 引用了此对象。");
|
||
if (edit.Action == "delete" && edit.WorkflowReferences.Count > 0)
|
||
summary.Add("删除后,保留的工作流按原名称查找时可能找不到对象。");
|
||
foreach (var hit in edit.ManualReferences)
|
||
summary.Add($"需核对工作流「{hit["Name"]}」第 {hit["line"]} 行。目标名称取决于运行时的值。");
|
||
if (summary.Count == 0) summary.Add("未发现关联的配置或工作流。");
|
||
ImpactSummary.Text = string.Join("\n", summary);
|
||
}
|
||
else if (change.Rename is { } rename)
|
||
{
|
||
var subject = KindName(rename.Kind);
|
||
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 ShowBatch(ProjectBatch batch)
|
||
{
|
||
bool undo = batch.UndoOf.HasValue;
|
||
Title = undo ? "确认整批撤销" : "确认整批保存";
|
||
WorkflowTitle.Text = $"{(undo ? "撤销" : "保存")}这批 {batch.Steps.Count} 项修改";
|
||
Location.Visibility = Visibility.Collapsed;
|
||
SaveChange.Content = undo ? "整批撤销" : "整批保存";
|
||
var lines = new List<string>();
|
||
var refs = new HashSet<string>();
|
||
var manual = new HashSet<string>();
|
||
var details = new List<string>(batch.ChangedFiles);
|
||
foreach (var step in undo ? batch.Steps.AsEnumerable().Reverse() : batch.Steps)
|
||
{
|
||
if (step.Edit is { } edit)
|
||
{
|
||
string action = (edit.Action, undo) switch { ("create", false) or ("delete", true) => "新增",
|
||
("delete", false) or ("create", true) => "删除", _ => "修改" };
|
||
var fields = new List<string>();
|
||
if (edit.Action == "update")
|
||
foreach (var (key, value) in edit.After)
|
||
{
|
||
if (System.Text.Json.Nodes.JsonNode.DeepEquals(edit.Before[key], value)) continue;
|
||
string from = (undo ? value : edit.Before[key])?.ToString() ?? "(空)";
|
||
string to = (undo ? edit.Before[key] : value)?.ToString() ?? "(空)";
|
||
string label = key switch { "Comment" => "备注", "Code" => "脚本", "ControlName" => "绑定控件", _ => key };
|
||
fields.Add(from.Length + to.Length <= 90 ? $"{label}由「{from}」改为「{to}」" : $"{label}有修改");
|
||
}
|
||
lines.Add($"{action}{KindName(edit.Kind)}「{step.Name}」" + (fields.Count == 0 ? "" : $",{string.Join(",", fields)}"));
|
||
foreach (var child in edit.OwnedRows) lines.Add($" 一并{(undo ? "恢复" : "删除")}{KindName(child.Kind)}「{child.Name}」");
|
||
foreach (var hit in edit.WorkflowReferences) refs.Add($"工作流「{hit["Name"]}」第 {hit["line"]} 行引用「{step.Name}」。");
|
||
foreach (var link in edit.ConfigReferences) refs.Add($"{KindName((string)link["table"])}的 {link["field"]} 引用「{step.Name}」。");
|
||
foreach (var hit in edit.ManualReferences) manual.Add($"「{hit["Name"]}」第 {hit["line"]} 行 · {hit["expression"]}");
|
||
details.Add($"{step.Name}\n{(undo ? edit.After : edit.Before)} → {(undo ? edit.Before : edit.After)}");
|
||
}
|
||
else if (step.Rename is { } rename)
|
||
{
|
||
lines.Add($"{KindName(rename.Kind)}「{(undo ? rename.NewName : rename.OldName)}」改名为「{(undo ? rename.OldName : rename.NewName)}」");
|
||
foreach (var flow in rename.Scripts) refs.Add($"工作流「{flow.Name}」同步更新。");
|
||
foreach (var hit in rename.ManualReferences) manual.Add($"「{hit["Name"]}」第 {hit["line"]} 行 · {hit["expression"]}");
|
||
foreach (var hit in rename.PointReferences.Concat(rename.MatrixReferences).Concat(rename.CylinderReferences))
|
||
refs.Add($"{KindName(hit.Table)}的 {hit.Field} 同步更新。");
|
||
foreach (var hit in rename.VisionReferences) refs.Add($"视觉功能的 {hit.Key} 同步更新。");
|
||
foreach (var flow in rename.Scripts) details.Add(flow.Review.Diff);
|
||
}
|
||
}
|
||
if (refs.Count > 0) { lines.Add("\n受影响的工作流和配置"); lines.AddRange(refs); }
|
||
if (manual.Count > 0) { lines.Add("\n需要人工核对的引用"); lines.AddRange(manual); }
|
||
CheckResult.Text = batch.Workflows.Count == 0 ? "本批没有需要编译的工作流。" :
|
||
$"工作流编译通过(未运行):{string.Join("、", batch.Workflows.Select(f=>f.Name))}";
|
||
foreach (var f in batch.Workflows)
|
||
foreach (var diagnostic in f.Diagnostics) details.Add($"{f.Name} · {diagnostic.Code} · {diagnostic.Message}");
|
||
ImpactSummary.Text = string.Join("\n", lines);
|
||
CodeDiff.Text = string.Join("\n", details);
|
||
DetailsToggle.Visibility = Visibility.Visible;
|
||
ToggleDetails(this, null);
|
||
}
|
||
|
||
private static string KindName(string kind) => 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" => "点位", "Controllers" => "运动控制器", "CanvasConfigs" => "主页画布", "Variables" => "配方变量", _ => kind };
|
||
|
||
private void ToggleDetails(object sender, RoutedEventArgs e)
|
||
{
|
||
bool details = DetailsToggle.IsChecked == true;
|
||
CodeDiff.Visibility = details ? Visibility.Visible : Visibility.Collapsed;
|
||
SummaryPanel.Visibility = details ? Visibility.Collapsed : Visibility.Visible;
|
||
}
|
||
|
||
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();
|
||
}
|
||
}
|
||
|