|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Newtonsoft.Json; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | + |
| 11 | +namespace PuppeteerSharp.DevicesFetcher |
| 12 | +{ |
| 13 | + class Program |
| 14 | + { |
| 15 | + const string DEVICES_URL = "https://raw.githubusercontent.com/ChromeDevTools/devtools-frontend/master/front_end/emulated_devices/module.json"; |
| 16 | + |
| 17 | + static string deviceDescriptorsOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptors.cs"; |
| 18 | + static string deviceDescriptorNameOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptorName.cs"; |
| 19 | + |
| 20 | + static async Task Main(string[] args) |
| 21 | + { |
| 22 | + var url = DEVICES_URL; |
| 23 | + if (args.Length > 0) |
| 24 | + { |
| 25 | + url = args[0]; |
| 26 | + } |
| 27 | + |
| 28 | + string chromeVersion = null; |
| 29 | + var fetcher = new BrowserFetcher(); |
| 30 | + await fetcher.DownloadAsync(BrowserFetcher.DefaultRevision); |
| 31 | + using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions())) |
| 32 | + { |
| 33 | + chromeVersion = (await browser.GetVersionAsync()).Split('/').Last(); |
| 34 | + } |
| 35 | + |
| 36 | + Console.WriteLine($"GET {url}"); |
| 37 | + var text = await HttpGET(url); |
| 38 | + RootObject json = null; |
| 39 | + try |
| 40 | + { |
| 41 | + json = JsonConvert.DeserializeObject<RootObject>(text); |
| 42 | + } |
| 43 | + catch (Exception ex) |
| 44 | + { |
| 45 | + Console.WriteLine($"FAILED: error parsing response - {ex.Message}"); |
| 46 | + } |
| 47 | + var devicePayloads = json.Extensions |
| 48 | + .Where(extension => extension.Type == "emulated-device") |
| 49 | + .Select(extension => extension.Device) |
| 50 | + .ToArray(); |
| 51 | + var devices = new List<OutputDevice>(); |
| 52 | + foreach (var payload in devicePayloads) |
| 53 | + { |
| 54 | + string[] names; |
| 55 | + if (payload.Title == "iPhone 6/7/8") |
| 56 | + { |
| 57 | + names = new[] { "iPhone 6", "iPhone 7", "iPhone 8" }; |
| 58 | + } |
| 59 | + else if (payload.Title == "iPhone 6/7/8 Plus") |
| 60 | + { |
| 61 | + names = new[] { "iPhone 6 Plus", "iPhone 7 Plus", "iPhone 8 Plus" }; |
| 62 | + } |
| 63 | + else if (payload.Title == "iPhone 5/SE") |
| 64 | + { |
| 65 | + names = new[] { "iPhone 5", "iPhone SE" }; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + names = new[] { payload.Title }; |
| 70 | + } |
| 71 | + foreach (var name in names) |
| 72 | + { |
| 73 | + var device = CreateDevice(chromeVersion, name, payload, false); |
| 74 | + var landscape = CreateDevice(chromeVersion, name, payload, true); |
| 75 | + devices.Add(device); |
| 76 | + if (landscape.Viewport.Width != device.Viewport.Width || landscape.Viewport.Height != device.Viewport.Height) |
| 77 | + { |
| 78 | + devices.Add(landscape); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + devices.RemoveAll(device => !device.Viewport.IsMobile); |
| 83 | + devices.Sort((a, b) => a.Name.CompareTo(b.Name)); |
| 84 | + |
| 85 | + WriteDeviceDescriptors(devices); |
| 86 | + WriteDeviceDescriptorName(devices); |
| 87 | + } |
| 88 | + |
| 89 | + static void WriteDeviceDescriptors(IEnumerable<OutputDevice> devices) |
| 90 | + { |
| 91 | + var builder = new StringBuilder(); |
| 92 | + var begin = @"using System.Collections.Generic; |
| 93 | +
|
| 94 | +namespace PuppeteerSharp.Mobile |
| 95 | +{ |
| 96 | + /// <summary> |
| 97 | + /// Device descriptors. |
| 98 | + /// </summary> |
| 99 | + public class DeviceDescriptors |
| 100 | + { |
| 101 | + private static readonly Dictionary<DeviceDescriptorName, DeviceDescriptor> Devices = new Dictionary<DeviceDescriptorName, DeviceDescriptor> |
| 102 | + { |
| 103 | +"; |
| 104 | + var end = @" |
| 105 | + }; |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Get the specified device description. |
| 109 | + /// </summary> |
| 110 | + /// <returns>The device descriptor.</returns> |
| 111 | + /// <param name=""name"">Device Name.</param> |
| 112 | + public static DeviceDescriptor Get(DeviceDescriptorName name) => Devices[name]; |
| 113 | + } |
| 114 | +}"; |
| 115 | + |
| 116 | + builder.Append(begin); |
| 117 | + builder.AppendJoin(",\n", devices.Select(GenerateCsharpFromDevice)); |
| 118 | + builder.Append(end); |
| 119 | + |
| 120 | + File.WriteAllText(deviceDescriptorsOutput, builder.ToString()); |
| 121 | + } |
| 122 | + |
| 123 | + static void WriteDeviceDescriptorName(IEnumerable<OutputDevice> devices) |
| 124 | + { |
| 125 | + var builder = new StringBuilder(); |
| 126 | + var begin = @"namespace PuppeteerSharp.Mobile |
| 127 | +{ |
| 128 | + /// <summary> |
| 129 | + /// Device descriptor name. |
| 130 | + /// </summary> |
| 131 | + public enum DeviceDescriptorName |
| 132 | + {"; |
| 133 | + var end = @" |
| 134 | + } |
| 135 | +}"; |
| 136 | + |
| 137 | + builder.Append(begin); |
| 138 | + builder.AppendJoin(",", devices.Select(device => |
| 139 | + { |
| 140 | + return $@" |
| 141 | + /// <summary> |
| 142 | + /// {device.Name} |
| 143 | + /// </summary> |
| 144 | + {DeviceNameToEnumValue(device)}"; |
| 145 | + })); |
| 146 | + builder.Append(end); |
| 147 | + |
| 148 | + File.WriteAllText(deviceDescriptorNameOutput, builder.ToString()); |
| 149 | + } |
| 150 | + |
| 151 | + static string GenerateCsharpFromDevice(OutputDevice device) |
| 152 | + { |
| 153 | + var w = string.Empty; |
| 154 | + return $@" [DeviceDescriptorName.{DeviceNameToEnumValue(device)}] = new DeviceDescriptor |
| 155 | + {{ |
| 156 | + Name = ""{device.Name}"", |
| 157 | + UserAgent = ""{device.UserAgent}"", |
| 158 | + ViewPort = new ViewPortOptions |
| 159 | + {{ |
| 160 | + Width = {device.Viewport.Width}, |
| 161 | + Height = {device.Viewport.Height}, |
| 162 | + DeviceScaleFactor = {device.Viewport.DeviceScaleFactor}, |
| 163 | + IsMobile = {device.Viewport.IsMobile.ToString().ToLower()}, |
| 164 | + HasTouch = {device.Viewport.HasTouch.ToString().ToLower()}, |
| 165 | + IsLandscape = {device.Viewport.IsLandscape.ToString().ToLower()} |
| 166 | + }} |
| 167 | + }}"; |
| 168 | + } |
| 169 | + static string DeviceNameToEnumValue(OutputDevice device) |
| 170 | + { |
| 171 | + var output = new StringBuilder(); |
| 172 | + output.Append(char.ToUpper(device.Name[0])); |
| 173 | + for (var i = 1; i < device.Name.Length; i++) |
| 174 | + { |
| 175 | + if (char.IsWhiteSpace(device.Name[i])) |
| 176 | + { |
| 177 | + output.Append(char.ToUpper(device.Name[i + 1])); |
| 178 | + i++; |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + output.Append(device.Name[i]); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return output.ToString(); |
| 187 | + } |
| 188 | + |
| 189 | + static OutputDevice CreateDevice(string chromeVersion, string deviceName, RootObject.Device descriptor, bool landscape) |
| 190 | + { |
| 191 | + var devicePayload = LoadFromJSONV1(descriptor); |
| 192 | + var viewportPayload = landscape ? devicePayload.Horizontal : devicePayload.Vertical; |
| 193 | + return new OutputDevice |
| 194 | + { |
| 195 | + Name = deviceName + (landscape ? " landscape" : string.Empty), |
| 196 | + UserAgent = devicePayload.UserAgent.Replace("%s", chromeVersion), |
| 197 | + Viewport = new OutputDevice.OutputDeviceViewport |
| 198 | + { |
| 199 | + Width = viewportPayload.Width, |
| 200 | + Height = viewportPayload.Height, |
| 201 | + DeviceScaleFactor = devicePayload.DeviceScaleFactor, |
| 202 | + IsMobile = devicePayload.Capabilities.Contains("mobile"), |
| 203 | + HasTouch = devicePayload.Capabilities.Contains("touch"), |
| 204 | + IsLandscape = landscape |
| 205 | + } |
| 206 | + }; |
| 207 | + } |
| 208 | + |
| 209 | + static DevicePayload LoadFromJSONV1(RootObject.Device json) => new DevicePayload |
| 210 | + { |
| 211 | + Type = json.Type, |
| 212 | + UserAgent = json.UserAgent, |
| 213 | + Capabilities = json.Capabilities.ToHashSet(), |
| 214 | + DeviceScaleFactor = json.Screen.DevicePixelRatio, |
| 215 | + Horizontal = new ViewportPayload |
| 216 | + { |
| 217 | + Height = json.Screen.Horizontal.Height, |
| 218 | + Width = json.Screen.Horizontal.Width |
| 219 | + }, |
| 220 | + Vertical = new ViewportPayload |
| 221 | + { |
| 222 | + Height = json.Screen.Vertical.Height, |
| 223 | + Width = json.Screen.Vertical.Width |
| 224 | + } |
| 225 | + }; |
| 226 | + |
| 227 | + static Task<string> HttpGET(string url) => new HttpClient().GetStringAsync(url); |
| 228 | + } |
| 229 | +} |
0 commit comments