Builder now support specifying max job count, less than 0 will use processor count.
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Progrart.Cmd
|
|||||||
{
|
{
|
||||||
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
|
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
|
||||||
string? project_file = null;
|
string? project_file = null;
|
||||||
bool isParallel = false;
|
int jobCount = 1;
|
||||||
string? configuration = null;
|
string? configuration = null;
|
||||||
for (int i = 0; i < args.Length; i++)
|
for (int i = 0; i < args.Length; i++)
|
||||||
{
|
{
|
||||||
@@ -18,7 +18,19 @@ namespace Progrart.Cmd
|
|||||||
switch (item)
|
switch (item)
|
||||||
{
|
{
|
||||||
case "-j":
|
case "-j":
|
||||||
isParallel=true;
|
i++;
|
||||||
|
if (i >= args.Length)
|
||||||
|
{
|
||||||
|
jobCount = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!int.TryParse(args[i], out jobCount))
|
||||||
|
{
|
||||||
|
jobCount = -1;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "-c":
|
case "-c":
|
||||||
case "--config":
|
case "--config":
|
||||||
@@ -58,16 +70,7 @@ namespace Progrart.Cmd
|
|||||||
{
|
{
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
};
|
};
|
||||||
Console.WriteLine("Start build...");
|
await builder.Build(configuration, jobCount);
|
||||||
//Task.Run(async () =>
|
|
||||||
//{
|
|
||||||
//});
|
|
||||||
//while (true)
|
|
||||||
//{
|
|
||||||
// Thread.Sleep(1000);
|
|
||||||
//}
|
|
||||||
|
|
||||||
await builder.Build(configuration, isParallel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,41 +40,38 @@ namespace Progrart.Core.ProjectSystem
|
|||||||
img.DrawingCore.ToData().SaveTo(img_stream);
|
img.DrawingCore.ToData().SaveTo(img_stream);
|
||||||
img_stream.Flush();
|
img_stream.Flush();
|
||||||
}
|
}
|
||||||
public async Task Build(string targetConfig, bool isParallel = false)
|
public async Task Build(string targetConfig, int maxJobCount = 1)
|
||||||
{
|
{
|
||||||
|
int finalJobCount= Math.Max(maxJobCount < 0 ? Environment.ProcessorCount : maxJobCount, 1);
|
||||||
foreach (var config in project.Configurations)
|
foreach (var config in project.Configurations)
|
||||||
{
|
{
|
||||||
Console.WriteLine(config.Name);
|
|
||||||
if (config.Name == targetConfig)
|
if (config.Name == targetConfig)
|
||||||
{
|
{
|
||||||
int index = 0;
|
int index = 0;
|
||||||
List<Task> tasks = new List<Task>();
|
//List<Task> tasks = new List<Task>();
|
||||||
foreach (var item in config.Items)
|
if (finalJobCount == 1)
|
||||||
{
|
{
|
||||||
if (!isParallel)
|
foreach (var item in config.Items)
|
||||||
{
|
{
|
||||||
|
|
||||||
await Execute(config, item);
|
await Execute(config, item);
|
||||||
index++;
|
index++;
|
||||||
OnProgressUpdate?.Invoke(config.Items.Count, index);
|
OnProgressUpdate?.Invoke(config.Items.Count, index);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
var t = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
index++;
|
|
||||||
int i = index;
|
|
||||||
await Execute(config, item);
|
|
||||||
OnProgressUpdate?.Invoke(config.Items.Count, i);
|
|
||||||
});
|
|
||||||
tasks.Add(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (isParallel)
|
else
|
||||||
{
|
{
|
||||||
await Task.WhenAll(tasks);
|
var options = new ParallelOptions
|
||||||
|
{
|
||||||
|
MaxDegreeOfParallelism = finalJobCount
|
||||||
|
};
|
||||||
|
|
||||||
|
await Parallel.ForEachAsync(config.Items, options, async (item, token) =>
|
||||||
|
{
|
||||||
|
await Execute(config, item);
|
||||||
|
|
||||||
|
int currentCount = Interlocked.Increment(ref index);
|
||||||
|
OnProgressUpdate?.Invoke(config.Items.Count, currentCount);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
OnCompleted?.Invoke();
|
OnCompleted?.Invoke();
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user