Plan recipe-scoped workflow renames from bound script references
This commit is contained in:
parent
3bd6a7872e
commit
2d4805a777
@ -7,7 +7,7 @@ using System.Text.Json.Nodes;
|
||||
namespace PLCSharp.Models;
|
||||
|
||||
public sealed record ProjectChange(Guid DraftId, Guid ProjectId, string Kind, Guid ObjectId, Guid RecipeId,
|
||||
string Name, string Path, string Before, string After, string Revision, string Status, WorkflowScriptReview Review = null, VariableRename Rename = null);
|
||||
string Name, string Path, string Before, string After, string Revision, string Status, WorkflowScriptReview Review = null, ProjectRename Rename = null);
|
||||
|
||||
public sealed class ProjectChanges
|
||||
{
|
||||
@ -28,7 +28,7 @@ public sealed class ProjectChanges
|
||||
{
|
||||
if (operation is "previewRename" or "previewUndo")
|
||||
{
|
||||
VariableRename plan;
|
||||
ProjectRename plan;
|
||||
if (operation == "previewRename")
|
||||
plan = project.PlanRename(Guid.Parse((string)args["id"]), (string)args["newName"]);
|
||||
else
|
||||
@ -38,7 +38,7 @@ public sealed class ProjectChanges
|
||||
throw new ArgumentException("需要当前会话中已保存且尚未撤销的变量重命名 ID。");
|
||||
plan = project.PlanUndo(saved.DraftId, saved.Rename);
|
||||
}
|
||||
return Remember(new(Guid.NewGuid(), project.Id, "Variables", plan.VariableId, plan.RecipeId, plan.OldName,
|
||||
return Remember(new(Guid.NewGuid(), project.Id, "Variables", plan.ObjectId, plan.RecipeId, plan.OldName,
|
||||
ProjectDirectory.VariablesPath, plan.OldName, plan.NewName, project.Revision(ProjectDirectory.VariablesPath),
|
||||
plan.Compiled ? "pending" : "invalid", Rename: plan));
|
||||
}
|
||||
|
||||
@ -11,14 +11,17 @@ 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,
|
||||
public sealed record ProjectRename(Guid ObjectId, Guid RecipeId, string OldName, string NewName,
|
||||
List<RenamedScript> Scripts, JsonArray ManualReferences, JsonArray ScanDiagnostics)
|
||||
{
|
||||
public string Kind { get; init; } = "Variables";
|
||||
public Guid? VariableId => Kind == "Variables" ? ObjectId : null;
|
||||
public string MetadataPath => Kind == "Workflows" ? $"recipes/{RecipeId:D}/recipe.json" : ProjectDirectory.VariablesPath;
|
||||
public bool Compiled => Scripts.All(s => s.Review.Compiled) && ScanDiagnostics.Count == 0;
|
||||
[JsonIgnore] internal string Scope { get; init; }
|
||||
[JsonIgnore] internal string OriginalVariables { get; init; }
|
||||
[JsonIgnore] internal string OriginalMetadata { get; init; }
|
||||
[JsonIgnore] internal string SavedScope { get; set; }
|
||||
[JsonIgnore] internal string SavedVariables { get; set; }
|
||||
[JsonIgnore] internal string SavedMetadata { get; set; }
|
||||
public Guid? UndoOf { get; init; }
|
||||
}
|
||||
|
||||
@ -28,7 +31,7 @@ public sealed partial class ProjectDirectory
|
||||
|
||||
internal string RenameScope(Guid recipe)
|
||||
{
|
||||
var inputs = Flows(recipe).OrderBy(w => w.ID).Select(w => new { w.ID, w.Code });
|
||||
var inputs = Flows(recipe).OrderBy(w => w.ID).Select(w => new { w.ID, w.Name, w.Code });
|
||||
return Convert.ToHexString(SHA256.HashData(JsonSerializer.SerializeToUtf8Bytes(inputs)));
|
||||
}
|
||||
|
||||
@ -42,54 +45,76 @@ public sealed partial class ProjectDirectory
|
||||
throw new ProjectConflictException("变量已更改或配方内存在重名。");
|
||||
}
|
||||
|
||||
internal VariableRename PlanRename(Guid id, string newName)
|
||||
internal ProjectRename 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);
|
||||
return PlanReferences("Variables", id, variable.RecipeID, variable.Name, newName);
|
||||
}
|
||||
|
||||
internal ProjectRename PlanWorkflowRename(Guid id, string newName)
|
||||
{
|
||||
var workflow = Recipes.SelectMany(r => Flows(r.ID)).Single(w => w.ID == id);
|
||||
CheckWorkflowName(workflow.RecipeID, id, workflow.Name, newName);
|
||||
return PlanReferences("Workflows", id, workflow.RecipeID, workflow.Name, newName);
|
||||
}
|
||||
|
||||
private void CheckWorkflowName(Guid recipeId, Guid id, string oldName, string newName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(newName) || newName != newName.Trim() || newName == oldName)
|
||||
throw new ArgumentException("请输入不同的新名称,首尾不能有空白。");
|
||||
var flows = Flows(recipeId);
|
||||
if (!flows.Any(w => w.ID == id && w.Name == oldName) ||
|
||||
flows.Any(w => w.ID != id && (w.Name == oldName || w.Name == newName)))
|
||||
throw new ProjectConflictException("工作流已更改或配方内存在重名。");
|
||||
}
|
||||
|
||||
private ProjectRename PlanReferences(string kind, Guid id, Guid recipeId, string oldName, string newName)
|
||||
{
|
||||
var analysis = WorkflowReferences.Find(this, kind, 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);
|
||||
var flow = Flows(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,
|
||||
edits.Add(new(flow.ID, flow.Name, ScriptPath(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] };
|
||||
return new(id, recipeId, oldName, newName, edits, manual, analysis["diagnostics"].AsArray())
|
||||
{ Kind = kind, Scope = RenameScope(recipeId), OriginalMetadata = baseline[kind == "Workflows" ? RecipePath(recipeId) : VariablesPath] };
|
||||
}
|
||||
internal VariableRename PlanUndo(Guid operationId, VariableRename saved)
|
||||
internal ProjectRename PlanUndo(Guid operationId, ProjectRename saved)
|
||||
{
|
||||
if (saved.SavedScope != RenameScope(saved.RecipeId) || saved.SavedVariables != baseline[VariablesPath])
|
||||
if (saved.SavedScope != RenameScope(saved.RecipeId) || saved.SavedMetadata != baseline[VariablesPath])
|
||||
throw new ProjectConflictException("保存后配方脚本或变量文件已改变,无法安全撤销。");
|
||||
CheckVariableName(saved.RecipeId, saved.VariableId, saved.NewName, saved.OldName);
|
||||
CheckVariableName(saved.RecipeId, saved.ObjectId, saved.NewName, saved.OldName);
|
||||
var inverse = saved.Scripts.Select(s => s with { Before = s.After, After = s.Before,
|
||||
Review = WorkflowScriptReview.Create(s.Id, s.After, s.Before) }).ToList();
|
||||
return new(saved.VariableId, saved.RecipeId, saved.NewName, saved.OldName, inverse,
|
||||
return new(saved.ObjectId, saved.RecipeId, saved.NewName, saved.OldName, inverse,
|
||||
saved.ManualReferences, new JsonArray()) { UndoOf = operationId, Scope = saved.SavedScope,
|
||||
OriginalVariables = saved.SavedVariables, SavedVariables = saved.OriginalVariables };
|
||||
OriginalMetadata = saved.SavedMetadata, SavedMetadata = saved.OriginalMetadata };
|
||||
}
|
||||
|
||||
internal void ApplyRename(VariableRename plan)
|
||||
internal void ApplyRename(ProjectRename plan)
|
||||
{
|
||||
lock (Data.Sync)
|
||||
{
|
||||
CheckVariableName(plan.RecipeId, plan.VariableId, plan.OldName, plan.NewName);
|
||||
if (RenameScope(plan.RecipeId) != plan.Scope || baseline[VariablesPath] != plan.OriginalVariables)
|
||||
CheckVariableName(plan.RecipeId, plan.ObjectId, plan.OldName, plan.NewName);
|
||||
if (RenameScope(plan.RecipeId) != plan.Scope || baseline[VariablesPath] != plan.OriginalMetadata)
|
||||
throw new ProjectConflictException("变量或工作流已经改变,请重新预览。");
|
||||
var files = new Dictionary<string, string>(baseline);
|
||||
var variables = JsonNode.Parse(files[VariablesPath]);
|
||||
var row = variables["Variables"].AsArray().Single(v => Guid.Parse((string)v["ID"]) == plan.VariableId);
|
||||
var row = variables["Variables"].AsArray().Single(v => Guid.Parse((string)v["ID"]) == plan.ObjectId);
|
||||
if ((string)row["Name"] != plan.OldName)
|
||||
throw new ProjectConflictException("变量名有未保存的编辑,请先保存或还原。");
|
||||
row["Name"] = plan.NewName;
|
||||
files[VariablesPath] = plan.UndoOf.HasValue ? plan.SavedVariables : Serialize(variables);
|
||||
files[VariablesPath] = plan.UndoOf.HasValue ? plan.SavedMetadata : Serialize(variables);
|
||||
var recipePath = RecipePath(plan.RecipeId);
|
||||
var flows = Flows(plan.RecipeId);
|
||||
var persisted = JsonNode.Parse(files[recipePath])["workflows"].AsArray().Select(w => Guid.Parse((string)w["id"]));
|
||||
@ -115,7 +140,7 @@ public sealed partial class ProjectDirectory
|
||||
PLCSharp.VVMs.Workflows.Workflow.SaveReviewedBatch(updates, () =>
|
||||
{
|
||||
using var transaction = Data.Database.BeginTransaction();
|
||||
if (Data.Variables.Where(v => v.ID == plan.VariableId && v.Name == plan.OldName)
|
||||
if (Data.Variables.Where(v => v.ID == plan.ObjectId && v.Name == plan.OldName)
|
||||
.ExecuteUpdate(s => s.SetProperty(v => v.Name, plan.NewName)) != 1)
|
||||
throw new ProjectConflictException("变量已发生变化。");
|
||||
try { WriteFiles(Root, files, scope.ToArray(), transaction.Commit); }
|
||||
@ -125,12 +150,12 @@ public sealed partial class ProjectDirectory
|
||||
throw new ProjectConflictException("工程文件已从外部修改,请重新打开并预览。");
|
||||
}
|
||||
configuration[VariablesPath] = files[VariablesPath];
|
||||
var variable = Data.Variables.Local.Single(v => v.ID == plan.VariableId);
|
||||
var variable = Data.Variables.Local.Single(v => v.ID == plan.ObjectId);
|
||||
variable.Name = plan.NewName;
|
||||
Data.Entry(variable).Property(v => v.Name).OriginalValue = plan.NewName;
|
||||
});
|
||||
plan.SavedScope = RenameScope(plan.RecipeId);
|
||||
plan.SavedVariables = files[VariablesPath];
|
||||
plan.SavedMetadata = files[VariablesPath];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -65,13 +65,13 @@ internal static class WorkflowReferences
|
||||
{
|
||||
if (model.GetSymbolInfo(member).Symbol is IPropertySymbol property && property.Name == "Name" &&
|
||||
property.ContainingType.ToDisplayString() == targetType.FullName)
|
||||
Record(value, comparison, !forRename || IsRecipeVariablePredicate(member, model));
|
||||
Record(value, comparison, !forRename || IsRecipePredicate(member, model, kind));
|
||||
}
|
||||
}
|
||||
}
|
||||
return JsonSerializer.SerializeToNode(new { recipeId, targetId = id, name, hits, diagnostics });
|
||||
}
|
||||
private static bool IsRecipeVariablePredicate(ExpressionSyntax expression, SemanticModel model)
|
||||
private static bool IsRecipePredicate(ExpressionSyntax expression, SemanticModel model, string kind)
|
||||
{
|
||||
if (expression is not MemberAccessExpressionSyntax access ||
|
||||
model.GetSymbolInfo(access.Expression).Symbol is not IParameterSymbol parameter ||
|
||||
@ -81,8 +81,15 @@ internal static class WorkflowReferences
|
||||
call.Expression is not MemberAccessExpressionSyntax method ||
|
||||
model.GetSymbolInfo(call).Symbol is not IMethodSymbol resolved ||
|
||||
resolved.ReducedFrom?.ContainingType.ToDisplayString() != "System.Linq.Enumerable") return false;
|
||||
return model.GetSymbolInfo(method.Expression).Symbol is IPropertySymbol source &&
|
||||
source.Name == "Variables" && source.ContainingType.ToDisplayString() == typeof(VariablesModel).FullName;
|
||||
if (model.GetSymbolInfo(method.Expression).Symbol is not IPropertySymbol source) return false;
|
||||
return kind switch
|
||||
{
|
||||
"Workflows" => source.Name == nameof(WorkflowsModel.Workflows) &&
|
||||
source.ContainingType.ToDisplayString() == typeof(WorkflowsModel).FullName,
|
||||
"Variables" => source.Name == nameof(VariablesModel.Variables) &&
|
||||
source.ContainingType.ToDisplayString() == typeof(VariablesModel).FullName,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user