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
& "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

View file

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

View file

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

View file

@ -1,3 +1,4 @@
using System.Xml.Linq;
using Common;
namespace LocalServer;
@ -6,6 +7,11 @@ public class LocalSyncServer
{
#pragma warning disable CA2211 // Non-constant fields should not be visible
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
private StateHelpBase StateHelper;
@ -14,6 +20,45 @@ public class LocalSyncServer
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()
{
return StateHelper;

View file

@ -8,7 +8,13 @@ configurationBuilder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("a
//加载文件
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.
builder.Services.AddControllers();
@ -19,6 +25,7 @@ builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<LocalSyncServerFactory>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{

View file

@ -150,39 +150,88 @@ public class DeployHelper(LocalSyncServer context)
{
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()
{
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 =
$" -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)
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
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
process.Start();
bprocess.Start();
// Read the output from the process
string output = process.StandardOutput.ReadToEnd();
string boutput = bprocess.StandardOutput.ReadToEnd();
// Wait for the process to exit
process.WaitForExit();
bprocess.WaitForExit();
if (process.ExitCode == 0)
if (bprocess.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("执行发布错误,错误信息参考上一条消息!");
var aTexts = boutput.Split('\n');
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
{
@ -298,8 +347,9 @@ public class DeployMSSqlHelper(LocalSyncServer context)
ProcessStartInfo startInfo =
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,
StandardOutputEncoding = System.Text.Encoding.UTF8,
// 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

View file

@ -6,5 +6,8 @@
}
},
"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
{
var h = new UnPackAndReleaseHelper(server);
server.StateHelper = h;
server.SetStateHelpBase(h);
h.UnPack();
}

View file

@ -14,6 +14,9 @@ RemoteSyncServerFactory.NamePwd =
[
.. (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";
builder.Services.AddControllers();
builder.Services.AddDbContext<SqliteDbContext>(opions =>

View file

@ -7,8 +7,19 @@ public class RemoteSyncServer
{
#pragma warning disable CA2211 // Non-constant fields should not be visible
public static string TempRootFile = "C:/TempPack";
public static string SqlPackageAbPath = "SqlPackageAbPath";
#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;

View file

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

View file

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

View file

@ -43,6 +43,12 @@ public class PipeTest
p3.Other = p4;
p4.Other = p3;
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";
RemoteSyncServerFactory.NamePwd = [new Tuple<string, string>("Test", "t123")];
var lf = new LocalSyncServerFactory();

View file

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