Skip to content

Commit 93ae49a

Browse files
Refactor message on signle lines and using colors. Also take the --no-progress option into account.
1 parent de372a4 commit 93ae49a

File tree

5 files changed

+76
-25
lines changed

5 files changed

+76
-25
lines changed

src/FileFetcher.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,40 @@ class FileFetcher {
2323
*/
2424
protected $io;
2525

26+
/**
27+
* @var bool
28+
*
29+
* A boolean indicating if progress should be displayed.
30+
*/
31+
protected $progress;
32+
2633
protected $source;
2734
protected $filenames;
2835
protected $fs;
2936

30-
public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = [], IOInterface $io) {
37+
public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = [], IOInterface $io, $progress = TRUE) {
3138
$this->remoteFilesystem = $remoteFilesystem;
3239
$this->io = $io;
3340
$this->source = $source;
3441
$this->filenames = $filenames;
3542
$this->fs = new Filesystem();
43+
$this->progress = $progress;
3644
}
3745

3846
public function fetch($version, $destination) {
3947
array_walk($this->filenames, function ($filename) use ($version, $destination) {
4048
$url = $this->getUri($filename, $version);
4149
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
42-
$this->io->write("Going to download the file $filename");
43-
$this->io->write(" from: $url");
44-
$this->io->write(" to: $destination/$filename");
45-
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename);
46-
// Used to put a new line because the remote file system does not put
47-
// one.
48-
$this->io->write('');
50+
if ($this->progress) {
51+
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
52+
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename, $this->progress);
53+
// Used to put a new line because the remote file system does not put
54+
// one.
55+
$this->io->writeError('');
56+
}
57+
else {
58+
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename, $this->progress);
59+
}
4960
});
5061
}
5162

src/Handler.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class Handler {
3232
*/
3333
protected $io;
3434

35+
/**
36+
* @var bool
37+
*
38+
* A boolean indicating if progress should be displayed.
39+
*/
40+
protected $progress;
41+
3542
/**
3643
* @var \Composer\Package\PackageInterface
3744
*/
@@ -46,6 +53,7 @@ class Handler {
4653
public function __construct(Composer $composer, IOInterface $io) {
4754
$this->composer = $composer;
4855
$this->io = $io;
56+
$this->progress = TRUE;
4957
}
5058

5159
/**
@@ -65,6 +73,15 @@ protected function getCorePackage($operation) {
6573
return NULL;
6674
}
6775

76+
/**
77+
* Get the command options.
78+
*
79+
* @param \Composer\Plugin\CommandEvent $event
80+
*/
81+
public function onCmdBeginsEvent(\Composer\Plugin\CommandEvent $event) {
82+
$this->progress = !($event->getInput()->getOption('no-progress'));
83+
}
84+
6885
/**
6986
* Marks scaffolding to be processed after an install or update command.
7087
*
@@ -113,10 +130,10 @@ public function downloadScaffold() {
113130

114131
$remoteFs = new RemoteFilesystem($this->io);
115132

116-
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->composer->getConfig());
133+
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->progress, $this->composer->getConfig());
117134
$fetcher->fetch($version, $webroot);
118135

119-
$initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial(), $this->io);
136+
$initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial(), $this->io, $this->progress);
120137
$initialFileFetcher->fetch($version, $webroot);
121138

122139
// Call post-scaffold scripts.

src/InitialFileFetcher.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ public function fetch($version, $destination) {
1515
if (!file_exists($target)) {
1616
$url = $this->getUri($sourceFilename, $version);
1717
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
18-
$this->io->write("Going to download the file $filename");
19-
$this->io->write(" from: $url");
20-
$this->io->write(" to: $target");
21-
$this->remoteFilesystem->copy($url, $url, $target);
22-
// Used to put a new line because the remote file system does not put
23-
// one.
24-
$this->io->write('');
18+
if ($this->progress) {
19+
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
20+
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
21+
// Used to put a new line because the remote file system does not put
22+
// one.
23+
$this->io->writeError('');
24+
}
25+
else {
26+
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
27+
}
2528
}
2629
});
2730
}

src/Plugin.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Composer\Installer\PackageEvent;
1212
use Composer\Installer\PackageEvents;
1313
use Composer\IO\IOInterface;
14+
use Composer\Plugin\CommandEvent;
15+
use Composer\Plugin\PluginEvents;
1416
use Composer\Plugin\PluginInterface;
1517
use Composer\Script\ScriptEvents;
1618

@@ -45,9 +47,19 @@ public static function getSubscribedEvents() {
4547
//PackageEvents::POST_PACKAGE_UNINSTALL => 'postPackage',
4648
//ScriptEvents::POST_INSTALL_CMD => 'postCmd',
4749
ScriptEvents::POST_UPDATE_CMD => 'postCmd',
50+
PluginEvents::COMMAND => 'cmdBegins',
4851
);
4952
}
5053

54+
/**
55+
* Command begins event callback.
56+
*
57+
* @param \Composer\Plugin\CommandEvent $event
58+
*/
59+
public function cmdBegins(\Composer\Plugin\CommandEvent $event) {
60+
$this->handler->onCmdBeginsEvent($event);
61+
}
62+
5163
/**
5264
* Post package event behaviour.
5365
*

src/PrestissimoFileFetcher.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class PrestissimoFileFetcher extends FileFetcher {
1919
*/
2020
protected $config;
2121

22-
public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, Config $config) {
23-
parent::__construct($remoteFilesystem, $source, $filenames, $io);
22+
public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, $progress = TRUE, Config $config) {
23+
parent::__construct($remoteFilesystem, $source, $filenames, $io, $progress);
2424
$this->config = $config;
2525
}
2626

@@ -37,10 +37,16 @@ protected function fetchWithPrestissimo($version, $destination) {
3737
array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) {
3838
$url = $this->getUri($filename, $version);
3939
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
40-
$this->io->write("Going to download the file $filename");
41-
$this->io->write(" from: $url");
42-
$requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config);
43-
$this->io->write(" to: $destination/$filename");
40+
if ($this->progress) {
41+
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
42+
$requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config);
43+
// Used to put a new line because the remote file system does not put
44+
// one.
45+
$this->io->writeError('');
46+
}
47+
else {
48+
$requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config);
49+
}
4450
});
4551

4652
$successCnt = $failureCnt = 0;
@@ -54,8 +60,10 @@ protected function fetchWithPrestissimo($version, $destination) {
5460
$result = $multi->getFinishedResults();
5561
$successCnt += $result['successCnt'];
5662
$failureCnt += $result['failureCnt'];
57-
foreach ($result['urls'] as $url) {
58-
$this->io->writeError(" <comment>$successCnt/$totalCnt</comment>:\t$url", true, \Composer\IO\IOInterface::VERBOSE);
63+
if ($this->progress) {
64+
foreach ($result['urls'] as $url) {
65+
$this->io->writeError(" <comment>$successCnt/$totalCnt</comment>:\t$url", true, \Composer\IO\IOInterface::VERBOSE);
66+
}
5967
}
6068
} while ($multi->remain());
6169
}

0 commit comments

Comments
 (0)