Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a command to get the ID of the selected device for use in tasks #4677

Closed
ajamespro opened this issue Aug 7, 2023 · 6 comments
Closed
Labels
in flutter Relates to running Flutter apps in tasks Relates to VS Code tasks, such as those provided by Task Providers is enhancement
Milestone

Comments

@ajamespro
Copy link

ajamespro commented Aug 7, 2023

I'm trying to pass the mobile deviceId to a flutter preLaunchTask. Minimal sample below, but ultimately it's a simctl script that uses booted as the device id. However, this breaks down when I have multiple simulator devices booted, so I'm trying to explicitly run the preLaunch script with the target device.

Does someone know how to do this? I've tried almost everything I could think of with variable substitution and environment variables (printenv shows nothing useful), but I can't find any hook into deviceId. It should be the same parameter passed to the -d argument. Caveat: I can't use an explicit deviceId in the launch config because the config is checked in and shared with others, and we all use various versions of devices and Xcode.

Can you help? :/

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Test My Shtuff",
      "type": "dart",
      "request": "launch",
      "flutterMode": "debug",
      "program": "./integration_test/",
      "templateFor": "./integration_test/",
      "preLaunchTask": "Thing that needs deviceId",
    },
  ]
}

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Thing that needs deviceId",
      "type": "shell",
      "command": "echo $DEVICE_ID", // <-- what do?
    },
  ]
}
@DanTup
Copy link
Member

DanTup commented Aug 7, 2023

I can't think of a way to do this currently.

Something that I think could be fairly easily supported though is a command that returns you the select devices ID, so you could do something like:

"command": "echo ${command:dart.getSelectedDeviceId}",

There's the possibility this command would return null if there's no selected device though (such as during startup before they have been discovered).

We could also set an environment variable (DART_CODE_DEVICE_ID) for debugger launches, but I don't think we can do it for shell command tasks, because the Dart extension is not involved at all in launching those.

@DanTup DanTup added the awaiting info Requires more information from the customer to progress label Aug 7, 2023
@ajamespro
Copy link
Author

Something that I think could be fairly easily supported though is a command that returns you the select devices ID, so you could do something like:

I love it. null is perfectly usable for my use case as well (I can fall back to booted). Can I do any heavy lifting or is this a officially a feature request? I'd love to help push this along or meet in the middle, but don't want to burden others with a contribution learning curve either.

@DanTup DanTup changed the title Dynamic deviceId available in preLaunchTask? Provide a command to get the ID of the selected device for use in tasks Aug 7, 2023
@DanTup DanTup added is enhancement in flutter Relates to running Flutter apps in tasks Relates to VS Code tasks, such as those provided by Task Providers and removed awaiting info Requires more information from the customer to progress labels Aug 7, 2023
@DanTup DanTup added this to the v3.72.0 milestone Aug 7, 2023
@DanTup
Copy link
Member

DanTup commented Aug 7, 2023

Contributions are always welcome!

I think you could probably do this with a new line below here:

context.subscriptions.push(vs.commands.registerCommand("flutter.selectDevice", deviceManager.showDevicePicker, deviceManager));
context.subscriptions.push(vs.commands.registerCommand("flutter.launchEmulator", deviceManager.promptForAndLaunchEmulator, deviceManager));

With something like:

context.subscriptions.push(vs.commands.registerCommand("flutter.getSelectedDeviceId", () => deviceManager?.currentDevice?.id));

You'd also need to add the command to package.json (it'd need to go into contributes/commands and also in contributes/menus/commandPalette with "when": false to prevent it showing up in the command palette).

To make the change and test it:

  • fork and clone this repo
  • run npm install
  • open the folder in VS Code
  • make a changes similar to the suggestion above
  • press F5 to build the extension and launch the Extension Development Host (another instance of VS Code running the extension from source)
  • open your project and test with something like "command": "echo ${command:flutter.getSelectedDeviceId}",

I don't have a good way to write an automated test for this right now (because the mock device manager isn't available in that code), but I think the code is simple enough to merge without. We should include a comment that the command may be used by users tasks and that it should not be removed even if it appears unused.

@ajamespro
Copy link
Author

Ok, I'll give it shot. And thanks for the spoon feeding. This makes it a lot easier and seems doable.

@DanTup DanTup closed this as completed in 087222c Aug 29, 2023
@DanTup
Copy link
Member

DanTup commented Aug 29, 2023

I'm just working through some smaller fixes for the upcoming release so I made this change.

image

@DanTup
Copy link
Member

DanTup commented Aug 29, 2023

This was included in the pre-release version pushed just now if you want to try it out ahead of the release (it's v3.71.20230829 which might take 20 min or so to show up):

Dart pre-release versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in flutter Relates to running Flutter apps in tasks Relates to VS Code tasks, such as those provided by Task Providers is enhancement
Projects
None yet
Development

No branches or pull requests

2 participants