Skip to content

Commit f0cd498

Browse files
author
yuanwj
committed
为项目添加 AllowParallel 配置。当AllowParallel 为false 或者AllowParallel 不存在时,忽略DependOn配置。所以构建任务按顺序一次执行。这样可以抱枕用户在升级新版本smarcode 后,不修改模版也能正常使用。
1 parent 43eb0ad commit f0cd498

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/SmartCode.Generator/GeneratorProjectBuilder.cs

+41-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,41 @@ public async Task Build()
3232
var dataSource = _pluginManager.Resolve<IDataSource>(_project.DataSource.Name);
3333
await dataSource.InitData();
3434

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+
{
47+
foreach (var buildKV in _project.BuildTasks)
48+
{
49+
_logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------");
50+
var context = new BuildContext
51+
{
52+
PluginManager = _pluginManager,
53+
Project = _project,
54+
DataSource = dataSource,
55+
BuildKey = buildKV.Key,
56+
Build = buildKV.Value,
57+
Output = buildKV.Value.Output?.Copy(),
58+
};
59+
60+
//执行自身任务
61+
await _pluginManager.Resolve<IBuildTask>(context.Build.Type).Build(context);
62+
63+
_logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------");
64+
}
65+
}
66+
67+
private Task ParallelBuild(IDataSource dataSource)
68+
{
69+
3570
IList<BuildContext> allContexts = _project.BuildTasks.Select(d => new BuildContext
3671
{
3772
PluginManager = _pluginManager,
@@ -57,8 +92,8 @@ public async Task Build()
5792
{
5893
context.CountDown.AddCount(context.DependOn.Count);
5994
}
60-
61-
ThreadPool.QueueUserWorkItem(this.BuildTask, (context, allContexts));
95+
96+
ThreadPool.QueueUserWorkItem((obj) => _ = this.BuildTask(obj), (context, allContexts));
6297
}
6398

6499
foreach (var context in allContexts)
@@ -68,9 +103,11 @@ public async Task Build()
68103

69104
countdown.Signal();
70105
countdown.Wait();
106+
107+
return Task.CompletedTask;
71108
}
72109

73-
private async void BuildTask(object obj)
110+
private async Task BuildTask(object obj)
74111
{
75112
var p = ((BuildContext context, IList<BuildContext> allContexts))obj;
76113

@@ -87,7 +124,7 @@ private async void BuildTask(object obj)
87124

88125
foreach (var c in p.allContexts)
89126
{
90-
if(c.DependOn==null || c.DependOn.Count == 0)
127+
if (c.DependOn == null || c.DependOn.Count == 0)
91128
{
92129
continue;
93130
}

src/SmartCode/Configuration/Project.cs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Project
1111
public String ConfigPath { get; set; }
1212
public String Module { get; set; }
1313
public String Author { get; set; }
14+
public bool AllowParallel { get; set; } = false;
1415
public ProjectMode? Mode { get; set; }
1516
public DataSource DataSource { get; set; }
1617
public TemplateEngine TemplateEngine { get; set; } = TemplateEngine.Default;

0 commit comments

Comments
 (0)