Skip to content

Commit 7d40067

Browse files
author
Allan Paiste
committed
add initial files (ported over from edgedriver) that allow building binary downloaders
0 parents  commit 7d40067

17 files changed

+1023
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Composer template
3+
composer.phar
4+
/vendor/
5+
6+
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
7+
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
8+
# composer.lock
9+
10+
.idea
11+
.DS_Store

LICENSE_VAIMO.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Copyright © 2009-2018 Vaimo Group.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# webdriver-binary-downloader
2+
3+
General-issue library that enables building any kind of WebDriver binary downloader.

changelog.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"1.0.0": {
3+
"feature": [
4+
"allow webdriver binary download based on passed-in configuration"
5+
]
6+
}
7+
}

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "vaimo/webdriver-binary-downloader",
3+
"type": "library",
4+
"license": "MIT",
5+
"description": "General-issue library that enables building any kind of WebDriver binary downloader",
6+
"require": {
7+
"composer/composer": "^1.0",
8+
"composer-plugin-api": "^1.0"
9+
},
10+
"authors": [
11+
{
12+
"name": "Allan Paiste",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"support": {
17+
"source": "https://github.com/vaimo/webdriver-binary-downloader",
18+
"docs": "https://github.com/vaimo/webdriver-binary-downloader",
19+
"issues": "https://github.com/vaimo/webdriver-binary-downloader/issues"
20+
},
21+
"keywords": [
22+
"browser driver",
23+
"webdriver",
24+
"downloader",
25+
"binary downloader",
26+
"binary"
27+
],
28+
"autoload": {
29+
"psr-4": {
30+
"Vaimo\\WebDriverBinaryDownloader\\": "src"
31+
}
32+
},
33+
"extra": {
34+
"changelog": {
35+
"source": "changelog.json"
36+
}
37+
}
38+
}

src/Analysers/EnvironmentAnalyser.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Vaimo Group. All rights reserved.
4+
* See LICENSE_VAIMO.txt for license details.
5+
*/
6+
namespace Vaimo\WebDriverBinaryDownloader\Analysers;
7+
8+
class EnvironmentAnalyser
9+
{
10+
/**
11+
* @var \Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface
12+
*/
13+
private $pluginConfig;
14+
15+
/**
16+
* @var \Vaimo\WebDriverBinaryDownloader\Analysers\PlatformAnalyser
17+
*/
18+
private $platformAnalyser;
19+
20+
/**
21+
* @var \Vaimo\WebDriverBinaryDownloader\Installer\VersionResolver
22+
*/
23+
private $versionResolver;
24+
25+
/**
26+
* @param \Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface $pluginConfig
27+
*/
28+
public function __construct(
29+
\Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface $pluginConfig
30+
) {
31+
$this->pluginConfig = $pluginConfig;
32+
33+
$this->platformAnalyser = new \Vaimo\WebDriverBinaryDownloader\Analysers\PlatformAnalyser();
34+
$this->versionResolver = new \Vaimo\WebDriverBinaryDownloader\Installer\VersionResolver();
35+
}
36+
37+
public function resolveBrowserVersion()
38+
{
39+
$platformCode = $this->platformAnalyser->getPlatformCode();
40+
$binaryPaths = $this->pluginConfig->getBrowserBinaryPaths();
41+
42+
if (!isset($binaryPaths[$platformCode])) {
43+
return '';
44+
}
45+
46+
return $this->versionResolver->pollForVersion(
47+
$binaryPaths[$platformCode],
48+
$this->pluginConfig->getBrowserVersionPollingConfig()
49+
);
50+
}
51+
}

src/Analysers/PackageAnalyser.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Vaimo Group. All rights reserved.
4+
* See LICENSE_VAIMO.txt for license details.
5+
*/
6+
namespace Vaimo\WebDriverBinaryDownloader\Analysers;
7+
8+
use Vaimo\WebDriverBinaryDownloader\Composer\Config as ComposerConfig;
9+
10+
class PackageAnalyser
11+
{
12+
public function isPluginPackage(\Composer\Package\PackageInterface $package)
13+
{
14+
return $package->getType() === ComposerConfig::COMPOSER_PLUGIN_TYPE;
15+
}
16+
17+
public function ownsNamespace(\Composer\Package\PackageInterface $package, $namespace)
18+
{
19+
$autoloadConfig = $package->getAutoload();
20+
21+
return (bool)array_filter(
22+
array_keys($autoloadConfig[ComposerConfig::PSR4_CONFIG] ?? []),
23+
function ($item) use ($namespace) {
24+
return strpos($namespace, rtrim($item, '\\')) === 0;
25+
}
26+
);
27+
}
28+
}

src/Analysers/PlatformAnalyser.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Vaimo Group. All rights reserved.
4+
* See LICENSE_VAIMO.txt for license details.
5+
*/
6+
namespace Vaimo\WebDriverBinaryDownloader\Analysers;
7+
8+
class PlatformAnalyser
9+
{
10+
const TYPE_LINUX32 = 'linux32';
11+
const TYPE_LINUX64 = 'linux64';
12+
const TYPE_WIN32 = 'win32';
13+
const TYPE_WIN64 = 'win64';
14+
const TYPE_MAC64 = 'mac64';
15+
16+
public function getPlatformCode()
17+
{
18+
if (stripos(PHP_OS, 'win') === 0) {
19+
if (PHP_INT_SIZE === 8) {
20+
return self::TYPE_WIN64;
21+
}
22+
23+
return self::TYPE_WIN32;
24+
}
25+
26+
if (stripos(PHP_OS, 'darwin') === 0) {
27+
return self::TYPE_MAC64;
28+
}
29+
30+
if (stripos(PHP_OS, 'linux') === 0) {
31+
if (PHP_INT_SIZE === 8) {
32+
return self::TYPE_LINUX64;
33+
}
34+
35+
return self::TYPE_LINUX32;
36+
}
37+
38+
throw new \Exception('Platform code detection failed');
39+
}
40+
41+
public function getPlatformName()
42+
{
43+
$names = [
44+
self::TYPE_LINUX32 => 'Linux 32Bits',
45+
self::TYPE_LINUX64 => 'Linux 64Bits',
46+
self::TYPE_MAC64 => 'Mac OS X',
47+
self::TYPE_WIN32 => 'Windows 32Bits',
48+
self::TYPE_WIN64 => 'Windows 64Bits'
49+
];
50+
51+
return $names[$this->getPlatformCode()];
52+
}
53+
}

src/Analysers/ProjectAnalyser.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
/**
3+
* Copyright © Vaimo Group. All rights reserved.
4+
* See LICENSE_VAIMO.txt for license details.
5+
*/
6+
namespace Vaimo\WebDriverBinaryDownloader\Analysers;
7+
8+
use Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface;
9+
10+
class ProjectAnalyser
11+
{
12+
/**
13+
* @var \Composer\Package\Version\VersionParser
14+
*/
15+
private $versionParser;
16+
17+
/**
18+
* @var \Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface
19+
*/
20+
private $pluginConfig;
21+
22+
/**
23+
* @var \Vaimo\WebDriverBinaryDownloader\Analysers\EnvironmentAnalyser
24+
*/
25+
private $environmentAnalyser;
26+
27+
/**
28+
* @var \Vaimo\WebDriverBinaryDownloader\Analysers\PlatformAnalyser
29+
*/
30+
private $platformAnalyser;
31+
32+
/**
33+
* @var \Vaimo\WebDriverBinaryDownloader\Installer\VersionResolver
34+
*/
35+
private $versionResolver;
36+
37+
/**
38+
* @var \Vaimo\WebDriverBinaryDownloader\Analysers\PackageAnalyser
39+
*/
40+
private $packageAnalyser;
41+
42+
/**
43+
* @var \Vaimo\WebDriverBinaryDownloader\Installer\Utils
44+
*/
45+
private $utils;
46+
47+
/**
48+
* @var \Composer\Package\CompletePackage
49+
*/
50+
private $ownerPackage;
51+
52+
/**
53+
* @param \Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface $pluginConfig
54+
*/
55+
public function __construct(
56+
\Vaimo\WebDriverBinaryDownloader\Interfaces\ConfigInterface $pluginConfig
57+
) {
58+
$this->pluginConfig = $pluginConfig;
59+
60+
$this->environmentAnalyser = new \Vaimo\WebDriverBinaryDownloader\Analysers\EnvironmentAnalyser($pluginConfig);
61+
62+
$this->versionParser = new \Composer\Package\Version\VersionParser();
63+
64+
$this->platformAnalyser = new \Vaimo\WebDriverBinaryDownloader\Analysers\PlatformAnalyser();
65+
$this->versionResolver = new \Vaimo\WebDriverBinaryDownloader\Installer\VersionResolver();
66+
$this->packageAnalyser = new \Vaimo\WebDriverBinaryDownloader\Analysers\PackageAnalyser();
67+
68+
$this->utils = new \Vaimo\WebDriverBinaryDownloader\Installer\Utils();
69+
}
70+
71+
public function resolvePlatformSupport()
72+
{
73+
$platformCode = $this->platformAnalyser->getPlatformCode();
74+
75+
$fileNames = $this->pluginConfig->getExecutableFileNames();
76+
77+
return (bool)($fileNames[$platformCode] ?? false);
78+
}
79+
80+
public function resolveInstalledDriverVersion($binaryDir)
81+
{
82+
$platformCode = $this->platformAnalyser->getPlatformCode();
83+
84+
$executableNames = $this->pluginConfig->getExecutableFileNames();
85+
$remoteFiles = $this->pluginConfig->getRemoteFileNames();
86+
87+
if (!isset($executableNames[$platformCode], $remoteFiles[$platformCode])) {
88+
throw new \Exception('Failed to resolve a file for the platform. Download driver manually');
89+
}
90+
91+
$executableName = $executableNames[$platformCode];
92+
93+
$driverPath = realpath($this->utils->composePath($binaryDir, $executableName));
94+
95+
return $this->versionResolver->pollForVersion(
96+
[$driverPath],
97+
$this->pluginConfig->getDriverVersionPollingConfig()
98+
);
99+
}
100+
101+
public function resolveRequiredDriverVersion()
102+
{
103+
$preferences = $this->pluginConfig->getPreferences();
104+
$requestConfig = $this->pluginConfig->getRequestUrlConfig();
105+
106+
$version = $preferences['version'];
107+
108+
if (!$preferences['version']) {
109+
$version = $this->resolveBrowserDriverVersion(
110+
$this->environmentAnalyser->resolveBrowserVersion()
111+
);
112+
113+
$versionCheckUrl = $requestConfig[ConfigInterface::REQUEST_VERSION];
114+
115+
if (!$version && $versionCheckUrl) {
116+
$version = trim(@file_get_contents($versionCheckUrl));
117+
}
118+
119+
if (!$version) {
120+
$versionMap = array_filter($this->pluginConfig->getBrowserDriverVersionMap());
121+
$version = reset($versionMap);
122+
}
123+
}
124+
125+
try {
126+
$this->versionParser->parseConstraints($version);
127+
} catch (\UnexpectedValueException $exception) {
128+
throw new \Exception(sprintf('Incorrect version string: "%s"', $version));
129+
}
130+
131+
return $version;
132+
}
133+
134+
private function resolveBrowserDriverVersion($browserVersion)
135+
{
136+
$chromeVersion = $browserVersion;
137+
138+
if (!$chromeVersion) {
139+
return '';
140+
}
141+
142+
$majorVersion = strtok($chromeVersion, '.');
143+
144+
$driverVersionMap = $this->pluginConfig->getBrowserDriverVersionMap();
145+
146+
foreach ($driverVersionMap as $browserMajor => $driverVersion) {
147+
if ($majorVersion < $browserMajor) {
148+
continue;
149+
}
150+
151+
return $driverVersion;
152+
}
153+
154+
return '';
155+
}
156+
157+
public function resolvePackageForNamespace(array $packages, $namespace)
158+
{
159+
if ($this->ownerPackage === null) {
160+
foreach ($packages as $package) {
161+
if (!$this->packageAnalyser->isPluginPackage($package)) {
162+
continue;
163+
}
164+
165+
if (!$this->packageAnalyser->ownsNamespace($package, $namespace)) {
166+
continue;
167+
}
168+
169+
$this->ownerPackage = $package;
170+
171+
break;
172+
}
173+
}
174+
175+
if (!$this->ownerPackage) {
176+
throw new \Exception('Failed to detect the plugin package');
177+
}
178+
179+
return $this->ownerPackage;
180+
}
181+
}

0 commit comments

Comments
 (0)