-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
CLIExtension.cs
121 lines (114 loc) · 4.69 KB
/
CLIExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace NetCore2Blockly
{
/// <summary>
/// extension to register to the startup
/// </summary>
public static class CLIExtension
{
static CLIExtension()
{
var assName = Assembly.GetExecutingAssembly().GetName();
Console.WriteLine($"{assName.Name} version:{assName.Version.ToString()}");
}
/// <summary>
/// Adds the blockly to startup
/// </summary>
/// <param name="serviceCollection">The service collection.</param>
/// <returns></returns>
public static IServiceCollection AddBlockly(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<GenerateBlocklyFilesHostedService>();
serviceCollection.AddHostedService(p => p.GetService<GenerateBlocklyFilesHostedService>());
return serviceCollection;
}
/// <summary>
/// use blockly
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder UseBlockly(this IApplicationBuilder app)
{
MapJS(app, "/blocklyDefinitions", b => b.BlocklyTypesDefinition);
MapJS(app, "/BlocklyToolBoxValueDefinitions", b => b.BlocklyToolBoxValueDefinition);
MapJS(app, "/blocklyAPIFunctions", b => b.BlocklyAPIFunctions);
MapJS(app, "/BlocklyToolBoxFunctionDefinitions", b => b.BlocklyToolBoxFunctionDefinition);
return app;
}
private static void MapJS(IApplicationBuilder app, string url,Func<GenerateBlocklyFilesHostedService,string> content)
{
app.Map(url, app =>
{
var blocklyFilesHostedService = app.ApplicationServices
.GetService<GenerateBlocklyFilesHostedService>();
app.Run(async context =>
{
await GetBlocklyFilesHostedServices(context, content(blocklyFilesHostedService));
});
});
}
private static async Task GetBlocklyFilesHostedServices(HttpContext context, string blocklyDefinitions)
{
if (blocklyDefinitions == null)
{
throw new Exception("blocklyDefinitions is null");
}
var mem = new Memory<byte>(Encoding.UTF8.GetBytes(blocklyDefinitions));
await context.Response.BodyWriter.WriteAsync(mem);
}
private static void mapFile(string dirName, IFileProvider provider, IApplicationBuilder appBuilder)
{
var folder = provider.GetDirectoryContents(dirName);
foreach (var item in folder)
{
if (item.IsDirectory)
{
mapFile(dirName +"/" + item.Name,provider, appBuilder);
continue;
}
string map = (dirName + "/" + item.Name).Substring("blocklyFiles".Length);
appBuilder.Map(map, app =>
{
var f = item;
app.Run(async cnt =>
{
//TODO: find from extension
//cnt.Response.ContentType = "text/html";
using var stream = new MemoryStream();
using var cs = f.CreateReadStream();
byte[] buffer = new byte[2048]; // read in chunks of 2KB
int bytesRead;
while ((bytesRead = cs.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, bytesRead);
}
byte[] result = stream.ToArray();
// TODO: do something with the result
var m = new Memory<byte>(result);
await cnt.Response.BodyWriter.WriteAsync(m);
});
});
}
}
/// <summary>
/// Uses the cli.
/// </summary>
/// <param name="appBuilder">The application builder.</param>
public static void UseBlocklyCli(this IApplicationBuilder appBuilder)
{
var manifestEmbeddedProvider =
new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly());
mapFile("blocklyFiles", manifestEmbeddedProvider, appBuilder);
}
}
}