Skip to content

Commit

Permalink
~fix typo and bugs, + self_update
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamarine5 committed Jul 25, 2023
1 parent 3781863 commit 74af791
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Aquc.BiliCommits
8 changes: 4 additions & 4 deletions Aquc.Stackbricks/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public StackbricksActionList(List<StackbricksActionData> list)
new StackbricksActionData("stbks.action.replaceall")
};
}
public void ExecuteList(StacebricksUpdatePackage updatePackage)
public void ExecuteList(StackbricksUpdatePackage updatePackage)
{
foreach (var actionData in actions)
{
Expand All @@ -92,12 +92,12 @@ public void ExecuteList(StacebricksUpdatePackage updatePackage)
public interface IStackbricksAction
{
public string Id { get; }
public void Execute(StackbricksActionData stackbricksAction, StacebricksUpdatePackage updatePackage);
public void Execute(StackbricksActionData stackbricksAction, StackbricksUpdatePackage updatePackage);
}
public class ActionOpen : IStackbricksAction
{
public string Id => "stbks.action.open";
public void Execute(StackbricksActionData stackbricksAction, StacebricksUpdatePackage updatePackage)
public void Execute(StackbricksActionData stackbricksAction, StackbricksUpdatePackage updatePackage)
{

throw new NotImplementedException();
Expand All @@ -107,7 +107,7 @@ public class ActionRunUpdatePackageActions : IStackbricksAction
{

public string Id => "stbks.action.runupdpkgactions";
public void Execute(StackbricksActionData stackbricksAction, StacebricksUpdatePackage updatePackage)
public void Execute(StackbricksActionData stackbricksAction, StackbricksUpdatePackage updatePackage)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion Aquc.Stackbricks/Actions/ActionReplaceAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ActionReplaceAll : IStackbricksAction
public string Id => _ActionId;

public static readonly string _ActionId = "stbks.action.replaceall";
public void Execute(StackbricksActionData stackbricksAction, StacebricksUpdatePackage updatePackage)
public void Execute(StackbricksActionData stackbricksAction, StackbricksUpdatePackage updatePackage)
{
CopyDirectory(updatePackage.depressedDir, updatePackage.programDir);
if (!stackbricksAction.ContainFlag("stbks.action.replaceall.keepzipfile"))
Expand Down
8 changes: 6 additions & 2 deletions Aquc.Stackbricks/Aquc.Stackbricks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

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

<ItemGroup>
Expand Down
23 changes: 22 additions & 1 deletion Aquc.Stackbricks/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Aquc.Stackbricks;

public class StackbricksConfig
{
public const string CONFIG_FILENAME = "Aquc.Stackbricks.config.json";
public int ConfigVersion = 1;
public StackbricksManifest StackbricksManifest;
public StackbricksManifest ProgramManifest;
Expand Down Expand Up @@ -54,6 +55,25 @@ public static StackbricksManifest CreateStackbricksManifest()
}, BiliCommitMsgPvder._MsgPvderId,"", new DirectoryInfo(Directory.GetCurrentDirectory()));
}
}
public class StackbricksApplyUpdateConfig
{
public const string APPLYUPDATE_FILENAME = "Aquc.Stackbricks.applyupd.json";

public string newfilePosition;
public bool needApply;
public StackbricksApplyUpdateConfig(string newfilePosition)
{
this.newfilePosition = newfilePosition;
needApply = true;
}
public StackbricksApplyUpdateConfig()
{
newfilePosition = "";
needApply=false;
}
}

#region CustomJsonConverter
public class VersionJsonConverter : JsonConverter<Version>
{
public override Version? ReadJson(JsonReader reader, Type objectType, Version? existingValue, bool hasExistingValue, JsonSerializer serializer)
Expand Down Expand Up @@ -115,4 +135,5 @@ public override void WriteJson(JsonWriter writer, StackbricksActionData? value,
writer.WriteEndArray();
writer.WriteEndObject();*/
}
}
}
#endregion
4 changes: 2 additions & 2 deletions Aquc.Stackbricks/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace Aquc.Stackbricks;

public class StacebricksUpdatePackage
public class StackbricksUpdatePackage
{
public StackbricksUpdateMessage updateMessage;
public DirectoryInfo programDir;
public string zipFile;
public DirectoryInfo depressedDir;
public StacebricksUpdatePackage(string zipFile,StackbricksUpdateMessage updateMessage,DirectoryInfo programDir)
public StackbricksUpdatePackage(string zipFile,StackbricksUpdateMessage updateMessage,DirectoryInfo programDir)
{
this.zipFile = zipFile;
this.updateMessage= updateMessage;
Expand Down
5 changes: 4 additions & 1 deletion Aquc.Stackbricks/PkgPvder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ namespace Aquc.Stackbricks;
public interface IStackbricksPkgPvder
{
public string PkgPvderId { get; }
public Task<StacebricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage,string savePosition);
public Task<StackbricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition);
public Task<StackbricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition, string zipFileName);

public Task<FileInfo> DownloadFileAsync(StackbricksUpdateMessage updateMessage, string savePosition, string fileName="");
}

public class StackbricksPkgPvderManager
Expand Down
28 changes: 20 additions & 8 deletions Aquc.Stackbricks/PkgPvder/GhProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@ public class GhProxyPkgPvder : IStackbricksPkgPvder
{

public string PkgPvderId => _PkgPvderId;

public static readonly string _PkgPvderId = "stbks.pkgpvder.ghproxy";
public async Task<StacebricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage,string savePosition)

public async Task<StackbricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition, string zipFileName)
{
// ncpe
var splitedData = updateMessage.PkgPvderArgs.Split("]]");
var downloadFile = Path.Combine(savePosition, $".StackbricksUpdatePackage_{updateMessage.version}.zip");
var responce = await StackbricksProgram._httpClient.GetAsync(CombineGhproxyUrl(splitedData));
using var fs = new FileStream(Path.Combine(savePosition, downloadFile), FileMode.Create);

var downloadFile = Path.Combine(savePosition, zipFileName);
await DownloadAsync(CombineGhproxyUrl(splitedData), Path.Combine(savePosition, downloadFile));
return new StackbricksUpdatePackage(downloadFile, updateMessage, updateMessage.stackbricksManifest.ProgramDir);
}
public async Task<StackbricksUpdatePackage> DownloadPackageAsync(StackbricksUpdateMessage updateMessage, string savePosition) =>
await DownloadPackageAsync(updateMessage, savePosition, $".StackbricksUpdatePackage_{updateMessage.version}.zip");
async Task DownloadAsync(string url, string savePosition)
{
var responce = await StackbricksProgram._httpClient.GetAsync(url);
using var fs = new FileStream(savePosition, FileMode.Create);
await responce.Content.CopyToAsync(fs);
fs.Dispose();
return new StacebricksUpdatePackage(downloadFile, updateMessage, updateMessage.stackbricksManifest.ProgramDir);
}
static string CombineGhproxyUrl(string[] data)
{
return $"https://ghproxy.com/github.com/{data[0]}/{data[1]}/releases/download/{data[2]}/{data[3]}";
}

public async Task<FileInfo> DownloadFileAsync(StackbricksUpdateMessage updateMessage, string savePosition, string fileName = "")
{
var splitedData = updateMessage.PkgPvderArgs.Split("]]");
var downloadFile = Path.Combine(savePosition, string.IsNullOrEmpty(fileName) ? splitedData[3] : fileName);
await DownloadAsync(CombineGhproxyUrl(splitedData), Path.Combine(savePosition, downloadFile));
return new FileInfo(downloadFile);
}
}
42 changes: 40 additions & 2 deletions Aquc.Stackbricks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Sentry;
using Serilog;
using Serilog.Core;
using System.CommandLine;
Expand Down Expand Up @@ -27,6 +28,30 @@ public class StackbricksProgram
public static StackbricksService stackbricksService = new StackbricksService();
public static void Main(string[] args)
{
SentrySdk.Init(options =>
{
// A Sentry Data Source Name (DSN) is required.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
// You can set it in the SENTRY_DSN environment variable, or you can set it in code here.
options.Dsn = "https://92a9029060f841219ef1306de87c345f@o4505418205364224.ingest.sentry.io/4505458345377792";

// When debug is enabled, the Sentry client will emit detailed debugging information to the console.
// This might be helpful, or might interfere with the normal operation of your application.
// We enable it here for demonstration purposes when first trying Sentry.
// You shouldn't do this in your applications unless you're troubleshooting issues with Sentry.
options.Debug = true;

// This option is recommended. It enables Sentry's "Release Health" feature.
options.AutoSessionTracking = true;

// This option is recommended for client applications only. It ensures all threads use the same global scope.
// If you're writing a background service of any kind, you should remove this.
options.IsGlobalModeEnabled = true;

// This option will enable Sentry's tracing features. You still need to start transactions and spans.
options.EnableTracing = true;
});

var updateCommand = new Command("update")
{

Expand All @@ -46,6 +71,18 @@ public static void Main(string[] args)
var configCreateCommand = new Command("create")
{

};
var selfUpdateCommand = new Command("update");
var selfApplyCommand = new Command("apply");
var selfCheckCommand = new Command("check")
{

};
var selfCommand = new Command("self")
{
selfApplyCommand,
selfCheckCommand,
selfUpdateCommand
};
var configCommand = new Command("config")
{
Expand All @@ -61,15 +98,16 @@ public static void Main(string[] args)
using var file = new FileStream("Aquc.Stackbricks.config.json", FileMode.Create, FileAccess.Write);
using var reader = new StreamWriter(file);
reader.Write(JsonConvert.SerializeObject(new StackbricksConfig(StackbricksManifest.CreateStackbricksManifest()),jsonSerializer));
//Console.WriteLine(JsonConvert.SerializeObject(new StackbricksConfig(StackbricksManifest.CreateStackbricksManifest()), jsonSerializer));
logger.Information("Success created default Aquc.Stackbricks.config.json");
});
var root = new RootCommand()
{
updateCommand,
checkCommand,
installCommand,
downloadCommand,
configCommand
configCommand,
selfCommand
};
root.Invoke(args);
}
Expand Down
15 changes: 15 additions & 0 deletions Aquc.Stackbricks/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net6.0\win-x64\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net6.0</TargetFramework>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>
90 changes: 80 additions & 10 deletions Aquc.Stackbricks/Service.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -13,39 +15,107 @@ public class StackbricksService
public StackbricksManifest stackbricksManifest;
public StackbricksManifest programManifest;

public const string PROGRAM_NAME = "Aquc.Stackbricks.exe";

[Obsolete]
public StackbricksService(StackbricksConfig stackbricksConfig)
{
this.stackbricksConfig= stackbricksConfig;
this.stackbricksConfig = stackbricksConfig;
stackbricksManifest = stackbricksConfig.StackbricksManifest;
programManifest = stackbricksConfig.ProgramManifest;
}
public StackbricksService()
{
if (File.Exists("Aquc.Stackbricks.config.json"))
if (File.Exists(StackbricksConfig.CONFIG_FILENAME))
{
using var fs = new FileStream("Aquc.Stackbricks.config.json", FileMode.Open, FileAccess.Read);
using var fs = new FileStream(StackbricksConfig.CONFIG_FILENAME, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs);
stackbricksConfig = JsonConvert.DeserializeObject<StackbricksConfig>(sr.ReadToEnd(),StackbricksProgram.jsonSerializer)!;
stackbricksConfig = JsonConvert.DeserializeObject<StackbricksConfig>(sr.ReadToEnd(), StackbricksProgram.jsonSerializer)!;
stackbricksManifest = stackbricksConfig.StackbricksManifest;
programManifest = stackbricksConfig.ProgramManifest;
}
else
throw new FileNotFoundException("Aquc.Stackbricks.config.json");
{
StackbricksProgram.logger.Error("Aquc.Stackbricks.config.json was not found.");
throw new FileNotFoundException(StackbricksConfig.CONFIG_FILENAME);
}
}
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}";
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = command,
CreateNoWindow = true
}
};
process.Start();
Environment.Exit(0);
}
public async Task<bool> UpdateWhenAvailable()
{
var stackbricksManifest = programManifest;
var message = await stackbricksManifest.GetMsgPvder().GetUpdateMessage(stackbricksManifest);
var manifest = programManifest;
var message = await manifest.GetMsgPvder().GetUpdateMessage(manifest);
if (message.NeedUpdate())
{
var package = await message.GetPkgPvder().DownloadPackageAsync(message, stackbricksManifest.ProgramDir.FullName);
var package = await message.GetPkgPvder().DownloadPackageAsync(message, manifest.ProgramDir.FullName);
package.ExecuteActions();
await UpdateManifest(message);
return true;
}
else return false;
else
{
await UpdateCheckedManifest();
return false;
}
}
public async Task<bool> UpdateStackbricksWhenAvailable()
{
return await Task.FromResult(true);
var manifest = stackbricksManifest;
var message = await manifest.GetMsgPvder().GetUpdateMessage(manifest);
if (message.NeedUpdate())
{
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);
return true;
}
else
{
await UpdateCheckedManifest(false);
return false;
}
}
public async Task UpdateCheckedManifest(bool isProgram = true)
{
var manifest = isProgram ? stackbricksConfig.ProgramManifest : stackbricksConfig.StackbricksManifest;
manifest.LastCheckTime = DateTime.Now;
await WriteConfig();
}
public async Task UpdateManifest(StackbricksUpdateMessage msg, bool isProgram = true)
{
var manifest = isProgram ? stackbricksConfig.ProgramManifest : stackbricksConfig.StackbricksManifest;
manifest.LastCheckTime = DateTime.Now;
manifest.LastUpdateTime = DateTime.Now;
manifest.Version = msg.version;
await WriteConfig();
}
private async Task WriteConfig()
{
using var fs = new FileStream(StackbricksConfig.CONFIG_FILENAME, FileMode.Truncate, FileAccess.ReadWrite);
using var sw = new StreamWriter(fs);
await sw.WriteAsync(JsonConvert.SerializeObject(stackbricksConfig, StackbricksProgram.jsonSerializer));
}
}

0 comments on commit 74af791

Please sign in to comment.