Skip to content

Commit

Permalink
Create new version-compare action and use it in phpunit.$
Browse files Browse the repository at this point in the history
  • Loading branch information
Hug0-Drelon committed Feb 20, 2024
1 parent c1e4651 commit f2c4cc3
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
55 changes: 55 additions & 0 deletions bin/version-compare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

version_compare () {
if [[ $1 == 'latest' ]] || [[ $1 == 'dev' ]] || [[ $1 == 'nightly' ]]
then
# Set first arg to a huge number considering latest, dev or nightly to be the highest version.
set -- 1000000 "${@:2:3}"
fi
if [[ $2 == 'latest' ]] || [[ $2 == 'dev' ]] || [[ $2 == 'nightly' ]]
then
# Set second arg to a huge number considering latest, dev or nightly to be the highest version.
set -- "${@:1}" 1000000 "${@:3}"
fi
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# Fill empty fields in ver1 with zeros.
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# Fill empty fields in ver2 with zeros.
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}

version_compare $1 $2
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
esac
if [[ $op != $3 ]]
then
echo "result=$(echo false)" >> $GITHUB_OUTPUT
else
echo "result=$(echo true)" >> $GITHUB_OUTPUT
fi
11 changes: 9 additions & 2 deletions phpunit/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ runs:
coverage: none
php-version: ${{ inputs.php-version }}

- name: Clean PHP Dependencies
- name: Check version requirement
id: version-compare
uses: polylang/actions/version-compare@ver-comp
with:
head: ${{ inputs.wordpress-version }}
base: "5.9"

- name: Clean PHP dependencies
run: |
if [[ ${{ inputs.wordpress-version }} == "5.8.x" ]]; then
if [[ ${{ steps.version-compare.outputs.result }} == true ]]; then
composer require --no-interaction --no-update --ignore-platform-reqs --dev "phpunit/phpunit ^7.5"
fi
composer remove --no-interaction --no-update --dev "phpstan/phpstan"
Expand Down
35 changes: 35 additions & 0 deletions version-compare/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Sementic Versions Compare

description: Compares two versions. If 'dev', 'nightly' or 'latest' is given it'll be considered as the highest version possible.

inputs:
head:
description: First version number.
required: true
type: string
base:
description: Second version number.
required: true
type: string
operator:
description: Operator, accepts '<', '>' and '='. Default to '<'.
required: true
type: string
default: '<'

outputs:
result:
description: Whether or not the version comparison it correct.
value: ${{ steps.compare.outputs.result }}

permissions:
contents: read

runs:
using: 'composite'

steps:
- name: Compare versions
id: compare
run: bash ${{ github.action_path }}/../bin/version-compare.sh "${{ inputs.head }}" "${{ inputs.base }}" "${{ inputs.operator }}"
shell: bash

0 comments on commit f2c4cc3

Please sign in to comment.