Search in sources :

Example 1 with DevToolsInstance

use of io.flutter.run.daemon.DevToolsInstance in project flutter-intellij by flutter.

the class LabelInput method addBrowserInspectorViewContent.

private void addBrowserInspectorViewContent(FlutterApp app, @Nullable InspectorService inspectorService, ToolWindow toolWindow, boolean isEmbedded, DevToolsInstance devToolsInstance) {
    assert (SwingUtilities.isEventDispatchThread());
    final ContentManager contentManager = toolWindow.getContentManager();
    final FlutterDevice device = app.device();
    final List<FlutterDevice> existingDevices = new ArrayList<>();
    for (FlutterApp otherApp : perAppViewState.keySet()) {
        existingDevices.add(otherApp.device());
    }
    final String tabName = device.getUniqueName(existingDevices);
    if (emptyContent != null) {
        contentManager.removeContent(emptyContent, true);
        emptyContent = null;
    }
    final String browserUrl = app.getConnector().getBrowserUrl();
    if (isEmbedded) {
        final String color = ColorUtil.toHex(UIUtil.getEditorPaneBackground());
        final DevToolsUrl devToolsUrl = new DevToolsUrl(devToolsInstance.host, devToolsInstance.port, browserUrl, "inspector", true, color, UIUtil.getFontSize(UIUtil.FontSize.NORMAL));
        // noinspection CodeBlock2Expr
        ApplicationManager.getApplication().invokeLater(() -> {
            embeddedBrowserOptional().ifPresent(embeddedBrowser -> embeddedBrowser.openPanel(contentManager, tabName, devToolsUrl, () -> {
                // If the embedded browser doesn't work, offer a link to open in the regular browser.
                final List<LabelInput> inputs = Arrays.asList(new LabelInput("The embedded browser failed to load."), openDevToolsLabel(app, inspectorService, toolWindow));
                presentClickableLabel(toolWindow, inputs);
            }));
        });
        if (!busSubscribed) {
            busConnection.subscribe(EditorColorsManager.TOPIC, scheme -> embeddedBrowserOptional().ifPresent(embeddedBrowser -> embeddedBrowser.updateColor(ColorUtil.toHex(UIUtil.getEditorPaneBackground()))));
            busConnection.subscribe(UISettingsListener.TOPIC, scheme -> embeddedBrowserOptional().ifPresent(embeddedBrowser -> embeddedBrowser.updateFontSize(UIUtil.getFontSize(UIUtil.FontSize.NORMAL))));
            busSubscribed = true;
        }
    } else {
        BrowserLauncher.getInstance().browse((new DevToolsUrl(devToolsInstance.host, devToolsInstance.port, browserUrl, "inspector", false, null, null).getUrlString()), null);
        presentLabel(toolWindow, "DevTools inspector has been opened in the browser.");
    }
}
Also used : Storage(com.intellij.openapi.components.Storage) UIUtil(com.intellij.util.ui.UIUtil) ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager) AllIcons(com.intellij.icons.AllIcons) Event(org.dartlang.vm.service.element.Event) PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) FlutterInitializer(io.flutter.FlutterInitializer) TimeoutException(java.util.concurrent.TimeoutException) InspectorService(io.flutter.inspector.InspectorService) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) JBLabel(com.intellij.ui.components.JBLabel) SideBorder(com.intellij.ui.SideBorder) AsyncUtils(io.flutter.utils.AsyncUtils) JBUI(com.intellij.util.ui.JBUI) InspectorSourceLocation(io.flutter.inspector.InspectorSourceLocation) Disposer(com.intellij.openapi.util.Disposer) LinkLabel(com.intellij.ui.components.labels.LinkLabel) InspectorGroupManagerService(io.flutter.inspector.InspectorGroupManagerService) Logger(com.intellij.openapi.diagnostic.Logger) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) JxBrowserUtils(io.flutter.utils.JxBrowserUtils) ToolWindow(com.intellij.openapi.wm.ToolWindow) ColorUtil(com.intellij.ui.ColorUtil) UISettingsListener(com.intellij.ide.ui.UISettingsListener) NonInjectable(com.intellij.serviceContainer.NonInjectable) ActionCallback(com.intellij.openapi.util.ActionCallback) ToolWindowManagerEx(com.intellij.openapi.wm.ex.ToolWindowManagerEx) FlutterIcons(icons.FlutterIcons) Content(com.intellij.ui.content.Content) ServiceExtensions(io.flutter.vmService.ServiceExtensions) BrowserLauncher(com.intellij.ide.browsers.BrowserLauncher) FlutterBundle(io.flutter.FlutterBundle) Nullable(org.jetbrains.annotations.Nullable) SimpleToolWindowPanel(com.intellij.openapi.ui.SimpleToolWindowPanel) List(java.util.List) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) ContentManagerEvent(com.intellij.ui.content.ContentManagerEvent) XSourcePosition(com.intellij.xdebugger.XSourcePosition) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ContentManager(com.intellij.ui.content.ContentManager) ExecutionUtil(com.intellij.execution.runners.ExecutionUtil) NotNull(org.jetbrains.annotations.NotNull) FlutterDevice(io.flutter.run.FlutterDevice) TabInfo(com.intellij.ui.tabs.TabInfo) VmServiceListenerAdapter(io.flutter.utils.VmServiceListenerAdapter) ActiveRunnable(com.intellij.openapi.util.ActiveRunnable) java.util(java.util) LinkListener(com.intellij.ui.components.labels.LinkListener) CompletableFuture(java.util.concurrent.CompletableFuture) FlutterSettings(io.flutter.settings.FlutterSettings) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) JBRunnerTabs(com.intellij.execution.ui.layout.impl.JBRunnerTabs) DevToolsUrl(io.flutter.devtools.DevToolsUrl) io.flutter.jxbrowser(io.flutter.jxbrowser) Project(com.intellij.openapi.project.Project) FlutterUtils(io.flutter.FlutterUtils) ContentManagerAdapter(com.intellij.ui.content.ContentManagerAdapter) FlutterApp(io.flutter.run.daemon.FlutterApp) DiagnosticsNode(io.flutter.inspector.DiagnosticsNode) FlutterViewToolWindowManagerListener(io.flutter.toolwindow.FlutterViewToolWindowManagerListener) StringUtil(com.intellij.openapi.util.text.StringUtil) DevToolsService(io.flutter.run.daemon.DevToolsService) VmService(org.dartlang.vm.service.VmService) Disposable(com.intellij.openapi.Disposable) VerticalFlowLayout(com.intellij.openapi.ui.VerticalFlowLayout) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) EventStream(io.flutter.utils.EventStream) IdeBorderFactory(com.intellij.ui.IdeBorderFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DevToolsInstance(io.flutter.run.daemon.DevToolsInstance) javax.swing(javax.swing) FlutterDevice(io.flutter.run.FlutterDevice) FlutterApp(io.flutter.run.daemon.FlutterApp) DevToolsUrl(io.flutter.devtools.DevToolsUrl) ContentManager(com.intellij.ui.content.ContentManager) List(java.util.List)

Example 2 with DevToolsInstance

use of io.flutter.run.daemon.DevToolsInstance in project flutter-intellij by flutter.

the class BazelFields method getLaunchCommand.

/**
 * Returns the command to use to launch the Flutter app. (Via running the Bazel target.)
 */
GeneralCommandLine getLaunchCommand(@NotNull Project project, @Nullable FlutterDevice device, @NotNull RunMode mode) throws ExecutionException {
    try {
        checkRunnable(project);
    } catch (RuntimeConfigurationError e) {
        throw new ExecutionException(e);
    }
    final Workspace workspace = getWorkspace(project);
    final String launchingScript = getRunScriptFromWorkspace(project);
    // already checked
    assert launchingScript != null;
    // if the workspace is null, then so is the launching script, therefore this was already checked.
    assert workspace != null;
    final String target = getTarget();
    // already checked
    assert target != null;
    final String additionalArgs = getAdditionalArgs();
    final GeneralCommandLine commandLine = new GeneralCommandLine().withWorkDirectory(workspace.getRoot().getPath());
    commandLine.setCharset(StandardCharsets.UTF_8);
    commandLine.setExePath(FileUtil.toSystemDependentName(launchingScript));
    final String inputBazelArgs = StringUtil.notNullize(bazelArgs);
    if (!inputBazelArgs.isEmpty()) {
        commandLine.addParameter(String.format("--bazel-options=%s", inputBazelArgs));
    }
    // Potentially add the flag related to build mode.
    if (enableReleaseMode) {
        commandLine.addParameter("--release");
    } else if (mode.equals(PROFILE)) {
        commandLine.addParameter("--profile");
    }
    // Tell the flutter command-line tools that we want a machine interface on stdio.
    commandLine.addParameter("--machine");
    // Pause the app at startup in order to set breakpoints.
    if (!enableReleaseMode && mode == DEBUG) {
        commandLine.addParameter("--start-paused");
    }
    // User specified additional target arguments.
    final CommandLineTokenizer additionalArgsTokenizer = new CommandLineTokenizer(StringUtil.notNullize(additionalArgs));
    while (additionalArgsTokenizer.hasMoreTokens()) {
        commandLine.addParameter(additionalArgsTokenizer.nextToken());
    }
    final String enableBazelHotRestartParam = "--enable-google3-hot-reload";
    final String disableBazelHotRestartParam = "--no-enable-google3-hot-reload";
    final boolean hasEnabledArg = StringUtil.notNullize(additionalArgs).contains(enableBazelHotRestartParam);
    final boolean hasDisabledArg = StringUtil.notNullize(additionalArgs).contains(disableBazelHotRestartParam);
    if (!FlutterSettings.getInstance().isEnableBazelHotRestart() && hasDisabledArg) {
        final Notification notification = new Notification(FlutterMessages.FLUTTER_NOTIFICATION_GROUP_ID, "Google3-specific hot restart is disabled by default", "You can now remove this flag from your configuration's additional args: " + disableBazelHotRestartParam, NotificationType.INFORMATION);
        Notifications.Bus.notify(notification, project);
    } else if (FlutterSettings.getInstance().isEnableBazelHotRestart() && !hasEnabledArg && !hasDisabledArg) {
        commandLine.addParameter(enableBazelHotRestartParam);
    }
    // Send in the deviceId.
    if (device != null) {
        commandLine.addParameter("-d");
        commandLine.addParameter(device.deviceId());
    }
    try {
        final ProgressManager progress = ProgressManager.getInstance();
        final CompletableFuture<DevToolsInstance> devToolsFuture = new CompletableFuture<>();
        progress.runProcessWithProgressSynchronously(() -> {
            progress.getProgressIndicator().setIndeterminate(true);
            try {
                final DevToolsService service = this.devToolsService == null ? DevToolsService.getInstance(project) : this.devToolsService;
                devToolsFuture.complete(service.getDevToolsInstance().get(30, TimeUnit.SECONDS));
            } catch (Exception e) {
                LOG.error(e);
            }
        }, "Starting DevTools", false, project);
        final DevToolsInstance instance = devToolsFuture.get();
        commandLine.addParameter("--devtools-server-address=http://" + instance.host + ":" + instance.port);
    } catch (Exception e) {
        LOG.error(e);
    }
    commandLine.addParameter(target);
    return commandLine;
}
Also used : Notification(com.intellij.notification.Notification) ExecutionException(com.intellij.execution.ExecutionException) InvalidDataException(com.intellij.openapi.util.InvalidDataException) CompletableFuture(java.util.concurrent.CompletableFuture) DevToolsService(io.flutter.run.daemon.DevToolsService) ProgressManager(com.intellij.openapi.progress.ProgressManager) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) DevToolsInstance(io.flutter.run.daemon.DevToolsInstance) ExecutionException(com.intellij.execution.ExecutionException) RuntimeConfigurationError(com.intellij.execution.configurations.RuntimeConfigurationError) Workspace(io.flutter.bazel.Workspace) CommandLineTokenizer(com.intellij.execution.configurations.CommandLineTokenizer)

Example 3 with DevToolsInstance

use of io.flutter.run.daemon.DevToolsInstance in project flutter-intellij by flutter.

the class SdkFields method createFlutterSdkRunCommand.

/**
 * Create a command to run 'flutter run --machine'.
 */
public GeneralCommandLine createFlutterSdkRunCommand(@NotNull Project project, @NotNull RunMode runMode, @NotNull FlutterLaunchMode flutterLaunchMode, @NotNull FlutterDevice device, boolean firstRun) throws ExecutionException {
    final MainFile main = MainFile.verify(filePath, project).get();
    final FlutterSdk flutterSdk = FlutterSdk.getFlutterSdk(project);
    if (flutterSdk == null) {
        throw new ExecutionException(FlutterBundle.message("flutter.sdk.is.not.configured"));
    }
    final PubRoot root = PubRoot.forDirectory(main.getAppDir());
    if (root == null) {
        throw new ExecutionException("Entrypoint isn't within a Flutter pub root");
    }
    final FlutterCommand command;
    String[] args = getAdditionalArgsParsed();
    if (buildFlavor != null) {
        args = ArrayUtil.append(args, "--flavor=" + buildFlavor);
    }
    if (FlutterSettings.getInstance().isShowStructuredErrors() && flutterSdk.getVersion().isDartDefineSupported()) {
        args = ArrayUtil.append(args, "--dart-define=flutter.inspector.structuredErrors=true");
    }
    if (flutterSdk.getVersion().flutterRunSupportsDevToolsUrl()) {
        try {
            final ProgressManager progress = ProgressManager.getInstance();
            final CompletableFuture<DevToolsInstance> devToolsFuture = new CompletableFuture<>();
            progress.runProcessWithProgressSynchronously(() -> {
                progress.getProgressIndicator().setIndeterminate(true);
                try {
                    final CompletableFuture<DevToolsInstance> futureInstance = DevToolsService.getInstance(project).getDevToolsInstance();
                    if (firstRun) {
                        devToolsFuture.complete(futureInstance.get(30, TimeUnit.SECONDS));
                    } else {
                        // Skip waiting if this isn't the first time running this project. If DevTools isn't available by now, there's likely to be
                        // something wrong that won't be fixed by restarting, so we don't want to keep delaying run.
                        final DevToolsInstance instance = futureInstance.getNow(null);
                        if (instance == null) {
                            devToolsFuture.completeExceptionally(new Exception("DevTools instance not available after first run."));
                        } else {
                            devToolsFuture.complete(instance);
                        }
                    }
                } catch (Exception e) {
                    devToolsFuture.completeExceptionally(e);
                }
            }, "Starting DevTools", false, project);
            final DevToolsInstance instance = devToolsFuture.get();
            args = ArrayUtil.append(args, "--devtools-server-address=http://" + instance.host + ":" + instance.port);
            if (firstRun) {
                FlutterInitializer.getAnalytics().sendEvent("devtools", "first-run-success");
            }
        } catch (Exception e) {
            LOG.info(e);
            FlutterInitializer.getAnalytics().sendExpectedException("devtools", e);
        }
    }
    command = flutterSdk.flutterRun(root, main.getFile(), device, runMode, flutterLaunchMode, project, args);
    final GeneralCommandLine commandLine = command.createGeneralCommandLine(project);
    commandLine.getEnvironment().putAll(getEnvs());
    commandLine.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE);
    return commandLine;
}
Also used : FlutterCommand(io.flutter.sdk.FlutterCommand) ExecutionException(com.intellij.execution.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) ProgressManager(com.intellij.openapi.progress.ProgressManager) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) FlutterSdk(io.flutter.sdk.FlutterSdk) DevToolsInstance(io.flutter.run.daemon.DevToolsInstance) PubRoot(io.flutter.pub.PubRoot) ExecutionException(com.intellij.execution.ExecutionException)

Example 4 with DevToolsInstance

use of io.flutter.run.daemon.DevToolsInstance in project flutter-intellij by flutter.

the class BazelFieldsTest method setUp.

@Before
public void setUp() {
    final CompletableFuture<DevToolsInstance> future = new CompletableFuture<>();
    future.complete(new DevToolsInstance("http://localhost", 1234));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DevToolsInstance(io.flutter.run.daemon.DevToolsInstance) Before(org.junit.Before)

Example 5 with DevToolsInstance

use of io.flutter.run.daemon.DevToolsInstance in project flutter-intellij by flutter.

the class LaunchCommandsTest method setUp.

@Before
public void setUp() {
    final CompletableFuture<DevToolsInstance> future = new CompletableFuture<>();
    future.complete(new DevToolsInstance("http://localhost", 1234));
    mockService = mock(DevToolsService.class);
    when(mockService.getDevToolsInstance()).thenReturn(future);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DevToolsService(io.flutter.run.daemon.DevToolsService) DevToolsInstance(io.flutter.run.daemon.DevToolsInstance) Before(org.junit.Before)

Aggregations

DevToolsInstance (io.flutter.run.daemon.DevToolsInstance)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 DevToolsService (io.flutter.run.daemon.DevToolsService)3 ExecutionException (com.intellij.execution.ExecutionException)2 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)2 ProgressManager (com.intellij.openapi.progress.ProgressManager)2 Before (org.junit.Before)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CommandLineTokenizer (com.intellij.execution.configurations.CommandLineTokenizer)1 RuntimeConfigurationError (com.intellij.execution.configurations.RuntimeConfigurationError)1 ExecutionUtil (com.intellij.execution.runners.ExecutionUtil)1 JBRunnerTabs (com.intellij.execution.ui.layout.impl.JBRunnerTabs)1 AllIcons (com.intellij.icons.AllIcons)1 BrowserLauncher (com.intellij.ide.browsers.BrowserLauncher)1 UISettingsListener (com.intellij.ide.ui.UISettingsListener)1 Notification (com.intellij.notification.Notification)1 Disposable (com.intellij.openapi.Disposable)1 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 PersistentStateComponent (com.intellij.openapi.components.PersistentStateComponent)1