diff options
author | Ankit Jain <radical@gmail.com> | 2021-01-19 21:32:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-19 21:32:16 +0300 |
commit | d26d673d0a1ff1e1c3f904c597c2ee6e9f974950 (patch) | |
tree | d96031e70bef6e959522b1c1b28751451f045123 /src/tasks/WasmAppBuilder/WasmAppBuilder.cs | |
parent | c6509a35bbdfc040f8bbf56b80b1c9f2b2cd4614 (diff) |
[wasm] Some cleanup of targets, especially around profiler use (#46958)
* [wasm] samples: makefile cleanup
* [wasm] Mark FileWrites as Output
* [wasm] Move driver-gen.c generation for and profile, !aot case to separate target
* [wasm] Add $(WasmProfilers) for aot compiler
* [wasm] update public properties/items list in WasmApp.targets
* wasmapp.targets - some more cleanup - differentiate internal properties
* [wasm] fix MonoAOTCompiler build error
* [wasm] WasmApp.targets - Add `WasmExtraConfig`, to add to `mono-config.js`
`WasmAppBuilder.EnableProfiler` was added to add a special entry to
`mono-config.js` (`config.enable_profiler=true`), for the
`browser-profile` sample. Instead of doing that, add a general way to
add extra json elements to the config file.
This can be done through `WasmAppBuilder.ExtraConfig` set via
`@(WasmExtraConfig)`. Examples:
```xml
<WasmExtraConfig Include="enable_profiler" Value="true" />
<!-- Value attribute can have a number, bool, quoted string, or json string -->
<WasmExtraConfig Include="json" Value="{ "abc": 4 }" />
<WasmExtraConfig Include="string_val" Value=""abc"" />
<WasmExtraConfig Include="string_with_json" Value=""{ "abc": 4 }"" />
```
This would add the following to the config file:
```json
"enable_profiler": true,
"json": {
"abc": 4
},
"string_val": "abc",
"string_with_json": "{ \"abc\": 4 }"
```
* [wasm] Remove BuildAOTProfiled, and instead use AOTProfilePath only
* [wasm] Fix typo
* [wasm] MonoAOTCompiler: fix NRE
* [wasm] WasmAppBuilder.ExtraConfig: add comments
* [wasm] WasmAppBuilder - fix json escaping
Diffstat (limited to 'src/tasks/WasmAppBuilder/WasmAppBuilder.cs')
-rw-r--r-- | src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 75 |
1 files changed, 69 insertions, 6 deletions
diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index c870de4c864..d43a09d07e6 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -9,6 +9,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Text; +using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Serialization; using System.Reflection; @@ -33,8 +34,6 @@ public class WasmAppBuilder : Task [Required] public string[]? Assemblies { get; set; } - public bool EnableProfiler { get; set; } - private List<string> _fileWrites = new(); [Output] @@ -51,6 +50,20 @@ public class WasmAppBuilder : Task public bool InvariantGlobalization { get; set; } public ITaskItem[]? ExtraFilesToDeploy { get; set; } + // <summary> + // Extra json elements to add to mono-config.js + // + // Metadata: + // - Value: can be a number, bool, quoted string, or json string + // + // Examples: + // <WasmExtraConfig Include="enable_profiler" Value="true" /> + // <WasmExtraConfig Include="json" Value="{ "abc": 4 }" /> + // <WasmExtraConfig Include="string_val" Value=""abc"" /> + // <WasmExtraConfig Include="string_with_json" Value=""{ "abc": 4 }"" /> + // </summary> + public ITaskItem[]? ExtraConfig { get; set; } + private class WasmAppConfig { [JsonPropertyName("assembly_root")] @@ -61,8 +74,8 @@ public class WasmAppBuilder : Task public List<object> Assets { get; } = new List<object>(); [JsonPropertyName("remote_sources")] public List<string> RemoteSources { get; set; } = new List<string>(); - [JsonPropertyName("enable_profiler")] - public bool EnableProfiler { get; set; } = false; + [JsonExtensionData] + public Dictionary<string, object?> Extra { get; set; } = new(); } private class AssetEntry @@ -231,9 +244,14 @@ public class WasmAppBuilder : Task if (source != null && source.ItemSpec != null) config.RemoteSources.Add(source.ItemSpec); } - if (EnableProfiler) + + foreach (ITaskItem extra in ExtraConfig ?? Enumerable.Empty<ITaskItem>()) { - config.EnableProfiler = true; + string name = extra.ItemSpec; + if (!TryParseExtraConfigValue(extra, out object? valueObject)) + return false; + + config.Extra[name] = valueObject; } string monoConfigPath = Path.Join(AppDir, "mono-config.js"); @@ -263,6 +281,51 @@ public class WasmAppBuilder : Task return true; } + private bool TryParseExtraConfigValue(ITaskItem extraItem, out object? valueObject) + { + valueObject = null; + string? rawValue = extraItem.GetMetadata("Value"); + if (string.IsNullOrEmpty(rawValue)) + return true; + + if (TryConvert(rawValue, typeof(double), out valueObject) || TryConvert(rawValue, typeof(bool), out valueObject)) + return true; + + // Try parsing as a quoted string + if (rawValue!.Length > 1 && rawValue![0] == '"' && rawValue![^1] == '"') + { + valueObject = rawValue![1..^1]; + return true; + } + + // try parsing as json + try + { + JsonDocument jdoc = JsonDocument.Parse(rawValue); + valueObject = jdoc.RootElement; + return true; + } + catch (JsonException je) + { + Log.LogError($"ExtraConfig: {extraItem.ItemSpec} with Value={rawValue} cannot be parsed as a number, boolean, string, or json object/array: {je.Message}"); + return false; + } + } + + private static bool TryConvert(string str, Type type, out object? value) + { + value = null; + try + { + value = Convert.ChangeType(str, type); + return true; + } + catch (Exception ex) when (ex is FormatException || ex is InvalidCastException || ex is OverflowException) + { + return false; + } + } + private bool FileCopyChecked(string src, string dst, string label) { if (!File.Exists(src)) |