65 lines
3.2 KiB
C#
65 lines
3.2 KiB
C#
using Microsoft.CodeAnalysis.CSharp;
|
|
using Microsoft.CodeAnalysis.Text;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace PLCSharp.Models;
|
|
|
|
public sealed record RenamedScript(Guid Id, string Name, string Path, string Before, string After, WorkflowScriptReview Review);
|
|
public sealed record VariableRename(Guid VariableId, Guid RecipeId, string OldName, string NewName,
|
|
List<RenamedScript> Scripts, JsonArray ManualReferences, JsonArray ScanDiagnostics)
|
|
{
|
|
public bool Compiled => Scripts.All(s => s.Review.Compiled) && ScanDiagnostics.Count == 0;
|
|
[JsonIgnore] internal string Scope { get; init; }
|
|
[JsonIgnore] internal string OriginalVariables { get; init; }
|
|
public Guid? UndoOf { get; init; }
|
|
}
|
|
|
|
public sealed partial class ProjectDirectory
|
|
{
|
|
internal const string VariablesPath = "configuration/variables.json";
|
|
|
|
internal string RenameScope(Guid recipe)
|
|
{
|
|
var inputs = Flows(recipe).OrderBy(w => w.ID).Select(w => new { w.ID, w.Code });
|
|
return Convert.ToHexString(SHA256.HashData(JsonSerializer.SerializeToUtf8Bytes(inputs)));
|
|
}
|
|
|
|
internal void CheckVariableName(Guid recipe, Guid id, string oldName, string newName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(newName) || newName != newName.Trim() || newName == oldName)
|
|
throw new ArgumentException("请输入不同的新名称,首尾不能有空白。");
|
|
var rows = Data.Variables.Where(v => v.RecipeID == recipe).ToList();
|
|
if (!rows.Any(v => v.ID == id && v.Name == oldName) ||
|
|
rows.Any(v => v.ID != id && (v.Name == oldName || v.Name == newName)))
|
|
throw new ProjectConflictException("变量已更改或配方内存在重名。");
|
|
}
|
|
|
|
internal VariableRename PlanRename(Guid id, string newName)
|
|
{
|
|
var variable = Data.Variables.Single(v => v.ID == id);
|
|
CheckVariableName(variable.RecipeID, id, variable.Name, newName);
|
|
var analysis = WorkflowReferences.Find(this, "Variables", id.ToString(), forRename: true);
|
|
var hits = analysis["hits"].AsArray();
|
|
var manual = new JsonArray();
|
|
foreach (var hit in hits.Where(h => (string)h["confidence"] != "confirmed")) manual.Add(hit.DeepClone());
|
|
var edits = new List<RenamedScript>();
|
|
foreach (var group in hits.Where(h => (string)h["confidence"] == "confirmed").GroupBy(h => (Guid)h["workflowId"]))
|
|
{
|
|
var flow = Flows(variable.RecipeID).Single(w => w.ID == group.Key);
|
|
string replacement = SymbolDisplay.FormatLiteral(newName, quote: true);
|
|
var changes = group.Select(h => new TextChange(new TextSpan((int)h["start"], (int)h["length"]), replacement))
|
|
.Distinct().OrderBy(c => c.Span.Start);
|
|
var next = SourceText.From(flow.Code).WithChanges(changes).ToString();
|
|
edits.Add(new(flow.ID, flow.Name, ScriptPath(variable.RecipeID, flow.ID), flow.Code, next,
|
|
WorkflowScriptReview.Create(flow.ID, flow.Code, next)));
|
|
}
|
|
return new(id, variable.RecipeID, variable.Name, newName, edits, manual, analysis["diagnostics"].AsArray())
|
|
{ Scope = RenameScope(variable.RecipeID), OriginalVariables = baseline[VariablesPath] };
|
|
}
|
|
}
|
|
|