void FindDevice() { if (device_name != null) { return; } device_name = Environment.GetEnvironmentVariable("DEVICE_NAME"); if (!string.IsNullOrEmpty(device_name)) { return; } var devs = new Devices() { Harness = Harness, }; Task.Run(async() => { await devs.LoadAsync(main_log); }).Wait(); string [] deviceClasses; switch (mode) { case "ios": deviceClasses = new string [] { "iPhone", "iPad", "iPod" }; break; case "watchos": deviceClasses = new string [] { "Watch" }; break; case "tvos": deviceClasses = new string [] { "AppleTV" }; // Untested break; default: throw new Exception($"unknown mode: {mode}"); } var selected = devs.ConnectedDevices.Where((v) => deviceClasses.Contains(v.DeviceClass) && v.IsUsableForDebugging != false); Device selected_data; if (selected.Count() == 0) { throw new Exception($"Could not find any applicable devices with device class(es): {string.Join (", ", deviceClasses)}"); } else if (selected.Count() > 1) { selected_data = selected .OrderBy((dev) => { Version v; if (Version.TryParse(dev.ProductVersion, out v)) { return(v); } return(new Version()); }) .First(); main_log.WriteLine("Found {0} devices for device class(es) '{1}': '{2}'. Selected: '{3}' (because it has the lowest version).", selected.Count(), string.Join("', '", deviceClasses), string.Join("', '", selected.Select((v) => v.Name).ToArray()), selected_data.Name); } else { selected_data = selected.First(); } device_name = selected_data.Name; if (mode == "watchos") { companion_device_name = devs.FindCompanionDevice(main_log, selected_data).Name; } }