Skip to content

Commit c2a4da8

Browse files
committed
添加任务依赖
1 parent b631dc0 commit c2a4da8

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed
Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using 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
}

src/SmartCode/BuildContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading.Tasks;
45
using SmartCode.Configuration;
56

67
namespace SmartCode
@@ -26,5 +27,9 @@ public void SetItem(string key, object item)
2627
{
2728
Items[key] = item;
2829
}
30+
31+
public IEnumerable<BuildContext> DependOn { get; set; }
32+
public IEnumerable<BuildContext> Next { get; set; }
33+
public Task BuildTask { get; set; }
2934
}
3035
}

src/SmartCode/Configuration/Build.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ public class Build
1313
/// </summary>
1414
public String Type { get; set; }
1515
public String Module { get; set; }
16-
public TemplateEngine TemplateEngine { get; set; }
16+
public TemplateEngine TemplateEngine { get; set; }
1717
public Output Output { get; set; }
1818
public IEnumerable<String> IncludeTables { get; set; }
1919
public IEnumerable<String> IgnoreTables { get; set; }
2020
public bool? IgnoreNoPKTable { get; set; }
2121
public bool? IgnoreView { get; set; }
2222
public NamingConverter NamingConverter { get; set; }
2323
/// <summary>
24+
/// 依赖于
25+
/// </summary>
26+
public IEnumerable<String> DependOn { get; set; }
27+
/// <summary>
2428
/// 自定义构建参数
2529
/// </summary>
2630
public IDictionary<String, object> Parameters { get; set; }

0 commit comments

Comments
 (0)