Search in sources :

Example 6 with RunConfigurationBase

use of com.intellij.execution.configurations.RunConfigurationBase in project android by JetBrains.

the class MakeBeforeRunTaskProvider method executeTask.

@Override
public boolean executeTask(DataContext context, RunConfiguration configuration, ExecutionEnvironment env, MakeBeforeRunTask task) {
    if (!AndroidProjectInfo.getInstance(myProject).requiresAndroidModel() || !isDirectGradleInvocationEnabled(myProject)) {
        CompileStepBeforeRun regularMake = new CompileStepBeforeRun(myProject);
        return regularMake.executeTask(context, configuration, env, new CompileStepBeforeRun.MakeBeforeRunTask());
    }
    AtomicReference<String> errorMsgRef = new AtomicReference<>();
    if (AndroidGradleBuildConfiguration.getInstance(myProject).SYNC_PROJECT_BEFORE_BUILD) {
        // If the model needs a sync, we need to sync "synchronously" before running.
        // See: https://code.google.com/p/android/issues/detail?id=70718
        GradleSyncState syncState = GradleSyncState.getInstance(myProject);
        if (syncState.isSyncNeeded() != ThreeState.NO) {
            GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setRunInBackground(false);
            GradleSyncInvoker.getInstance().requestProjectSync(myProject, request, new GradleSyncListener.Adapter() {

                @Override
                public void syncFailed(@NotNull Project project, @NotNull String errorMessage) {
                    errorMsgRef.set(errorMessage);
                }
            });
        }
    }
    String errorMsg = errorMsgRef.get();
    if (errorMsg != null) {
        // Sync failed. There is no point on continuing, because most likely the model is either not there, or has stale information,
        // including the path of the APK.
        LOG.info("Unable to launch '" + TASK_NAME + "' task. Project sync failed with message: " + errorMsg);
        return false;
    }
    if (myProject.isDisposed()) {
        return false;
    }
    // Some configurations (e.g. native attach) don't require a build while running the configuration
    if (configuration instanceof RunConfigurationBase && ((RunConfigurationBase) configuration).excludeCompileBeforeLaunchOption()) {
        return true;
    }
    // Note: this before run task provider may be invoked from a context such as Java unit tests, in which case it doesn't have
    // the android run config context
    AndroidRunConfigContext runConfigContext = env.getCopyableUserData(AndroidRunConfigContext.KEY);
    DeviceFutures deviceFutures = runConfigContext == null ? null : runConfigContext.getTargetDevices();
    List<AndroidDevice> targetDevices = deviceFutures == null ? Collections.emptyList() : deviceFutures.getDevices();
    List<String> cmdLineArgs = getCommonArguments(configuration, targetDevices);
    BeforeRunBuilder builder = createBuilder(env, getModules(myProject, context, configuration), configuration, runConfigContext, task.getGoal());
    try {
        boolean success = builder.build(GradleTaskRunner.newRunner(myProject), cmdLineArgs);
        LOG.info("Gradle invocation complete, success = " + success);
        return success;
    } catch (InvocationTargetException e) {
        LOG.info("Unexpected error while launching gradle before run tasks", e);
        return false;
    } catch (InterruptedException e) {
        LOG.info("Interrupted while launching gradle before run tasks");
        Thread.currentThread().interrupt();
        return false;
    }
}
Also used : GradleSyncInvoker(com.android.tools.idea.gradle.project.sync.GradleSyncInvoker) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationTargetException(java.lang.reflect.InvocationTargetException) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) AndroidProject(com.android.builder.model.AndroidProject) Project(com.intellij.openapi.project.Project) ApkProjects.isApkProject(com.android.tools.idea.apk.ApkProjects.isApkProject) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener) CompileStepBeforeRun(com.intellij.compiler.options.CompileStepBeforeRun)

Example 7 with RunConfigurationBase

use of com.intellij.execution.configurations.RunConfigurationBase in project android by JetBrains.

the class ConnectJavaDebuggerTask method launchDebugger.

@Override
public ProcessHandler launchDebugger(@NotNull LaunchInfo currentLaunchInfo, @NotNull final Client client, @NotNull ProcessHandlerLaunchStatus launchStatus, @NotNull ProcessHandlerConsolePrinter printer) {
    String debugPort = Integer.toString(client.getDebuggerListenPort());
    final int pid = client.getClientData().getPid();
    Logger.getInstance(ConnectJavaDebuggerTask.class).info(String.format(Locale.US, "Attempting to connect debugger to port %1$s [client %2$d]", debugPort, pid));
    // detach old process handler
    RunContentDescriptor descriptor = currentLaunchInfo.env.getContentToReuse();
    // reach here before the EDT gets around to creating the descriptor?
    assert descriptor != null : "ConnectJavaDebuggerTask expects an existing descriptor that will be reused";
    final ProcessHandler processHandler = descriptor.getProcessHandler();
    assert processHandler != null;
    // create a new process handler
    RemoteConnection connection = new RemoteConnection(true, "localhost", debugPort, false);
    final AndroidRemoteDebugProcessHandler debugProcessHandler = new AndroidRemoteDebugProcessHandler(myProject, myMonitorRemoteProcess);
    // switch the launch status and console printers to point to the new process handler
    // this is required, esp. for AndroidTestListener which holds a reference to the launch status and printers, and those should
    // be updated to point to the new process handlers, otherwise test results will not be forwarded appropriately
    launchStatus.setProcessHandler(debugProcessHandler);
    printer.setProcessHandler(debugProcessHandler);
    // detach after the launch status has been updated to point to the new process handler
    processHandler.detachProcess();
    AndroidDebugState debugState = new AndroidDebugState(myProject, debugProcessHandler, connection, currentLaunchInfo.consoleProvider);
    RunContentDescriptor debugDescriptor;
    try {
        // @formatter:off
        ExecutionEnvironment debugEnv = new ExecutionEnvironmentBuilder(currentLaunchInfo.env).executor(currentLaunchInfo.executor).runner(currentLaunchInfo.runner).contentToReuse(descriptor).build();
        debugDescriptor = DebuggerPanelsManager.getInstance(myProject).attachVirtualMachine(debugEnv, debugState, connection, false);
    // @formatter:on
    } catch (ExecutionException e) {
        processHandler.notifyTextAvailable("ExecutionException: " + e.getMessage() + '.', STDERR);
        return null;
    }
    if (debugDescriptor == null) {
        processHandler.notifyTextAvailable("Unable to connect debugger to Android application", STDERR);
        return null;
    }
    // re-run the collected text from the old process handler to the new
    // TODO: is there a race between messages received once the debugger has been connected, and these messages that are printed out?
    final AndroidProcessText oldText = AndroidProcessText.get(processHandler);
    if (oldText != null) {
        oldText.printTo(debugProcessHandler);
    }
    RunProfile runProfile = currentLaunchInfo.env.getRunProfile();
    int uniqueId = runProfile instanceof RunConfigurationBase ? ((RunConfigurationBase) runProfile).getUniqueID() : -1;
    AndroidSessionInfo value = new AndroidSessionInfo(debugProcessHandler, debugDescriptor, uniqueId, currentLaunchInfo.executor.getId(), InstantRunUtils.isInstantRunEnabled(currentLaunchInfo.env));
    debugProcessHandler.putUserData(AndroidSessionInfo.KEY, value);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEBUG_CLIENT, client);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEVICE_API_LEVEL, client.getDevice().getVersion());
    final String pkgName = client.getClientData().getClientDescription();
    final IDevice device = client.getDevice();
    // kill the process when the debugger is stopped
    debugProcessHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(ProcessEvent event) {
            debugProcessHandler.removeProcessListener(this);
            Client currentClient = device.getClient(pkgName);
            if (currentClient != null && currentClient.getClientData().getPid() != pid) {
                // a new process has been launched for the same package name, we aren't interested in killing this
                return;
            }
            Logger.getInstance(ConnectJavaDebuggerTask.class).info("Debugger terminating, so terminating process: " + pkgName);
            // Note: client.kill() doesn't work when the debugger is attached, we explicitly stop by package id..
            try {
                device.executeShellCommand("am force-stop " + pkgName, new NullOutputReceiver());
            } catch (Exception e) {
            // don't care..
            }
        }
    });
    return debugProcessHandler;
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) IDevice(com.android.ddmlib.IDevice) RunProfile(com.intellij.execution.configurations.RunProfile) ExecutionException(com.intellij.execution.ExecutionException) RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) ProcessHandler(com.intellij.execution.process.ProcessHandler) RemoteConnection(com.intellij.execution.configurations.RemoteConnection) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException) Client(com.android.ddmlib.Client) NullOutputReceiver(com.android.ddmlib.NullOutputReceiver)

Example 8 with RunConfigurationBase

use of com.intellij.execution.configurations.RunConfigurationBase in project intellij-community by JetBrains.

the class DebuggerSessionTabBase method attachNotificationTo.

protected void attachNotificationTo(final Content content) {
    if (myConsole instanceof ObservableConsoleView) {
        ObservableConsoleView observable = (ObservableConsoleView) myConsole;
        observable.addChangeListener(types -> {
            if (types.contains(ConsoleViewContentType.ERROR_OUTPUT) || types.contains(ConsoleViewContentType.NORMAL_OUTPUT)) {
                content.fireAlert();
            }
        }, content);
        RunProfile profile = getRunProfile();
        if (profile instanceof RunConfigurationBase && !ApplicationManager.getApplication().isUnitTestMode()) {
            observable.addChangeListener(new RunContentBuilder.ConsoleToFrontListener((RunConfigurationBase) profile, myProject, DefaultDebugExecutor.getDebugExecutorInstance(), myRunContentDescriptor, myUi), content);
        }
    }
}
Also used : RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) ObservableConsoleView(com.intellij.execution.ui.ObservableConsoleView) RunProfile(com.intellij.execution.configurations.RunProfile) RunContentBuilder(com.intellij.execution.runners.RunContentBuilder)

Example 9 with RunConfigurationBase

use of com.intellij.execution.configurations.RunConfigurationBase in project intellij-community by JetBrains.

the class RunTab method initLogConsoles.

protected final void initLogConsoles(@NotNull RunProfile runConfiguration, @NotNull RunContentDescriptor contentDescriptor, @Nullable ExecutionConsole console) {
    ProcessHandler processHandler = contentDescriptor.getProcessHandler();
    if (runConfiguration instanceof RunConfigurationBase) {
        RunConfigurationBase configuration = (RunConfigurationBase) runConfiguration;
        if (myManager == null) {
            myManager = new LogFilesManager(myProject, getLogConsoleManager(), contentDescriptor);
        }
        myManager.addLogConsoles(configuration, processHandler);
        if (processHandler != null) {
            OutputFileUtil.attachDumpListener(configuration, processHandler, console);
        }
    }
}
Also used : RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) LogFilesManager(com.intellij.diagnostic.logging.LogFilesManager) ProcessHandler(com.intellij.execution.process.ProcessHandler)

Example 10 with RunConfigurationBase

use of com.intellij.execution.configurations.RunConfigurationBase in project intellij-community by JetBrains.

the class LogConsoleManagerBase method doAddLogConsole.

private void doAddLogConsole(@NotNull final LogConsoleBase log, String id, Icon icon, @Nullable RunProfile runProfile) {
    if (runProfile instanceof RunConfigurationBase) {
        ((RunConfigurationBase) runProfile).customizeLogConsole(log);
    }
    log.attachStopLogConsoleTrackingListener(getProcessHandler());
    addAdditionalTabComponent(log, id, icon);
    getUi().addListener(new ContentManagerAdapter() {

        @Override
        public void selectionChanged(final ContentManagerEvent event) {
            log.activate();
        }
    }, log);
}
Also used : RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) ContentManagerAdapter(com.intellij.ui.content.ContentManagerAdapter) ContentManagerEvent(com.intellij.ui.content.ContentManagerEvent)

Aggregations

RunConfigurationBase (com.intellij.execution.configurations.RunConfigurationBase)12 RunProfile (com.intellij.execution.configurations.RunProfile)6 ProcessHandler (com.intellij.execution.process.ProcessHandler)5 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)4 AndroidSessionInfo (com.android.tools.idea.run.AndroidSessionInfo)2 Module (com.intellij.openapi.module.Module)2 Project (com.intellij.openapi.project.Project)2 NotNull (org.jetbrains.annotations.NotNull)2 AndroidProject (com.android.builder.model.AndroidProject)1 Client (com.android.ddmlib.Client)1 IDevice (com.android.ddmlib.IDevice)1 NullOutputReceiver (com.android.ddmlib.NullOutputReceiver)1 ApkProjects.isApkProject (com.android.tools.idea.apk.ApkProjects.isApkProject)1 GradleSyncInvoker (com.android.tools.idea.gradle.project.sync.GradleSyncInvoker)1 GradleSyncListener (com.android.tools.idea.gradle.project.sync.GradleSyncListener)1 GradleSyncState (com.android.tools.idea.gradle.project.sync.GradleSyncState)1 AndroidTestRunConfiguration (com.android.tools.idea.testartifacts.instrumented.AndroidTestRunConfiguration)1 CompileStepBeforeRun (com.intellij.compiler.options.CompileStepBeforeRun)1 BaseCoverageSuite (com.intellij.coverage.BaseCoverageSuite)1 LogFilesManager (com.intellij.diagnostic.logging.LogFilesManager)1