1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . IO ;
4
3
using System . Linq ;
5
4
using System . Reflection ;
6
- using System . Text ;
7
5
using Avalonia . Markup . Xaml . XamlIl . CompilerExtensions ;
8
6
using Microsoft . Build . Framework ;
9
7
using Mono . Cecil ;
10
- using Avalonia . Utilities ;
11
8
using Mono . Cecil . Cil ;
12
9
using Mono . Cecil . Rocks ;
13
10
using XamlX ;
@@ -44,16 +41,23 @@ public static CompileResult Compile(IBuildEngine engine, string input, string[]
44
41
string projectDirectory ,
45
42
string output , bool verifyIl , MessageImportance logImportance , string strongNameKey , bool patchCom ,
46
43
bool skipXamlCompilation )
44
+ {
45
+ return Compile ( engine , input , references , projectDirectory , output , verifyIl , logImportance , strongNameKey , patchCom , skipXamlCompilation , debuggerLaunch : false ) ;
46
+ }
47
+
48
+ internal static CompileResult Compile ( IBuildEngine engine , string input , string [ ] references ,
49
+ string projectDirectory ,
50
+ string output , bool verifyIl , MessageImportance logImportance , string strongNameKey , bool patchCom , bool skipXamlCompilation , bool debuggerLaunch )
47
51
{
48
52
var typeSystem = new CecilTypeSystem ( references
49
53
. Where ( r => ! r . ToLowerInvariant ( ) . EndsWith ( "avalonia.build.tasks.dll" ) )
50
54
. Concat ( new [ ] { input } ) , input ) ;
51
-
55
+
52
56
var asm = typeSystem . TargetAssemblyDefinition ;
53
57
54
58
if ( ! skipXamlCompilation )
55
59
{
56
- var compileRes = CompileCore ( engine , typeSystem , projectDirectory , verifyIl , logImportance ) ;
60
+ var compileRes = CompileCore ( engine , typeSystem , projectDirectory , verifyIl , logImportance , debuggerLaunch ) ;
57
61
if ( compileRes == null && ! patchCom )
58
62
return new CompileResult ( true ) ;
59
63
if ( compileRes == false )
@@ -62,21 +66,51 @@ public static CompileResult Compile(IBuildEngine engine, string input, string[]
62
66
63
67
if ( patchCom )
64
68
ComInteropHelper . PatchAssembly ( asm , typeSystem ) ;
65
-
69
+
66
70
var writerParameters = new WriterParameters { WriteSymbols = asm . MainModule . HasSymbols } ;
67
71
if ( ! string . IsNullOrWhiteSpace ( strongNameKey ) )
68
72
writerParameters . StrongNameKeyBlob = File . ReadAllBytes ( strongNameKey ) ;
69
73
70
74
asm . Write ( output , writerParameters ) ;
71
75
72
76
return new CompileResult ( true , true ) ;
73
-
77
+
74
78
}
75
-
79
+
76
80
static bool ? CompileCore ( IBuildEngine engine , CecilTypeSystem typeSystem ,
77
81
string projectDirectory , bool verifyIl ,
78
- MessageImportance logImportance )
82
+ MessageImportance logImportance
83
+ , bool debuggerLaunch = false )
79
84
{
85
+ if ( debuggerLaunch )
86
+ {
87
+ // According this https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debugger.launch?view=net-6.0#remarks
88
+ // documentation, on not windows platform Debugger.Launch() always return true without running a debugger.
89
+ if ( System . Diagnostics . Debugger . Launch ( ) )
90
+ {
91
+ // Set timeout at 1 minut.
92
+ var time = new System . Diagnostics . Stopwatch ( ) ;
93
+ var timeout = TimeSpan . FromMinutes ( 1 ) ;
94
+ time . Start ( ) ;
95
+
96
+ // wait for the debugger to be attacked or timeout.
97
+ while ( ! System . Diagnostics . Debugger . IsAttached && time . Elapsed < timeout )
98
+ {
99
+ engine . LogMessage ( $ "[PID:{ System . Diagnostics . Process . GetCurrentProcess ( ) . Id } ] Wating attach debugger. Elapsed { time . Elapsed } ...", MessageImportance . High ) ;
100
+ System . Threading . Thread . Sleep ( 100 ) ;
101
+ }
102
+
103
+ time . Stop ( ) ;
104
+ if ( time . Elapsed >= timeout )
105
+ {
106
+ engine . LogMessage ( "Wating attach debugger timeout." , MessageImportance . Normal ) ;
107
+ }
108
+ }
109
+ else
110
+ {
111
+ engine . LogMessage ( "Debugging cancelled." , MessageImportance . Normal ) ;
112
+ }
113
+ }
80
114
var asm = typeSystem . TargetAssemblyDefinition ;
81
115
var emres = new EmbeddedResources ( asm ) ;
82
116
var avares = new AvaloniaResources ( asm , projectDirectory ) ;
0 commit comments