From 195be800eb66b4c8026f4677418b1ad88f65e4af Mon Sep 17 00:00:00 2001 From: the1812 Date: Fri, 15 Nov 2019 08:06:29 +0800 Subject: [PATCH] Fix bugs --- builder/dotnet/Extensions.cs | 38 ++++++++++++++----- builder/dotnet/OfflineBuilder.cs | 24 ++++++------ builder/dotnet/Watcher/PreprocessorWatcher.cs | 13 ++++++- builder/dotnet/Watcher/WatchBuilder.cs | 31 ++++++++++++--- builder/dotnet/Watcher/Watcher.cs | 23 +++++++---- 5 files changed, 93 insertions(+), 36 deletions(-) diff --git a/builder/dotnet/Extensions.cs b/builder/dotnet/Extensions.cs index b623c7ebc..0ada462fc 100644 --- a/builder/dotnet/Extensions.cs +++ b/builder/dotnet/Extensions.cs @@ -1,18 +1,38 @@ using System; using System.Collections.Generic; -using System.Linq; +using System.Collections.Concurrent; +using System.IO; +using System.Security.Cryptography; using System.Text; +using System.Linq; +using System.Threading.Tasks; namespace BilibiliEvolved.Build { - static class Extensions + static class Extensions + { + public static void ForEach(this IEnumerable collection, Action action) { - public static void ForEach(this IEnumerable collection, Action action) - { - foreach (var item in collection) - { - action(item); - } - } + foreach (var item in collection) + { + action(item); + } } + public static Action Debounce(Action action, int waitTime) + { + var queue = new ConcurrentQueue(); + return async () => + { + queue.Enqueue(action); + await Task.Delay(waitTime); + if (queue.TryDequeue(out var a)) + { + if (queue.Count == 0) + { + a?.Invoke(); + } + } + }; + } + } } diff --git a/builder/dotnet/OfflineBuilder.cs b/builder/dotnet/OfflineBuilder.cs index e8130399c..62f8c836e 100644 --- a/builder/dotnet/OfflineBuilder.cs +++ b/builder/dotnet/OfflineBuilder.cs @@ -71,20 +71,20 @@ namespace BilibiliEvolved.Build } private void buildFile(string path) { - if (File.Exists(path)) - { - var offlineFileText = File.ReadAllText(path); + // if (File.Exists(path)) + // { + // var offlineFileText = File.ReadAllText(path); - var noVersion = new Regex(@"// @version[ ]*(.*)" + Environment.NewLine); - var originalOffline = noVersion.Replace(offlineFileText, ""); - var currentOffline = noVersion.Replace(offlineText, ""); + // var noVersion = new Regex(@"// @version[ ]*(.*)" + Environment.NewLine); + // var originalOffline = noVersion.Replace(offlineFileText, ""); + // var currentOffline = noVersion.Replace(offlineText, ""); - if (currentOffline == originalOffline) - { - offlineVersion = noVersion.Match(offlineFileText).Groups[1].Value.Trim(); - return; - } - } + // if (currentOffline == originalOffline) + // { + // offlineVersion = noVersion.Match(offlineFileText).Groups[1].Value.Trim(); + // return; + // } + // } File.WriteAllText(path, offlineText); } diff --git a/builder/dotnet/Watcher/PreprocessorWatcher.cs b/builder/dotnet/Watcher/PreprocessorWatcher.cs index b04581a05..846a39ba6 100644 --- a/builder/dotnet/Watcher/PreprocessorWatcher.cs +++ b/builder/dotnet/Watcher/PreprocessorWatcher.cs @@ -33,11 +33,22 @@ namespace BilibiliEvolved.Build.Watcher return !cache.Contains(originalFile); }; } + private void KillExternalWatcher() { + if (!externalWatcherProcess.HasExited) + { + externalWatcherProcess.Kill(); + } + } public override void Stop() { - externalWatcherProcess.Kill(); + KillExternalWatcher(); base.Stop(); } + public override void Dispose() + { + KillExternalWatcher(); + base.Dispose(); + } protected override sealed void OnFileChanged(FileSystemEventArgs e) { var originalFile = GetOriginalFilePath(e.FullPath); diff --git a/builder/dotnet/Watcher/WatchBuilder.cs b/builder/dotnet/Watcher/WatchBuilder.cs index 418b76894..ea2b3fea8 100644 --- a/builder/dotnet/Watcher/WatchBuilder.cs +++ b/builder/dotnet/Watcher/WatchBuilder.cs @@ -9,7 +9,8 @@ using System.Threading.Tasks; namespace BilibiliEvolved.Build.Watcher { - public class WatchBuilder { + public class WatchBuilder + { private List watchers = new List{ new ClientWatcher(), new JavaScriptWatcher(), @@ -20,23 +21,41 @@ namespace BilibiliEvolved.Build.Watcher new VueWatcher(), }; private ProjectBuilder builder = ProjectBuilder.CreateBuilder(); - public void StartWatching() { + public void StartWatching() + { builder .BuildClient() .BuildVersions() .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."); var input = ""; - Console.CancelKeyPress += (s, e) => { + Console.CancelKeyPress += (s, e) => + { StopWatching(); }; - while (input.ToLowerInvariant() != "q") { + while (input.ToLowerInvariant() != "q") + { input = Console.ReadLine(); } StopWatching(); } - private void StopWatching() { + private void StopWatching() + { watchers.ForEach(w => w.Stop()); builder.WriteInfo("Watcher stopped."); } diff --git a/builder/dotnet/Watcher/Watcher.cs b/builder/dotnet/Watcher/Watcher.cs index 012b2bf74..51c4c50de 100644 --- a/builder/dotnet/Watcher/Watcher.cs +++ b/builder/dotnet/Watcher/Watcher.cs @@ -19,6 +19,8 @@ namespace BilibiliEvolved.Build.Watcher public string GenericFilter { get; set; } = ""; public Predicate FileFilter { get; set; } = s => true; public ConcurrentBag ChangedFilesHistory { get; } = new ConcurrentBag(); + public event Action FileChanged; + public event Action FileDeleted; protected abstract void OnFileChanged(FileSystemEventArgs e); protected virtual void OnFileDeleted(FileSystemEventArgs e) { @@ -27,10 +29,11 @@ namespace BilibiliEvolved.Build.Watcher { File.Delete(minimizedFilename); } - builder - .GetBundleFiles() - .BuildBundle(); - RebuildOutputs(); + builder.GetBundleFiles(); + // builder + // .GetBundleFiles() + // .BuildBundle(); + // RebuildOutputs(); } // protected void RebuildBundle() // { @@ -53,7 +56,7 @@ namespace BilibiliEvolved.Build.Watcher private bool started = false; // https://github.com/dotnet/corefx/issues/25117 private ConcurrentBag changedFiles = new ConcurrentBag(); - private const int HandleFileChangesPeriod = 200; + public const int HandleFileChangesPeriod = 200; private void HandleFileChange(FileSystemEventArgs e) { switch (e.ChangeType) @@ -62,10 +65,12 @@ namespace BilibiliEvolved.Build.Watcher case WatcherChangeTypes.Created: // Console.WriteLine($"OnChange {e.Name}"); OnFileChanged(e); + FileChanged?.Invoke(e); break; case WatcherChangeTypes.Deleted: // Console.WriteLine($"delete {e.Name} {e.ChangeType}"); OnFileDeleted(e); + FileDeleted?.Invoke(e); break; default: break; @@ -134,14 +139,16 @@ namespace BilibiliEvolved.Build.Watcher distinctChanges.ForEach(e => { HandleFileChange(e); + builder.UpdateCachedMinFile(e.FullPath); ChangedFilesHistory.Add(e.FullPath); }); changedFiles.Clear(); - builder.BuildBundle(); - RebuildOutputs(); + // builder.BuildBundle(); + // RebuildOutputs(); } } }); + } public virtual void Stop() { @@ -149,7 +156,7 @@ namespace BilibiliEvolved.Build.Watcher started = false; } - public void Dispose() + public virtual void Dispose() { cache?.Dispose(); watcher?.Dispose();