-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCompileJS.cs
71 lines (61 loc) · 2.28 KB
/
CompileJS.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
using System;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace MudBlazor.JSCompiler
{
public class CompileJS : Task
{
public string SourceDirectory { get; set; } = "TScripts";
public string DestinationFile { get; set; } = "wwwroot/MudBlazor.min.js";
[Output]
public ITaskItem GeneratedFile { get; set; }
public override bool Execute()
{
var sourceFiles = new DirectoryInfo(SourceDirectory).GetFiles("*.js");
var maxSourceWriteTime = new DateTime();
foreach (var sourceFile in sourceFiles)
{
maxSourceWriteTime = sourceFile.LastWriteTime > maxSourceWriteTime ? sourceFile.LastWriteTime : maxSourceWriteTime;
}
using (Mutex mutex = new Mutex(false, "MudBlazorminjs"))
{
mutex.WaitOne(5000);
if (File.Exists(DestinationFile))
{
if (File.GetLastWriteTime(DestinationFile) < maxSourceWriteTime)
{
WriteFile();
Log.LogMessage(MessageImportance.High, $"{DestinationFile} Updated");
}
else
{
Log.LogMessage(MessageImportance.High, $"{DestinationFile} UpToDate");
}
}
else
{
WriteFile();
Log.LogMessage(MessageImportance.High, $"{DestinationFile} Created");
}
mutex.ReleaseMutex();
}
GeneratedFile = new TaskItem(DestinationFile);
return true;
}
private void WriteFile()
{
var sourceFiles = new DirectoryInfo(SourceDirectory).GetFiles("*.js");
var combinedJS = new StringBuilder();
foreach (var sourceFile in sourceFiles)
{
var fileText = File.ReadAllText(sourceFile.FullName);
combinedJS.Append(fileText);
}
var compressedJS = JavaScriptCompressor.Compress(combinedJS.ToString());
File.WriteAllText(DestinationFile, compressedJS);
}
}
}