Skip to content

Commit e424668

Browse files
Refactor message on signle lines and using colors. Also take the --no-progress option into account.
1 parent 96a1726 commit e424668

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
@@ -33,6 +33,13 @@ class Handler {
3333
*/
3434
protected $io;
3535

36+
/**
37+
* @var bool
38+
*
39+
* A boolean indicating if progress should be displayed.
40+
*/
41+
protected $progress;
42+
3643
/**
3744
* @var \Composer\Package\PackageInterface
3845
*/
@@ -47,6 +54,7 @@ class Handler {
4754
public function __construct(Composer $composer, IOInterface $io) {
4855
$this->composer = $composer;
4956
$this->io = $io;
57+
$this->progress = TRUE;
5058
}
5159

5260
/**
@@ -66,6 +74,15 @@ protected function getCorePackage($operation) {
6674
return NULL;
6775
}
6876

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

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

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

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

123140
// 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)