Skip to content

Commit 215ffb3

Browse files
FlorentTorregrosawebflo
authored andcommitted
Display information when downloading a file. (#65)
1 parent 745f0a2 commit 215ffb3

File tree

6 files changed

+107
-58
lines changed

6 files changed

+107
-58
lines changed

src/FileFetcher.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace DrupalComposer\DrupalScaffold;
99

10+
use Composer\IO\IOInterface;
1011
use Composer\Util\Filesystem;
1112
use Composer\Util\RemoteFilesystem;
1213

@@ -17,23 +18,52 @@ class FileFetcher {
1718
*/
1819
protected $remoteFilesystem;
1920

21+
/**
22+
* @var \Composer\IO\IOInterface
23+
*/
24+
protected $io;
25+
26+
/**
27+
* @var bool
28+
*
29+
* A boolean indicating if progress should be displayed.
30+
*/
31+
protected $progress;
32+
2033
protected $source;
2134
protected $filenames;
2235
protected $fs;
2336

24-
public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = []) {
37+
public function __construct(RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE) {
2538
$this->remoteFilesystem = $remoteFilesystem;
39+
$this->io = $io;
2640
$this->source = $source;
27-
$this->filenames = $filenames;
2841
$this->fs = new Filesystem();
42+
$this->progress = $progress;
2943
}
3044

31-
public function fetch($version, $destination) {
32-
array_walk($this->filenames, function ($filename) use ($version, $destination) {
33-
$url = $this->getUri($filename, $version);
34-
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
35-
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename);
36-
});
45+
public function fetch($version, $destination, $override) {
46+
foreach ($this->filenames as $sourceFilename => $filename) {
47+
$target = "$destination/$filename";
48+
if ($override || !file_exists($target)) {
49+
$url = $this->getUri($sourceFilename, $version);
50+
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
51+
if ($this->progress) {
52+
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
53+
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
54+
// Used to put a new line because the remote file system does not put
55+
// one.
56+
$this->io->writeError('');
57+
}
58+
else {
59+
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
60+
}
61+
}
62+
}
63+
}
64+
65+
public function setFilenames(array $filenames) {
66+
$this->filenames = $filenames;
3767
}
3868

3969
protected function getUri($filename, $version) {

src/Handler.php

Lines changed: 27 additions & 4 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,20 @@ 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+
if ($event->getInput()->hasOption('no-progress')) {
84+
$this->progress = !($event->getInput()->getOption('no-progress'));
85+
}
86+
else {
87+
$this->progress = TRUE;
88+
}
89+
}
90+
6991
/**
7092
* Marks scaffolding to be processed after an install or update command.
7193
*
@@ -114,11 +136,12 @@ public function downloadScaffold() {
114136

115137
$remoteFs = new RemoteFilesystem($this->io);
116138

117-
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->composer->getConfig());
118-
$fetcher->fetch($version, $webroot);
139+
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $this->io, $this->progress, $this->composer->getConfig());
140+
$fetcher->setFilenames(array_combine($files, $files));
141+
$fetcher->fetch($version, $webroot, true);
119142

120-
$initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial());
121-
$initialFileFetcher->fetch($version, $webroot);
143+
$fetcher->setFilenames($this->getInitial());
144+
$fetcher->fetch($version, $webroot, false);
122145

123146
// Call post-scaffold scripts.
124147
$dispatcher->dispatch(self::POST_DRUPAL_SCAFFOLD_CMD);

src/InitialFileFetcher.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

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: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,35 @@
1414

1515
class PrestissimoFileFetcher extends FileFetcher {
1616

17-
/**
18-
* @var \Composer\IO\IOInterface
19-
*/
20-
protected $io;
21-
2217
/**
2318
* @var \Composer\Config
2419
*/
2520
protected $config;
2621

27-
public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, Config $config) {
28-
parent::__construct($remoteFilesystem, $source, $filenames);
29-
$this->io = $io;
22+
public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE, Config $config) {
23+
parent::__construct($remoteFilesystem, $source, $io, $progress);
3024
$this->config = $config;
3125
}
3226

33-
public function fetch($version, $destination) {
27+
public function fetch($version, $destination, $override) {
3428
if (class_exists(CurlMulti::class)) {
35-
$this->fetchWithPrestissimo($version, $destination);
29+
$this->fetchWithPrestissimo($version, $destination, $override);
3630
return;
3731
}
38-
parent::fetch($version, $destination);
32+
parent::fetch($version, $destination, $override);
3933
}
4034

41-
protected function fetchWithPrestissimo($version, $destination) {
35+
protected function fetchWithPrestissimo($version, $destination, $override) {
4236
$requests = [];
43-
array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) {
44-
$url = $this->getUri($filename, $version);
45-
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
46-
$requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config);
47-
});
37+
38+
foreach ($this->filenames as $sourceFilename => $filename) {
39+
$target = "$destination/$filename";
40+
if ($override || !file_exists($target)) {
41+
$url = $this->getUri($sourceFilename, $version);
42+
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
43+
$requests[] = new CopyRequest($url, $target, false, $this->io, $this->config);
44+
}
45+
}
4846

4947
$successCnt = $failureCnt = 0;
5048
$totalCnt = count($requests);
@@ -57,8 +55,10 @@ protected function fetchWithPrestissimo($version, $destination) {
5755
$result = $multi->getFinishedResults();
5856
$successCnt += $result['successCnt'];
5957
$failureCnt += $result['failureCnt'];
60-
foreach ($result['urls'] as $url) {
61-
$this->io->writeError(" <comment>$successCnt/$totalCnt</comment>:\t$url", true, \Composer\IO\IOInterface::VERBOSE);
58+
if ($this->progress) {
59+
foreach ($result['urls'] as $url) {
60+
$this->io->writeError(" - Downloading <comment>$successCnt</comment>/<comment>$totalCnt</comment>: <info>$url</info>", true);
61+
}
6262
}
6363
} while ($multi->remain());
6464
}

tests/FetcherTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,22 @@ protected function ensureDirectoryExistsAndClear($directory) {
6363
}
6464

6565
public function testFetch() {
66-
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', ['.htaccess', 'sites/default/default.settings.php']);
67-
$fetcher->fetch('8.1.1', $this->tmpDir);
66+
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO());
67+
$fetcher->setFilenames([
68+
'.htaccess' => '.htaccess',
69+
'sites/default/default.settings.php' => 'sites/default/default.settings.php',
70+
]);
71+
$fetcher->fetch('8.1.1', $this->tmpDir, true);
6872
$this->assertFileExists($this->tmpDir . '/.htaccess');
6973
$this->assertFileExists($this->tmpDir . '/sites/default/default.settings.php');
7074
}
7175

7276
public function testInitialFetch() {
73-
$fetcher = new InitialFileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', ['sites/default/default.settings.php' => 'sites/default/settings.php']);
74-
$fetcher->fetch('8.1.1', $this->tmpDir);
77+
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO());
78+
$fetcher->setFilenames([
79+
'sites/default/default.settings.php' => 'sites/default/settings.php',
80+
]);
81+
$fetcher->fetch('8.1.1', $this->tmpDir, false);
7582
$this->assertFileExists($this->tmpDir . '/sites/default/settings.php');
7683
}
7784
}

0 commit comments

Comments
 (0)