using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Xml.Linq; using System.Threading.Tasks; namespace BilibiliEvolved.Build { public sealed class VueFile { public string TamplateLang { get; private set; } public string Tamplate { get; private set; } public string ScriptLang { get; private set; } public string Script { get; private set; } public string StyleLang { get; private set; } public string Style { get; private set; } public VueFile(string text) { parseTamplate(text); parseScript(text); parseStyle(text); } private void parseTamplate(string text) { var regex = @"([^\0]*)"; var match = new Regex(regex, RegexOptions.Multiline).Match(text); if (!match.Success) { TamplateLang = null; Tamplate = null; return; } Tamplate = match.Groups[3].Value.Trim().Replace("`", "\\`"); TamplateLang = match.Groups[2].Value.Trim(); if (TamplateLang == "") { TamplateLang = "html"; } } private void parseScript(string text) { var regex = @"([^\0]*)"; var match = new Regex(regex, RegexOptions.Multiline).Match(text); if (!match.Success) { ScriptLang = null; Script = null; return; } Script = match.Groups[3].Value.Trim(); ScriptLang = match.Groups[2].Value.Trim(); if (ScriptLang == "") { ScriptLang = "js"; } } private void parseStyle(string text) { var regex = @"([^\0]*)"; var match = new Regex(regex, RegexOptions.Multiline).Match(text); if (!match.Success) { StyleLang = null; Style = null; return; } Style = match.Groups[3].Value.Trim().Trim('\ufeff'); StyleLang = match.Groups[2].Value.Trim(); if (StyleLang == "") { StyleLang = "css"; } } } public sealed class VueBuildException : Exception { public VueBuildException(string message) : base(message) { } } partial class ProjectBuilder { public ProjectBuilder PrebuildVue() { var files = ResourceMinifier.GetFiles(file => file.Extension == ".vue" ); using (var cache = new BuildCache()) { var changedFiles = files.Where(file => !cache.Contains(file)).ToArray(); if (changedFiles.Any()) { Parallel.ForEach(changedFiles, file => { var source = File.ReadAllText(file); var vueFile = new VueFile(source); if (vueFile.Script is null) { return; } if (vueFile.ScriptLang == "ts" || vueFile.ScriptLang == "typescript") { File.WriteAllText(file + ".ts", vueFile.Script); } }); } } return this; } public ProjectBuilder BuildVue() { var files = ResourceMinifier.GetFiles(file => file.Extension == ".vue" ); using (var cache = new BuildCache()) { var changedFiles = files.Where(file => !cache.Contains(file)).ToArray(); if (changedFiles.Any()) { Parallel.ForEach(changedFiles, file => { cache.AddCache(file); WriteInfo($"Vue build: {file}"); var source = File.ReadAllText(file); var vueFile = new VueFile(source); var compiledText = new StringBuilder(""); if (vueFile.Tamplate is null) { throw new VueBuildException($"{file}: Missing "); } else { var uglifyHtml = new UglifyHtml(); if (vueFile.TamplateLang == "html") { compiledText.Append($"const template = /*html*/`{uglifyHtml.Run(vueFile.Tamplate)}`;"); } else { throw new VueBuildException($"{file}: Unsupported