From 30633664a185a8a03b67fed363221d33f04f5e3a Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 5 Jul 2022 16:07:15 +0200 Subject: [PATCH] MOBILE-4110 behat: Configure auto-generated plugin --- .github/scripts/functions.sh | 21 ++ .github/scripts/update_behat_plugin.sh | 68 ++++++ .github/workflows/update_behat_plugin.yml | 18 ++ package-lock.json | 255 +++++++++++++++++++++- package.json | 1 + scripts/build-behat-plugin.js | 63 ++++-- 6 files changed, 395 insertions(+), 31 deletions(-) create mode 100755 .github/scripts/update_behat_plugin.sh create mode 100644 .github/workflows/update_behat_plugin.yml diff --git a/.github/scripts/functions.sh b/.github/scripts/functions.sh index f36fba1fc..33ee4febb 100644 --- a/.github/scripts/functions.sh +++ b/.github/scripts/functions.sh @@ -63,3 +63,24 @@ function notify_on_error_exit { exit 1 fi } + +function get_behat_plugin_changes_diff { + i=0 + previoushash="" + currenthash=`git rev-parse HEAD` + initialhash=`git rev-list HEAD | tail -n 1` + totalcommits=`git log --oneline | wc -l` + repositoryname=`echo $GITHUB_REPOSITORY | sed "s/\\//\\\\\\\\\\//"` + + ((totalcommits--)) + while [ $i -lt $totalcommits ] && [[ -z $previoushash ]]; do + previoushash=`git rev-list --format=%B --max-count=1 HEAD~$i | grep -o "https:\/\/github\.com\/$repositoryname\/compare\/[^.]\+\.\.\.[^.]\+" | sed "s/https:\/\/github\.com\/$repositoryname\/compare\/[^.]\+\.\.\.//"` + ((i++)) + done + + if [[ -z $previoushash ]]; then + previoushash=$initialhash + fi + + echo "$previoushash...$currenthash" +} diff --git a/.github/scripts/update_behat_plugin.sh b/.github/scripts/update_behat_plugin.sh new file mode 100755 index 000000000..5419d831a --- /dev/null +++ b/.github/scripts/update_behat_plugin.sh @@ -0,0 +1,68 @@ +#!/bin/bash +source "./.github/scripts/functions.sh" + +if [ -z $GIT_TOKEN ] || [ -z $BEHAT_PLUGIN_GITHUB_REPOSITORY ] || [ -z $BEHAT_PLUGIN_BRANCH ]; then + print_error "Env vars not correctly defined" + exit 1 +fi + +if [[ $BEHAT_PLUGIN_BRANCH != $GITHUB_REF_NAME ]]; then + echo "Script disabled for this branch" + exit 0 +fi + +# Clone plugin repository. +print_title "Cloning Behat plugin repository..." + +git clone https://$GIT_TOKEN@github.com/$BEHAT_PLUGIN_GITHUB_REPOSITORY.git tmp/local_moodleappbehat -b integration +pluginversion=$(cat tmp/local_moodleappbehat/version.php | grep "\$plugin->version" | grep -o -E "[0-9]+") + +# Auto-generate plugin. +print_title "Building Behat plugin..." + +if [ -z $BEHAT_PLUGIN_EXCLUDE_FEATURES ]; then + scripts/build-behat-plugin.js tmp/local_moodleappbehat +else + scripts/build-behat-plugin.js tmp/local_moodleappbehat --exclude-features +fi +notify_on_error_exit "Unsuccessful build, stopping..." + +# Check if there are any changes (ignoring plugin version). +print_title "Checking changes..." + +newpluginversion=$(cat tmp/local_moodleappbehat/version.php | grep "\$plugin->version" | grep -o -E "[0-9]+") +sed -i s/\$plugin-\>version\ =\ [0-9]\\+\;/\$plugin-\>version\ =\ $pluginversion\;/ tmp/local_moodleappbehat/version.php + +if [[ -z `git -C tmp/local_moodleappbehat/ status --short` ]]; then + echo "There weren't any changes to apply!" + exit +fi + +if [[ $pluginversion -eq $newpluginversion ]]; then + ((newpluginversion++)) +fi + +sed -i s/\$plugin-\>version\ =\ [0-9]\\+\;/\$plugin-\>version\ =\ $newpluginversion\;/ tmp/local_moodleappbehat/version.php + +# Apply new changes +print_title "Applying changes to repository..." + +cd tmp/local_moodleappbehat + +diff=`get_behat_plugin_changes_diff` + +# Set up Github Actions bot user +# See https://github.community/t/github-actions-bot-email-address/17204/6 +git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" +git config --local user.name "github-actions[bot]" +git add . +git commit -m "[auto-generated] Update plugin files +Check out the commits that caused these changes: https://github.com/$GITHUB_REPOSITORY/compare/$diff +" +notify_on_error_exit "Unsuccessful commit, stopping..." + +echo "Pushing changes..." +git push +notify_on_error_exit "Unsuccessful push, stopping..." + +echo "Behat plugin updated!" diff --git a/.github/workflows/update_behat_plugin.yml b/.github/workflows/update_behat_plugin.yml new file mode 100644 index 000000000..1f6430a6a --- /dev/null +++ b/.github/workflows/update_behat_plugin.yml @@ -0,0 +1,18 @@ +name: Update Behat plugin + +on: ['push'] + +jobs: + update: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v3 + - name: Install npm packages + run: npm ci --no-audit + - name: Update Behat plugin + env: + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} + BEHAT_PLUGIN_GITHUB_REPOSITORY: ${{ secrets.BEHAT_PLUGIN_GITHUB_REPOSITORY }} + BEHAT_PLUGIN_BRANCH: ${{ secrets.BEHAT_PLUGIN_BRANCH }} + run: ./.github/scripts/update_behat_plugin.sh diff --git a/package-lock.json b/package-lock.json index 58bff4929..635045583 100644 --- a/package-lock.json +++ b/package-lock.json @@ -229,12 +229,31 @@ "uri-js": "^4.2.2" } }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "core-js": { "version": "3.6.4", "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.6.4.tgz", "integrity": "sha512-4paDGScNgZP2IXXilaffL9X7968RuvwlkK3xWtZRVqgd8SYNiVKRJvkFd1aqqEuPfN7E68ZHEp9hDj6lHj4Hyw==", "dev": true }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, "open": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/open/-/open-7.0.4.tgz", @@ -3889,6 +3908,16 @@ "strip-json-comments": "^3.1.1" }, "dependencies": { + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "debug": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", @@ -3913,6 +3942,15 @@ "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -11772,12 +11810,12 @@ } }, "brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, "requires": { - "balanced-match": "^1.0.0", - "concat-map": "0.0.1" + "balanced-match": "^1.0.0" } }, "braces": { @@ -12327,6 +12365,16 @@ "integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==", "dev": true }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", @@ -12447,6 +12495,17 @@ "table": "^5.2.3", "text-table": "^0.2.0", "v8-compile-cache": "^2.0.3" + }, + "dependencies": { + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + } } }, "eslint-scope": { @@ -13433,7 +13492,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, "concat-stream": { "version": "1.6.2", @@ -16449,6 +16508,16 @@ "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", "dev": true }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "chalk": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", @@ -16493,6 +16562,15 @@ "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -16625,6 +16703,16 @@ "tsconfig-paths": "^3.9.0" }, "dependencies": { + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "doctrine": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-1.5.0.tgz", @@ -16634,6 +16722,15 @@ "esutils": "^2.0.2", "isarray": "^1.0.0" } + }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } } } }, @@ -17635,6 +17732,16 @@ "color-convert": "^1.9.0" } }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "braces": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/braces/-/braces-2.3.2.tgz", @@ -17766,6 +17873,15 @@ "to-regex": "^3.0.2" } }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", @@ -18326,6 +18442,25 @@ "minimatch": "^3.0.4", "once": "^1.3.0", "path-is-absolute": "^1.0.0" + }, + "dependencies": { + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "requires": { + "brace-expansion": "^1.1.7" + } + } } }, "glob-base": { @@ -19962,6 +20097,25 @@ "integrity": "sha512-m7o6xuOaT1aqheYHKf8W6J5pYH85ZI9w077erOzLje3JsB1gkafkAhHHY19dqjulgIZHFm32Cp5uNZgcQqdJKw==", "requires": { "minimatch": "^3.0.4" + }, + "dependencies": { + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "requires": { + "brace-expansion": "^1.1.7" + } + } } }, "image-size": { @@ -20959,6 +21113,16 @@ "integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==", "dev": true }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -20968,6 +21132,15 @@ "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" } + }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } } } }, @@ -23349,11 +23522,12 @@ "dev": true }, "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.0.tgz", + "integrity": "sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg==", + "dev": true, "requires": { - "brace-expansion": "^1.1.7" + "brace-expansion": "^2.0.1" } }, "minimist": { @@ -25138,6 +25312,16 @@ "which": "^1.3.1" }, "dependencies": { + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "cacache": { "version": "12.0.4", "resolved": "https://registry.npmjs.org/cacache/-/cacache-12.0.4.tgz", @@ -25200,6 +25384,15 @@ "yallist": "^3.0.2" } }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, "minipass": { "version": "2.9.0", "resolved": "https://registry.npmjs.org/minipass/-/minipass-2.9.0.tgz", @@ -28127,6 +28320,27 @@ "dev": true, "requires": { "minimatch": "3.0.4" + }, + "dependencies": { + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + } } }, "reflect-metadata": { @@ -31856,6 +32070,27 @@ "@istanbuljs/schema": "^0.1.2", "glob": "^7.1.4", "minimatch": "^3.0.4" + }, + "dependencies": { + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + } } }, "text-table": { diff --git a/package.json b/package.json index 31cd28163..ea93e1d63 100644 --- a/package.json +++ b/package.json @@ -172,6 +172,7 @@ "jest": "^26.5.2", "jest-preset-angular": "^8.3.1", "jsonc-parser": "^2.3.1", + "minimatch": "^5.1.0", "native-run": "^1.4.0", "patch-package": "^6.4.7", "terser-webpack-plugin": "^4.2.3", diff --git a/scripts/build-behat-plugin.js b/scripts/build-behat-plugin.js index 1d0570d7b..dad19ff1a 100755 --- a/scripts/build-behat-plugin.js +++ b/scripts/build-behat-plugin.js @@ -14,6 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +const minimatch = require('minimatch'); const { existsSync, readFileSync, writeFileSync, statSync, renameSync, rmSync } = require('fs'); const { readdir } = require('fs').promises; const { mkdirSync, copySync } = require('fs-extra'); @@ -21,12 +22,22 @@ const { resolve, extname, dirname, basename, relative } = require('path'); async function main() { const pluginPath = process.argv[2] || guessPluginPath() || fail('Folder argument missing!'); + const excludeFeatures = process.argv.some(arg => arg === '--exclude-features'); + const exclusions = excludeFeatures + ? [ + '*.feature', + '**/js/mobile/index.js', + '**/db/mobile.php', + '**/classes/output/mobile.php', + ] + : []; if (!existsSync(pluginPath)) { mkdirSync(pluginPath); } else { // Empty directory, except the excluding list. const excludeFromErase = [ + ...exclusions, '.git', '.gitignore', 'README.md', @@ -34,7 +45,7 @@ async function main() { const files = await readdir(pluginPath, { withFileTypes: true }); for (const file of files) { - if (excludeFromErase.indexOf(file.name) >= 0) { + if (isExcluded(file.name, excludeFromErase)) { continue; } @@ -43,13 +54,17 @@ async function main() { } } - // Copy plugin template. const { version: appVersion } = require(projectPath('package.json')); const templatePath = projectPath('local-moodleappbehat'); + for await (const file of getDirectoryFiles(templatePath)) { + if (isExcluded(file, exclusions)) { + continue; + } - copySync(templatePath, pluginPath); + copySync(file, file.replace(templatePath, pluginPath)); + } // Update version.php const pluginFilePath = pluginPath + '/version.php'; @@ -62,28 +77,30 @@ async function main() { writeFileSync(pluginFilePath, replaceArguments(fileContents, replacements)); // Copy feature files. - const behatTempFeaturesPath = `${pluginPath}/behat-tmp`; - copySync(projectPath('src'), behatTempFeaturesPath, { filter: isFeatureFileOrDirectory }); + if (!excludeFeatures) { + const behatTempFeaturesPath = `${pluginPath}/behat-tmp`; + copySync(projectPath('src'), behatTempFeaturesPath, { filter: isFeatureFileOrDirectory }); - const behatFeaturesPath = `${pluginPath}/tests/behat`; - if (!existsSync(behatFeaturesPath)) { - mkdirSync(behatFeaturesPath, {recursive: true}); - } - - for await (const featureFile of getDirectoryFiles(behatTempFeaturesPath)) { - const featurePath = dirname(featureFile); - if (!featurePath.endsWith('/tests/behat')) { - continue; + const behatFeaturesPath = `${pluginPath}/tests/behat`; + if (!existsSync(behatFeaturesPath)) { + mkdirSync(behatFeaturesPath, {recursive: true}); } - const newPath = featurePath.substring(0, featurePath.length - ('/tests/behat'.length)); - const searchRegExp = new RegExp('/', 'g'); - const prefix = relative(behatTempFeaturesPath, newPath).replace(searchRegExp,'-') || 'core'; - const featureFilename = prefix + '-' + basename(featureFile); - renameSync(featureFile, behatFeaturesPath + '/' + featureFilename); - } + for await (const featureFile of getDirectoryFiles(behatTempFeaturesPath)) { + const featurePath = dirname(featureFile); + if (!featurePath.endsWith('/tests/behat')) { + continue; + } - rmSync(behatTempFeaturesPath, {recursive: true}); + const newPath = featurePath.substring(0, featurePath.length - ('/tests/behat'.length)); + const searchRegExp = new RegExp('/', 'g'); + const prefix = relative(behatTempFeaturesPath, newPath).replace(searchRegExp,'-') || 'core'; + const featureFilename = prefix + '-' + basename(featureFile); + renameSync(featureFile, behatFeaturesPath + '/' + featureFilename); + } + + rmSync(behatTempFeaturesPath, {recursive: true}); + } } function isFeatureFileOrDirectory(src) { @@ -92,6 +109,10 @@ function isFeatureFileOrDirectory(src) { return stats.isDirectory() || extname(src) === '.feature'; } +function isExcluded(file, exclusions) { + return exclusions.some(exclusion => minimatch(file, exclusion)); +} + function fail(message) { console.error(message); process.exit(1);