forked from hardkoded/puppeteer-sharp
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (135 loc) · 5.07 KB
/
Program.cs
File metadata and controls
155 lines (135 loc) · 5.07 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace PuppeteerSharp.DevicesFetcher
{
class Program
{
const string DEVICES_URL =
"https://raw.githubusercontent.com/puppeteer/puppeteer/master/src/common/DeviceDescriptors.ts";
static readonly string deviceDescriptorsOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptors.cs";
static readonly string deviceDescriptorNameOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptorName.cs";
static async Task Main(string[] args)
{
var url = DEVICES_URL;
if (args.Length > 0)
{
url = args[0];
}
Console.WriteLine($"GET {url}");
var text = await HttpGET(url);
const string DeviceArray = "Device[] = [";
var startIndex = text.IndexOf(DeviceArray) + DeviceArray.Length;
var endIndex = text.IndexOf("];", startIndex);
var length = endIndex - startIndex;
text = "[" + text.Substring(startIndex, length) + "]";
Device[] devices;
try
{
devices = JsonConvert.DeserializeObject<Device[]>(text);
}
catch (Exception ex)
{
Console.WriteLine($"FAILED: error parsing response - {ex.Message}");
return;
}
WriteDeviceDescriptors(devices);
WriteDeviceDescriptorName(devices);
}
static void WriteDeviceDescriptors(IEnumerable<Device> devices)
{
var builder = new StringBuilder();
var begin = @"using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace CefSharp.Dom.Mobile
{
/// <summary>
/// Device descriptors.
/// </summary>
public static class DeviceDescriptors
{
private static readonly Dictionary<DeviceDescriptorName, DeviceDescriptor> Devices = new Dictionary<DeviceDescriptorName, DeviceDescriptor>
{
";
var end = @"
};
private static readonly Lazy<IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>> _readOnlyDevices =
new Lazy<IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>>(() => new ReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>(Devices));
internal static IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor> ToReadOnly() => _readOnlyDevices.Value;
}
}";
builder.Append(begin);
builder.AppendJoin(",\n", devices.Select(GenerateCsharpFromDevice));
builder.Append(end);
File.WriteAllText(deviceDescriptorsOutput, builder.ToString());
}
static void WriteDeviceDescriptorName(IEnumerable<Device> devices)
{
var builder = new StringBuilder();
var begin = @"namespace CefSharp.Dom.Mobile
{
/// <summary>
/// Device descriptor name.
/// </summary>
public enum DeviceDescriptorName
{";
var end = @"
}
}";
builder.Append(begin);
builder.AppendJoin(",", devices.Select(device =>
{
return $@"
/// <summary>
/// {device.Name}
/// </summary>
{DeviceNameToEnumValue(device)}";
}));
builder.Append(end);
File.WriteAllText(deviceDescriptorNameOutput, builder.ToString());
}
static string GenerateCsharpFromDevice(Device device)
{
return $@" [DeviceDescriptorName.{DeviceNameToEnumValue(device)}] = new DeviceDescriptor
{{
Name = ""{device.Name}"",
UserAgent = ""{device.UserAgent}"",
ViewPort = new ViewPortOptions
{{
Width = {device.Viewport.Width},
Height = {device.Viewport.Height},
DeviceScaleFactor = {device.Viewport.DeviceScaleFactor},
IsMobile = {device.Viewport.IsMobile.ToString().ToLower()},
HasTouch = {device.Viewport.HasTouch.ToString().ToLower()},
IsLandscape = {device.Viewport.IsLandscape.ToString().ToLower()}
}}
}}";
}
static string DeviceNameToEnumValue(Device device)
{
var dotNetName = device.Name.Replace("+", "Plus");
var output = new StringBuilder();
output.Append(char.ToUpper(dotNetName[0]));
for (var i = 1; i < dotNetName.Length; i++)
{
if (char.IsWhiteSpace(dotNetName[i]))
{
output.Append(char.ToUpper(dotNetName[i + 1]));
i++;
}
else
{
output.Append(dotNetName[i]);
}
}
return output.ToString();
}
static Task<string> HttpGET(string url) => new HttpClient().GetStringAsync(url);
}
}