Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/SmartCode.App/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,23 @@ private IEnumerable<TPlugin> ResolvePlugins<TPlugin>() where TPlugin : IPlugin
var plugins = _serviceProvider.GetServices<TPlugin>();
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;

}
}
}
6 changes: 4 additions & 2 deletions src/SmartCode.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace SmartCode.CLI
{
class Program
{
static async Task Main(string[] args)
static async Task Main(string[] args)
{
await CommandLineApplication.ExecuteAsync<SmartCodeCommand>(args);
ThreadPool.SetMinThreads(1000, 1000);
ThreadPool.SetMaxThreads(1000, 1000);
await CommandLineApplication.ExecuteAsync<SmartCodeCommand>(args);
}
}
}
1 change: 1 addition & 0 deletions src/SmartCode.Generator/BuildTasks/TableBuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只添加了 System.Threading 没有使用?

using System.Threading.Tasks;

namespace SmartCode.Generator.BuildTasks
Expand Down
104 changes: 99 additions & 5 deletions src/SmartCode.Generator/GeneratorProjectBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SmartCode.Configuration;
Expand All @@ -20,28 +24,118 @@ Project project
_logger = logger;
}

CountdownEvent countdown = new CountdownEvent(1);


public async Task Build()
{
BuildContext buildContext = null;
var dataSource = _pluginManager.Resolve<IDataSource>(_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 output = buildKV.Value.Output;
buildContext = new BuildContext
var context = new BuildContext
{
PluginManager = _pluginManager,
Project = _project,
DataSource = dataSource,
BuildKey = buildKV.Key,
Build = buildKV.Value,
Output = output?.Copy()
Output = buildKV.Value.Output?.Copy(),
};
await _pluginManager.Resolve<IBuildTask>(buildKV.Value.Type).Build(buildContext);

//ִ����������
await _pluginManager.Resolve<IBuildTask>(context.Build.Type).Build(context);

_logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------");
}
}

private Task ParallelBuild(IDataSource dataSource)
{

IList<BuildContext> allContexts = _project.BuildTasks.Select(d => new BuildContext
{
PluginManager = _pluginManager,
Project = _project,
DataSource = dataSource,
BuildKey = d.Key,
Build = d.Value,
Output = d.Value.Output?.Copy(),
}).ToArray();
foreach (var context in allContexts)
{
if (context.Build.DependOn != null && context.Build.DependOn.Count() > 0)
{
context.DependOn = allContexts.Where(d => context.Build.DependOn.Contains(d.BuildKey)).ToArray();
}
}
countdown.Reset();
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((obj) => _ = this.BuildTask(obj), (context, allContexts));
}

foreach (var context in allContexts)
{
context.CountDown.Signal();
}

countdown.Signal();
countdown.Wait();

return Task.CompletedTask;
}

private async Task BuildTask(object obj)
{
var p = ((BuildContext context, IList<BuildContext> 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())}]---------");
}
//�ȴ���������
p.context.CountDown.Wait();

_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} Start! ---------");
//ִ����������
await _pluginManager.Resolve<IBuildTask>(p.context.Build.Type).Build(p.context);

foreach (var c in p.allContexts)
{
if (c.DependOn == null || c.DependOn.Count == 0)
{
continue;
}
if (c.DependOn.Contains(p.context))
{
c.CountDown.Signal();
}
}

countdown.Signal();
_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} End! ---------");
}
}
}
7 changes: 7 additions & 0 deletions src/SmartCode/BuildContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SmartCode.Configuration;

namespace SmartCode
{
public class BuildContext
{

public Project Project { get; set; }
public IDataSource DataSource { get; set; }
public IPluginManager PluginManager { get; set; }
Expand All @@ -26,5 +29,9 @@ public void SetItem(string key, object item)
{
Items[key] = item;
}

public IList<BuildContext> DependOn { get; set; }
//public Task BuildTask { get; set; }
public CountdownEvent CountDown { get; } = new CountdownEvent(1);
}
}
6 changes: 5 additions & 1 deletion src/SmartCode/Configuration/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public class Build
/// </summary>
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<String> IncludeTables { get; set; }
public IEnumerable<String> IgnoreTables { get; set; }
public bool? IgnoreNoPKTable { get; set; }
public bool? IgnoreView { get; set; }
public NamingConverter NamingConverter { get; set; }
/// <summary>
/// 依赖于
/// </summary>
public IEnumerable<String> DependOn { get; set; }
/// <summary>
/// 自定义构建参数
/// </summary>
public IDictionary<String, object> Parameters { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/SmartCode/Configuration/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down