forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWasmAppBuilder.cs
More file actions
123 lines (106 loc) · 4.37 KB
/
WasmAppBuilder.cs
File metadata and controls
123 lines (106 loc) · 4.37 KB
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
122
123
// -*- indent-tabs-mode: nil -*-
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class WasmAppBuilder : Task
{
// FIXME: Document
[Required]
public string? AppDir { get; set; }
[Required]
public string? RuntimePackDir { get; set; }
[Required]
public string? MainAssembly { get; set; }
[Required]
public string? MainJS { get; set; }
[Required]
public ITaskItem[]? AssemblySearchPaths { get; set; }
public ITaskItem[]? ExtraAssemblies { get; set; }
Dictionary<string, Assembly>? Assemblies;
Resolver? Resolver;
public override bool Execute () {
if (!File.Exists (MainAssembly))
throw new ArgumentException ($"File MainAssembly='{MainAssembly}' doesn't exist.");
if (!File.Exists (MainJS))
throw new ArgumentException ($"File MainJS='{MainJS}' doesn't exist.");
var paths = new List<string> ();
Assemblies = new Dictionary<string, Assembly> ();
// Collect and load assemblies used by the app
foreach (var v in AssemblySearchPaths!) {
var dir = v.ItemSpec;
if (!Directory.Exists (dir))
throw new ArgumentException ($"Directory '{dir}' doesn't exist or not a directory.");
paths.Add (dir);
}
Resolver = new Resolver (paths);
var mlc = new MetadataLoadContext (Resolver, "System.Private.CoreLib");
var mainAssembly = mlc.LoadFromAssemblyPath (MainAssembly);
Add (mlc, mainAssembly);
if (ExtraAssemblies != null) {
foreach (var item in ExtraAssemblies) {
var refAssembly = mlc.LoadFromAssemblyPath (item.ItemSpec);
Add (mlc, refAssembly);
}
}
// Create app
Directory.CreateDirectory (AppDir!);
Directory.CreateDirectory (Path.Join (AppDir, "managed"));
foreach (var assembly in Assemblies!.Values)
File.Copy (assembly.Location, Path.Join (AppDir, "managed", Path.GetFileName (assembly.Location)), true);
foreach (var f in new string [] { "dotnet.wasm", "dotnet.js" })
File.Copy (Path.Join (RuntimePackDir, "native", "wasm", "runtimes", "release", f), Path.Join (AppDir, f), true);
File.Copy (MainJS!, Path.Join (AppDir, "runtime.js"), true);
using (var sw = File.CreateText (Path.Join (AppDir, "mono-config.js"))) {
sw.WriteLine ("config = {");
sw.WriteLine ("\tvfs_prefix: \"managed\",");
sw.WriteLine ("\tdeploy_prefix: \"managed\",");
sw.WriteLine ("\tenable_debugging: 0,");
sw.WriteLine ("\tfile_list: [");
foreach (var assembly in Assemblies.Values) {
sw.Write ("\"" + Path.GetFileName (assembly.Location) + "\"");
sw.Write (", ");
}
sw.WriteLine ("],");
sw.WriteLine ("}");
}
using (var sw = File.CreateText (Path.Join (AppDir, "run-v8.sh"))) {
sw.WriteLine ("v8 --expose_wasm runtime.js -- --enable-gc --run " + Path.GetFileName (MainAssembly) + " $*");
}
return true;
}
void Add (MetadataLoadContext mlc, Assembly assembly) {
Assemblies! [assembly.GetName ().Name!] = assembly;
foreach (var aname in assembly.GetReferencedAssemblies ()) {
var refAssembly = mlc.LoadFromAssemblyName (aname);
Add (mlc, refAssembly);
}
}
}
class Resolver : MetadataAssemblyResolver
{
List<String> SearchPaths;
public Resolver (List<string> searchPaths) {
this.SearchPaths = searchPaths;
}
public override Assembly? Resolve (MetadataLoadContext context, AssemblyName assemblyName) {
var name = assemblyName.Name;
foreach (var dir in SearchPaths) {
var path = Path.Combine (dir, name + ".dll");
if (File.Exists (path)) {
Console.WriteLine (path);
return context.LoadFromAssemblyPath (path);
}
}
return null;
}
}