Search in sources :

Example 6 with CapturingAnsiEscapesAwareProcessHandler

use of com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler in project android by JetBrains.

the class UnpackOperation method untar.

/**
   * @throws IOException     when tar fails in a way that we may retry the operation
   * @throws WizardException if retry is not possible (e.g. no tar executable)
   */
@NotNull
private static File untar(final File archive, File destination, final InstallContext context, final ProgressIndicator indicator) throws IOException, WizardException {
    if (!destination.mkdirs()) {
        throw new WizardException("Cannot create temporary directory to extract files");
    }
    indicator.start();
    // 0%
    indicator.setFraction(0.0);
    try {
        GeneralCommandLine line = new GeneralCommandLine(getTarExecutablePath(), TAR_FLAGS_EXTRACT_UNPACK_VERBOSE_FILENAME_TARGETDIR, archive.getAbsolutePath(), destination.getAbsolutePath());
        CapturingAnsiEscapesAwareProcessHandler handler = new CapturingAnsiEscapesAwareProcessHandler(line);
        handler.addProcessListener(new ProcessAdapter() {

            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
                String string = event.getText();
                if (!StringUtil.isEmptyOrSpaces(string)) {
                    if (string.startsWith(EXTRACT_OPERATION_OUTPUT)) {
                        // Extract operation prefix
                        String fileName = string.substring(EXTRACT_OPERATION_OUTPUT.length()).trim();
                        indicator.setText(fileName);
                    } else if (ProcessOutputTypes.STDOUT.equals(outputType)) {
                        indicator.setText(string.trim());
                    } else {
                        context.print(string, ConsoleViewContentType.getConsoleViewType(outputType));
                    }
                }
            }
        });
        if (handler.runProcess().getExitCode() != 0) {
            throw new IOException("Unable to unpack archive file");
        }
        return destination;
    } catch (ExecutionException e) {
        throw new WizardException("Unable to run tar utility");
    } finally {
        // 100%
        indicator.setFraction(1.0);
        indicator.stop();
    }
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) CapturingAnsiEscapesAwareProcessHandler(com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler) ProcessEvent(com.intellij.execution.process.ProcessEvent) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CapturingAnsiEscapesAwareProcessHandler (com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler)6 ExecutionException (com.intellij.execution.ExecutionException)5 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)4 ProcessOutput (com.intellij.execution.process.ProcessOutput)3 File (java.io.File)3 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)2 ProcessEvent (com.intellij.execution.process.ProcessEvent)2 Key (com.intellij.openapi.util.Key)2 IOException (java.io.IOException)2 Revision (com.android.repository.Revision)1 LocalPackage (com.android.repository.api.LocalPackage)1 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)1 AndroidSdkInitializer (com.android.tools.idea.startup.AndroidSdkInitializer)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Matcher (java.util.regex.Matcher)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1