diff --git a/Aquc.Stackbricks/Aquc.Stackbricks.csproj b/Aquc.Stackbricks/Aquc.Stackbricks.csproj index 86dfcdb..57d9887 100644 --- a/Aquc.Stackbricks/Aquc.Stackbricks.csproj +++ b/Aquc.Stackbricks/Aquc.Stackbricks.csproj @@ -5,13 +5,12 @@ net6.0 enable enable - +true embedded en-US $(Version) $(Version) 3.0.1.725 - true diff --git a/Aquc.Stackbricks/Config.cs b/Aquc.Stackbricks/Config.cs index 2334d7e..6b0fe7e 100644 --- a/Aquc.Stackbricks/Config.cs +++ b/Aquc.Stackbricks/Config.cs @@ -108,7 +108,7 @@ public class StackbricksActionDataJsonConverter : JsonConverter matchDict = new Dictionary + static readonly Dictionary matchDict = new Dictionary { - {"stbks.msgpvder.bilicmts",new BiliCommitMsgPvder() } + {"stbks.msgpvder.bilicmts",new BiliCommitMsgPvder() }, + {WeiboCommitMsgPvder.ID, new WeiboCommitMsgPvder() } }; public static IStackbricksMsgPvder ParseMsgPvder(string msgPvderId) { diff --git a/Aquc.Stackbricks/MsgPvder/BiliCommit.cs b/Aquc.Stackbricks/MsgPvder/BiliCommit.cs index 8e6c8b7..f5cd12c 100644 --- a/Aquc.Stackbricks/MsgPvder/BiliCommit.cs +++ b/Aquc.Stackbricks/MsgPvder/BiliCommit.cs @@ -13,9 +13,8 @@ public class BiliCommitMsgPvder : IStackbricksMsgPvder public static readonly string _MsgPvderId = "stbks.msgpvder.bilicmts"; public async Task GetUpdateMessage(StackbricksManifest stackbricksManifest) { - - //stbks.msgpvder.bilicmts@1;;0.2.0;;stbks.pkgpvder.ghproxy;; var message=await BiliCommitsClass.GetReply(StackbricksProgram._httpClient,stackbricksManifest.MsgPvderData); + StackbricksProgram.logger.Information(message); var msgData = message.Split(";;"); if (msgData.Length > 0) { if (msgData[0]==MsgPvderId+"@1") diff --git a/Aquc.Stackbricks/MsgPvder/WeiboCommit.cs b/Aquc.Stackbricks/MsgPvder/WeiboCommit.cs new file mode 100644 index 0000000..a1d0787 --- /dev/null +++ b/Aquc.Stackbricks/MsgPvder/WeiboCommit.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Nodes; +using System.Threading.Tasks; + +namespace Aquc.Stackbricks.MsgPvder; + +public class WeiboCommitMsgPvder : IStackbricksMsgPvder +{ + public string MsgPvderId => ID; + public const string ID = "stbks.msgpvder.weibocmts"; + + public async Task GetUpdateMessage(StackbricksManifest stackbricksManifest) + { + var url = $"https://weibo.com/ajax/statuses/buildComments?flow=1&is_reload=1&id={stackbricksManifest.MsgPvderData}&is_show_bulletin=2&is_mix=0&count=10&fetch_level=0"; + var response = JsonNode.Parse(await StackbricksProgram._httpClient.GetAsync(url).Result.EnsureSuccessStatusCode().Content.ReadAsStringAsync())!; + var message = response["data"]!.AsArray()[0]!["text"]!.ToString().Split(";;"); + StackbricksProgram.logger.Debug(message[3][..^1]); + StackbricksProgram.logger.Debug($"{message[0]}: Successful received update message: ver={message[1]}, pkg_pvder={message[2]}"); + return new StackbricksUpdateMessage( + stackbricksManifest, + new Version(message[1]), + message[2], + message[3][..^1] + ); + } +} diff --git a/Aquc.Stackbricks/Package.cs b/Aquc.Stackbricks/Package.cs index 65ee937..a99f168 100644 --- a/Aquc.Stackbricks/Package.cs +++ b/Aquc.Stackbricks/Package.cs @@ -30,6 +30,7 @@ protected DirectoryInfo DepressedZipFile() depressedDir.Create(); } ZipFile.ExtractToDirectory(zipFile, depressedDir.FullName); + StackbricksProgram.logger.Debug($"Extract zipFile successfully, to={depressedDir.FullName}"); return depressedDir; } public void ExecuteActions() diff --git a/Aquc.Stackbricks/PkgPvder/GhProxy.cs b/Aquc.Stackbricks/PkgPvder/GhProxy.cs index d3a5f19..cc371c3 100644 --- a/Aquc.Stackbricks/PkgPvder/GhProxy.cs +++ b/Aquc.Stackbricks/PkgPvder/GhProxy.cs @@ -9,8 +9,8 @@ namespace Aquc.Stackbricks.PkgPvder; public class GhProxyPkgPvder : IStackbricksPkgPvder { - public string PkgPvderId => _PkgPvderId; - public static readonly string _PkgPvderId = "stbks.pkgpvder.ghproxy"; + public string PkgPvderId => ID; + public const string ID = "stbks.pkgpvder.ghproxy"; public async Task DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition, string zipFileName) { @@ -18,6 +18,7 @@ public async Task DownloadPackageAsync(StackbricksUpda var splitedData = updateMessage.PkgPvderArgs.Split("]]"); var downloadFile = Path.Combine(savePosition, zipFileName); await DownloadAsync(CombineGhproxyUrl(splitedData), Path.Combine(savePosition, downloadFile)); + StackbricksProgram.logger.Debug($"{ID}: Download zipPackageFile successfull, file={zipFileName}"); return new StackbricksUpdatePackage(downloadFile, updateMessage, updateMessage.stackbricksManifest.ProgramDir); } public async Task DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition) => @@ -37,7 +38,9 @@ public async Task DownloadFileAsync(StackbricksUpdateMessage updateMes { var splitedData = updateMessage.PkgPvderArgs.Split("]]"); var downloadFile = Path.Combine(savePosition, string.IsNullOrEmpty(fileName) ? splitedData[3] : fileName); + StackbricksProgram.logger.Information(CombineGhproxyUrl(splitedData)); await DownloadAsync(CombineGhproxyUrl(splitedData), Path.Combine(savePosition, downloadFile)); + StackbricksProgram.logger.Debug($"{ID}: Download file successfull, file={Path.GetFileName(downloadFile)}"); return new FileInfo(downloadFile); } } diff --git a/Aquc.Stackbricks/Program.cs b/Aquc.Stackbricks/Program.cs index 94c14fb..de1456f 100644 --- a/Aquc.Stackbricks/Program.cs +++ b/Aquc.Stackbricks/Program.cs @@ -12,6 +12,7 @@ public class StackbricksProgram public static readonly Logger logger=new LoggerConfiguration() .WriteTo.Console() .WriteTo.File($"log/{DateTime.Now:yyyyMMdd}.log") + .MinimumLevel.Verbose() .CreateLogger(); public static readonly JsonSerializerSettings jsonSerializer = new Func(() => { @@ -90,8 +91,15 @@ public static void Main(string[] args) }; updateCommand.SetHandler(async () => { + logger.Information("Start to update program if the program has newest version."); await stackbricksService.UpdateWhenAvailable(); }); + selfUpdateCommand.SetHandler(async () => + { + + logger.Information("Start to update Aquc.Stackbricks if the program has newest version."); + await stackbricksService.UpdateStackbricksWhenAvailable(); + }); configCreateCommand.SetHandler(() => { diff --git a/Aquc.Stackbricks/Service.cs b/Aquc.Stackbricks/Service.cs index 782d6ac..39d7026 100644 --- a/Aquc.Stackbricks/Service.cs +++ b/Aquc.Stackbricks/Service.cs @@ -45,19 +45,24 @@ protected void ApplyStackbricksUpdate(FileInfo newFileInfo,Version version) var resultFile = ".Aquc.Stackbricks.applyres"; if (File.Exists(resultFile)) File.Delete(resultFile); var command= - "timeout /t 5 /nobreak && "+ - $"cd /d {newFileInfo.DirectoryName} && "+ - $"rename {newFileInfo.Name} {PROGRAM_NAME} && "+ - $"echo success_executed:{version} > {resultFile}"; + "/C timeout /t 5 /nobreak && "+ + $"cd /d \"{newFileInfo.DirectoryName}\" && "+ + $"del /S \"{PROGRAM_NAME}\" && " + + $"echo 1 && "+ + $"rename \"{newFileInfo.Name}\" \"{PROGRAM_NAME}\" && echo 2 && " + + $"echo success_executed:{version} > \"{resultFile}\" && "+ + "exit /B && exit"; + StackbricksProgram.logger.Information(command); using var process = new Process { StartInfo = new ProcessStartInfo { - FileName = command, - CreateNoWindow = true + FileName = "cmd", + Arguments=command } }; process.Start(); + StackbricksProgram.logger.Debug("Executed to apply Aquc.Stackbricks.exe updated actions."); Environment.Exit(0); } public async Task UpdateWhenAvailable() @@ -66,6 +71,7 @@ public async Task UpdateWhenAvailable() var message = await manifest.GetMsgPvder().GetUpdateMessage(manifest); if (message.NeedUpdate()) { + StackbricksProgram.logger.Information($"Got {message.version} to update, currently version is {manifest.Version}"); var package = await message.GetPkgPvder().DownloadPackageAsync(message, manifest.ProgramDir.FullName); package.ExecuteActions(); await UpdateManifest(message); @@ -73,6 +79,7 @@ public async Task UpdateWhenAvailable() } else { + StackbricksProgram.logger.Information("No newest version to update"); await UpdateCheckedManifest(); return false; } @@ -83,17 +90,20 @@ public async Task UpdateStackbricksWhenAvailable() var message = await manifest.GetMsgPvder().GetUpdateMessage(manifest); if (message.NeedUpdate()) { + StackbricksProgram.logger.Information($"Got {message.version} to update, currently version is {manifest.Version}"); var file = await message.GetPkgPvder() .DownloadFileAsync(message, manifest.ProgramDir.FullName, $".Aquc.Stackbricks.updated_{message.version}.exe"); using var fs = new FileStream(StackbricksApplyUpdateConfig.APPLYUPDATE_FILENAME, FileMode.Create, FileAccess.Write); using var sw = new StreamWriter(fs); await sw.WriteAsync(JsonConvert.SerializeObject(new StackbricksApplyUpdateConfig(file.FullName), StackbricksProgram.jsonSerializer)); - await UpdateManifest(message, false); - ApplyStackbricksUpdate(file, message.version); + //await UpdateManifest(message, false); + //ApplyStackbricksUpdate(file, message.version); return true; } else { + + StackbricksProgram.logger.Information($"Received {message.version}, currently version is {manifest.Version}. No newest version to update."); await UpdateCheckedManifest(false); return false; } @@ -103,6 +113,7 @@ public async Task UpdateCheckedManifest(bool isProgram = true) var manifest = isProgram ? stackbricksConfig.ProgramManifest : stackbricksConfig.StackbricksManifest; manifest.LastCheckTime = DateTime.Now; await WriteConfig(); + StackbricksProgram.logger.Debug($"Write config to {StackbricksConfig.CONFIG_FILENAME}"); } public async Task UpdateManifest(StackbricksUpdateMessage msg, bool isProgram = true) {