-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Single responsibility and new binary script #1
Open
Chofoteddy
wants to merge
3
commits into
tecnocen-com:master
Choose a base branch
from
Chofoteddy:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,25 @@ | ||
<?php | ||
|
||
use CodeceptionSkeleton\Installer; | ||
use Composer\Script\Event; | ||
|
||
class ComposerListener | ||
{ | ||
const DEFAULT_SELENIUM_VERSION = '3.10.0'; | ||
const DEFAULT_GECKODRIVER_VERSION = '0.19.1'; | ||
const DEFAULT_CHROMEDRIVER_VERSION = '2.35'; | ||
const SELENIUM_FILENAME = 'selenium-ss.jar'; | ||
const GECKODRIVER_FILENAME = 'geckodriver.tar.gz'; | ||
const CHROMEDRIVER_FILENAME = 'chromedriver.zip'; | ||
|
||
const SUPPORTED_OPERATIVE_SYSTEMS = [ | ||
'linux32', | ||
'linux64', | ||
'mac64', | ||
'win32', | ||
]; | ||
|
||
/** | ||
* @param Event $event | ||
*/ | ||
public static function downloadSelenium(Event $event) | ||
{ | ||
$args = self::parseArguments($event->getArguments()); | ||
self::downloadFile( | ||
self::SELENIUM_FILENAME, | ||
self::buildSeleniumUrl( | ||
$args['selenium'] ?? self::DEFAULT_SELENIUM_VERSION | ||
) | ||
); | ||
$args = Installer::parseArguments($event->getArguments()); | ||
Installer::downloadSelenium($args); | ||
} | ||
|
||
/** | ||
* @param Event $event | ||
*/ | ||
public static function downloadDrivers(Event $event) | ||
{ | ||
$args = self::parseArguments($event->getArguments()); | ||
$os = $args['os'] ?? self::determineOS(); | ||
$gdVersion = $args['gdVersion'] ?? self::DEFAULT_GECKODRIVER_VERSION; | ||
$cdVersion = $args['cdVersion'] ?? self::DEFAULT_CHROMEDRIVER_VERSION; | ||
if (!in_array($os, self::SUPPORTED_OPERATIVE_SYSTEMS)) { | ||
$event->stopPropagation(); | ||
echo "Provided OS '$os' is not supported.\n"; | ||
exit(1); | ||
} | ||
self::downloadFile( | ||
self::GECKODRIVER_FILENAME, | ||
self::buildGeckoDriverUrl($os, $gdVersion) | ||
); | ||
self::downloadFile( | ||
self::CHROMEDRIVER_FILENAME, | ||
self::buildChromeDriverUrl($os, $cdVersion) | ||
); | ||
} | ||
|
||
private static function determineOS(): string | ||
{ | ||
return strtolower(php_uname('s')) . (8 * PHP_INT_SIZE); | ||
} | ||
|
||
private static function buildGeckoDriverUrl( | ||
string $os, | ||
string $version | ||
): string { | ||
return 'https://github.com/mozilla/geckodriver/releases/download/' | ||
. "v{$version}/geckodriver-v{$version}-{$os}.tar.gz"; | ||
} | ||
|
||
private static function buildChromeDriverUrl( | ||
string $os, | ||
string $version | ||
): string { | ||
return "https://chromedriver.storage.googleapis.com/{$version}/" | ||
. "chromedriver_{$os}.zip"; | ||
} | ||
|
||
private static function buildSeleniumUrl(string $version): string | ||
{ | ||
return 'http://selenium-release.storage.googleapis.com/' | ||
. substr($version, 0, strpos($version, '.', 2)) | ||
. "/selenium-server-standalone-{$version}.jar"; | ||
$args = Installer::parseArguments($event->getArguments()); | ||
Installer::downloadDrivers($args); | ||
} | ||
|
||
private static function downloadFile($filename, $url) | ||
{ | ||
echo "Downloading $url into $filename.\n"; | ||
stream_copy_to_stream( | ||
fopen($url, 'r'), | ||
fopen($filename, 'w') | ||
); | ||
} | ||
|
||
/** | ||
* Arguments in string are parsed to key-pair values. | ||
* | ||
* ```php | ||
* $args = self::parseArguments([ | ||
* 'opt=value', | ||
* 'foo=bar' | ||
* ]); | ||
* | ||
* print_r($args); | ||
* ``` | ||
* | ||
* The output will be: | ||
* | ||
* > Array | ||
* > ( | ||
* > [opt] => value | ||
* > [foo] => bar | ||
* > ) | ||
* | ||
* @param array[] $args | ||
* @return array[] | ||
*/ | ||
public static function parseArguments(array $args) | ||
{ | ||
$parsed = []; | ||
foreach ($args as $arg) { | ||
$parse = explode('=', $arg); | ||
if (!isset($parse[1])) { | ||
$parsed[$parse[0]] = true; | ||
} else { | ||
$parsed[$parse[0]] = $parse[1]; | ||
} | ||
} | ||
|
||
return $parsed; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/php | ||
<?php | ||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
use CodeceptionSkeleton\Installer; | ||
use CodeceptionSkeleton\WebFile\{ | ||
SeleniumDriver, | ||
ChromeDriver, | ||
GeckoDriver | ||
}; | ||
|
||
$cmd = $argv[0]; | ||
|
||
if ($argc === 1 || in_array('-h', $argv)) { | ||
usage(); | ||
exit(1); | ||
} | ||
|
||
if (!is_writable(__DIR__)) { | ||
echo 'Required writing permissions'; | ||
exit(1); | ||
} | ||
|
||
if (in_array('-i', $argv)) { | ||
$options = array_merge([ | ||
'os' => null, | ||
'selenium' => null, | ||
'gecko' => null, | ||
'chrome' => null, | ||
], getopt('', ['os:', 'selenium:', 'gecko:', 'chrome:'])); | ||
|
||
foreach ($options as $option => $value) { | ||
switch ($option) { | ||
case 'os': | ||
Installer::downloadDrivers(array_merge([ | ||
'gdVersion' => $options['gecko'], | ||
'cdVersion' => $options['chrome'], | ||
], $options)); | ||
|
||
extractDrivers(); | ||
flushTempFiles(); | ||
break; | ||
|
||
case 'selenium': | ||
Installer::downloadSelenium($options); | ||
break; | ||
} | ||
} | ||
|
||
} | ||
|
||
function extractDrivers() | ||
{ | ||
$zip = new ZipArchive(); | ||
if ($zip->open(ChromeDriver::$fileName)) { | ||
$zip->extractTo(__DIR__); | ||
$zip->close(); | ||
} | ||
|
||
$gzip = new PharData(__DIR__ .'/'. GeckoDriver::$fileName); | ||
$gzip->extractTo(__DIR__); | ||
} | ||
|
||
function flushTempFiles() { | ||
unlink(__DIR__.'/'.GeckoDriver::$fileName); | ||
unlink(__DIR__.'/'.ChromeDriver::$fileName); | ||
} | ||
|
||
function usage() | ||
{ | ||
global $cmd; | ||
|
||
echo <<<EOL | ||
\033[1;31mNAME \033[0m | ||
\033[0;32m$cmd \033[0m- Skeleton to create codeception tests using for external projects | ||
|
||
\033[1;31mSYNOPSIS \033[0m | ||
\033[0;32m$cmd \033[0m-i [--os OS] [--selenium VERSION] | ||
|
||
\033[1;31mDESCRIPTION \033[0m | ||
\033[0;32m$cmd \033[0mwill install the php dependencies using composer and download "selenium standalone" and the supported | ||
webdrivers in the files: selenium-ss.jar, geckodriver and chromedriver. | ||
|
||
\033[1;31mEXAMPLES \033[0m | ||
\033[0;32m$cmd \033[0m-i --os linux64 | ||
Download the drivers for GNU/Linux 32 version. | ||
|
||
\033[0;32m$cmd \033[0m-i --selenium 3.11.0 | ||
Download Selenium Standalone Server 3.11.0. | ||
|
||
EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why put this as a binary file? it wont be executed on third party repos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Chofoteddy answer back asap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason: is to be able to use it as both (library or binary). The need arose because I wanted to use the functionality offered by the library, but I could not execute it from composer unless it was binary. Example: Codeception has its library and binary at the same time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The deployer is only meant to be used in the skeleton, it wont work as a library.
So I dont understand how you intend to use the binary.
Codeception is a library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Codeception is a library, with a binary that supports the construction of the initial skeleton: the same purpose as this pull-request.
https://github.com/Codeception/Codeception/blob/2.4/composer.json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok it seems the use case for bin files is not clear.
When someone adds a dependency on a project the 'require' section of composer.json its called a library, said library can include bin files to be executed by the project.
This repository is not a library and is never meant to be included as dependency in the 'require' section of any project. This repository is meant to be used as the skeleton for a project. So there is no use for any bin file here.