Ver código fonte

Add OutputConsole and format the download output

François Pluchino 13 anos atrás
pai
commit
2f0162053d

+ 1 - 1
src/Composer/Console/Application.php

@@ -15,10 +15,10 @@ namespace Composer\Console;
 use Symfony\Component\Console\Application as BaseApplication;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Output\ConsoleOutput;
 use Symfony\Component\Console\Formatter\OutputFormatter;
 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
 use Symfony\Component\Finder\Finder;
+use Composer\Console\Output\ConsoleOutput;
 use Composer\Command;
 use Composer\Composer;
 use Composer\Installer;

+ 104 - 0
src/Composer/Console/Output/ConsoleOutput.php

@@ -0,0 +1,104 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Console\Output;
+
+use Symfony\Component\Console\Output\ConsoleOutput as BaseConsoleOutput;
+
+/**
+ * ConsoleOutput is the default class for all CLI output.
+ *
+ * @author François Pluchino <francois.pluchino@opendisplay.com>
+ */
+class ConsoleOutput extends BaseConsoleOutput
+{
+    /**
+     * Overwrites a previous message to the output.
+     *
+     * @param string|array $messages The message as an array of lines of a single string
+     * @param integer      $size     The size of line
+     * @param Boolean      $newline  Whether to add a newline or not
+     * @param integer      $type     The type of output
+     */
+    public function overwrite($messages, $size = 80, $newline = false, $type = 0)
+    {
+        for ($place = $size; $place > 0; $place--) {
+            $this->write("\x08");
+        }
+
+        $this->write($messages, false, $type);
+
+        for ($place = ($size - strlen($line)); $place > 0; $place--) {
+            $this->write(' ');
+        }
+
+        // clean up the end line
+        for ($place = ($size - strlen($messages)); $place > 0; $place--) {
+            $this->write("\x08");
+        }
+
+        if ($newline) {
+            $this->writeln('');
+        }
+    }
+
+    /**
+     * Overwrites a previous message to the output and adds a newline at the end.
+     *
+     * @param string|array $messages The message as an array of lines of a single string
+     * @param integer      $size     The size of line
+     * @param integer      $type     The type of output
+     */
+    public function overwriteln($messages, $size = 80, $type = 0)
+    {
+        $this->write($messages, $size, true, $type);
+    }
+
+    /**
+     * Interactively prompts for input without echoing to the terminal.
+     * Requires a bash shell or Windows and won't work with safe_mode
+     * settings (Uses `shell_exec`).
+     *
+     * @param string $title The title of prompt (only for windows)
+     *
+     * @return string The value
+     */
+    public function promptSilent($title = '')
+    {
+        if (preg_match('/^win/i', PHP_OS)) {
+            $vbscript = sys_get_temp_dir() . '/prompt_password.vbs';
+            file_put_contents($vbscript,
+                    'wscript.echo(Inputbox("' . addslashes($title) . '","'
+                            . addslashes($title) . '", ""))');
+            $command = "cscript //nologo " . escapeshellarg($vbscript);
+            $value = rtrim(shell_exec($command));
+            unlink($vbscript);
+            $this->writeln('');
+
+            return $value;
+
+        } else {
+            $command = "/usr/bin/env bash -c 'echo OK'";
+
+            if (rtrim(shell_exec($command)) !== 'OK') {
+                trigger_error("Can't invoke bash");
+                return;
+            }
+
+            $command = "/usr/bin/env bash -c 'read -s mypassword && echo \$mypassword'";
+            $value = rtrim(shell_exec($command));
+            $this->writeln('');
+
+            return $value;
+        }
+    }
+}

+ 5 - 3
src/Composer/Downloader/FileDownloader.php

@@ -20,6 +20,7 @@ use Symfony\Component\Console\Input\InputInterface;
  *
  * @author Kirill chEbba Chebunin <iam@chebba.org>
  * @author Jordi Boggiano <j.boggiano@seld.be>
+ * @author François Pluchino <francois.pluchino@opendisplay.com>
  */
 abstract class FileDownloader implements DownloaderInterface
 {
@@ -65,7 +66,8 @@ abstract class FileDownloader implements DownloaderInterface
 
         $fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.');
 
-        echo 'Downloading '.$url.' to '.$fileName.PHP_EOL;
+        //echo 'Downloading '.$url.' to '.$fileName.PHP_EOL;
+        $this->output->writeln("  - <info>Downloading</info> <comment>" . $package->getName() . "</comment> (" . $package->getPrettyVersion() . ")");
 
         if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
             // bypass https for github if openssl is disabled
@@ -106,11 +108,11 @@ abstract class FileDownloader implements DownloaderInterface
             throw new \UnexpectedValueException('The checksum verification of the archive failed (downloaded from '.$url.')');
         }
 
-        echo 'Unpacking archive'.PHP_EOL;
+        $this->output->writeln('    Unpacking archive');
         $this->extract($fileName, $path);
 
 
-        echo 'Cleaning up'.PHP_EOL;
+        $this->output->writeln('    Cleaning up');
         unlink($fileName);
 
         // If we have only a one dir inside it suppose to be a package itself