1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Threading ;
1
5
using System . Threading . Tasks ;
2
6
using Microsoft . Extensions . Logging ;
3
7
using SmartCode . Configuration ;
@@ -20,28 +24,118 @@ Project project
20
24
_logger = logger ;
21
25
}
22
26
27
+ CountdownEvent countdown = new CountdownEvent ( 1 ) ;
28
+
23
29
24
30
public async Task Build ( )
25
31
{
26
- BuildContext buildContext = null ;
27
32
var dataSource = _pluginManager . Resolve < IDataSource > ( _project . DataSource . Name ) ;
28
33
await dataSource . InitData ( ) ;
34
+
35
+ if ( _project . AllowParallel )
36
+ {
37
+ await this . ParallelBuild ( dataSource ) ;
38
+ }
39
+ else
40
+ {
41
+ await this . SerialBuild ( dataSource ) ;
42
+ }
43
+ }
44
+
45
+ public async Task SerialBuild ( IDataSource dataSource )
46
+ {
29
47
foreach ( var buildKV in _project . BuildTasks )
30
48
{
31
49
_logger . LogInformation ( $ "-------- BuildTask:{ buildKV . Key } Start! ---------") ;
32
- var output = buildKV . Value . Output ;
33
- buildContext = new BuildContext
50
+ var context = new BuildContext
34
51
{
35
52
PluginManager = _pluginManager ,
36
53
Project = _project ,
37
54
DataSource = dataSource ,
38
55
BuildKey = buildKV . Key ,
39
56
Build = buildKV . Value ,
40
- Output = output ? . Copy ( )
57
+ Output = buildKV . Value . Output ? . Copy ( ) ,
41
58
} ;
42
- await _pluginManager . Resolve < IBuildTask > ( buildKV . Value . Type ) . Build ( buildContext ) ;
59
+
60
+ //执行自身任务
61
+ await _pluginManager . Resolve < IBuildTask > ( context . Build . Type ) . Build ( context ) ;
62
+
43
63
_logger . LogInformation ( $ "-------- BuildTask:{ buildKV . Key } End! ---------") ;
44
64
}
45
65
}
66
+
67
+ private Task ParallelBuild ( IDataSource dataSource )
68
+ {
69
+
70
+ IList < BuildContext > allContexts = _project . BuildTasks . Select ( d => new BuildContext
71
+ {
72
+ PluginManager = _pluginManager ,
73
+ Project = _project ,
74
+ DataSource = dataSource ,
75
+ BuildKey = d . Key ,
76
+ Build = d . Value ,
77
+ Output = d . Value . Output ? . Copy ( ) ,
78
+ } ) . ToArray ( ) ;
79
+ foreach ( var context in allContexts )
80
+ {
81
+ if ( context . Build . DependOn != null && context . Build . DependOn . Count ( ) > 0 )
82
+ {
83
+ context . DependOn = allContexts . Where ( d => context . Build . DependOn . Contains ( d . BuildKey ) ) . ToArray ( ) ;
84
+ }
85
+ }
86
+ countdown . Reset ( ) ;
87
+ countdown . AddCount ( allContexts . Count ) ;
88
+ foreach ( var context in allContexts )
89
+ {
90
+ context . CountDown . Reset ( ) ;
91
+ if ( context . DependOn != null && context . DependOn . Count > 0 )
92
+ {
93
+ context . CountDown . AddCount ( context . DependOn . Count ) ;
94
+ }
95
+
96
+ ThreadPool . QueueUserWorkItem ( ( obj ) => _ = this . BuildTask ( obj ) , ( context , allContexts ) ) ;
97
+ }
98
+
99
+ foreach ( var context in allContexts )
100
+ {
101
+ context . CountDown . Signal ( ) ;
102
+ }
103
+
104
+ countdown . Signal ( ) ;
105
+ countdown . Wait ( ) ;
106
+
107
+ return Task . CompletedTask ;
108
+ }
109
+
110
+ private async Task BuildTask ( object obj )
111
+ {
112
+ var p = ( ( BuildContext context , IList < BuildContext > allContexts ) ) obj ;
113
+
114
+ if ( p . context . DependOn != null && p . context . DependOn . Count > 0 )
115
+ {
116
+ _logger . LogInformation ( $ "-------- BuildTask:{ p . context . BuildKey } Wait [{ string . Join ( "," , p . context . DependOn ? . Select ( d => d . BuildKey ) ? . ToArray ( ) ) } ]---------") ;
117
+ }
118
+ //等待依赖任务
119
+ p . context . CountDown . Wait ( ) ;
120
+
121
+ _logger . LogInformation ( $ "-------- BuildTask:{ p . context . BuildKey } Start! ---------") ;
122
+ //执行自身任务
123
+ await _pluginManager . Resolve < IBuildTask > ( p . context . Build . Type ) . Build ( p . context ) ;
124
+
125
+ foreach ( var c in p . allContexts )
126
+ {
127
+ if ( c . DependOn == null || c . DependOn . Count == 0 )
128
+ {
129
+ continue ;
130
+ }
131
+ if ( c . DependOn . Contains ( p . context ) )
132
+ {
133
+ c . CountDown . Signal ( ) ;
134
+ }
135
+ }
136
+
137
+ countdown . Signal ( ) ;
138
+ _logger . LogInformation ( $ "-------- BuildTask:{ p . context . BuildKey } End! ---------") ;
139
+ }
46
140
}
47
141
}
0 commit comments