fix: 修改了一堆的bug

1. msdepoly 删除
2. 修改删除的文件做一个备份
3. websocket 测试
This commit is contained in:
zerlei 2024-09-27 15:02:55 +08:00
parent bfdda810d1
commit 0e32d8c64b
14 changed files with 174 additions and 39 deletions

View file

@ -5,6 +5,10 @@
msbuild .\HMES-H7-HNFYMF.WEB\HMES_H7_HNFYMF.WEB.csproj /t:ResolveReferences /t:Compile /t:_CopyWebApplication /p:Configuration=Release /p:WebProjectOutputDir=C:\publish /p:OutputPath=C:\publish\bin msbuild .\HMES-H7-HNFYMF.WEB\HMES_H7_HNFYMF.WEB.csproj /t:ResolveReferences /t:Compile /t:_CopyWebApplication /p:Configuration=Release /p:WebProjectOutputDir=C:\publish /p:OutputPath=C:\publish\bin
& "C:\Program Files\Microsoft Visual Studio\2022\Community\\MSBuild\Current\Bin\amd64\MSBuild.exe" D:/git/HMES-H7-HNFY/HMES-H7-HNFYMF/HMES-H7-HNFYMF.WEB/HMES_H7_HNFYMF.WEB.csproj /t:ResolveReferences /t:Compile /p:Configuration=Release /t:_CopyWebApplication /p:OutputPath=D:/FileSyncTest/src/bin /p:WebProjectOutputDir=D:/FileSyncTest/src
# 此命令是一个完整的发布命令 # 此命令是一个完整的发布命令
msdeploy.exe -verb:sync -source:contentPath=D:\git\HMES-H7-HNFY\HMES-H7-HNFYMF\HMES-H7-HNFYMF.WEB -dest:contentPath=D:\git\HMES-H7-HNFY\HMES-H7-HNFYMF\release -disablerule:BackupRule msdeploy.exe -verb:sync -source:contentPath=D:\git\HMES-H7-HNFY\HMES-H7-HNFYMF\HMES-H7-HNFYMF.WEB -dest:contentPath=D:\git\HMES-H7-HNFY\HMES-H7-HNFYMF\release -disablerule:BackupRule

View file

@ -140,7 +140,9 @@ public static class DirExtension
{ {
lIndex_f++; lIndex_f++;
rIndex_f++; rIndex_f++;
if (l.MTime != r.MTime) // 如果最新版的时间大于旧版时间超过5s才更新文件时间在传输过程中产生了精度损失。
// Warrning 如果旧版文件的最后修改时间大于新版文件,将不会更新
if ((l.MTime - r.MTime).TotalSeconds > 5)
{ {
cDir.Children.Add( cDir.Children.Add(
new File new File

View file

@ -27,8 +27,7 @@ public abstract class FileDirOpStra
/// 文件目录打包 /// 文件目录打包
/// </summary> /// </summary>
/// <param name="dstRootPath"></param> /// <param name="dstRootPath"></param>
public class FileDirOpForPack(string srcRootPath, string dstRootPath) public class FileDirOpForPack(string srcRootPath, string dstRootPath) : FileDirOpStra
: FileDirOpStra
{ {
/// <summary> /// <summary>
/// 目标根目录 /// 目标根目录
@ -166,8 +165,7 @@ public class FileDirOpForPack(string srcRootPath, string dstRootPath)
public override void DirDel(Dir dir, bool IsRecursion = true) { } public override void DirDel(Dir dir, bool IsRecursion = true) { }
} }
public class FileDirOpForUnpack(string srcRootPath, string dstRootPath) public class FileDirOpForUnpack(string srcRootPath, string dstRootPath) : FileDirOpStra
: FileDirOpStra
{ {
/// <summary> /// <summary>
/// 解压缩,必须首先调用 /// 解压缩,必须首先调用
@ -196,11 +194,8 @@ public class FileDirOpForUnpack(string srcRootPath, string dstRootPath)
} }
if (fileName != String.Empty) if (fileName != String.Empty)
{ {
using ( string fullFilePath = Path.Combine(directoryName, fileName);
FileStream streamWriter = System.IO.File.Create( using (FileStream streamWriter = System.IO.File.Create(fullFilePath))
directoryName + "/" + fileName
)
)
{ {
int size = 2048; int size = 2048;
byte[] data = new byte[2048]; byte[] data = new byte[2048];
@ -217,6 +212,7 @@ public class FileDirOpForUnpack(string srcRootPath, string dstRootPath)
} }
} }
} }
System.IO.File.SetLastWriteTime(fullFilePath, theEntry.DateTime);
} }
} }
} }
@ -277,7 +273,7 @@ public class FileDirOpForUnpack(string srcRootPath, string dstRootPath)
public override void FileModify(string absolutePath, DateTime mtime) public override void FileModify(string absolutePath, DateTime mtime)
{ {
this.FileCreate(absolutePath,mtime); this.FileCreate(absolutePath, mtime);
} }
public override void FileDel(string absolutePath) public override void FileDel(string absolutePath)
@ -287,7 +283,7 @@ public class FileDirOpForUnpack(string srcRootPath, string dstRootPath)
public override void DirDel(Dir dir, bool IsRecursion = true) public override void DirDel(Dir dir, bool IsRecursion = true)
{ {
System.IO.Directory.Delete(dir.FormatedPath,IsRecursion); System.IO.Directory.Delete(dir.FormatedPath, IsRecursion);
} }
} }

View file

@ -1,3 +1,4 @@
using System.Xml.Linq;
using Common; using Common;
namespace LocalServer; namespace LocalServer;
@ -6,6 +7,11 @@ public class LocalSyncServer
{ {
#pragma warning disable CA2211 // Non-constant fields should not be visible #pragma warning disable CA2211 // Non-constant fields should not be visible
public static string TempRootFile = "C:/TempPack"; public static string TempRootFile = "C:/TempPack";
public static string SqlPackageAbPath = "sqlpackage";
public static string MsdeployAbPath = "msdeploy";
//与visual studio 匹配的Msbuild 路径。在vs 中打开power shell 命令行,使用 `(get-Command -Name msbuild).Source `
public static string MSBuildAbPath = "MSBuild";
#pragma warning restore CA2211 // Non-constant fields should not be visible #pragma warning restore CA2211 // Non-constant fields should not be visible
private StateHelpBase StateHelper; private StateHelpBase StateHelper;
@ -14,6 +20,45 @@ public class LocalSyncServer
StateHelper = helper; StateHelper = helper;
} }
public static string GetProjectOutPath(string project)
{
try
{
XDocument xdoc = XDocument.Load(project);
// 获取根元素
XElement rootElement = xdoc.Root ?? throw new NullReferenceException("Root");
Console.WriteLine("根元素: " + rootElement.Name);
// 遍历子节点
foreach (XElement element in rootElement.Elements())
{
if (element.Name.LocalName.Contains("PropertyGroup"))
{
var Conditon = element.Attribute("Condition");
if (Conditon != null)
{
if (Conditon.Value.Contains("Release"))
{
foreach (XElement element2 in element.Elements())
{
if (element2.Name.LocalName == "OutputPath")
{
return element2.Value;
}
}
}
}
}
}
return "bin/";
}
catch (Exception)
{
return "bin/";
}
}
public StateHelpBase GetStateHelper() public StateHelpBase GetStateHelper()
{ {
return StateHelper; return StateHelper;

View file

@ -1,14 +1,20 @@
using LocalServer; using LocalServer;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
ConfigurationBuilder configurationBuilder = new (); ConfigurationBuilder configurationBuilder = new();
//添加配置文件路径 //添加配置文件路径
configurationBuilder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); configurationBuilder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
//加载文件 //加载文件
IConfiguration _configuration = configurationBuilder.Build(); IConfiguration _configuration = configurationBuilder.Build();
LocalSyncServer.TempRootFile = _configuration["TempDir"]??"C:/TempPack";; LocalSyncServer.TempRootFile = _configuration["TempDir"] ?? "C:/TempPack";
LocalSyncServer.SqlPackageAbPath =
_configuration["SqlPackageAbPath"] ?? "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe";
LocalSyncServer.MsdeployAbPath =
_configuration["MsdeployAbPath"]
?? "C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe";
// Add services to the container. // Add services to the container.
builder.Services.AddControllers(); builder.Services.AddControllers();
@ -19,6 +25,7 @@ builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<LocalSyncServerFactory>(); builder.Services.AddSingleton<LocalSyncServerFactory>();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {

View file

@ -150,39 +150,88 @@ public class DeployHelper(LocalSyncServer context)
{ {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
ProcessStartInfo startInfo = //构建
//var OutputPath = LocalSyncServer.GetProjectOutPath(
// Context.NotNullSyncConfig.LocalProjectAbsolutePath
//);
//var AbOutPath = Path.Combine(
// Context.NotNullSyncConfig.LocalProjectAbsolutePath,
// OutputPath
//);
ProcessStartInfo startbuildInfo =
new() new()
{ {
FileName = "C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe", // The command to execute (can be any command line tool) FileName = LocalSyncServer.MSBuildAbPath, // The command to execute (can be any command line tool)
Arguments = Arguments =
$" -verb:sync -source:contentPath={Context.NotNullSyncConfig.LocalProjectAbsolutePath} -dest:contentPath={Context.NotNullSyncConfig.LocalRootPath} -disablerule:BackupRule", $" {Context.NotNullSyncConfig.LocalProjectAbsolutePath} /t:ResolveReferences"
+ $" /t:Compile /p:Configuration=Release /t:_CopyWebApplication /p:OutputPath={LocalSyncServer.TempRootFile}/bin"
+ $" /p:WebProjectOutputDir={LocalSyncServer.TempRootFile}",
// The arguments to pass to the command (e.g., list directory contents) // The arguments to pass to the command (e.g., list directory contents)
RedirectStandardOutput = true, // Redirect the standard output to a string RedirectStandardOutput = true, // Redirect the standard output to a string
RedirectStandardError = true,
StandardOutputEncoding = System.Text.Encoding.UTF8,
UseShellExecute = false, // Do not use the shell to execute the command UseShellExecute = false, // Do not use the shell to execute the command
CreateNoWindow = true // Do not create a new window for the command CreateNoWindow = true // Do not create a new window for the command
}; };
using Process process = new() { StartInfo = startInfo }; using Process bprocess = new() { StartInfo = startbuildInfo };
// Start the process // Start the process
process.Start(); bprocess.Start();
// Read the output from the process // Read the output from the process
string output = process.StandardOutput.ReadToEnd(); string boutput = bprocess.StandardOutput.ReadToEnd();
// Wait for the process to exit // Wait for the process to exit
process.WaitForExit(); bprocess.WaitForExit();
if (process.ExitCode == 0) if (bprocess.ExitCode == 0)
{ {
Context.LocalPipe.SendMsg(CreateMsg("本地编译成功!")).Wait(); Context.LocalPipe.SendMsg(CreateMsg("本地编译成功!")).Wait();
var h = new DiffFileAndPackHelper(Context);
Context.SetStateHelper(h);
h.DiffProcess();
} }
else else
{ {
Context.LocalPipe.SendMsg(CreateErrMsg(output)).Wait(); var aTexts = boutput.Split('\n');
throw new Exception("执行发布错误,错误信息参考上一条消息!"); if (aTexts.Length > 10)
{
boutput = string.Join('\n', aTexts.Skip(aTexts.Length - 10));
}
Context.LocalPipe.SendMsg(CreateErrMsg(boutput)).Wait();
throw new Exception("执行编译错误,错误信息参考上一条消息!");
} }
//发布
//ProcessStartInfo startInfo =
// new()
// {
// FileName = LocalSyncServer.MsdeployAbPath, // The command to execute (can be any command line tool)
// Arguments =
// $" -verb:sync -source:contentPath={Context.NotNullSyncConfig.LocalProjectAbsolutePath} -dest:contentPath={Context.NotNullSyncConfig.LocalRootPath} -disablerule:BackupRule",
// // The arguments to pass to the command (e.g., list directory contents)
// RedirectStandardOutput = true, // Redirect the standard output to a string
// UseShellExecute = false, // Do not use the shell to execute the command
// CreateNoWindow = true // Do not create a new window for the command
// };
//using Process process = new() { StartInfo = startInfo };
//// Start the process
//process.Start();
//// Read the output from the process
//string output = process.StandardOutput.ReadToEnd();
//// Wait for the process to exit
//process.WaitForExit();
//if (process.ExitCode == 0)
//{
// Context.LocalPipe.SendMsg(CreateMsg("本地发布成功!")).Wait();
// var h = new DiffFileAndPackHelper(Context);
// Context.SetStateHelper(h);
// h.DiffProcess();
//}
//else
//{
// Context.LocalPipe.SendMsg(CreateErrMsg(output)).Wait();
// throw new Exception("执行发布错误,错误信息参考上一条消息!");
//}
} }
else else
{ {
@ -298,8 +347,9 @@ public class DeployMSSqlHelper(LocalSyncServer context)
ProcessStartInfo startInfo = ProcessStartInfo startInfo =
new() new()
{ {
FileName = "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe", // The command to execute (can be any command line tool) FileName = LocalSyncServer.SqlPackageAbPath, // The command to execute (can be any command line tool)
Arguments = arguments, Arguments = arguments,
StandardOutputEncoding = System.Text.Encoding.UTF8,
// The arguments to pass to the command (e.g., list directory contents) // The arguments to pass to the command (e.g., list directory contents)
RedirectStandardOutput = true, // Redirect the standard output to a string RedirectStandardOutput = true, // Redirect the standard output to a string
UseShellExecute = false, // Do not use the shell to execute the command UseShellExecute = false, // Do not use the shell to execute the command

View file

@ -6,5 +6,8 @@
} }
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"TempDir":"D:/TempPack" "TempDir": "D:/TempPack",
"SqlPackageAbPath": "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe",
"MsdeployAbPath": "C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe",
"MSBuildAbPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe"
} }

View file

@ -187,7 +187,7 @@ public class SyncFilesController(RemoteSyncServerFactory factory, SqliteDbContex
else else
{ {
var h = new UnPackAndReleaseHelper(server); var h = new UnPackAndReleaseHelper(server);
server.StateHelper = h; server.SetStateHelpBase(h);
h.UnPack(); h.UnPack();
} }

View file

@ -14,6 +14,9 @@ RemoteSyncServerFactory.NamePwd =
[ [
.. (builder.Configuration.GetSection("NamePwds").Get<Tuple<string, string>[]>() ?? []) .. (builder.Configuration.GetSection("NamePwds").Get<Tuple<string, string>[]>() ?? [])
]; ];
RemoteSyncServer.SqlPackageAbPath =
builder.Configuration["SqlPackageAbPath"]
?? "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe";
RemoteSyncServer.TempRootFile = builder.Configuration["TempDir"] ?? "C:/TempPack"; RemoteSyncServer.TempRootFile = builder.Configuration["TempDir"] ?? "C:/TempPack";
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddDbContext<SqliteDbContext>(opions => builder.Services.AddDbContext<SqliteDbContext>(opions =>
@ -37,4 +40,4 @@ app.UseWebSockets();
app.Urls.Clear(); app.Urls.Clear();
app.Urls.Add("http://0.0.0.0:6818"); app.Urls.Add("http://0.0.0.0:6818");
app.MapControllers(); app.MapControllers();
app.Run(); app.Run();

View file

@ -7,8 +7,19 @@ public class RemoteSyncServer
{ {
#pragma warning disable CA2211 // Non-constant fields should not be visible #pragma warning disable CA2211 // Non-constant fields should not be visible
public static string TempRootFile = "C:/TempPack"; public static string TempRootFile = "C:/TempPack";
public static string SqlPackageAbPath = "SqlPackageAbPath";
#pragma warning restore CA2211 // Non-constant fields should not be visible #pragma warning restore CA2211 // Non-constant fields should not be visible
public StateHelpBase StateHelper; private StateHelpBase StateHelper;
public void SetStateHelpBase(StateHelpBase stateHelper)
{
StateHelper = stateHelper;
}
public StateHelpBase GetStateHelpBase()
{
return StateHelper;
}
public Config? SyncConfig; public Config? SyncConfig;

View file

@ -73,7 +73,7 @@ public class ConnectAuthorityHelper(RemoteSyncServer context)
if (msg.Body == Context.Pwd) if (msg.Body == Context.Pwd)
{ {
var h = new DiffFileHelper(Context); var h = new DiffFileHelper(Context);
Context.StateHelper = h; Context.SetStateHelpBase(h);
Context.Pipe.SendMsg(CreateMsg("RemoteServer: 密码验证成功!")); Context.Pipe.SendMsg(CreateMsg("RemoteServer: 密码验证成功!"));
} }
else else
@ -106,6 +106,13 @@ public class DiffFileHelper(RemoteSyncServer context)
Children = [] Children = []
}; };
nd.ExtractInfo(e.CherryPicks, e.Excludes); nd.ExtractInfo(e.CherryPicks, e.Excludes);
var nl = e.LocalDirInfo.Clone();
nl.ResetRootPath(
Context.NotNullSyncConfig.LocalRootPath,
Context.NotNullSyncConfig.RemoteRootPath
);
//var x = JsonSerializer.Serialize(nd);
//var x2 = JsonSerializer.Serialize(nl);
e.DiffDirInfo = e.LocalDirInfo.Diff(nd); e.DiffDirInfo = e.LocalDirInfo.Diff(nd);
e.RemoteDirInfo = nd; e.RemoteDirInfo = nd;
diffConfigs.Add( diffConfigs.Add(
@ -114,7 +121,7 @@ public class DiffFileHelper(RemoteSyncServer context)
} }
}); });
var h = new UnPackAndReleaseHelper(Context); var h = new UnPackAndReleaseHelper(Context);
Context.StateHelper = h; Context.SetStateHelpBase(h);
//将对比结果发送到Local //将对比结果发送到Local
Context.Pipe.SendMsg(CreateMsg(JsonSerializer.Serialize(diffConfigs))); Context.Pipe.SendMsg(CreateMsg(JsonSerializer.Serialize(diffConfigs)));
} }
@ -131,7 +138,7 @@ public class UnPackAndReleaseHelper(RemoteSyncServer context)
); );
Context.Pipe.SendMsg(CreateMsg("解压完成!")).Wait(); Context.Pipe.SendMsg(CreateMsg("解压完成!")).Wait();
var h = new FinallyPublishHelper(Context); var h = new FinallyPublishHelper(Context);
Context.StateHelper = h; Context.SetStateHelpBase(h);
h.FinallyPublish(); h.FinallyPublish();
} }
@ -156,8 +163,8 @@ public class FinallyPublishHelper(RemoteSyncServer context)
ProcessStartInfo startInfo = ProcessStartInfo startInfo =
new() new()
{ {
FileName = "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe", // The command to execute (can be any command line tool) StandardOutputEncoding = System.Text.Encoding.UTF8,
Arguments = arguments, FileName = RemoteSyncServer.SqlPackageAbPath, // The command to execute (can be any command line tool)
// The arguments to pass to the command (e.g., list directory contents) // The arguments to pass to the command (e.g., list directory contents)
RedirectStandardOutput = true, // Redirect the standard output to a string RedirectStandardOutput = true, // Redirect the standard output to a string
UseShellExecute = false, // Do not use the shell to execute the command UseShellExecute = false, // Do not use the shell to execute the command

View file

@ -12,5 +12,6 @@
"TempDir":"D:/TempPack2", "TempDir":"D:/TempPack2",
"NamePwds":[ "NamePwds":[
["test","testpwd"] ["test","testpwd"]
] ],
"SqlPackageAbPath": "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe",
} }

View file

@ -43,6 +43,12 @@ public class PipeTest
p3.Other = p4; p3.Other = p4;
p4.Other = p3; p4.Other = p3;
LocalSyncServer.TempRootFile = "D:/FileSyncTest/stemp"; LocalSyncServer.TempRootFile = "D:/FileSyncTest/stemp";
RemoteSyncServer.SqlPackageAbPath = "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe";
LocalSyncServer.MsdeployAbPath =
"C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe";
LocalSyncServer.SqlPackageAbPath = "C:\\Users\\ZHAOLEI\\.dotnet\\tools\\sqlpackage.exe";
LocalSyncServer.MSBuildAbPath =
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe";
RemoteSyncServer.TempRootFile = "D:/FileSyncTest/dtemp"; RemoteSyncServer.TempRootFile = "D:/FileSyncTest/dtemp";
RemoteSyncServerFactory.NamePwd = [new Tuple<string, string>("Test", "t123")]; RemoteSyncServerFactory.NamePwd = [new Tuple<string, string>("Test", "t123")];
var lf = new LocalSyncServerFactory(); var lf = new LocalSyncServerFactory();

View file

@ -46,7 +46,7 @@ namespace ServerTest
{ {
var it = syncServerFactory.GetServerByName("Test"); var it = syncServerFactory.GetServerByName("Test");
var h = new UnPackAndReleaseHelper(it); var h = new UnPackAndReleaseHelper(it);
it.StateHelper = h; it.SetStateHelpBase(h);
h.UnPack(); h.UnPack();
}); });
}); });