11using System . Collections . Generic ;
2+ using System . Linq ;
23using System . Threading ;
34using System . Threading . Tasks ;
45using Microsoft . Extensions . Logging ;
@@ -11,7 +12,6 @@ public class GeneratorProjectBuilder : IProjectBuilder
1112 private readonly Project _project ;
1213 private readonly IPluginManager _pluginManager ;
1314 private readonly ILogger < GeneratorProjectBuilder > _logger ;
14- private CountdownEvent countdown = new CountdownEvent ( 1 ) ;
1515
1616 public GeneratorProjectBuilder (
1717 Project project
@@ -23,29 +23,69 @@ Project project
2323 _logger = logger ;
2424 }
2525
26+ CountdownEvent countdown = new CountdownEvent ( 1 ) ;
2627
2728 public async Task Build ( )
2829 {
2930 var dataSource = _pluginManager . Resolve < IDataSource > ( _project . DataSource . Name ) ;
3031 await dataSource . InitData ( ) ;
3132
32- this . countdown . Reset ( ) ;
33- foreach ( var buildKV in _project . BuildTasks )
33+ //foreach (var buildKV in _project.BuildTasks)
34+ //{
35+ // _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------");
36+ // var output = buildKV.Value.Output;
37+ // var buildContext = new BuildContext
38+ // {
39+ // PluginManager = _pluginManager,
40+ // Project = _project,
41+ // DataSource = dataSource,
42+ // BuildKey = buildKV.Key,
43+ // Build = buildKV.Value,
44+ // Output = output?.Copy()
45+ // };
46+ // await _pluginManager.Resolve<IBuildTask>(buildKV.Value.Type).Build(buildContext);
47+ // _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------");
48+ //}
49+ BuildContext [ ] contexts = _project . BuildTasks . Select ( d => new BuildContext
3450 {
35- _logger . LogInformation ( $ "-------- BuildTask:{ buildKV . Key } Start! ---------") ;
36- var output = buildKV . Value . Output ;
37- var buildContext = new BuildContext
51+ PluginManager = _pluginManager ,
52+ Project = _project ,
53+ DataSource = dataSource ,
54+ BuildKey = d . Key ,
55+ Build = d . Value ,
56+ Output = d . Value . Output ? . Copy ( ) ,
57+ } ) . ToArray ( ) ;
58+ foreach ( var context in contexts )
59+ {
60+ context . DependOn = contexts . Where ( d => d . Build . DependOn . Contains ( d . BuildKey ) ) . ToArray ( ) ;
61+ }
62+ countdown . Reset ( ) ;
63+ foreach ( var context in contexts )
64+ {
65+ context . BuildTask = Task . Factory . StartNew ( this . BuildTask , null , TaskCreationOptions . LongRunning ) ;
66+ }
67+
68+ countdown . Signal ( ) ;
69+ }
70+ private async void BuildTask ( object obj )
71+ {
72+ countdown . Wait ( ) ;
73+ var context = ( BuildContext ) obj ;
74+ _logger . LogInformation ( $ "-------- BuildTask:{ context . BuildKey } Wait! ---------") ;
75+ //等待依赖任务
76+ if ( context . DependOn != null )
77+ {
78+ foreach ( var dcontext in context . DependOn )
3879 {
39- PluginManager = _pluginManager ,
40- Project = _project ,
41- DataSource = dataSource ,
42- BuildKey = buildKV . Key ,
43- Build = buildKV . Value ,
44- Output = output ? . Copy ( )
45- } ;
46- await _pluginManager . Resolve < IBuildTask > ( buildKV . Value . Type ) . Build ( buildContext ) ;
47- _logger . LogInformation ( $ "-------- BuildTask:{ buildKV . Key } End! ---------") ;
80+ await dcontext . BuildTask ;
81+ }
4882 }
83+
84+ _logger . LogInformation ( $ "-------- BuildTask:{ context . BuildKey } Start! ---------") ;
85+ //执行自身任务
86+ await _pluginManager . Resolve < IBuildTask > ( context . Build . Type ) . Build ( context ) ;
87+
88+ _logger . LogInformation ( $ "-------- BuildTask:{ context . BuildKey } End! ---------") ;
4989 }
5090 }
5191}
0 commit comments