1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Threading ;
15using System . Threading . Tasks ;
26using Microsoft . Extensions . Logging ;
37using SmartCode . Configuration ;
@@ -20,28 +24,118 @@ Project project
2024 _logger = logger ;
2125 }
2226
27+ CountdownEvent countdown = new CountdownEvent ( 1 ) ;
28+
2329
2430 public async Task Build ( )
2531 {
26- BuildContext buildContext = null ;
2732 var dataSource = _pluginManager . Resolve < IDataSource > ( _project . DataSource . Name ) ;
2833 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+ {
2947 foreach ( var buildKV in _project . BuildTasks )
3048 {
3149 _logger . LogInformation ( $ "-------- BuildTask:{ buildKV . Key } Start! ---------") ;
32- var output = buildKV . Value . Output ;
33- buildContext = new BuildContext
50+ var context = new BuildContext
3451 {
3552 PluginManager = _pluginManager ,
3653 Project = _project ,
3754 DataSource = dataSource ,
3855 BuildKey = buildKV . Key ,
3956 Build = buildKV . Value ,
40- Output = output ? . Copy ( )
57+ Output = buildKV . Value . Output ? . Copy ( ) ,
4158 } ;
42- await _pluginManager . Resolve < IBuildTask > ( buildKV . Value . Type ) . Build ( buildContext ) ;
59+
60+ //执行自身任务
61+ await _pluginManager . Resolve < IBuildTask > ( context . Build . Type ) . Build ( context ) ;
62+
4363 _logger . LogInformation ( $ "-------- BuildTask:{ buildKV . Key } End! ---------") ;
4464 }
4565 }
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+ }
46140 }
47141}
0 commit comments