-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMCGenerator.cs
118 lines (98 loc) · 4.44 KB
/
MCGenerator.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
namespace MessengerComparison
{
class MCGenerator
{
static string RepresentationPath => Path.Combine("representation");
static string DataPath => Path.Combine("data");
static string OutputPath => Path.Combine("Output");
static int Main(string[] args)
{
try
{
//Step 1: Prepare Output folder by copying files from representation
CopyFolder(RepresentationPath, OutputPath);
//Step 2: Get a list of data languages
MCHtmlBuilder.Languages = GetLanguages(DataPath);
//Step 3: Determine original comparison updated date
MCHtmlBuilder.OriginalUpdatedDate = GetLastModifiedDateFromGit(
Path.Combine(DataPath, "uk", "comparison-data.json"));
//Step 4: Go over languages
foreach (var lang in MCHtmlBuilder.Languages)
{
//Step 5: Prepare the data for a language
var generalDataPath = Path.Combine(DataPath, lang, "general-data.json");
var comparisonDataPath = Path.Combine(DataPath, lang, "comparison-data.json");
var generalData = GetObjectFromFile<Dictionary<string,string>>(generalDataPath);
var comparisonData = GetObjectFromFile<List<Group>>(comparisonDataPath);
DateTime lastModified = GetLastModifiedDateFromGit(comparisonDataPath);
//Step 6: Generate HTML file from the data for a language
string html = new MCHtmlBuilder(lang, generalData, comparisonData, lastModified)
.Build();
//Step 7: Save HTML file to the Output folder for a language
var outputFilePath = Path.Combine(OutputPath, lang, "index.html");
new FileInfo(outputFilePath).Directory.Create();
File.WriteAllText(outputFilePath, html);
}
return 0;
}
catch
{
return -1;
}
}
static void CopyFolder(string inputFolderPath, string outputFolderPath)
{
//Create all the child directories
foreach (string dirPath in Directory.GetDirectories(inputFolderPath, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(inputFolderPath, outputFolderPath));
//Copy all the files and replace any files with the same name
foreach (string filePath in Directory.GetFiles(inputFolderPath, "*.*",
SearchOption.AllDirectories))
File.Copy(filePath, filePath.Replace(inputFolderPath, outputFolderPath), true);
}
static List<string> GetLanguages(string dataFolder)
{
var result = new List<string>();
foreach (string dirPath in Directory.GetDirectories(dataFolder, "*"))
result.Add(new DirectoryInfo(dirPath).Name);
result.Sort();
return result;
}
static T GetObjectFromFile<T>(string filePath)
{
var jsonString = File.ReadAllText(filePath);
return JsonSerializer.Deserialize<T>(jsonString);
}
static DateTime GetLastModifiedDateFromGit(string filePath)
{
try
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = $"log -1 --format=%cd --date=unix {filePath}",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string unixTime = process.StandardOutput.ReadToEnd().TrimEnd('\n');
process.WaitForExit();
return unixTime.ToDateTime();
}
catch
{
return DateTime.UtcNow;
}
}
}
}