Skip to content

Commit

Permalink
优化 HE 输出以及修复 依赖库无法发出通知的 BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
NMSAzulX committed Jul 4, 2024
1 parent d6ef2b5 commit 70bffdb
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ void ISourceGenerator.Execute(GeneratorExecutionContext context)

string coreScript = "HEProxy.SetProjectKind(HEProjectKind.Console);";
string winformAndwpfLoggerScript = @"
string debugFilePath = Path.Combine(VSCSharpProjectInfomation.MainCsprojPath,""HEDebug.txt"");
if(File.Exists(debugFilePath)){ File.Delete(debugFilePath); }
string debugFilePath = Path.Combine(VSCSharpProjectInfomation.HEOutputPath,""Debug.txt"");
if (Directory.Exists(VSCSharpProjectInfomation.HEOutputPath))
{
var files = Directory.GetFiles(VSCSharpProjectInfomation.HEOutputPath);
foreach (var file in files)
{
File.Delete(file);
}
}
HEFileLogger logger = new HEFileLogger(debugFilePath);
HEProxy.ShowMessage = async msg => {
await logger.WriteUtf8FileAsync(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,56 @@ internal static class WinformRewriter

if (runNode != null)
{

return blockSyntax.ReplaceNode(runNode, [SyntaxFactory.ParseStatement(@$"
HEProxy.SetAftHotExecut(() =>
{{
Task.Run(() =>
System.Threading.Tasks.Task.Run(() =>
{{
Application.ExitThread();
System.Windows.Forms.Application.ExitThread();
while(!DiposeWindows()){{}};
var __heProxInstance = {runArgumentScript};
Form tempForm;
if (__heProxInstance is Form)
System.Windows.Forms.Form tempForm;
if (__heProxInstance is System.Windows.Forms.Form)
{{
tempForm = ((object)__heProxInstance as Form)!;
tempForm = ((object)__heProxInstance as System.Windows.Forms.Form)!;
}}
else
{{
tempForm = (Form)(typeof(ApplicationContext).GetProperty(""MainForm"")!.GetValue(__heProxInstance)!);
tempForm = (System.Windows.Forms.Form)(typeof(System.Windows.Forms.ApplicationContext).GetProperty(""MainForm"")!.GetValue(__heProxInstance)!);
}}
tempForm.FormClosed += (s, e) =>
{{
if (!HEProxy.IsHotCompiling)
{{
var result = MessageBox.Show(""请确认是否退出主程序?"", ""HotExecutor 提醒"", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (result.HasFlag(DialogResult.OK))
var result = System.Windows.Forms.MessageBox.Show(""请确认是否退出主程序?"", ""HotExecutor 提醒"", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (result.HasFlag(System.Windows.Forms.DialogResult.OK))
{{
Application.Exit();
System.Windows.Forms.Application.Exit();
}}
}}
}};
try{{
Application.Run(__heProxInstance);
}}catch(Exception ex)
System.Windows.Forms.Application.Run(__heProxInstance);
}}catch(System.Exception ex)
{{
HEProxy.ShowMessage(ex.Message);
}}
static bool DiposeWindows()
{{
try{{
for (int i = 0; i < Application.OpenForms.Count; i++)
for (int i = 0; i < System.Windows.Forms.Application.OpenForms.Count; i++)
{{
try{{
var form = Application.OpenForms[i];
var form = System.Windows.Forms.Application.OpenForms[i];
if (form!=null)
{{
HEProxy.ShowMessage($""当前将被注销的开放窗体 {{form.Name}}"");
form.Dispose();
DelegateHelper<FormCollection, Form>.Execute.Invoke(Application.OpenForms, form);
Natasha.CSharp.HotExecutor.Component.DelegateHelper<System.Windows.Forms.FormCollection, System.Windows.Forms.Form>.Execute.Invoke(System.Windows.Forms.Application.OpenForms, form);
}}
}}catch{{
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Natasha.CSharp.HotExecutor.Component.SyntaxUtils
Expand All @@ -10,31 +11,50 @@ internal static class WpfWriter
{
public static BlockSyntax? Handle(BlockSyntax blockSyntax)
{
return blockSyntax.WithStatements([SyntaxFactory.ParseStatement(@$"HEProxy.SetAftHotExecut(() => {{
Task.Run(() => {{
while(!DiposeWindows()){{}};
Application.Current.Run();
var mainStatement = SyntaxFactory.ParseStatement(@"
System.Windows.Application.Current.Dispatcher.Invoke(()=>{
while(!DiposeWindows()){};
static bool DiposeWindows()
{{
try{{
for (int i = 0; i < Application.Current.Windows.Count; i++)
{{
var window = Application.Current.Windows[i];
try{{
window.Close();
}}catch{{
}}
}}
}}catch{{
return false;
}}
{
try{
//HEProxy.ShowMessage($""当前将被注销的开放窗体个数 {System.Windows.Application.Current.Windows.Count}"");
for (int i = 0; i < System.Windows.Application.Current.Windows.Count; i++)
{
try{
var window = System.Windows.Application.Current.Windows[i];
window.Dispatcher.Invoke(()=>{ window.Close(); });
}catch{
}
}
}catch(System.Exception ex){
HEProxy.ShowMessage($""出现异常 {ex.GetType()}{ex.Message}"");
if(ex is not System.InvalidOperationException){
return false;
}
}
return true;
}}
}});
}});")]);
}
});
System.Windows.Application.Current.Shutdown();
");

var newStatement = SyntaxFactory.ParseStatement(@$"
HEProxy.SetAftHotExecut(() => {{
{blockSyntax.Statements.ToFullString()}
}});");
Debug.WriteLine(mainStatement.ToFullString());
return blockSyntax.WithStatements([newStatement]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class VSCSharpProjectInfomation
public static readonly HashSet<string> ExpectFiles;
public static readonly bool EnableImplicitUsings;
public static IEnumerable<string>? MainAssemblyUsings;
public static readonly string HEOutputPath;
static VSCSharpProjectInfomation()
{
ExpectFiles = [];
Expand All @@ -29,6 +30,7 @@ static VSCSharpProjectInfomation()
ExecutePath = new FileInfo(currentExeFilePath).Directory!.FullName;
var currentDirectoryInfo = new DirectoryInfo(ExecutePath);
MainCsprojPath = FindFileDirectory(currentDirectoryInfo,"*.csproj");
HEOutputPath = Path.Combine(MainCsprojPath, "HEOutput");
var files = Directory.GetFiles(MainCsprojPath,"*.csproj");
CSProjFilePath = files[0];
BinPath = Path.Combine(MainCsprojPath, "bin");
Expand Down Expand Up @@ -93,9 +95,13 @@ public static bool CheckFileAvailiable(string file)
return CheckFileAvivaliable(file, BinPath, ObjPath);
}

public static bool IsXamlAndResxFile(string file)
{
return file.EndsWith(".xaml") || file.EndsWith(".resx");
}
public static bool CheckFileAvivaliable(string file, string binPath, string objPath)
{
if (file.EndsWith(".cs") || file.EndsWith(".xaml") || file.EndsWith(".resx"))
if (file.EndsWith(".cs"))
{
if (file.StartsWith(binPath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public void DeployMonitor()
{
return;
}
if (VSCSharpProjectInfomation.IsXamlAndResxFile(e.FullPath))
{
await ExecuteAfterFunction();

if (VSCSharpProjectInfomation.CheckFileAvailiable(e.FullPath))
}
else if (VSCSharpProjectInfomation.CheckFileAvailiable(e.FullPath))
{
#if DEBUG
HEProxy.ShowMessage($"Created: {e.FullPath}");
Expand All @@ -62,6 +66,7 @@ public void DeployMonitor()
_compileLock.GetAndWaitLock();
CreateFileAction(e.FullPath);
_compileLock.ReleaseLock();

await ExecuteAfterFunction();
}

Expand All @@ -73,7 +78,12 @@ public void DeployMonitor()
{
return;
}
if (VSCSharpProjectInfomation.CheckFileAvailiable(e.FullPath))
if (VSCSharpProjectInfomation.IsXamlAndResxFile(e.FullPath))
{
await ExecuteAfterFunction();

}
else if (VSCSharpProjectInfomation.CheckFileAvailiable(e.FullPath))
{
#if DEBUG
HEProxy.ShowMessage($"Deleted: {e.FullPath}");
Expand All @@ -83,7 +93,7 @@ public void DeployMonitor()
_compileLock.ReleaseLock();
await ExecuteAfterFunction();
}

};

_mainWatcher.Renamed += async (sender, e) =>
Expand All @@ -92,10 +102,13 @@ public void DeployMonitor()
{
return;
}
if (VSCSharpProjectInfomation.IsXamlAndResxFile(e.OldFullPath) || VSCSharpProjectInfomation.IsXamlAndResxFile(e.FullPath))
{
await ExecuteAfterFunction();

if (e.OldFullPath.EndsWith(".cs") || e.OldFullPath.EndsWith(".xaml"))
}else if (e.OldFullPath.EndsWith(".cs"))
{
if (e.FullPath.EndsWith(".cs") || e.FullPath.EndsWith(".xaml"))
if (e.FullPath.EndsWith(".cs"))
{
#if DEBUG
HEProxy.ShowMessage($"Renamed: {e.OldFullPath} -> {e.FullPath}");
Expand Down Expand Up @@ -150,10 +163,10 @@ private async Task ExecuteAfterFunction()
catch
{


}
}

private static void Error(object sender, ErrorEventArgs e)
{
PrintException(e.GetException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void SetExecute(Func<Task> execute)
public void Notify()
{
#if DEBUG
Console.WriteLine($"接收重建通知!");
Debug.WriteLine($"接收重建通知!");
#endif
_timeStamp = DateTime.UtcNow.Ticks / 10000;
_needExecuted = true;
Expand All @@ -46,7 +46,7 @@ public void StartMonitor()
{
Clean();
#if DEBUG
Console.WriteLine($"时间戳差值: {DateTime.UtcNow.Ticks / 10000 - _timeStamp}");
Debug.WriteLine($"时间戳差值: {DateTime.UtcNow.Ticks / 10000 - _timeStamp}");
#endif
if (DateTime.UtcNow.Ticks / 10000 - _timeStamp > 2000)
{
Expand Down Expand Up @@ -129,21 +129,36 @@ public VSCSharpProjectFileInternalWatcher(string csprojPath, ConcurrentDictionar
var fileName = Path.GetFileName(csprojPath);
var binFolder = Path.Combine(folder, "bin");
var objFolder = Path.Combine(folder, "obj");
FileSystemEventHandler handler = (sender, e) => {
FileSystemEventHandler csFileHandler = (sender, e) => {

if (VSCSharpProjectInfomation.CheckFileAvivaliable(e.FullPath, binFolder, objFolder))
{
_notify?.Invoke();
}

};
RenamedEventHandler reNameHandler = (sender, e) => {
RenamedEventHandler csRenameHandler = (sender, e) => {

if (VSCSharpProjectInfomation.CheckFileAvivaliable(e.FullPath, binFolder, objFolder))
{
_notify?.Invoke();
}
};
FileSystemEventHandler csprojFileHandler = (sender, e) => {

if (e.FullPath == csprojPath)
{
_notify?.Invoke();
}

};
RenamedEventHandler csprojRenameHandler = (sender, e) => {

if (e.FullPath == csprojPath)
{
_notify?.Invoke();
}
};

var csfileWatcher = new FileSystemWatcher()
{
Expand All @@ -152,10 +167,10 @@ public VSCSharpProjectFileInternalWatcher(string csprojPath, ConcurrentDictionar
EnableRaisingEvents = true,
IncludeSubdirectories = true,
};
csfileWatcher.Changed += handler;
csfileWatcher.Deleted += handler;
csfileWatcher.Created += handler;
csfileWatcher.Renamed += reNameHandler;
csfileWatcher.Changed += csFileHandler;
csfileWatcher.Deleted += csFileHandler;
csfileWatcher.Created += csFileHandler;
csfileWatcher.Renamed += csRenameHandler;

var csprojWatcher = new FileSystemWatcher()
{
Expand All @@ -166,8 +181,8 @@ public VSCSharpProjectFileInternalWatcher(string csprojPath, ConcurrentDictionar
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.LastAccess
};

csprojWatcher.Changed += handler;
csprojWatcher.Renamed += reNameHandler;
csprojWatcher.Changed += csprojFileHandler;
csprojWatcher.Renamed += csprojRenameHandler;
csprojWatcher.Deleted += (sender, e) =>
{
if (e.FullPath == csprojPath)
Expand Down
Loading

0 comments on commit 70bffdb

Please sign in to comment.