Skip to content

Commit

Permalink
Merge pull request #66 from tm1000/feature/add-progress-bar
Browse files Browse the repository at this point in the history
Add Progress Bar to upload output
  • Loading branch information
Elendev authored Jun 27, 2022
2 parents 1ddd2d1 + 206b18d commit 992ed87
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"phpunit/phpunit": "^8 || ^9 || ^10",
"composer/composer": "^1.8 || ^2.0"
},
"scripts": {
"test": "phpunit tests",
"cs-fixer": "php-cs-fixer fix src"

},
"replace": {
"elendev/nexus-composer-push": "*"
},
Expand Down
2 changes: 1 addition & 1 deletion src/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->getIO()
->write(
'Execute the push for the URL ' . $provider->getUrl() . '...',
'Pushing archive to URL: ' . $provider->getUrl() . '...',
true
);

Expand Down
52 changes: 52 additions & 0 deletions src/RepositoryProvider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ abstract class AbstractProvider
*/
private $io;

/**
* @var \Symfony\Component\Console\Helper\ProgressBar|null
*/
private $progress = null;

public function __construct(Configuration $configuration, IOInterface $io, Client $client = null)
{
$this->configuration = $configuration;
$this->io = $io;
$this->client = $client;
if (method_exists($io, 'getProgressBar')) {
$this->progress = $io->getProgressBar();
}
}

/**
Expand Down Expand Up @@ -228,4 +236,48 @@ protected function getClient()
}
return $this->client;
}

/**
* Return the Progress Callback for Guzzle
*
* @return callback
*/
protected function getProgressCallback()
{
if ($this->progress === null) {
return function (
$downloadTotal,
$downloadedBytes,
$uploadTotal,
$uploadedBytes
) {
//Do nothing
};
}
return function (
$downloadTotal,
$downloadedBytes,
$uploadTotal,
$uploadedBytes
) {
if ($uploadTotal === 0) {
return;
}
if ($uploadedBytes === 0) {
$this->progress->start(100);
return;
}

if ($uploadedBytes === $uploadTotal) {
if ($this->progress->getProgress() != 100) {
$this->progress->setProgress(100);
$this->progress->finish();
$this->getIO()->write('');
}
return;
}

$this->progress->setProgress(($uploadedBytes / $uploadTotal) * 100);
};
}
}
1 change: 1 addition & 0 deletions src/RepositoryProvider/ArtifactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected function apiCall($file, $options)
{
$options['debug'] = $this->getIO()->isVeryVerbose();
$options['body'] = fopen($file, 'r');
$options['progress'] = $this->getProgressCallback();
$url = $this->getUrl() . '.' . pathinfo($file, PATHINFO_EXTENSION) . '?properties=composer.version=' . $this->getConfiguration()->getVersion();
$this->getClient()->request('PUT', $url, $options);
}
Expand Down
2 changes: 2 additions & 0 deletions src/RepositoryProvider/NexusProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ protected function apiCall($file, $options)
$options['body'] = fopen($file, 'r');
}

$options['progress'] = $this->getProgressCallback();

$this->getClient()->request('PUT', $url, $options);
}
}

0 comments on commit 992ed87

Please sign in to comment.