1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Threading ;
@@ -25,12 +26,13 @@ Project project
25
26
26
27
CountdownEvent countdown = new CountdownEvent ( 1 ) ;
27
28
29
+
28
30
public async Task Build ( )
29
31
{
30
32
var dataSource = _pluginManager . Resolve < IDataSource > ( _project . DataSource . Name ) ;
31
33
await dataSource . InitData ( ) ;
32
34
33
- BuildContext [ ] contexts = _project . BuildTasks . Select ( d => new BuildContext
35
+ IList < BuildContext > allContexts = _project . BuildTasks . Select ( d => new BuildContext
34
36
{
35
37
PluginManager = _pluginManager ,
36
38
Project = _project ,
@@ -39,40 +41,64 @@ public async Task Build()
39
41
Build = d . Value ,
40
42
Output = d . Value . Output ? . Copy ( ) ,
41
43
} ) . ToArray ( ) ;
42
- foreach ( var context in contexts )
44
+ foreach ( var context in allContexts )
43
45
{
44
- context . DependOn = contexts . Where ( d => context . Build . DependOn != null && context . Build . DependOn . Contains ( d . BuildKey ) ) . ToArray ( ) ;
46
+ if ( context . Build . DependOn != null && context . Build . DependOn . Count ( ) > 0 )
47
+ {
48
+ context . DependOn = allContexts . Where ( d => context . Build . DependOn . Contains ( d . BuildKey ) ) . ToArray ( ) ;
49
+ }
45
50
}
46
-
47
51
countdown . Reset ( ) ;
48
- foreach ( var context in contexts )
52
+ countdown . AddCount ( allContexts . Count ) ;
53
+ foreach ( var context in allContexts )
54
+ {
55
+ context . CountDown . Reset ( ) ;
56
+ if ( context . DependOn != null && context . DependOn . Count > 0 )
57
+ {
58
+ context . CountDown . AddCount ( context . DependOn . Count ) ;
59
+ }
60
+
61
+ ThreadPool . QueueUserWorkItem ( this . BuildTask , ( context , allContexts ) ) ;
62
+ }
63
+
64
+ foreach ( var context in allContexts )
49
65
{
50
- context . BuildTask = Task . Factory . StartNew ( this . BuildTask , context , TaskCreationOptions . LongRunning ) ;
66
+ context . CountDown . Signal ( ) ;
51
67
}
52
68
53
69
countdown . Signal ( ) ;
54
-
55
- await Task . WhenAll ( contexts . Select ( d => d . BuildTask ) . ToArray ( ) ) ;
70
+ countdown . Wait ( ) ;
56
71
}
72
+
57
73
private async void BuildTask ( object obj )
58
74
{
59
- var context = ( BuildContext ) obj ;
60
- _logger . LogInformation ( $ "-------- BuildTask:{ context . BuildKey } Wait! ---------") ;
61
- countdown . Wait ( ) ;
75
+ var p = ( ( BuildContext context , IList < BuildContext > allContexts ) ) obj ;
76
+
77
+ if ( p . context . DependOn != null && p . context . DependOn . Count > 0 )
78
+ {
79
+ _logger . LogInformation ( $ "-------- BuildTask:{ p . context . BuildKey } Wait [{ string . Join ( "," , p . context . DependOn ? . Select ( d => d . BuildKey ) ? . ToArray ( ) ) } ]---------") ;
80
+ }
62
81
//等待依赖任务
63
- if ( context . DependOn != null )
82
+ p . context . CountDown . Wait ( ) ;
83
+
84
+ _logger . LogInformation ( $ "-------- BuildTask:{ p . context . BuildKey } Start! ---------") ;
85
+ //执行自身任务
86
+ await _pluginManager . Resolve < IBuildTask > ( p . context . Build . Type ) . Build ( p . context ) ;
87
+
88
+ foreach ( var c in p . allContexts )
64
89
{
65
- foreach ( var dcontext in context . DependOn )
90
+ if ( c . DependOn == null || c . DependOn . Count == 0 )
91
+ {
92
+ continue ;
93
+ }
94
+ if ( c . DependOn . Contains ( p . context ) )
66
95
{
67
- await dcontext . BuildTask ;
96
+ c . CountDown . Signal ( ) ;
68
97
}
69
98
}
70
99
71
- _logger . LogInformation ( $ "-------- BuildTask:{ context . BuildKey } Start! ---------") ;
72
- //执行自身任务
73
- await _pluginManager . Resolve < IBuildTask > ( context . Build . Type ) . Build ( context ) ;
74
-
75
- _logger . LogInformation ( $ "-------- BuildTask:{ context . BuildKey } End! ---------") ;
100
+ countdown . Signal ( ) ;
101
+ _logger . LogInformation ( $ "-------- BuildTask:{ p . context . BuildKey } End! ---------") ;
76
102
}
77
103
}
78
104
}
0 commit comments