-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.cmd
356 lines (333 loc) · 13.2 KB
/
pack.cmd
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
355
356
<# :
@echo off & setlocal & set __args=%* & %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command Invoke-Expression ('. { ' + (Get-Content -LiteralPath ""%~f0"" -Raw) + ' }' + $env:__args) & exit /b %ERRORLEVEL%
#> Add-Type @'
// ========== BEGIN C# CODE ==========
// This script performs the following tasks:
// - update version and year in all source and documentation files (if changed)
// - create the distribution archives
// - update file sizes in readme file
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Threading;
public class App
{
public static void Run(string[] args)
{
// Read latest versions from change log
string version = GetVersion(@"doc\changes.html");
string versionYear = GetVersionYear(@"doc\changes.html");
Console.WriteLine("Version " + version + ", Copyright " + versionYear);
// Write version to source and documentation files
Console.WriteLine("Updating version and year in all files...");
bool modified = false;
// Code
modified |= UpdateFile(@"src\js\arraylist.js", @"(ArrayList\.version = "")[^""]*("";)", "${1}" + version + "$2");
// File license headers
string headerPattern = @"(/\*!\s*[0-9a-z-]+.(?:scss|js)\s+v)\S*";
string headerReplacement = "${1}" + version;
modified |= UpdateFile(@"src\css\frontfire-ui-complete.scss", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\css\frontfire-ui-minimal.scss", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\arraylist.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\color.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\datacolor.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\frontfire-core.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\frontfire-core-singlefile.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\frontfire-ui-complete.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\frontfire-ui-complete-singlefile.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\frontfire-ui-minimal.js", headerPattern, headerReplacement);
modified |= UpdateFile(@"src\js\frontfire-ui-minimal-singlefile.js", headerPattern, headerReplacement);
// Documentation
string versionPattern = @"(<span\s+id=""version-id"">).*?(</span>)";
string versionReplacement = "${1}" + version + "$2";
modified |= UpdateFile(@"doc\*.html", versionPattern, versionReplacement);
// Write copyright year to source and documentation files
// File license headers
modified |= UpdateFileYear(@"src\css\frontfire-ui-complete.scss", versionYear);
modified |= UpdateFileYear(@"src\css\frontfire-ui-minimal.scss", versionYear);
modified |= UpdateFileYear(@"src\js\arraylist.js", versionYear);
modified |= UpdateFileYear(@"src\js\color.js", versionYear);
modified |= UpdateFileYear(@"src\js\datacolor.js", versionYear);
modified |= UpdateFileYear(@"src\js\frontfire-core.js", versionYear);
modified |= UpdateFileYear(@"src\js\frontfire-core-singlefile.js", versionYear);
modified |= UpdateFileYear(@"src\js\frontfire-ui-complete.js", versionYear);
modified |= UpdateFileYear(@"src\js\frontfire-ui-complete-singlefile.js", versionYear);
modified |= UpdateFileYear(@"src\js\frontfire-ui-minimal.js", versionYear);
modified |= UpdateFileYear(@"src\js\frontfire-ui-minimal-singlefile.js", versionYear);
// Documentation
modified |= UpdateFileYear(@"doc\*.html", versionYear);
if (modified)
{
Console.WriteLine();
Console.WriteLine("NOTE: Files have been modified.");
Console.WriteLine("Please wait until the build files are updated,");
Console.WriteLine("then press Enter to continue.");
Console.ReadLine();
}
else
{
Console.WriteLine("All files up-to-date.");
}
// Remove all previous dist files
Console.WriteLine("Creating distribution archive...");
if (Directory.Exists("dist"))
Directory.Delete("dist", true);
Directory.CreateDirectory("dist");
Directory.SetCurrentDirectory("dist");
// Create new archive's directories and collect all files
string dir = "js";
Copy(@"..\src\js\arraylist.js", dir);
Copy(@"..\src\js\color.js", dir);
Copy(@"..\src\js\datacolor.js", dir);
Copy(@"..\src\js\frontfire-core.js", dir);
Copy(@"..\src\js\build\frontfire-ui-minimal.js*", dir);
Copy(@"..\src\js\build\frontfire-ui-complete.js*", dir);
dir = "js-min";
Copy(@"..\src\js\build\arraylist.min.js*", dir);
Copy(@"..\src\js\build\color.min.js*", dir);
Copy(@"..\src\js\build\datacolor.min.js*", dir);
Copy(@"..\src\js\build\frontfire-core.min.js*", dir);
Copy(@"..\src\js\build\frontfire-ui-minimal.min.js*", dir);
Copy(@"..\src\js\build\frontfire-ui-complete.min.js*", dir);
dir = "js-singlefile";
Copy(@"..\src\js\build\frontfire-core-singlefile.js*", dir);
Copy(@"..\src\js\build\frontfire-ui-minimal-singlefile.js*", dir);
Copy(@"..\src\js\build\frontfire-ui-complete-singlefile.js*", dir);
dir = "js-singlefile-min";
Copy(@"..\src\js\build\frontfire-core-singlefile.min.js*", dir);
Copy(@"..\src\js\build\frontfire-ui-minimal-singlefile.min.js*", dir);
Copy(@"..\src\js\build\frontfire-ui-complete-singlefile.min.js*", dir);
dir = "css";
Copy(@"..\src\css\build\frontfire-ui-minimal.css*", dir);
Copy(@"..\src\css\build\frontfire-ui-complete.css*", dir);
dir = "css-min";
Copy(@"..\src\css\build\frontfire-ui-minimal.min.css*", dir);
Copy(@"..\src\css\build\frontfire-ui-complete.min.css*", dir);
dir = "doc";
Copy(@"..\doc\*.html", dir);
Copy(@"..\license.txt", dir);
File.WriteAllText(@"doc\version.txt", version);
dir = @"doc\res";
Copy(@"..\doc\res\*", dir);
Copy(@"..\logo\frontfire.svg", dir);
dir = "";
Copy(@"..\readme.md", dir);
// Adjust include paths in doc files
UpdateFile(@"doc\*.html", @"\.\./src/css/build", "../css-min", true);
UpdateFile(@"doc\*.html", @"\.\./logo", "res", true);
UpdateFile(@"doc\*.html", @"\.\./src/js/build", "../js-singlefile-min", true);
UpdateFile(@".\readme.md", @"src=""logo/", @"src=""doc/res/", true);
UpdateFile(@".\readme.md", @"\(license\.txt\)", "(doc/license.txt)", true);
// Create the archive
Exec("7za", "a frontfire_" + version + ".zip js js-min js-singlefile js-singlefile-min css css-min doc readme.md");
Exec("7za", "a frontfire_" + version + ".7z js js-min js-singlefile js-singlefile-min css css-min doc readme.md");
// Delete the uncompressed files, leaving only the archive in the dist directory
Directory.Delete("js", true);
Directory.Delete("js-min", true);
Directory.Delete("js-singlefile", true);
Directory.Delete("js-singlefile-min", true);
Directory.Delete("css", true);
Directory.Delete("css-min", true);
Directory.Delete("doc", true);
File.Delete("readme.md");
Directory.SetCurrentDirectory("..");
// Update minified compressed file sizes in readme file
Console.WriteLine("Updating minified compressed file sizes in readme...");
modified = false;
modified |= UpdateFileSize(@"src\css\build\frontfire-ui-minimal.min.css");
modified |= UpdateFileSize(@"src\css\build\frontfire-ui-complete.min.css");
modified |= UpdateFileSize(@"src\js\build\arraylist.min.js");
modified |= UpdateFileSize(@"src\js\build\color.min.js");
modified |= UpdateFileSize(@"src\js\build\datacolor.min.js");
modified |= UpdateFileSize(@"src\js\build\frontfire-core.min.js");
modified |= UpdateFileSize(@"src\js\build\frontfire-core-singlefile.min.js");
modified |= UpdateFileSize(@"src\js\build\frontfire-ui-minimal.min.js");
modified |= UpdateFileSize(@"src\js\build\frontfire-ui-minimal-singlefile.min.js");
modified |= UpdateFileSize(@"src\js\build\frontfire-ui-complete.min.js");
modified |= UpdateFileSize(@"src\js\build\frontfire-ui-complete-singlefile.min.js");
if (modified)
{
Console.WriteLine("Readme file has been modified.");
}
else
{
Console.WriteLine("Readme file already up-to-date.");
}
// Exit
Console.WriteLine("Finished.");
Thread.Sleep(2000);
// Process return codes are not supported by PowerShell with this method.
}
private static string GetVersion(string changesFile)
{
foreach (string line in File.ReadLines(changesFile))
{
var match = Regex.Match(line, @"^\s*<h2\s+id="".*?"">\s*Version\s+(\S+)\s*</h2>");
if (match.Success)
{
return match.Groups[1].Value;
}
}
throw new Exception("Version could not be determined.");
}
private static string GetVersionYear(string changesFile)
{
foreach (string line in File.ReadLines(changesFile))
{
var match = Regex.Match(line, @"^\s*<p>\s*Released:\s+([0-9]{4})-[0-9]{2}-[0-9]{2}\s*</p>");
if (match.Success)
{
return match.Groups[1].Value;
}
}
throw new Exception("Version year could not be determined.");
}
private static bool UpdateFile(string fileNames, string pattern, string replacement, bool replaceAll = false)
{
string dir = Path.GetDirectoryName(fileNames);
string name = Path.GetFileName(fileNames);
bool anyModified = false;
foreach (string fileName in Directory.GetFiles(dir, name))
{
bool modified = false;
string[] lines = File.ReadAllLines(fileName);
for (int i = 0; i < lines.Length; i++)
{
if (Regex.IsMatch(lines[i], pattern))
{
string newLine = Regex.Replace(lines[i], pattern, replacement);
if (newLine != lines[i])
{
lines[i] = newLine;
modified = true;
}
if (!replaceAll)
break;
}
}
if (modified)
File.WriteAllLines(fileName, lines);
anyModified |= modified;
}
return anyModified;
}
private static bool UpdateFileYear(string fileNames, string year)
{
string dir = Path.GetDirectoryName(fileNames);
string name = Path.GetFileName(fileNames);
bool anyModified = false;
foreach (string fileName in Directory.GetFiles(dir, name))
{
bool modified = false;
string[] lines = File.ReadAllLines(fileName);
for (int i = 0; i < lines.Length; i++)
{
// U+00A9: COPYRIGHT SIGN
// U+2013: EN DASH
var match = Regex.Match(lines[i], @"Copyright\s+(\u00a9|\(c\))\s+([0-9]{4})([-\u2013][0-9]{4})?");
if (match.Success)
{
string newLine = lines[i];
if (match.Groups[3].Success)
{
// Year range specified, only update range end
newLine = Regex.Replace(lines[i], @"(Copyright\s+(?:\u00a9|\(c\))\s+[0-9]{4}[-\u2013])[0-9]{4}", "${1}" + year);
}
else if (match.Groups[2].Value != year)
{
// Single year specified, extend range from there
string sep = "-";
if (match.Groups[1].Value == "\u00A9")
sep = "\u2013";
newLine = Regex.Replace(lines[i], @"(Copyright\s+(?:\u00a9|\(c\))\s+[0-9]{4})", "${1}" + sep + year);
}
if (newLine != lines[i])
{
lines[i] = newLine;
modified = true;
}
break;
}
}
if (modified)
File.WriteAllLines(fileName, lines);
anyModified |= modified;
}
return anyModified;
}
private static bool UpdateFileSize(string filePath)
{
string fileName = Path.GetFileName(filePath);
string pattern = @"(?<=\W" + Regex.Escape(fileName) + @"\s*)\([0-9.]+\s*KiB\)";
double sizeKiB = (double)GetCompressedSize(filePath) / 1024;
string sizeStr = sizeKiB.ToString("0", CultureInfo.InvariantCulture);
if (sizeKiB < 9.5)
sizeStr = sizeKiB.ToString("0.0", CultureInfo.InvariantCulture);
bool modified = false;
string[] lines = File.ReadAllLines("readme.md");
for (int i = 0; i < lines.Length; i++)
{
if (Regex.IsMatch(lines[i], pattern))
{
string newLine = Regex.Replace(lines[i], pattern, "(" + sizeStr + "\xA0KiB)");
if (newLine != lines[i])
{
lines[i] = newLine;
modified = true;
}
break;
}
}
if (modified)
File.WriteAllLines("readme.md", lines);
return modified;
}
private static long GetCompressedSize(string fileName)
{
try
{
using (var ms = new MemoryStream())
{
using (var fs = File.OpenRead(fileName))
using (var gs = new GZipStream(ms, CompressionMode.Compress, leaveOpen: true))
{
fs.CopyTo(gs);
}
return ms.Length;
}
}
catch (IOException)
{
return -1;
}
}
private static void Copy(string source, string destDir)
{
if (destDir != "")
Directory.CreateDirectory(destDir);
string sourceDir = Path.GetDirectoryName(source);
string sourceFiles = Path.GetFileName(source);
foreach (string file in Directory.GetFiles(sourceDir, sourceFiles))
{
File.Copy(file, Path.Combine(destDir, Path.GetFileName(file)));
}
}
private static void Exec(string fileName, string args)
{
var process = Process.Start(new ProcessStartInfo
{
FileName = fileName,
Arguments = args,
CreateNoWindow = true,
UseShellExecute = false
});
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("Process " + fileName + " exited with error code " + process.ExitCode + ".");
}
}
// ========== END C# CODE ==========
'@; [App]::Run($args)