-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
122 lines (112 loc) · 6.28 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Elsa.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace ElsaConverter
{
class Program
{
static void Main(string[] args)
{
var path = @"C:\Users\Alex\Downloads\Workflows\";
foreach (var file in Directory.GetFiles(path))
{
var previousWF = JsonConvert.DeserializeObject<ElsaConverter.Previous.Elsa.Models.WorkflowDefinitionVersion>(File.ReadAllText(file));
var startActivityId = previousWF.Activities.Where(c => c.Type == "Start").FirstOrDefault().Id;
previousWF.DefinitionId = previousWF.Id = Path.GetFileNameWithoutExtension(file).Split(".").Last();
previousWF.Name = Path.GetFileNameWithoutExtension(file).Split(".").Skip(1).FirstOrDefault();
Elsa.Models.WorkflowDefinition workflowDefinition = new Elsa.Models.WorkflowDefinition
{
Id = previousWF.Id,
DefinitionId = previousWF.DefinitionId,
Description = previousWF.Description,
IsLatest = previousWF.IsLatest,
IsPublished = previousWF.IsPublished,
IsSingleton = previousWF.IsSingleton,
Name = previousWF.Name,
Version = previousWF.Version,
Activities = previousWF.Activities.Where(c => c.Type != "Start").Select(c => new ActivityDefinition
{
Name = c.Name,
DisplayName = c.DisplayName,
Description = c.Description,
ActivityId = c.Id,
Properties = buildProperties(c.State),
Type = c.Type switch
{
"CronEvent" => "Cron",
"Signaled" => "SignalReceived",
_ => c.Type
},
//c.State.ToObject<ElsaConverter.Previous.State>().Variables.Select(c => new ActivityDefinitionProperty { Expressions = c.Value == null?null: new Dictionary<string, string> { [c.Value.Syntax] = c.Value?.Expression?.ToString() }, Name = c.Key, Syntax = c.Value?.Syntax }).ToList()
}).ToList(),
Connections = previousWF.Connections.Where(c => c.SourceActivityId != startActivityId).Select(c => new ConnectionDefinition { Outcome = c.Outcome, SourceActivityId = c.SourceActivityId, TargetActivityId = c.DestinationActivityId }).ToList()
};
foreach (var item in workflowDefinition.Activities.Where(c => c.Name == null || c.DisplayName == null || c.Description == null))
{
var nameProp = item.Properties.FirstOrDefault(c => c.Name == "name");
if (nameProp != null)
item.Name = nameProp.Expressions?.FirstOrDefault().Value;
else item.Name = item.Type;
var displayProp = item.Properties.FirstOrDefault(c => c.Name == "title");
if (displayProp != null)
item.DisplayName = displayProp.Expressions?.FirstOrDefault().Value;
else item.DisplayName = item.Type;
var desc = item.Properties.FirstOrDefault(c => c.Name == "description");
if (desc != null)
item.Description = desc.Expressions?.FirstOrDefault().Value;
}
var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
serializerSettings.Formatting = Formatting.Indented;
var resultJson = JsonConvert.SerializeObject(workflowDefinition, serializerSettings);
resultJson = resultJson.Replace("json", "Json").Replace("branches", "Branches").Replace("literal", "Literal").Replace("liquid", "Liquid").Replace("javaScript", "JavaScript");
File.WriteAllText(Path.Combine(path, "out", Path.GetFileName(file)), resultJson);
}
}
private static ICollection<ActivityDefinitionProperty> buildProperties(JObject state)
{
List<ActivityDefinitionProperty> res = new List<ActivityDefinitionProperty>();
foreach (var item in state)
{
if (item.Key == "valueExpression")
{
res.Add(new ActivityDefinitionProperty { Name = "Value", Syntax = item.Value.Value<string>("syntax"), Expressions = new Dictionary<string, string> { [item.Value.Value<string>("syntax")] = item.Value.Value<string>("expression") } });
}
else {
var newAct =
new ActivityDefinitionProperty
{
Name = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(item.Key)/*item.Key switch
{
"branches" => "Branches",
_ => item.Key
}*/
,
Expressions = item.Value.HasValues ? new Dictionary<string, string>
{
[
item.Value.Contains("syntax") ?
item.Value?.Value<string>("syntax") : "Literal"] =
item.Value.SelectToken("expression") != null ?
item.Value?.Value<string>("expression") : item.Value.ToString()
} :
new Dictionary<string, string>
{
["value"] = item.Value.ToString()
}
};
if (newAct.Name == "Branches")
newAct.Expressions["Json"] = newAct.Expressions["Literal"];
res.Add(newAct);
}
}
return res;
}
}
}