Skip to content

Commit

Permalink
+WeiboCmts ~selfUpdateBugs
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamarine5 committed Jul 25, 2023
1 parent 74af791 commit bc573a7
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Aquc.Stackbricks/Aquc.Stackbricks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<PublishSingleFile>true</PublishSingleFile>
<DebugType>embedded</DebugType>
<SatelliteResourceLanguages>en-US</SatelliteResourceLanguages>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Version>3.0.1.725</Version>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Aquc.Stackbricks/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class StackbricksActionDataJsonConverter : JsonConverter<StackbricksActio

public override void WriteJson(JsonWriter writer, StackbricksActionData? value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
new JsonSerializer().Serialize(writer, value);
/*
writer.WriteStartObject();
writer.WriteStartObject();
Expand Down
5 changes: 3 additions & 2 deletions Aquc.Stackbricks/MsgPvder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public StackbricksUpdateMessage(StackbricksManifest stackbricksManifest,Version
}
public class StackbricksMsgPvderManager
{
static Dictionary<string, IStackbricksMsgPvder> matchDict = new Dictionary<string, IStackbricksMsgPvder>
static readonly Dictionary<string, IStackbricksMsgPvder> matchDict = new Dictionary<string, IStackbricksMsgPvder>
{
{"stbks.msgpvder.bilicmts",new BiliCommitMsgPvder() }
{"stbks.msgpvder.bilicmts",new BiliCommitMsgPvder() },
{WeiboCommitMsgPvder.ID, new WeiboCommitMsgPvder() }
};
public static IStackbricksMsgPvder ParseMsgPvder(string msgPvderId)
{
Expand Down
3 changes: 1 addition & 2 deletions Aquc.Stackbricks/MsgPvder/BiliCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public class BiliCommitMsgPvder : IStackbricksMsgPvder
public static readonly string _MsgPvderId = "stbks.msgpvder.bilicmts";
public async Task<StackbricksUpdateMessage> 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")
Expand Down
29 changes: 29 additions & 0 deletions Aquc.Stackbricks/MsgPvder/WeiboCommit.cs
Original file line number Diff line number Diff line change
@@ -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<StackbricksUpdateMessage> 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]
);
}
}
1 change: 1 addition & 0 deletions Aquc.Stackbricks/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions Aquc.Stackbricks/PkgPvder/GhProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ 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<StackbricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition, string zipFileName)
{
// ncpe
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<StackbricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition) =>
Expand All @@ -37,7 +38,9 @@ public async Task<FileInfo> 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);
}
}
8 changes: 8 additions & 0 deletions Aquc.Stackbricks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonSerializerSettings>(() =>
{
Expand Down Expand Up @@ -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(() =>
{

Expand Down
27 changes: 19 additions & 8 deletions Aquc.Stackbricks/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> UpdateWhenAvailable()
Expand All @@ -66,13 +71,15 @@ public async Task<bool> 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);
return true;
}
else
{
StackbricksProgram.logger.Information("No newest version to update");
await UpdateCheckedManifest();
return false;
}
Expand All @@ -83,17 +90,20 @@ public async Task<bool> 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;
}
Expand All @@ -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)
{
Expand Down

0 comments on commit bc573a7

Please sign in to comment.