generated from unoplatform/template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
GenerateWasmSplashAssets.cs
124 lines (94 loc) · 2.95 KB
/
GenerateWasmSplashAssets.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
122
123
124
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Uno.Resizetizer;
public class GenerateWasmSplashAssets_v0 : Task
{
[Required]
public string IntermediateOutputPath { get; set; }
[Required]
public string OutputFile { get; set; }
[Required]
public ITaskItem[] UnoSplashScreen { get; set; }
[Required]
public ITaskItem[] EmbeddedResources { get; set; }
[Output]
public ITaskItem UserAppManifest { get; set; }
public override bool Execute()
{
#if DEBUG_RESIZETIZER
if (System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.Debugger.Break();
}
System.Diagnostics.Debugger.Launch();
#endif
if (UnoSplashScreen is null)
{
Log.LogWarning("Didn't find UnoSplashScreen.");
return false;
}
var splash = UnoSplashScreen[0];
var info = ResizeImageInfo.Parse(splash);
UserAppManifest = EmbeddedResources.FirstOrDefault(x =>
{
var name = x.ToString();
return name.EndsWith("AppManifest.js", StringComparison.OrdinalIgnoreCase)
|| name.EndsWith("AppManifest", StringComparison.OrdinalIgnoreCase);
});
if (UserAppManifest is null)
{
Log.LogWarning("Didn't find AppManifest.js file.");
return false;
}
var dir = Path.GetDirectoryName(OutputFile);
Directory.CreateDirectory(dir);
using var writer = File.CreateText(OutputFile);
ProcessAppManifestFile(UserAppManifest.ToString(), info, writer);
return true;
}
void ProcessAppManifestFile(in string appManifestPath, ResizeImageInfo info, StreamWriter writer)
{
using var reader = new StreamReader(File.OpenRead(appManifestPath));
var fileToProcess = reader.ReadToEnd();
var dic = FindWhatINeed(fileToProcess);
dic["splashScreenImage"] = $"\"{info.OutputName}.scale-400.png\"";
dic["splashScreenColor"] = ProcessSplashScreenColor(info);
WriteToFile(dic, writer);
}
static void WriteToFile(Dictionary<string, string> dic, StreamWriter writer)
{
var sb = new StringBuilder(@"var UnoAppManifest = {").AppendLine();
foreach (var kvp in dic)
{
var key = kvp.Key;
var value = kvp.Value;
sb.AppendLine($" {key}: {value},");
}
sb.Append('}');
var final = sb.ToString();
writer.Write(final);
}
static Dictionary<string, string> FindWhatINeed(string fileToProcess)
{
var indexOfSymbol = fileToProcess.IndexOf('{');
var indexOfSymbolClose = fileToProcess.IndexOf('}');
var input = fileToProcess.Substring(++indexOfSymbol, indexOfSymbolClose - indexOfSymbol);
var dictionary = (from pair in input.Split(',')
let component = pair.Split(':')
where component.Length == 2
select new { Key = component[0].Trim(), Value = component[1].Trim() })
.ToDictionary(x => x.Key, x => x.Value);
return dictionary;
}
static string ProcessSplashScreenColor(ResizeImageInfo info)
{
var color = Utils.SkiaColorWithoutAlpha(info.Color);
return $"\"{color}\"";
}
}