-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
354 lines (294 loc) · 14.9 KB
/
Program.cs
File metadata and controls
354 lines (294 loc) · 14.9 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using log4net;
using log4net.Config;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
namespace tds2scs
{
internal class Program
{
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static readonly IConfigurationRoot Config = GetAppSettings();
static void Main(string[] args)
{
var solutionFile = !string.IsNullOrEmpty(Config["solutionFile"]) ? Config["solutionFile"] : String.Empty;
var helixModule = Config["helixModule"];
foreach (string arg in args)
{
if (arg.EndsWith(".sln"))
{
solutionFile = arg;
}
}
if (string.IsNullOrEmpty(solutionFile) && !File.Exists(solutionFile))
{
Console.WriteLine($"ERROR: No solution file was specified or configured.");
OutputHelp();
Log.Error($"No solution file was specified.");
Environment.Exit(1);
}
var path = Path.GetFullPath(solutionFile);
var pathRoot = Path.GetDirectoryName(path);
var rtn = ParseTdsProjects(solutionFile, pathRoot, helixModule);
if (rtn)
{
Console.WriteLine($"Modules created! Go check Visual Studio.");
}
}
static bool ParseTdsProjects(string solutionFile, string pathRoot, string helixModule)
{
foreach (string projLine in File.ReadLines(solutionFile))
{
if (!projLine.StartsWith("Project(\"{CAA73BB0-EF22-4D79-A57E-DF67B3BA9C80}\""))
{
continue;
}
// It's a TDS project!
var tdsProjFile = projLine.Replace("Project(\"{CAA73BB0-EF22-4D79-A57E-DF67B3BA9C80}\") = ", "");
var tdsProjAr = tdsProjFile.Split(", ");
var tdsFile = $"{pathRoot}\\" + tdsProjAr[1].Replace("\"", "");
if (!File.Exists(tdsFile))
{
continue;
}
var projectName = Path.GetFileName(tdsFile).Replace(".scproj", "");
var modulePath = Path.GetFullPath(Path.Combine(tdsFile, @"..\..\..\"));
DirectoryInfo moduleDir = new DirectoryInfo(tdsFile);
// This will be the filename of the generated module
var moduleFilename = modulePath + projectName + ".module.json";
Log.Info($"=[ Parsing: {projectName} => {tdsFile}");
Log.Info($"==[ Creating module file: {moduleFilename} ");
// Create a rule list and an include list
var rules = new List<ScsModuleModel.Rule>();
var includes = new List<ScsModuleModel.Include>();
// Parse TDS file
using (var fileStream = File.Open(tdsFile, FileMode.Open))
{
// Open the TDS project file, and XML Deserialize
var tdsProjectBase = Path.GetDirectoryName(tdsFile);
XmlSerializer serializer = new XmlSerializer(typeof(TdsProjectModel.Project));
var tdsProj = (TdsProjectModel.Project)serializer.Deserialize(fileStream);
// Clean some things up on start of new project
var lastSection = string.Empty;
var firstPath = string.Empty;
var lastRule = string.Empty;
var filterItem = string.Empty;
var includePath = string.Empty;
var includeBasePath = string.Empty;
var addRules = new List<ScsModuleModel.Rule>();
var addIncludes = new List<ScsModuleModel.Include>();
List<string> tdsItems = new List<string>();
string tdsDatabase = "master";
// Now to parse the list of TDS items in the TDS Project and load in the file
foreach (var item in tdsProj.ItemGroup)
{
var itemFile = tdsProjectBase + "\\" + item.Include.ToString();
// it doesn't handle $(?) proper
if (itemFile.Contains("%2524"))
{
itemFile = itemFile.Replace("%2524", "%24");
}
foreach (string line in File.ReadLines(itemFile))
{
if (line.StartsWith("database:"))
{
var scDatabase = line[10..];
if (!string.IsNullOrEmpty(scDatabase))
tdsDatabase = scDatabase;
}
if (line.StartsWith("path:"))
{
var scPath = line[6..];
if (!string.IsNullOrEmpty(scPath))
tdsItems.Add(scPath);
}
}
}
var firstRuleAdded = false;
//now create the module
foreach (var scPath in tdsItems)
{
if (scPath.Contains($"Feature/{helixModule}", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"Foundation/{helixModule}", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"Project/{helixModule}", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"Content/{helixModule}", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"client/Your Apps", StringComparison.InvariantCultureIgnoreCase))
{
var newSection = false;
var scSection = GetSitecoreSection(scPath);
if (string.IsNullOrEmpty(lastSection))
lastSection = scSection;
if (scSection != lastSection)
newSection = true;
if (string.IsNullOrEmpty(includePath))
includeBasePath = includePath;
if (!scPath.Contains(firstPath) && firstRuleAdded)
firstRuleAdded = false;
// This handles most the helix type modules
if ((scPath.Contains($"Feature/{helixModule}/", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"Foundation/{helixModule}/", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"Project/{helixModule}/", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"Content/{helixModule}/", StringComparison.InvariantCultureIgnoreCase)
|| scPath.Contains($"client/Your Apps/", StringComparison.InvariantCultureIgnoreCase))
&& !firstRuleAdded)
{
// first added
firstPath = scPath;
includePath = scPath.Substring(0, firstPath.LastIndexOf('/'));
filterItem = scPath.Substring(firstPath.LastIndexOf('/'));
// create include here
Log.Info($"===[Rule]: {filterItem} / {scPath}");
var rule = new ScsModuleModel.Rule()
{
Path = filterItem,
AllowedPushOperations = "createAndUpdate",
Scope = "itemAndDescendants"
};
addRules.Add(rule);
firstRuleAdded = true;
}
// Create the include
if (newSection)
{
if (!string.IsNullOrEmpty(includePath))
{
Log.Info($"==[Include]: {scSection} / {includePath}");
var finalRule = new ScsModuleModel.Rule()
{
Path = "*",
Scope = "ignored",
};
addRules.Add(finalRule);
var include = new ScsModuleModel.Include()
{
AllowedPushOperations = "createAndUpdate",
Name = lastSection,
Path = includePath,
Scope = "itemAndDescendants",
Database = tdsDatabase,
Rules = new List<ScsModuleModel.Rule>(addRules),
};
if (!string.IsNullOrEmpty(Config["maxRelativePathLength"]))
{
include.MaxRelativePathLength = Config["maxRelativePathLength"];
}
addIncludes.Add(include);
// reset
addRules.Clear();
firstPath = string.Empty;
includePath = string.Empty;
filterItem = string.Empty;
lastSection = scSection;
}
else
{
Log.Warn($"==[Include]: Missing: {scSection} / {scPath}");
}
}
}
// If it's a system folder
else if (scPath.StartsWith($"/sitecore/system/", StringComparison.InvariantCultureIgnoreCase))
{
// We need to be careful with these ones
// TODO: Maybe support this, idk
}
else
{
Log.Warn($"Unable to find a place for {scPath}");
}
}
var items = new ScsModuleModel.Items()
{
Includes = addIncludes
};
if (!string.IsNullOrEmpty(Config["serialisationFolder"]))
{
items.Path = Config["serialisationFolder"];
}
var scsModule = new ScsModuleModel.Root()
{
Namespace = projectName,
Description = projectName,
Items = items,
};
// Create that module file yo
string json = JsonConvert.SerializeObject(scsModule, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(moduleFilename, json);
Log.Info($"Module written for {moduleFilename}");
}
}
return true;
}
static void OutputHelp()
{
Console.WriteLine($" .___..__ __. _, __. __ __.");
Console.WriteLine($" | | \\(__ '_) (__ / `(__ ");
Console.WriteLine($" | |__/.__) * / _. * .__)\\__..__)");
Console.WriteLine($"===================================[tds2scs]=");
Console.WriteLine("\n");
Console.WriteLine("Usage: ");
Console.WriteLine("> tds2scs <fullpathofsolution>");
Console.WriteLine("\n");
}
/// <summary>
/// Returns the 'section' in Sitecore, eg templates or media library
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static string GetSitecoreSection(string path)
{
// if it's one of the ones below, then do it
if (path.StartsWith("/sitecore/Layout/Layouts", StringComparison.InvariantCultureIgnoreCase))
{
return "Layouts";
}
if (path.StartsWith("/sitecore/Layout/Renderings", StringComparison.InvariantCultureIgnoreCase))
{
return "Renderings";
}
if (path.StartsWith("/sitecore/Layout/Placeholder Settings", StringComparison.InvariantCultureIgnoreCase))
{
return "Placeholders";
}
if (path.StartsWith("/sitecore/Media Library", StringComparison.InvariantCultureIgnoreCase))
{
return "Media Library";
}
if (path.StartsWith("/sitecore/system", StringComparison.InvariantCultureIgnoreCase))
{
return "System";
}
if (path.StartsWith("/sitecore/client/Your Apps", StringComparison.InvariantCultureIgnoreCase))
{
return "Your Apps";
}
return path.Split('/')[2];
}
#region Configuration
/// <summary>
/// Application specific configuration, including log4net config
/// </summary>
/// <returns></returns>
public static IConfigurationRoot GetAppSettings()
{
// get log4net config
// ref: https://jakubwajs.wordpress.com/2019/11/28/logging-with-log4net-in-net-core-3-0-console-app/
var logRepository = LogManager.GetRepository(System.Reflection.Assembly.GetEntryAssembly());
log4net.Config.XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
// get config settings from appsettings.json
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var config = builder.Build();
return config;
}
public static void DumpObj(object obj)
{
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented)); // your logger
}
#endregion
}
}