From 028aed6ff80e93c4c2edc0707ba3a4046187f2fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=BB=91=E5=85=94?= <173592829@qq.com> Date: Mon, 7 Dec 2020 10:32:35 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E7=A7=BB=E9=99=A4Input=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=90=91=EF=BC=8C=E8=A7=A3=E5=86=B3win7=E4=B8=8BPowerShell=20?= =?UTF-8?q?=E6=97=A0=E6=B3=95Exit=20=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SmartCode.App/BuildTasks/ProcessBuildTask.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SmartCode.App/BuildTasks/ProcessBuildTask.cs b/src/SmartCode.App/BuildTasks/ProcessBuildTask.cs index 8929ab0..1544dcf 100644 --- a/src/SmartCode.App/BuildTasks/ProcessBuildTask.cs +++ b/src/SmartCode.App/BuildTasks/ProcessBuildTask.cs @@ -37,7 +37,7 @@ public Task Build(BuildContext context) var startInfo = new ProcessStartInfo(fileName) { UseShellExecute = false, - RedirectStandardInput = true, + RedirectStandardInput = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = DEFAULT_CREATE_NO_WINDOW @@ -103,7 +103,7 @@ public Task Build(BuildContext context) } _logger.LogDebug($"--------Process.FileName:{startInfo.FileName},Args:{startInfo.Arguments},Taken:{process.TotalProcessorTime.TotalMilliseconds} End--------"); } - return Task.CompletedTask; + return Task.CompletedTask; } private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) From a382c557cf2f926219a2ceb56fdfbd79333f7091 Mon Sep 17 00:00:00 2001 From: yuanwj Date: Thu, 3 Mar 2022 11:05:31 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=87=BA=E6=9D=A5=E7=9A=84=E6=96=87=E4=BB=B6=E5=9C=A8VS?= =?UTF-8?q?=E4=B8=AD=E6=8F=90=E7=A4=BA=E2=80=9C=E4=B8=8D=E4=B8=80=E8=87=B3?= =?UTF-8?q?=E7=9A=84=E8=A1=8C=E5=B0=BE=E2=80=9D=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SmartCode.App/Outputs/FileOutput.cs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/SmartCode.App/Outputs/FileOutput.cs b/src/SmartCode.App/Outputs/FileOutput.cs index 1efd8a3..1c089e5 100644 --- a/src/SmartCode.App/Outputs/FileOutput.cs +++ b/src/SmartCode.App/Outputs/FileOutput.cs @@ -69,21 +69,21 @@ public async Task Output(BuildContext context, Output output = null) { case Configuration.CreateMode.None: case Configuration.CreateMode.Incre: - { - _logger.LogWarning( - $"------ Mode:{output.Mode},Build:{context.BuildKey},FilePath:{filePath} Exists ignore output End! ------"); - return; - } + { + _logger.LogWarning( + $"------ Mode:{output.Mode},Build:{context.BuildKey},FilePath:{filePath} Exists ignore output End! ------"); + return; + } case Configuration.CreateMode.Full: - { - File.Delete(filePath); - _logger.LogWarning($"------ Mode:{output.Mode},FilePath:{filePath} Exists Deleted ! ------"); - break; - } + { + File.Delete(filePath); + _logger.LogWarning($"------ Mode:{output.Mode},FilePath:{filePath} Exists Deleted ! ------"); + break; + } } } - using (StreamWriter streamWriter = new StreamWriter(filePath)) + using (StreamWriter streamWriter = new StreamWriter(filePath, false, new UTF8Encoding(true))) { await streamWriter.WriteAsync(context.Result.Trim()); } From a1005f4fbd9642359f6caec735b2e06684528279 Mon Sep 17 00:00:00 2001 From: yuanwj Date: Thu, 3 Mar 2022 11:45:52 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=A1=8C=E5=B0=BE?= =?UTF-8?q?=E4=B8=BA=20\R\N?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SmartCode.App/Outputs/FileOutput.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/SmartCode.App/Outputs/FileOutput.cs b/src/SmartCode.App/Outputs/FileOutput.cs index 1c089e5..602a4f0 100644 --- a/src/SmartCode.App/Outputs/FileOutput.cs +++ b/src/SmartCode.App/Outputs/FileOutput.cs @@ -7,6 +7,7 @@ using HandlebarsDotNet; using SmartCode.Configuration; using SmartCode.Utilities; +using System.Text.RegularExpressions; namespace SmartCode.App.Outputs { @@ -83,9 +84,12 @@ public async Task Output(BuildContext context, Output output = null) } } + //采购VS默认的UTF-8 WITH BOM 编码 using (StreamWriter streamWriter = new StreamWriter(filePath, false, new UTF8Encoding(true))) { - await streamWriter.WriteAsync(context.Result.Trim()); + //强制行尾为 \r\n + var result = Regex.Replace(context.Result.Trim(), @"[\r\n]+", "\r\n", RegexOptions.Multiline); + await streamWriter.WriteAsync(result); } _logger.LogInformation($"------ Mode:{output.Mode},Build:{context.BuildKey} -> {filePath} End! ------"); From 4e80a8de4bbce1048255c792d4b86b00560fc23c Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 3 Mar 2022 16:14:34 +0800 Subject: [PATCH 04/14] update version --- build/version.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/version.props b/build/version.props index 8e0c968..83651c8 100644 --- a/build/version.props +++ b/build/version.props @@ -2,7 +2,7 @@ 2 3 - 6 + 7 $(VersionMajor).$(VersionMinor).$(VersionPatch) From 704734fe3c83239169081984e4099d9f6a53aedf Mon Sep 17 00:00:00 2001 From: yuanwj Date: Fri, 11 Mar 2022 16:54:44 +0800 Subject: [PATCH 05/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=9A=E5=B0=91?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=9E=84=E5=BB=BA=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?WaitPre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GeneratorProjectBuilder.cs | 53 ++++++++++++++----- src/SmartCode/Configuration/Build.cs | 4 ++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/SmartCode.Generator/GeneratorProjectBuilder.cs b/src/SmartCode.Generator/GeneratorProjectBuilder.cs index 7944eb3..1378ddf 100644 --- a/src/SmartCode.Generator/GeneratorProjectBuilder.cs +++ b/src/SmartCode.Generator/GeneratorProjectBuilder.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using SmartCode.Configuration; @@ -9,6 +11,7 @@ public class GeneratorProjectBuilder : IProjectBuilder private readonly Project _project; private readonly IPluginManager _pluginManager; private readonly ILogger _logger; + private CountdownEvent countdown = new CountdownEvent(1); public GeneratorProjectBuilder( Project project @@ -23,25 +26,49 @@ Project project public async Task Build() { - BuildContext buildContext = null; var dataSource = _pluginManager.Resolve(_project.DataSource.Name); await dataSource.InitData(); + + this.countdown.Reset(); foreach (var buildKV in _project.BuildTasks) { - _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); - var output = buildKV.Value.Output; - buildContext = new BuildContext + if (!buildKV.Value.WaitPre) { - PluginManager = _pluginManager, - Project = _project, - DataSource = dataSource, - BuildKey = buildKV.Key, - Build = buildKV.Value, - Output = output?.Copy() - }; - await _pluginManager.Resolve(buildKV.Value.Type).Build(buildContext); - _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); + this.countdown.AddCount(); + Thread t = new Thread((obj) => + { + var p = ((KeyValuePair buildKV, IDataSource dataSource))obj; + BuildTask(buildKV, dataSource); + this.countdown.Signal(); + }); + + t.Start((buildKV, dataSource)); + continue; + } + + this.countdown.Signal(); + this.countdown.Wait(); + this.countdown.Reset(); + + BuildTask(buildKV, dataSource); } } + + private void BuildTask(KeyValuePair buildKV, IDataSource dataSource) + { + _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); + var output = buildKV.Value.Output; + BuildContext buildContext = new BuildContext + { + PluginManager = _pluginManager, + Project = _project, + DataSource = dataSource, + BuildKey = buildKV.Key, + Build = buildKV.Value, + Output = output?.Copy() + }; + _pluginManager.Resolve(buildKV.Value.Type).Build(buildContext).Wait(); + _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); + } } } \ No newline at end of file diff --git a/src/SmartCode/Configuration/Build.cs b/src/SmartCode/Configuration/Build.cs index 72a75aa..50589e4 100644 --- a/src/SmartCode/Configuration/Build.cs +++ b/src/SmartCode/Configuration/Build.cs @@ -21,6 +21,10 @@ public class Build public bool? IgnoreView { get; set; } public NamingConverter NamingConverter { get; set; } /// + /// 是否等待前序任务 + /// + public bool WaitPre { get; set; } = true; + /// /// 自定义构建参数 /// public IDictionary Parameters { get; set; } From b631dc095113c08e054677ae53850121a875452c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=BB=91=E5=85=94?= Date: Sun, 13 Mar 2022 23:56:40 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E5=8D=95=E4=B8=AA=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=86=85=E5=B9=B6=E8=A1=8C=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BuildTasks/TableBuildTask.cs | 35 ++++++++++---- .../GeneratorProjectBuilder.cs | 47 +++++-------------- src/SmartCode/Configuration/Build.cs | 4 -- 3 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs b/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs index 92ff6ed..065e8a3 100644 --- a/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs +++ b/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs @@ -28,16 +28,33 @@ public override async Task Build(BuildContext context) var filterTables = FilterTable(context.GetDataSource().Tables, context.BuildKey, context.Build); context.SetCurrentAllTable(filterTables); - foreach (var table in filterTables) + var tasks = filterTables.Select(table => { - _logger.LogInformation($"BuildTable:{table.Name} Start!"); - context.SetCurrentTable(table); - _pluginManager.Resolve().Convert(context); - context.Result = await _pluginManager.Resolve(context.Build.TemplateEngine.Name) - .Render(context); - await _pluginManager.Resolve(context.Build.Output.Type).Output(context); - _logger.LogInformation($"BuildTable:{table.Name} End!"); - } + BuildContext newContext = new BuildContext(); + newContext.Project = context.Project; + newContext.DataSource = context.DataSource; + newContext.PluginManager = context.PluginManager; + newContext.BuildKey = context.BuildKey; + newContext.Build = context.Build; + newContext.Result = context.Result; + newContext.Output = context.Output; + newContext.SetCurrentTable(table); + + return BuildTable(newContext); + }).ToArray(); + + await Task.WhenAll(tasks); + } + + private async Task BuildTable(BuildContext context) + { + var table = context.GetCurrentTable(); + _logger.LogInformation($"BuildTable:{table.Name} Start!"); + _pluginManager.Resolve().Convert(context); + context.Result = await _pluginManager.Resolve(context.Build.TemplateEngine.Name) + .Render(context); + await _pluginManager.Resolve(context.Build.Output.Type).Output(context); + _logger.LogInformation($"BuildTable:{table.Name} End!"); } public override void Initialize(IDictionary parameters) diff --git a/src/SmartCode.Generator/GeneratorProjectBuilder.cs b/src/SmartCode.Generator/GeneratorProjectBuilder.cs index 1378ddf..b2eb087 100644 --- a/src/SmartCode.Generator/GeneratorProjectBuilder.cs +++ b/src/SmartCode.Generator/GeneratorProjectBuilder.cs @@ -32,43 +32,20 @@ public async Task Build() this.countdown.Reset(); foreach (var buildKV in _project.BuildTasks) { - if (!buildKV.Value.WaitPre) + _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); + var output = buildKV.Value.Output; + var buildContext = new BuildContext { - this.countdown.AddCount(); - Thread t = new Thread((obj) => - { - var p = ((KeyValuePair buildKV, IDataSource dataSource))obj; - BuildTask(buildKV, dataSource); - this.countdown.Signal(); - }); - - t.Start((buildKV, dataSource)); - continue; - } - - this.countdown.Signal(); - this.countdown.Wait(); - this.countdown.Reset(); - - BuildTask(buildKV, dataSource); + PluginManager = _pluginManager, + Project = _project, + DataSource = dataSource, + BuildKey = buildKV.Key, + Build = buildKV.Value, + Output = output?.Copy() + }; + await _pluginManager.Resolve(buildKV.Value.Type).Build(buildContext); + _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); } } - - private void BuildTask(KeyValuePair buildKV, IDataSource dataSource) - { - _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); - var output = buildKV.Value.Output; - BuildContext buildContext = new BuildContext - { - PluginManager = _pluginManager, - Project = _project, - DataSource = dataSource, - BuildKey = buildKV.Key, - Build = buildKV.Value, - Output = output?.Copy() - }; - _pluginManager.Resolve(buildKV.Value.Type).Build(buildContext).Wait(); - _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); - } } } \ No newline at end of file diff --git a/src/SmartCode/Configuration/Build.cs b/src/SmartCode/Configuration/Build.cs index 50589e4..72a75aa 100644 --- a/src/SmartCode/Configuration/Build.cs +++ b/src/SmartCode/Configuration/Build.cs @@ -21,10 +21,6 @@ public class Build public bool? IgnoreView { get; set; } public NamingConverter NamingConverter { get; set; } /// - /// 是否等待前序任务 - /// - public bool WaitPre { get; set; } = true; - /// /// 自定义构建参数 /// public IDictionary Parameters { get; set; } From 724bf74b889517a355687c3d427cfb269dc24e90 Mon Sep 17 00:00:00 2001 From: yuanwj Date: Mon, 14 Mar 2022 01:50:44 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E5=8D=95=E4=BB=BB=E5=8A=A1=E5=A4=9A?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BuildTasks/TableBuildTask.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs b/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs index 065e8a3..d34cbff 100644 --- a/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs +++ b/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs @@ -28,6 +28,11 @@ public override async Task Build(BuildContext context) var filterTables = FilterTable(context.GetDataSource().Tables, context.BuildKey, context.Build); context.SetCurrentAllTable(filterTables); + + var nameConverter = _pluginManager.Resolve(); + var templateEngine = _pluginManager.Resolve(context.Build.TemplateEngine.Name); + var output = _pluginManager.Resolve(context.Build.Output.Type); + var tasks = filterTables.Select(table => { BuildContext newContext = new BuildContext(); @@ -40,20 +45,20 @@ public override async Task Build(BuildContext context) newContext.Output = context.Output; newContext.SetCurrentTable(table); - return BuildTable(newContext); + return Task.Factory.StartNew(BuildTable,(newContext, nameConverter, templateEngine, output), TaskCreationOptions.LongRunning); }).ToArray(); await Task.WhenAll(tasks); } - private async Task BuildTable(BuildContext context) + private async void BuildTable(object obj) { - var table = context.GetCurrentTable(); + var p=((BuildContext context, INamingConverter nameConverter, ITemplateEngine templateEngine, IOutput output)) obj; + var table = p.context.GetCurrentTable(); _logger.LogInformation($"BuildTable:{table.Name} Start!"); - _pluginManager.Resolve().Convert(context); - context.Result = await _pluginManager.Resolve(context.Build.TemplateEngine.Name) - .Render(context); - await _pluginManager.Resolve(context.Build.Output.Type).Output(context); + p.nameConverter.Convert(p.context); + p.context.Result = await p.templateEngine.Render(p.context); + await p.output.Output(p.context); _logger.LogInformation($"BuildTable:{table.Name} End!"); } From c2a4da82296382f2e36fcedcd0efd896dbf6f0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=BB=91=E5=85=94?= Date: Tue, 15 Mar 2022 01:22:00 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GeneratorProjectBuilder.cs | 70 +++++++++++++++---- src/SmartCode/BuildContext.cs | 5 ++ src/SmartCode/Configuration/Build.cs | 6 +- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/SmartCode.Generator/GeneratorProjectBuilder.cs b/src/SmartCode.Generator/GeneratorProjectBuilder.cs index b2eb087..a1bbff8 100644 --- a/src/SmartCode.Generator/GeneratorProjectBuilder.cs +++ b/src/SmartCode.Generator/GeneratorProjectBuilder.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -11,7 +12,6 @@ public class GeneratorProjectBuilder : IProjectBuilder private readonly Project _project; private readonly IPluginManager _pluginManager; private readonly ILogger _logger; - private CountdownEvent countdown = new CountdownEvent(1); public GeneratorProjectBuilder( Project project @@ -23,29 +23,69 @@ Project project _logger = logger; } + CountdownEvent countdown = new CountdownEvent(1); public async Task Build() { var dataSource = _pluginManager.Resolve(_project.DataSource.Name); await dataSource.InitData(); - this.countdown.Reset(); - foreach (var buildKV in _project.BuildTasks) + //foreach (var buildKV in _project.BuildTasks) + //{ + // _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); + // var output = buildKV.Value.Output; + // var buildContext = new BuildContext + // { + // PluginManager = _pluginManager, + // Project = _project, + // DataSource = dataSource, + // BuildKey = buildKV.Key, + // Build = buildKV.Value, + // Output = output?.Copy() + // }; + // await _pluginManager.Resolve(buildKV.Value.Type).Build(buildContext); + // _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); + //} + BuildContext[] contexts = _project.BuildTasks.Select(d => new BuildContext { - _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); - var output = buildKV.Value.Output; - var buildContext = new BuildContext + PluginManager = _pluginManager, + Project = _project, + DataSource = dataSource, + BuildKey = d.Key, + Build = d.Value, + Output = d.Value.Output?.Copy(), + }).ToArray(); + foreach (var context in contexts) + { + context.DependOn = contexts.Where(d => d.Build.DependOn.Contains(d.BuildKey)).ToArray(); + } + countdown.Reset(); + foreach (var context in contexts) + { + context.BuildTask = Task.Factory.StartNew(this.BuildTask, null, TaskCreationOptions.LongRunning); + } + + countdown.Signal(); + } + private async void BuildTask(object obj) + { + countdown.Wait(); + var context = (BuildContext)obj; + _logger.LogInformation($"-------- BuildTask:{context.BuildKey} Wait! ---------"); + //�ȴ��������� + if (context.DependOn != null) + { + foreach (var dcontext in context.DependOn) { - PluginManager = _pluginManager, - Project = _project, - DataSource = dataSource, - BuildKey = buildKV.Key, - Build = buildKV.Value, - Output = output?.Copy() - }; - await _pluginManager.Resolve(buildKV.Value.Type).Build(buildContext); - _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); + await dcontext.BuildTask; + } } + + _logger.LogInformation($"-------- BuildTask:{context.BuildKey} Start! ---------"); + //ִ���������� + await _pluginManager.Resolve(context.Build.Type).Build(context); + + _logger.LogInformation($"-------- BuildTask:{context.BuildKey} End! ---------"); } } } \ No newline at end of file diff --git a/src/SmartCode/BuildContext.cs b/src/SmartCode/BuildContext.cs index 3abddaa..2259d52 100644 --- a/src/SmartCode/BuildContext.cs +++ b/src/SmartCode/BuildContext.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; using SmartCode.Configuration; namespace SmartCode @@ -26,5 +27,9 @@ public void SetItem(string key, object item) { Items[key] = item; } + + public IEnumerable DependOn { get; set; } + public IEnumerable Next { get; set; } + public Task BuildTask { get; set; } } } diff --git a/src/SmartCode/Configuration/Build.cs b/src/SmartCode/Configuration/Build.cs index 72a75aa..f00f859 100644 --- a/src/SmartCode/Configuration/Build.cs +++ b/src/SmartCode/Configuration/Build.cs @@ -13,7 +13,7 @@ public class Build /// public String Type { get; set; } public String Module { get; set; } - public TemplateEngine TemplateEngine { get; set; } + public TemplateEngine TemplateEngine { get; set; } public Output Output { get; set; } public IEnumerable IncludeTables { get; set; } public IEnumerable IgnoreTables { get; set; } @@ -21,6 +21,10 @@ public class Build public bool? IgnoreView { get; set; } public NamingConverter NamingConverter { get; set; } /// + /// 依赖于 + /// + public IEnumerable DependOn { get; set; } + /// /// 自定义构建参数 /// public IDictionary Parameters { get; set; } From 96f10dce0dcfafd53f0b16609ce2d075b010c44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=BB=91=E5=85=94?= Date: Tue, 15 Mar 2022 01:40:43 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GeneratorProjectBuilder.cs | 25 +++++-------------- src/SmartCode/BuildContext.cs | 1 - 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/src/SmartCode.Generator/GeneratorProjectBuilder.cs b/src/SmartCode.Generator/GeneratorProjectBuilder.cs index a1bbff8..de5ba8a 100644 --- a/src/SmartCode.Generator/GeneratorProjectBuilder.cs +++ b/src/SmartCode.Generator/GeneratorProjectBuilder.cs @@ -30,22 +30,6 @@ public async Task Build() var dataSource = _pluginManager.Resolve(_project.DataSource.Name); await dataSource.InitData(); - //foreach (var buildKV in _project.BuildTasks) - //{ - // _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); - // var output = buildKV.Value.Output; - // var buildContext = new BuildContext - // { - // PluginManager = _pluginManager, - // Project = _project, - // DataSource = dataSource, - // BuildKey = buildKV.Key, - // Build = buildKV.Value, - // Output = output?.Copy() - // }; - // await _pluginManager.Resolve(buildKV.Value.Type).Build(buildContext); - // _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); - //} BuildContext[] contexts = _project.BuildTasks.Select(d => new BuildContext { PluginManager = _pluginManager, @@ -57,21 +41,24 @@ public async Task Build() }).ToArray(); foreach (var context in contexts) { - context.DependOn = contexts.Where(d => d.Build.DependOn.Contains(d.BuildKey)).ToArray(); + context.DependOn = contexts.Where(d => context.Build.DependOn != null && context.Build.DependOn.Contains(d.BuildKey)).ToArray(); } + countdown.Reset(); foreach (var context in contexts) { - context.BuildTask = Task.Factory.StartNew(this.BuildTask, null, TaskCreationOptions.LongRunning); + context.BuildTask = Task.Factory.StartNew(this.BuildTask, context, TaskCreationOptions.LongRunning); } countdown.Signal(); + + await Task.WhenAll(contexts.Select(d => d.BuildTask).ToArray()); } private async void BuildTask(object obj) { - countdown.Wait(); var context = (BuildContext)obj; _logger.LogInformation($"-------- BuildTask:{context.BuildKey} Wait! ---------"); + countdown.Wait(); //�ȴ��������� if (context.DependOn != null) { diff --git a/src/SmartCode/BuildContext.cs b/src/SmartCode/BuildContext.cs index 2259d52..7a45494 100644 --- a/src/SmartCode/BuildContext.cs +++ b/src/SmartCode/BuildContext.cs @@ -29,7 +29,6 @@ public void SetItem(string key, object item) } public IEnumerable DependOn { get; set; } - public IEnumerable Next { get; set; } public Task BuildTask { get; set; } } } From 43eb0ade8f88c9f895fa384b382c6e18e32deb46 Mon Sep 17 00:00:00 2001 From: yuanwj Date: Tue, 15 Mar 2022 11:52:06 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=EF=BC=8C=E7=A7=BB=E9=99=A4=E5=8D=95=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=B9=B6=E8=A1=8C=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SmartCode.App/PluginManager.cs | 22 ++++--- src/SmartCode.CLI/Program.cs | 6 +- .../BuildTasks/TableBuildTask.cs | 41 +++--------- .../GeneratorProjectBuilder.cs | 64 +++++++++++++------ src/SmartCode/BuildContext.cs | 7 +- 5 files changed, 77 insertions(+), 63 deletions(-) diff --git a/src/SmartCode.App/PluginManager.cs b/src/SmartCode.App/PluginManager.cs index c0128a6..498911f 100644 --- a/src/SmartCode.App/PluginManager.cs +++ b/src/SmartCode.App/PluginManager.cs @@ -56,19 +56,23 @@ private IEnumerable ResolvePlugins() where TPlugin : IPlugin var plugins = _serviceProvider.GetServices(); foreach (var plugin in plugins) { - if (!plugin.Initialized) + lock (this) { - var pluginType = plugin.GetType(); - var names = pluginType.AssemblyQualifiedName.Split(','); - var typeName = names[0].Trim(); - var assName = names[1].Trim(); - var pluginConfig = _smartCodeOptions - .Plugins - .FirstOrDefault(m => m.ImplAssemblyName == assName && m.ImplTypeName == typeName); - plugin.Initialize(pluginConfig.Parameters); + if (!plugin.Initialized) + { + var pluginType = plugin.GetType(); + var names = pluginType.AssemblyQualifiedName.Split(','); + var typeName = names[0].Trim(); + var assName = names[1].Trim(); + var pluginConfig = _smartCodeOptions + .Plugins + .FirstOrDefault(m => m.ImplAssemblyName == assName && m.ImplTypeName == typeName); + plugin.Initialize(pluginConfig.Parameters); + } } } return plugins; + } } } diff --git a/src/SmartCode.CLI/Program.cs b/src/SmartCode.CLI/Program.cs index fa00641..e54f256 100644 --- a/src/SmartCode.CLI/Program.cs +++ b/src/SmartCode.CLI/Program.cs @@ -12,9 +12,11 @@ namespace SmartCode.CLI { class Program { - static async Task Main(string[] args) + static async Task Main(string[] args) { - await CommandLineApplication.ExecuteAsync(args); + ThreadPool.SetMinThreads(1000, 1000); + ThreadPool.SetMaxThreads(1000, 1000); + await CommandLineApplication.ExecuteAsync(args); } } } diff --git a/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs b/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs index d34cbff..42dc537 100644 --- a/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs +++ b/src/SmartCode.Generator/BuildTasks/TableBuildTask.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace SmartCode.Generator.BuildTasks @@ -28,38 +29,16 @@ public override async Task Build(BuildContext context) var filterTables = FilterTable(context.GetDataSource().Tables, context.BuildKey, context.Build); context.SetCurrentAllTable(filterTables); - - var nameConverter = _pluginManager.Resolve(); - var templateEngine = _pluginManager.Resolve(context.Build.TemplateEngine.Name); - var output = _pluginManager.Resolve(context.Build.Output.Type); - - var tasks = filterTables.Select(table => + foreach (var table in filterTables) { - BuildContext newContext = new BuildContext(); - newContext.Project = context.Project; - newContext.DataSource = context.DataSource; - newContext.PluginManager = context.PluginManager; - newContext.BuildKey = context.BuildKey; - newContext.Build = context.Build; - newContext.Result = context.Result; - newContext.Output = context.Output; - newContext.SetCurrentTable(table); - - return Task.Factory.StartNew(BuildTable,(newContext, nameConverter, templateEngine, output), TaskCreationOptions.LongRunning); - }).ToArray(); - - await Task.WhenAll(tasks); - } - - private async void BuildTable(object obj) - { - var p=((BuildContext context, INamingConverter nameConverter, ITemplateEngine templateEngine, IOutput output)) obj; - var table = p.context.GetCurrentTable(); - _logger.LogInformation($"BuildTable:{table.Name} Start!"); - p.nameConverter.Convert(p.context); - p.context.Result = await p.templateEngine.Render(p.context); - await p.output.Output(p.context); - _logger.LogInformation($"BuildTable:{table.Name} End!"); + _logger.LogInformation($"BuildTable:{table.Name} Start!"); + context.SetCurrentTable(table); + _pluginManager.Resolve().Convert(context); + context.Result = await _pluginManager.Resolve(context.Build.TemplateEngine.Name) + .Render(context); + await _pluginManager.Resolve(context.Build.Output.Type).Output(context); + _logger.LogInformation($"BuildTable:{table.Name} End!"); + } } public override void Initialize(IDictionary parameters) diff --git a/src/SmartCode.Generator/GeneratorProjectBuilder.cs b/src/SmartCode.Generator/GeneratorProjectBuilder.cs index de5ba8a..7a99583 100644 --- a/src/SmartCode.Generator/GeneratorProjectBuilder.cs +++ b/src/SmartCode.Generator/GeneratorProjectBuilder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -25,12 +26,13 @@ Project project CountdownEvent countdown = new CountdownEvent(1); + public async Task Build() { var dataSource = _pluginManager.Resolve(_project.DataSource.Name); await dataSource.InitData(); - BuildContext[] contexts = _project.BuildTasks.Select(d => new BuildContext + IList allContexts = _project.BuildTasks.Select(d => new BuildContext { PluginManager = _pluginManager, Project = _project, @@ -39,40 +41,64 @@ public async Task Build() Build = d.Value, Output = d.Value.Output?.Copy(), }).ToArray(); - foreach (var context in contexts) + foreach (var context in allContexts) { - context.DependOn = contexts.Where(d => context.Build.DependOn != null && context.Build.DependOn.Contains(d.BuildKey)).ToArray(); + if (context.Build.DependOn != null && context.Build.DependOn.Count() > 0) + { + context.DependOn = allContexts.Where(d => context.Build.DependOn.Contains(d.BuildKey)).ToArray(); + } } - countdown.Reset(); - foreach (var context in contexts) + countdown.AddCount(allContexts.Count); + foreach (var context in allContexts) + { + context.CountDown.Reset(); + if (context.DependOn != null && context.DependOn.Count > 0) + { + context.CountDown.AddCount(context.DependOn.Count); + } + + ThreadPool.QueueUserWorkItem(this.BuildTask, (context, allContexts)); + } + + foreach (var context in allContexts) { - context.BuildTask = Task.Factory.StartNew(this.BuildTask, context, TaskCreationOptions.LongRunning); + context.CountDown.Signal(); } countdown.Signal(); - - await Task.WhenAll(contexts.Select(d => d.BuildTask).ToArray()); + countdown.Wait(); } + private async void BuildTask(object obj) { - var context = (BuildContext)obj; - _logger.LogInformation($"-------- BuildTask:{context.BuildKey} Wait! ---------"); - countdown.Wait(); + var p = ((BuildContext context, IList allContexts))obj; + + if (p.context.DependOn != null && p.context.DependOn.Count > 0) + { + _logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} Wait [{string.Join(",", p.context.DependOn?.Select(d => d.BuildKey)?.ToArray())}]---------"); + } //�ȴ��������� - if (context.DependOn != null) + p.context.CountDown.Wait(); + + _logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} Start! ---------"); + //ִ���������� + await _pluginManager.Resolve(p.context.Build.Type).Build(p.context); + + foreach (var c in p.allContexts) { - foreach (var dcontext in context.DependOn) + if(c.DependOn==null || c.DependOn.Count == 0) + { + continue; + } + if (c.DependOn.Contains(p.context)) { - await dcontext.BuildTask; + c.CountDown.Signal(); } } - _logger.LogInformation($"-------- BuildTask:{context.BuildKey} Start! ---------"); - //ִ���������� - await _pluginManager.Resolve(context.Build.Type).Build(context); - - _logger.LogInformation($"-------- BuildTask:{context.BuildKey} End! ---------"); + countdown.Signal(); + _logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} End! ---------"); } } } \ No newline at end of file diff --git a/src/SmartCode/BuildContext.cs b/src/SmartCode/BuildContext.cs index 7a45494..9746c0d 100644 --- a/src/SmartCode/BuildContext.cs +++ b/src/SmartCode/BuildContext.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading; using System.Threading.Tasks; using SmartCode.Configuration; @@ -8,6 +9,7 @@ namespace SmartCode { public class BuildContext { + public Project Project { get; set; } public IDataSource DataSource { get; set; } public IPluginManager PluginManager { get; set; } @@ -28,7 +30,8 @@ public void SetItem(string key, object item) Items[key] = item; } - public IEnumerable DependOn { get; set; } - public Task BuildTask { get; set; } + public IList DependOn { get; set; } + //public Task BuildTask { get; set; } + public CountdownEvent CountDown { get; } = new CountdownEvent(1); } } From f0cd4985d623d2c0d3efe9cd969649d7f2ab3184 Mon Sep 17 00:00:00 2001 From: yuanwj Date: Tue, 15 Mar 2022 15:56:47 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E4=B8=BA=E9=A1=B9=E7=9B=AE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20AllowParallel=20=E9=85=8D=E7=BD=AE=E3=80=82?= =?UTF-8?q?=E5=BD=93AllowParallel=20=E4=B8=BAfalse=20=E6=88=96=E8=80=85All?= =?UTF-8?q?owParallel=20=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=BF=BD=E7=95=A5DependOn=E9=85=8D=E7=BD=AE=E3=80=82=E6=89=80?= =?UTF-8?q?=E4=BB=A5=E6=9E=84=E5=BB=BA=E4=BB=BB=E5=8A=A1=E6=8C=89=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E4=B8=80=E6=AC=A1=E6=89=A7=E8=A1=8C=E3=80=82=E8=BF=99?= =?UTF-8?q?=E6=A0=B7=E5=8F=AF=E4=BB=A5=E6=8A=B1=E6=9E=95=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=9C=A8=E5=8D=87=E7=BA=A7=E6=96=B0=E7=89=88=E6=9C=ACsmarcode?= =?UTF-8?q?=20=E5=90=8E=EF=BC=8C=E4=B8=8D=E4=BF=AE=E6=94=B9=E6=A8=A1?= =?UTF-8?q?=E7=89=88=E4=B9=9F=E8=83=BD=E6=AD=A3=E5=B8=B8=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GeneratorProjectBuilder.cs | 45 +++++++++++++++++-- src/SmartCode/Configuration/Project.cs | 1 + 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/SmartCode.Generator/GeneratorProjectBuilder.cs b/src/SmartCode.Generator/GeneratorProjectBuilder.cs index 7a99583..d43ac21 100644 --- a/src/SmartCode.Generator/GeneratorProjectBuilder.cs +++ b/src/SmartCode.Generator/GeneratorProjectBuilder.cs @@ -32,6 +32,41 @@ public async Task Build() var dataSource = _pluginManager.Resolve(_project.DataSource.Name); await dataSource.InitData(); + if (_project.AllowParallel) + { + await this.ParallelBuild(dataSource); + } + else + { + await this.SerialBuild(dataSource); + } + } + + public async Task SerialBuild(IDataSource dataSource) + { + foreach (var buildKV in _project.BuildTasks) + { + _logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------"); + var context = new BuildContext + { + PluginManager = _pluginManager, + Project = _project, + DataSource = dataSource, + BuildKey = buildKV.Key, + Build = buildKV.Value, + Output = buildKV.Value.Output?.Copy(), + }; + + //ִ���������� + await _pluginManager.Resolve(context.Build.Type).Build(context); + + _logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------"); + } + } + + private Task ParallelBuild(IDataSource dataSource) + { + IList allContexts = _project.BuildTasks.Select(d => new BuildContext { PluginManager = _pluginManager, @@ -57,8 +92,8 @@ public async Task Build() { context.CountDown.AddCount(context.DependOn.Count); } - - ThreadPool.QueueUserWorkItem(this.BuildTask, (context, allContexts)); + + ThreadPool.QueueUserWorkItem((obj) => _ = this.BuildTask(obj), (context, allContexts)); } foreach (var context in allContexts) @@ -68,9 +103,11 @@ public async Task Build() countdown.Signal(); countdown.Wait(); + + return Task.CompletedTask; } - private async void BuildTask(object obj) + private async Task BuildTask(object obj) { var p = ((BuildContext context, IList allContexts))obj; @@ -87,7 +124,7 @@ private async void BuildTask(object obj) foreach (var c in p.allContexts) { - if(c.DependOn==null || c.DependOn.Count == 0) + if (c.DependOn == null || c.DependOn.Count == 0) { continue; } diff --git a/src/SmartCode/Configuration/Project.cs b/src/SmartCode/Configuration/Project.cs index ed9d7d2..92deb0b 100644 --- a/src/SmartCode/Configuration/Project.cs +++ b/src/SmartCode/Configuration/Project.cs @@ -11,6 +11,7 @@ public class Project public String ConfigPath { get; set; } public String Module { get; set; } public String Author { get; set; } + public bool AllowParallel { get; set; } = false; public ProjectMode? Mode { get; set; } public DataSource DataSource { get; set; } public TemplateEngine TemplateEngine { get; set; } = TemplateEngine.Default; From f6fd7bdcf41635fff33e45af05ffc3e7615c5c51 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Sat, 7 May 2022 12:45:30 +0800 Subject: [PATCH 12/14] Update version.props --- build/version.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/version.props b/build/version.props index 83651c8..e177903 100644 --- a/build/version.props +++ b/build/version.props @@ -1,8 +1,8 @@ 2 - 3 - 7 + 4 + 0-preview.0 $(VersionMajor).$(VersionMinor).$(VersionPatch) From e445669c96e9aa1c965af7774403e262cceb0fa6 Mon Sep 17 00:00:00 2001 From: nidl Date: Tue, 14 Jun 2022 20:58:35 +0800 Subject: [PATCH 13/14] =?UTF-8?q?=E5=8D=87=E7=BA=A7MySql.Data=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=88=B08.0.29?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SmartCode.Db/SmartCode.Db.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SmartCode.Db/SmartCode.Db.csproj b/src/SmartCode.Db/SmartCode.Db.csproj index 6c40434..22f33f7 100644 --- a/src/SmartCode.Db/SmartCode.Db.csproj +++ b/src/SmartCode.Db/SmartCode.Db.csproj @@ -10,7 +10,7 @@ - + From 7af415d35da4eba6e2e6c1d9e5a917e76bc06b16 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Wed, 15 Jun 2022 11:07:13 +0800 Subject: [PATCH 14/14] Update version.props --- build/version.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/version.props b/build/version.props index e177903..8013612 100644 --- a/build/version.props +++ b/build/version.props @@ -2,7 +2,7 @@ 2 4 - 0-preview.0 + 0-preview.1 $(VersionMajor).$(VersionMinor).$(VersionPatch)