Skip to content

Commit 377f5a3

Browse files
author
Allan Paiste
committed
added download retries + support for multiple version queries to support sources where that is a thing (like chromedriver repository)
1 parent 6ae3eed commit 377f5a3

File tree

11 files changed

+196
-22
lines changed

11 files changed

+196
-22
lines changed

LICENSE_VAIMO.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright © 2009-2018 Vaimo Group.
1+
Copyright © 2009-2019 Vaimo Group.
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of
44
this software and associated documentation files (the "Software"), to deal in

bin/analyse

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
: <<'COPYRIGHT'
3+
Copyright (c) Vaimo Group. All rights reserved.
4+
See LICENSE_VAIMO.txt for license details.
5+
COPYRIGHT
6+
7+
type=${1}
8+
9+
if [ "${type}" == "" ] || [ "${type}" == "phpcs" ] ; then
10+
vendor/bin/phpcs -p src
11+
12+
result=${?}
13+
14+
if [ ${result} -gt 0 ] ; then
15+
exit 1
16+
fi
17+
fi
18+
19+
if [ "${type}" == "" ] || [ "${type}" == "phpcpd" ] ; then
20+
vendor/bin/phpcpd src
21+
22+
result=${?}
23+
24+
if [ ${result} -gt 0 ] ; then
25+
exit 1
26+
fi
27+
fi
28+
29+
if [ "${type}" == "" ] || [ "${type}" == "phpmd" ] ; then
30+
vendor/bin/phpmd src text phpmd.xml --suffixes php
31+
32+
result=${?}
33+
34+
if [ ${result} -gt 0 ] ; then
35+
exit 1
36+
fi
37+
fi

bin/bootstrap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
: <<'COPYRIGHT'
3+
Copyright (c) Vaimo Group. All rights reserved.
4+
See LICENSE_VAIMO.txt for license details.
5+
COPYRIGHT
6+
7+
vendor/bin/phpcs --config-set installed_paths 'vendor/phpcompatibility/php-compatibility'
8+
vendor/bin/phpcs --config-set error_severity 1
9+
vendor/bin/phpcs --config-set default_standard 'LOCAL'

bin/normalise

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
: <<'COPYRIGHT'
3+
Copyright (c) Vaimo Group. All rights reserved.
4+
See LICENSE_VAIMO.txt for license details.
5+
COPYRIGHT
6+
7+
vendor/bin/phpcbf -p src
8+
9+
exit 0

changelog.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
{
2+
"_readme": [
3+
"The contents of this file are used to generate CHANGELOG.md; It's kept in JSON/parsable format to make it",
4+
"possible to generate change-logs in other formats as well (when needed) and to do automatic releases based on",
5+
"added change-log records. More on how to use it: https://github.com/vaimo/composer-changelogs"
6+
],
7+
"2.1.0": {
8+
"feature": [
9+
"allow multiple version checking url's to cater for download sources that might have different mechanisms for fetching information about latest driver versions",
10+
"new path variables for having partial version in file names: major (X), major-minor (X.Y)",
11+
"retry driver download when download failed for some reason (2 retries)"
12+
],
13+
"maintenance": [
14+
"introduced the use of Static Code Analysis tools",
15+
"code downgraded so to make the package installable on relatively old php versions"
16+
]
17+
},
218
"2.0.0": {
319
"breaking": [
4-
"(config) version polling output parser switched to use refex pattern matching instead of relying on sscanf"
20+
"(config) version polling output parser switched to use RegEx pattern matching instead of relying on sscanf"
521
],
622
"feature": [
723
"allow driver version polling to use version aliases (required for some drivers that use multiple versioning schemes (like EdgeDriver))",

composer.json

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,36 @@
44
"license": "MIT",
55
"description": "General-issue library that enables building any kind of WebDriver binary downloader",
66
"require": {
7+
"php": ">=5.3.0",
78
"composer/composer": "^1.0",
89
"composer-plugin-api": "^1.0"
910
},
11+
"require-dev": {
12+
"composer/composer": "^1.0.0",
13+
"vaimo/composer-changelogs": "^0.15.4",
14+
"squizlabs/php_codesniffer": "^2.9.2",
15+
"phpcompatibility/php-compatibility": "^9.1.1",
16+
"phpmd/phpmd": "^2.6.0",
17+
"phpunit/phpunit": "^4.8.36",
18+
"sebastian/phpcpd": "^1.4.3"
19+
},
20+
"config": {
21+
"platform": {
22+
"php": "5.3.9"
23+
}
24+
},
25+
"scripts-descriptions": {
26+
"test": "Run tests",
27+
"code:analyse": "Run static code analysis for the source code",
28+
"code:normalise": "Apply automatic fixes to the code based on the static code analysis (where applicable)"
29+
},
30+
"scripts": {
31+
"test": "vendor/bin/phpunit",
32+
"code:analyse": "bin/analyse",
33+
"code:normalise": "bin/normalise",
34+
"post-install-cmd": "bin/bootstrap",
35+
"post-update-cmd" : "bin/bootstrap"
36+
},
1037
"authors": [
1138
{
1239
"name": "Allan Paiste",
@@ -32,7 +59,10 @@
3259
},
3360
"extra": {
3461
"changelog": {
35-
"source": "changelog.json"
62+
"source": "changelog.json",
63+
"output": {
64+
"md": "CHANGELOG.md"
65+
}
3666
}
3767
}
3868
}

phpcs.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Copyright (c) Vaimo Group. All rights reserved.
4+
See LICENSE_VAIMO.txt for license details.
5+
-->
6+
<ruleset name="LOCAL">
7+
<description>My rules for PHP CodeSniffer</description>
8+
9+
<rule ref="PHPCompatibility" />
10+
11+
<config name="testVersion" value="5.3-"/>
12+
13+
<rule ref="PSR1"/>
14+
<rule ref="PSR2"/>
15+
</ruleset>

phpmd.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Copyright (c) Vaimo Group. All rights reserved.
4+
See LICENSE_VAIMO.txt for license details.
5+
-->
6+
<ruleset name="LOCAL">
7+
<rule ref="rulesets/cleancode.xml" />
8+
<rule ref="rulesets/codesize.xml" />
9+
<rule ref="rulesets/design.xml" />
10+
<rule ref="rulesets/naming.xml" />
11+
<rule ref="rulesets/unusedcode.xml" />
12+
<rule ref="rulesets/controversial.xml" />
13+
</ruleset>

src/Analysers/ProjectAnalyser.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,20 @@ public function resolveRequiredDriverVersion()
136136
$this->environmentAnalyser->resolveBrowserVersion()
137137
);
138138

139-
$versionCheckUrl = $requestConfig[ConfigInterface::REQUEST_VERSION];
139+
$versionCheckUrls = $requestConfig[ConfigInterface::REQUEST_VERSION] ?? [];
140+
141+
if (!is_array($versionCheckUrls)) {
142+
$versionCheckUrls = [$versionCheckUrls];
143+
}
144+
145+
foreach ($versionCheckUrls as $versionCheckUrl) {
146+
if (!$version) {
147+
break;
148+
}
140149

141-
if (!$version && $versionCheckUrl) {
142-
$version = trim(@file_get_contents($versionCheckUrl));
150+
$version = trim(
151+
@file_get_contents($versionCheckUrl)
152+
);
143153
}
144154

145155
if (!$version) {

src/Installer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function __construct(
4040

4141
public function executeWithConfig(ConfigInterface $pluginConfig)
4242
{
43-
$binaryDir = $this->composerRuntime->getConfig()->get('bin-dir');
43+
$composerConfig = $this->composerRuntime->getConfig();
44+
45+
$binaryDir = $composerConfig->get('bin-dir');
4446

4547
$projectAnalyser = new \Vaimo\WebDriverBinaryDownloader\Analysers\ProjectAnalyser($pluginConfig);
4648
$packageManager = new \Vaimo\WebDriverBinaryDownloader\Installer\PackageManager($pluginConfig);
@@ -91,7 +93,7 @@ public function executeWithConfig(ConfigInterface $pluginConfig)
9193
);
9294

9395
try {
94-
$package = $downloadManager->downloadRelease([$version]);
96+
$package = $downloadManager->downloadRelease([$version], 2);
9597
} catch (\Exception $exception) {
9698
$this->io->write(
9799
sprintf('<error>%s</error>', $exception->getMessage())
@@ -114,7 +116,9 @@ public function executeWithConfig(ConfigInterface $pluginConfig)
114116

115117
private function createCacheManager($cacheName)
116118
{
117-
$cacheDir = $this->composerRuntime->getConfig()->get('cache-dir');
119+
$composerConfig = $this->composerRuntime->getConfig();
120+
121+
$cacheDir = $composerConfig->get('cache-dir');
118122

119123
return new \Composer\Cache(
120124
$this->io,

src/Installer/DownloadManager.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Vaimo\WebDriverBinaryDownloader\Installer;
77

8+
use Composer\Package\PackageInterface;
89
use Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface;
910

1011
class DownloadManager
@@ -67,7 +68,7 @@ public function __construct(
6768
$this->utils = new \Vaimo\WebDriverBinaryDownloader\Installer\Utils();
6869
}
6970

70-
public function downloadRelease(array $versions)
71+
public function downloadRelease(array $versions, $retryCount = 0)
7172
{
7273
$targetDir = $this->utils->composePath(
7374
rtrim($this->cacheManager->getRoot(), DIRECTORY_SEPARATOR),
@@ -78,16 +79,7 @@ public function downloadRelease(array $versions)
7879
$package = $this->createComposerVirtualPackage($version, $targetDir);
7980

8081
try {
81-
/** @var \Composer\Downloader\DownloaderInterface $downloader */
82-
$downloader = $this->downloadManager->getDownloaderForInstalledPackage($package);
83-
84-
/**
85-
* Some downloaders have the option to mute the output, which is why
86-
* there the third call argument.
87-
*/
88-
$downloader->download($package, $targetDir, false);
89-
90-
return $package;
82+
return $this->downloadPackage($package, $targetDir, $retryCount);
9183
} catch (\Composer\Downloader\TransportException $exception) {
9284
if ($exception->getStatusCode() === 404 && $versions) {
9385
continue;
@@ -115,6 +107,29 @@ public function downloadRelease(array $versions)
115107
throw new \Exception('Failed to download requested driver');
116108
}
117109

110+
private function downloadPackage(PackageInterface $package, $targetDir, $retryCount = 0)
111+
{
112+
do {
113+
try {
114+
/** @var \Composer\Downloader\DownloaderInterface $downloader */
115+
$downloader = $this->downloadManager->getDownloaderForInstalledPackage($package);
116+
117+
/**
118+
* Some downloader types have the option to mute the output,
119+
* which is why there is the third call argument (not present
120+
* in interface footprint).
121+
*/
122+
$downloader->download($package, $targetDir, false);
123+
} catch (\Exception $exception) {
124+
if (!$retryCount) {
125+
throw $exception;
126+
}
127+
}
128+
} while ($retryCount-- > 0);
129+
130+
return $package;
131+
}
132+
118133
private function createComposerVirtualPackage($version, $targetDir)
119134
{
120135
$remoteFile = $this->getDownloadUrl($version);
@@ -181,15 +196,31 @@ private function getDownloadUrl($version)
181196
$driverHashes = $this->pluginConfig->getDriverVersionHashMap();
182197

183198
$fileHash = $driverHashes[$version] ?? '';
199+
200+
$variables = [
201+
'version' => $version,
202+
'hash' => $fileHash,
203+
'major' => $this->extractPartialVersion($version, 1),
204+
'major-minor' => $this->extractPartialVersion($version, 2)
205+
];
184206

185207
$fileName = $this->utils->stringFromTemplate(
186208
$remoteFiles[$platformCode],
187-
['version' => $version, 'hash' => $fileHash]
209+
$variables
188210
);
189211

190212
return $this->utils->stringFromTemplate(
191213
$requestConfig[ConfigInterface::REQUEST_DOWNLOAD],
192-
['version' => $version, 'file' => $fileName, 'hash' => $fileHash]
214+
array_replace($variables, ['file' => $fileName])
215+
);
216+
}
217+
218+
private function extractPartialVersion($version, $segmentCount)
219+
{
220+
return substr(
221+
$version,
222+
0,
223+
strpos($version, '.', $segmentCount) ?: strlen($version)
193224
);
194225
}
195226
}

0 commit comments

Comments
 (0)