Skip to content

Commit

Permalink
Refactor scripts and fix reading php-version
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Dec 22, 2019
1 parent a507be7 commit 935e74f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 135 deletions.
26 changes: 23 additions & 3 deletions __tests__/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jest.mock('../src/install', () => ({
async (): Promise<string> => {
const os_version: string = process.env['RUNNER_OS'] || '';
let version: string = process.env['php-version'] || '';
version = version.length > 1 ? version : version + '.0';
version = version.length > 1 ? version.slice(0, 3) : version + '.0';
let script = '';
switch (os_version) {
case 'darwin':
Expand Down Expand Up @@ -69,14 +69,14 @@ jest.mock('../src/install', () => ({
* @param coverage_driver
*/
function setEnv(
version: string,
version: string | number,
os: string,
extension_csv: string,
ini_values_csv: string,
coverage_driver: string,
pecl: string
): void {
process.env['php-version'] = version;
process.env['php-version'] = version.toString();
process.env['RUNNER_OS'] = os;
process.env['extensions'] = extension_csv;
process.env['ini-values'] = ini_values_csv;
Expand Down Expand Up @@ -150,4 +150,24 @@ describe('Install', () => {
expect(script).toContain('set coverage driver');
expect(script).toContain('sh script.sh 7.3 ' + __dirname);
});

it('Test malformed version inputs', async () => {
setEnv('7.4.1', 'darwin', '', '', '', '');
// @ts-ignore
let script: string = await install.run();
expect(script).toContain('initial script');
expect(script).toContain('sh script.sh 7.4 ' + __dirname);

setEnv(8.0, 'darwin', '', '', '', '');
// @ts-ignore
script = await install.run();
expect(script).toContain('initial script');
expect(script).toContain('sh script.sh 8.0 ' + __dirname);

setEnv(8, 'darwin', '', '', '', '');
// @ts-ignore
script = await install.run();
expect(script).toContain('initial script');
expect(script).toContain('sh script.sh 8.0 ' + __dirname);
});
});
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ function run() {
try {
const os_version = process.platform;
let version = yield utils.getInput('php-version', true);
version = version.length > 1 ? version : version + '.0';
version = version.length > 1 ? version.slice(0, 3) : version + '.0';
// check the os version and run the respective script
let script_path = '';
switch (os_version) {
Expand Down
2 changes: 1 addition & 1 deletion src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function run(): Promise<void> {
try {
const os_version: string = process.platform;
let version: string = await utils.getInput('php-version', true);
version = version.length > 1 ? version : version + '.0';
version = version.length > 1 ? version.slice(0, 3) : version + '.0';
// check the os version and run the respective script
let script_path = '';
switch (os_version) {
Expand Down
72 changes: 33 additions & 39 deletions src/scripts/darwin.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
tick=""
cross=""

# Function to log start of a operation
step_log() {
message=$1
printf "\n\033[90;1m==> \033[0m\033[37;1m%s\033[0m\n" "$message"
}

# Function to log result of a operation
add_log() {
mark=$1
subject=$2
Expand All @@ -17,56 +16,51 @@ add_log() {
fi
}

get_extension_regex() {
extension=$1
case $extension in
"opcache")
echo "^Zend\sOPcache$"
;;
"xdebug")
echo "^Xdebug$"
;;
*)
echo ^"$extension"$
;;
esac
}

step_log "Setup PHP and Composer"
export HOMEBREW_NO_INSTALL_CLEANUP=TRUE
brew tap shivammathur/homebrew-php >/dev/null 2>&1
brew install shivammathur/php/php@"$1" composer >/dev/null 2>&1
brew link --force --overwrite php@"$1" >/dev/null 2>&1
ini_file=$(php -d "date.timezone=UTC" --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
echo "date.timezone=UTC" >> "$ini_file"
ext_dir=$(php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||")
sudo chmod 777 "$ini_file"
mkdir -p "$(pecl config-get ext_dir)"
composer global require hirak/prestissimo >/dev/null 2>&1
semver=$(php -v | head -n 1 | cut -f 2 -d ' ')
add_log "$tick" "PHP" "Installed PHP $semver"
add_log "$tick" "Composer" "Installed"

# Function to setup extensions
add_extension() {
extension=$1
install_command=$2
prefix=$3
extension_regex="$(get_extension_regex "$extension")"
if ! php -m | grep -i -q "$extension_regex" && [ -e "$ext_dir/$extension.so" ]; then
if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then
echo "$prefix=$extension" >>"$ini_file" && add_log "$tick" "$extension" "Enabled"
elif php -m | grep -i -q "$extension_regex"; then
elif php -m | grep -i -q -w "$extension"; then
add_log "$tick" "$extension" "Enabled"
elif ! php -m | grep -i -q "$extension_regex"; then
elif ! php -m | grep -i -q -w "$extension"; then
exists=$(curl -sL https://pecl.php.net/json.php?package="$extension" -w "%{http_code}" -o /dev/null)
if [ "$exists" = "200" ]; then
(
eval "$install_command" && \
add_log "$tick" "$extension" "Installed and enabled"
) || add_log "$cross" "$extension" "Could not install $extension on PHP $semver"
else
if ! php -m | grep -i -q "$extension_regex"; then
if ! php -m | grep -i -q -w "$extension"; then
add_log "$cross" "$extension" "Could not find $extension for PHP $semver on PECL"
fi
fi
fi
}
}

# Function to setup PHP and composer
setup_php_and_composer() {
export HOMEBREW_NO_INSTALL_CLEANUP=TRUE
brew tap shivammathur/homebrew-php >/dev/null 2>&1
brew install shivammathur/php/php@"$version" composer >/dev/null 2>&1
brew link --force --overwrite php@"$version" >/dev/null 2>&1
}

# Variables
tick=""
cross=""
version=$1

# Setup PHP and composer
step_log "Setup PHP and Composer"
setup_php_and_composer
ini_file=$(php -d "date.timezone=UTC" --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
echo "date.timezone=UTC" >> "$ini_file"
ext_dir=$(php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||")
sudo chmod 777 "$ini_file"
mkdir -p "$(pecl config-get ext_dir)"
semver=$(php -v | head -n 1 | cut -f 2 -d ' ')
add_log "$tick" "PHP" "Installed PHP $semver"
add_log "$tick" "Composer" "Installed"
Loading

0 comments on commit 935e74f

Please sign in to comment.