Bilibili-Evolved/builder/dotnet/Watcher/PreprocessorWatcher.cs
2019-11-13 16:27:00 +08:00

52 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
namespace BilibiliEvolved.Build.Watcher
{
public abstract class PreprocessorWatcher : Watcher
{
private Process externalWatcherProcess;
private string originalExtension;
protected abstract ResourceMinifier Minifier { get; }
protected abstract NodeInteract WatcherComplier { get; }
protected abstract string Name { get; }
protected abstract string PostBuild(string content);
private string GetOriginalFilePath(string path)
{
return Path.ChangeExtension(path, originalExtension).Replace(WatcherPath, "src");
}
public PreprocessorWatcher(string originalExtension, string compliedExtension, string watcherPath) : base(watcherPath)
{
this.originalExtension = originalExtension;
externalWatcherProcess = WatcherComplier.Run();
GenericFilter = $"*{compliedExtension}";
FileFilter = file =>
{
var originalFile = GetOriginalFilePath(file);
return !cache.Contains(originalFile);
};
}
public override void Stop()
{
externalWatcherProcess.Kill();
base.Stop();
}
protected override sealed void OnFileChanged(FileSystemEventArgs e)
{
var originalFile = GetOriginalFilePath(e.FullPath);
builder.WriteInfo($"[{Name}] {Path.GetFileName(originalFile)} changed.");
var content = File.ReadAllText(e.FullPath);
cache.AddCache(originalFile);
cache.SaveCache();
File.WriteAllText(ResourceMinifier.GetMinimizedFileName(e.FullPath), Minifier.Minify(PostBuild(content)));
}
}
}