-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
164 lines (137 loc) · 5.65 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Sitecore.Data.DataProviders.ReadOnly.Protobuf.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Rainbow.Storage;
using Rainbow.Storage.Yaml;
using System.Xml;
using CommandLine;
using Rainbow.Filtering;
using Rainbow.Model;
namespace yaml2dat
{
public class Options
{
[Option('p', "YamlPath", Required = true, HelpText = "Path to the yaml files")]
public string YamlPath { get; set; }
[Option('o', "OutputFile", Required = true, HelpText = "Path to the output file")]
public string OutputFile { get; set; }
}
public class Program
{
private static readonly List<string> log = new List<string>();
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args).WithParsed(RunOptions);
}
private static void RunOptions(Options options)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
string path = options.YamlPath;
try
{
path = new DirectoryInfo(options.YamlPath).FullName;
}
catch (ArgumentException)
{
Console.WriteLine($"I don't understand the path {path}");
return;
}
CreateDatFile(path, options.OutputFile);
stopwatch.Stop();
log.Add($"Elapsed time: {stopwatch.ElapsedMilliseconds / 1000} seconds");
foreach (string text in log)
{
Console.WriteLine(text);
}
}
private static void CreateDatFile(string physicalRootPath, string outputFileName)
{
ItemsData itemsData = new ItemsData();
XmlElement node = new XmlDocument().CreateElement("node");
SerializationFileSystemTree tree = new SerializationFileSystemTree("Yaml2dat",
"/sitecore",
"master",
physicalRootPath,
new YamlSerializationFormatter(node, new ConfigurationFieldFilter(node)), false);
List<IItemData> snapshot = tree.GetSnapshot().ToList();
log.Add($"Found {snapshot.Count} items to process");
foreach (IItemData item in snapshot)
{
AddItem(item, itemsData);
}
using FileStream fileStream = new FileInfo(outputFileName).Create();
ProtoBuf.Serializer.Serialize(fileStream, itemsData);
log.Add($"Created file: {outputFileName}");
}
private static void AddItem(IItemData item, ItemsData itemsData)
{
if (itemsData.Definitions.ContainsKey(item.Id))
{
log.Add($"Skipping yaml for path {item.Path} as the itemId already has been added");
return;
}
itemsData.Definitions.Add(item.Id, new ItemRecord
{
ID = item.Id,
MasterID = item.BranchId,
Name = item.Name,
ParentID = item.ParentId,
TemplateID = item.TemplateId
});
itemsData.SharedData.Add(item.Id, item.SharedFields.ToDictionary(key => key.FieldId, value => FixValue(value.Value)));
// extract the versioned fields
Dictionary<string, VersionsData> languageAndVersions = new Dictionary<string, VersionsData>();
foreach (IItemVersion version in item.Versions)
{
if (!languageAndVersions.ContainsKey(version.Language.ToString()))
{
languageAndVersions.Add(version.Language.ToString(), new VersionsData());
}
VersionsData versionData = languageAndVersions[version.Language.ToString()];
versionData.Add(version.VersionNumber, PopulateFieldsData(version));
}
// add the versioned fields
ItemLanguagesData languageFields = new ItemLanguagesData();
foreach (KeyValuePair<string, VersionsData> pair in languageAndVersions)
{
languageFields.Add(pair.Key, pair.Value);
}
// add the unversioned fields (hardcoded to version 0)
foreach (IItemLanguage language in item.UnversionedFields)
{
VersionsData versionsData = languageFields[language.Language.ToString()];
if (versionsData == null)
{
languageFields.Add(language.Language.ToString(), new VersionsData());
versionsData = languageFields[language.Language.ToString()];
}
versionsData.Add(0, PopulateFieldsData(language));
}
itemsData.LanguageData.Add(item.Id, languageFields);
}
private static FieldsData PopulateFieldsData(IItemLanguage language)
{
FieldsData fieldsData = new FieldsData();
foreach (IItemFieldValue fieldValue in language.Fields)
{
fieldsData.Add(fieldValue.FieldId, fieldValue.Value);
}
return fieldsData;
}
private static string FixValue(string value)
{
// this is an attempt to fix issues when converting from yaml to fields with multiple Sitecore IDs, probably because of using the Rainbow engine instead of the Sitecore yaml serialization logic.
// TODO: find better way of fixing this!
if (value.StartsWith("{") && value.Contains("}\r\n{") && value.EndsWith("}"))
{
return value.Replace("}\r\n{", "}|{");
}
return value;
}
}
}