|
| 1 | +#!/bin/bash |
| 2 | +# Quit script if something goes wrong |
| 3 | +set -o errexit -o nounset -o pipefail; |
| 4 | + |
| 5 | +SCRIPTDIR="$( dirname "$(readlink -f "$0")" )"; |
| 6 | +OUTFILE="${SCRIPTDIR}/../whitelists/magento2.yar"; |
| 7 | +TMPFILE="${OUTFILE}.new"; |
| 8 | + |
| 9 | +# First empty the target whitelist so we can completely generate a new one |
| 10 | +cat <<EOF >"${OUTFILE}"; |
| 11 | +private rule Magento2 : ECommerce |
| 12 | +{ |
| 13 | + condition: |
| 14 | + false |
| 15 | +} |
| 16 | +EOF |
| 17 | + |
| 18 | +# Create a temporary directory and make sure it is empty |
| 19 | +GENTEMPDIR="$( mktemp -d --suffix="_gen_whitelist_m2" )"; |
| 20 | + |
| 21 | +# Composer access tokens |
| 22 | +if [ ! -f "${HOME}/.composer/auth.json" ]; then |
| 23 | + echo -e "\nYou have no '.composer/auth.json' in your home dir. We will create it from a template and open an editor."; |
| 24 | + echo -e "Press [Enter] to continue. Press Ctrl-C if you wish to leave."; |
| 25 | + read; |
| 26 | + mkdir -p "${HOME}/.composer"; |
| 27 | + cat <<EOF >"${HOME}/.composer/auth.json" |
| 28 | +{ |
| 29 | + "INFO_GITHUB": "==== GET TOKEN: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ ====", |
| 30 | + "github-oauth": { |
| 31 | + "github.com": "---github-token-goes-here---" |
| 32 | + }, |
| 33 | + "INFO_MAGENTO": "==== GET TOKEN: https://devdocs.magento.com/guides/v2.0/install-gde/prereq/connect-auth.html ====", |
| 34 | + "http-basic": { |
| 35 | + "repo.magento.com": { |
| 36 | + "username": "---public-key-goes-here---", |
| 37 | + "password": "---private-key-goes-here---" |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +EOF |
| 42 | + editor "${HOME}/.composer/auth.json"; |
| 43 | +fi |
| 44 | + |
| 45 | +# Add header to whitelist tempfile |
| 46 | +cat <<EOF | tee "${TMPFILE}"; |
| 47 | +private rule Magento2 : ECommerce |
| 48 | +{ |
| 49 | + condition: |
| 50 | +EOF |
| 51 | + |
| 52 | +# Fetch tags (releases) from Github repo |
| 53 | +TAGS=$( git ls-remote --tags https://github.com/magento/magento2.git | cut -d '/' -f3 | grep -P '^[\d\.]+$' | sort -V ); |
| 54 | + |
| 55 | +# Foreach tag (release) |
| 56 | +while read -r TAG; do |
| 57 | + # Download tarball of release |
| 58 | + wget "https://github.com/magento/magento2/archive/${TAG}.tar.gz" -O "${GENTEMPDIR}/${TAG}.tgz"; |
| 59 | + # Unpack tarball |
| 60 | + tar -C "${GENTEMPDIR}" -xpzf "${GENTEMPDIR}/${TAG}.tgz"; |
| 61 | + # Run 'composer install' inside unpacked release |
| 62 | + SOURCEDIR="${GENTEMPDIR}/magento2-${TAG}"; |
| 63 | + composer --working-dir="${SOURCEDIR}" -- install; |
| 64 | + # Add version comment to whitelist |
| 65 | + echo " /* Magento2 ${TAG} */" | tee -a "${TMPFILE}"; |
| 66 | + # Generate whitelist for version, add output to whitelist tempfile |
| 67 | + ${SCRIPTDIR}/generate_whitelist.py "Magento2 ${TAG}" "${SOURCEDIR}" | grep 'hash.sha1' | sed "s|// ${SOURCEDIR}/|// |" | tee -a "${TMPFILE}"; |
| 68 | + # Add white line, with indent |
| 69 | + echo " " | tee -a "${TMPFILE}"; |
| 70 | +done <<< "${TAGS}"; |
| 71 | + |
| 72 | +# Add footer to whitelist tempfile |
| 73 | +cat <<EOF | tee -a "${TMPFILE}"; |
| 74 | + false |
| 75 | +} |
| 76 | +EOF |
| 77 | + |
| 78 | +# Copy temporary file to target whitelist while removing duplicate lines except empty ones |
| 79 | +cat "${TMPFILE}" | awk 'match($0,/^\s*$/)||!seen[$0]++' > "${OUTFILE}"; |
| 80 | + |
| 81 | +# Clean up |
| 82 | +rm "${TMPFILE}"; |
| 83 | +rm -rf "${GENTEMPDIR}"; |
0 commit comments