Skip to content

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

Closed

Description

@ajamespro

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?
    },
  ]
}

Activity

  1. DanTup commented on Aug 7, 2023

    @DanTup
    Member

    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.

  2. added
    awaiting infoRequires more information from the customer to progress
    on Aug 7, 2023
  3. ajamespro commented on Aug 7, 2023

    @ajamespro
    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.

  4. changed the title [-]Dynamic deviceId available in preLaunchTask?[/-] [+]Provide a command to get the ID of the selected device for use in tasks[/+] on Aug 7, 2023
  5. added
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    in flutterRelates to running Flutter apps
    in tasksRelates to VS Code tasks, such as those provided by Task Providers
    and removed
    awaiting infoRequires more information from the customer to progress
    on Aug 7, 2023
  6. added this to the v3.72.0 milestone on Aug 7, 2023
  7. DanTup commented on Aug 7, 2023

    @DanTup
    Member

    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.

  8. ajamespro commented on Aug 7, 2023

    @ajamespro
    Author

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

  9. DanTup commented on Aug 29, 2023

    @DanTup
    Member

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

    image

  10. DanTup commented on Aug 29, 2023

    @DanTup
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in flutterRelates to running Flutter appsin tasksRelates to VS Code tasks, such as those provided by Task Providersis enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions