This commit is contained in:
the1812 2019-11-15 08:06:29 +08:00
parent b4637b411a
commit 195be800eb
5 changed files with 93 additions and 36 deletions

View File

@ -1,18 +1,38 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Collections.Concurrent;
using System.IO;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Linq;
using System.Threading.Tasks;
namespace BilibiliEvolved.Build namespace BilibiliEvolved.Build
{ {
static class Extensions static class Extensions
{
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
{ {
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action) foreach (var item in collection)
{ {
foreach (var item in collection) action(item);
{ }
action(item);
}
}
} }
public static Action Debounce(Action action, int waitTime)
{
var queue = new ConcurrentQueue<Action>();
return async () =>
{
queue.Enqueue(action);
await Task.Delay(waitTime);
if (queue.TryDequeue(out var a))
{
if (queue.Count == 0)
{
a?.Invoke();
}
}
};
}
}
} }

View File

@ -71,20 +71,20 @@ namespace BilibiliEvolved.Build
} }
private void buildFile(string path) private void buildFile(string path)
{ {
if (File.Exists(path)) // if (File.Exists(path))
{ // {
var offlineFileText = File.ReadAllText(path); // var offlineFileText = File.ReadAllText(path);
var noVersion = new Regex(@"// @version[ ]*(.*)" + Environment.NewLine); // var noVersion = new Regex(@"// @version[ ]*(.*)" + Environment.NewLine);
var originalOffline = noVersion.Replace(offlineFileText, ""); // var originalOffline = noVersion.Replace(offlineFileText, "");
var currentOffline = noVersion.Replace(offlineText, ""); // var currentOffline = noVersion.Replace(offlineText, "");
if (currentOffline == originalOffline) // if (currentOffline == originalOffline)
{ // {
offlineVersion = noVersion.Match(offlineFileText).Groups[1].Value.Trim(); // offlineVersion = noVersion.Match(offlineFileText).Groups[1].Value.Trim();
return; // return;
} // }
} // }
File.WriteAllText(path, offlineText); File.WriteAllText(path, offlineText);
} }

View File

@ -33,11 +33,22 @@ namespace BilibiliEvolved.Build.Watcher
return !cache.Contains(originalFile); return !cache.Contains(originalFile);
}; };
} }
private void KillExternalWatcher() {
if (!externalWatcherProcess.HasExited)
{
externalWatcherProcess.Kill();
}
}
public override void Stop() public override void Stop()
{ {
externalWatcherProcess.Kill(); KillExternalWatcher();
base.Stop(); base.Stop();
} }
public override void Dispose()
{
KillExternalWatcher();
base.Dispose();
}
protected override sealed void OnFileChanged(FileSystemEventArgs e) protected override sealed void OnFileChanged(FileSystemEventArgs e)
{ {
var originalFile = GetOriginalFilePath(e.FullPath); var originalFile = GetOriginalFilePath(e.FullPath);

View File

@ -9,7 +9,8 @@ using System.Threading.Tasks;
namespace BilibiliEvolved.Build.Watcher namespace BilibiliEvolved.Build.Watcher
{ {
public class WatchBuilder { public class WatchBuilder
{
private List<Watcher> watchers = new List<Watcher>{ private List<Watcher> watchers = new List<Watcher>{
new ClientWatcher(), new ClientWatcher(),
new JavaScriptWatcher(), new JavaScriptWatcher(),
@ -20,23 +21,41 @@ namespace BilibiliEvolved.Build.Watcher
new VueWatcher(), new VueWatcher(),
}; };
private ProjectBuilder builder = ProjectBuilder.CreateBuilder(); private ProjectBuilder builder = ProjectBuilder.CreateBuilder();
public void StartWatching() { public void StartWatching()
{
builder builder
.BuildClient() .BuildClient()
.BuildVersions() .BuildVersions()
.BuildMaster(); .BuildMaster();
watchers.ForEach(w => w.Start(builder)); var rebuild = Extensions.Debounce(() =>
{
builder
.BuildBundle()
.BuildPreviewOffline()
.BuildOffline()
.BuildPreviewData()
.BuildFinalOutput();
}, 200);
watchers.ForEach(w =>
{
w.Start(builder);
w.FileChanged += e => rebuild();
w.FileDeleted += e => rebuild();
});
builder.WriteInfo("Watcher started, input 'q' or press 'Ctrl + C' to exit."); builder.WriteInfo("Watcher started, input 'q' or press 'Ctrl + C' to exit.");
var input = ""; var input = "";
Console.CancelKeyPress += (s, e) => { Console.CancelKeyPress += (s, e) =>
{
StopWatching(); StopWatching();
}; };
while (input.ToLowerInvariant() != "q") { while (input.ToLowerInvariant() != "q")
{
input = Console.ReadLine(); input = Console.ReadLine();
} }
StopWatching(); StopWatching();
} }
private void StopWatching() { private void StopWatching()
{
watchers.ForEach(w => w.Stop()); watchers.ForEach(w => w.Stop());
builder.WriteInfo("Watcher stopped."); builder.WriteInfo("Watcher stopped.");
} }

View File

@ -19,6 +19,8 @@ namespace BilibiliEvolved.Build.Watcher
public string GenericFilter { get; set; } = ""; public string GenericFilter { get; set; } = "";
public Predicate<string> FileFilter { get; set; } = s => true; public Predicate<string> FileFilter { get; set; } = s => true;
public ConcurrentBag<string> ChangedFilesHistory { get; } = new ConcurrentBag<string>(); public ConcurrentBag<string> ChangedFilesHistory { get; } = new ConcurrentBag<string>();
public event Action<FileSystemEventArgs> FileChanged;
public event Action<FileSystemEventArgs> FileDeleted;
protected abstract void OnFileChanged(FileSystemEventArgs e); protected abstract void OnFileChanged(FileSystemEventArgs e);
protected virtual void OnFileDeleted(FileSystemEventArgs e) protected virtual void OnFileDeleted(FileSystemEventArgs e)
{ {
@ -27,10 +29,11 @@ namespace BilibiliEvolved.Build.Watcher
{ {
File.Delete(minimizedFilename); File.Delete(minimizedFilename);
} }
builder builder.GetBundleFiles();
.GetBundleFiles() // builder
.BuildBundle(); // .GetBundleFiles()
RebuildOutputs(); // .BuildBundle();
// RebuildOutputs();
} }
// protected void RebuildBundle() // protected void RebuildBundle()
// { // {
@ -53,7 +56,7 @@ namespace BilibiliEvolved.Build.Watcher
private bool started = false; private bool started = false;
// https://github.com/dotnet/corefx/issues/25117 // https://github.com/dotnet/corefx/issues/25117
private ConcurrentBag<FileSystemEventArgs> changedFiles = new ConcurrentBag<FileSystemEventArgs>(); private ConcurrentBag<FileSystemEventArgs> changedFiles = new ConcurrentBag<FileSystemEventArgs>();
private const int HandleFileChangesPeriod = 200; public const int HandleFileChangesPeriod = 200;
private void HandleFileChange(FileSystemEventArgs e) private void HandleFileChange(FileSystemEventArgs e)
{ {
switch (e.ChangeType) switch (e.ChangeType)
@ -62,10 +65,12 @@ namespace BilibiliEvolved.Build.Watcher
case WatcherChangeTypes.Created: case WatcherChangeTypes.Created:
// Console.WriteLine($"OnChange {e.Name}"); // Console.WriteLine($"OnChange {e.Name}");
OnFileChanged(e); OnFileChanged(e);
FileChanged?.Invoke(e);
break; break;
case WatcherChangeTypes.Deleted: case WatcherChangeTypes.Deleted:
// Console.WriteLine($"delete {e.Name} {e.ChangeType}"); // Console.WriteLine($"delete {e.Name} {e.ChangeType}");
OnFileDeleted(e); OnFileDeleted(e);
FileDeleted?.Invoke(e);
break; break;
default: default:
break; break;
@ -134,14 +139,16 @@ namespace BilibiliEvolved.Build.Watcher
distinctChanges.ForEach(e => distinctChanges.ForEach(e =>
{ {
HandleFileChange(e); HandleFileChange(e);
builder.UpdateCachedMinFile(e.FullPath);
ChangedFilesHistory.Add(e.FullPath); ChangedFilesHistory.Add(e.FullPath);
}); });
changedFiles.Clear(); changedFiles.Clear();
builder.BuildBundle(); // builder.BuildBundle();
RebuildOutputs(); // RebuildOutputs();
} }
} }
}); });
} }
public virtual void Stop() public virtual void Stop()
{ {
@ -149,7 +156,7 @@ namespace BilibiliEvolved.Build.Watcher
started = false; started = false;
} }
public void Dispose() public virtual void Dispose()
{ {
cache?.Dispose(); cache?.Dispose();
watcher?.Dispose(); watcher?.Dispose();