Skip to content

Commit b631dc0

Browse files
committed
单个任务内并行生成
1 parent 704734f commit b631dc0

File tree

3 files changed

+38
-48
lines changed

3 files changed

+38
-48
lines changed

src/SmartCode.Generator/BuildTasks/TableBuildTask.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,33 @@ public override async Task Build(BuildContext context)
2828
var filterTables = FilterTable(context.GetDataSource<ITableSource>().Tables, context.BuildKey,
2929
context.Build);
3030
context.SetCurrentAllTable(filterTables);
31-
foreach (var table in filterTables)
31+
var tasks = filterTables.Select(table =>
3232
{
33-
_logger.LogInformation($"BuildTable:{table.Name} Start!");
34-
context.SetCurrentTable(table);
35-
_pluginManager.Resolve<INamingConverter>().Convert(context);
36-
context.Result = await _pluginManager.Resolve<ITemplateEngine>(context.Build.TemplateEngine.Name)
37-
.Render(context);
38-
await _pluginManager.Resolve<IOutput>(context.Build.Output.Type).Output(context);
39-
_logger.LogInformation($"BuildTable:{table.Name} End!");
40-
}
33+
BuildContext newContext = new BuildContext();
34+
newContext.Project = context.Project;
35+
newContext.DataSource = context.DataSource;
36+
newContext.PluginManager = context.PluginManager;
37+
newContext.BuildKey = context.BuildKey;
38+
newContext.Build = context.Build;
39+
newContext.Result = context.Result;
40+
newContext.Output = context.Output;
41+
newContext.SetCurrentTable(table);
42+
43+
return BuildTable(newContext);
44+
}).ToArray();
45+
46+
await Task.WhenAll(tasks);
47+
}
48+
49+
private async Task BuildTable(BuildContext context)
50+
{
51+
var table = context.GetCurrentTable();
52+
_logger.LogInformation($"BuildTable:{table.Name} Start!");
53+
_pluginManager.Resolve<INamingConverter>().Convert(context);
54+
context.Result = await _pluginManager.Resolve<ITemplateEngine>(context.Build.TemplateEngine.Name)
55+
.Render(context);
56+
await _pluginManager.Resolve<IOutput>(context.Build.Output.Type).Output(context);
57+
_logger.LogInformation($"BuildTable:{table.Name} End!");
4158
}
4259

4360
public override void Initialize(IDictionary<string, object> parameters)

src/SmartCode.Generator/GeneratorProjectBuilder.cs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,20 @@ public async Task Build()
3232
this.countdown.Reset();
3333
foreach (var buildKV in _project.BuildTasks)
3434
{
35-
if (!buildKV.Value.WaitPre)
35+
_logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------");
36+
var output = buildKV.Value.Output;
37+
var buildContext = new BuildContext
3638
{
37-
this.countdown.AddCount();
38-
Thread t = new Thread((obj) =>
39-
{
40-
var p = ((KeyValuePair<string, Build> buildKV, IDataSource dataSource))obj;
41-
BuildTask(buildKV, dataSource);
42-
this.countdown.Signal();
43-
});
44-
45-
t.Start((buildKV, dataSource));
46-
continue;
47-
}
48-
49-
this.countdown.Signal();
50-
this.countdown.Wait();
51-
this.countdown.Reset();
52-
53-
BuildTask(buildKV, dataSource);
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! ---------");
5448
}
5549
}
56-
57-
private void BuildTask(KeyValuePair<string, Build> buildKV, IDataSource dataSource)
58-
{
59-
_logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------");
60-
var output = buildKV.Value.Output;
61-
BuildContext buildContext = new BuildContext
62-
{
63-
PluginManager = _pluginManager,
64-
Project = _project,
65-
DataSource = dataSource,
66-
BuildKey = buildKV.Key,
67-
Build = buildKV.Value,
68-
Output = output?.Copy()
69-
};
70-
_pluginManager.Resolve<IBuildTask>(buildKV.Value.Type).Build(buildContext).Wait();
71-
_logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------");
72-
}
7350
}
7451
}

src/SmartCode/Configuration/Build.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ public class Build
2121
public bool? IgnoreView { get; set; }
2222
public NamingConverter NamingConverter { get; set; }
2323
/// <summary>
24-
/// 是否等待前序任务
25-
/// </summary>
26-
public bool WaitPre { get; set; } = true;
27-
/// <summary>
2824
/// 自定义构建参数
2925
/// </summary>
3026
public IDictionary<String, object> Parameters { get; set; }

0 commit comments

Comments
 (0)