Skip to content

Commit

Permalink
fea(run): bin/console polyfill for PS 1.6 to auto-install modules (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
jokesterfr authored Aug 16, 2024
1 parent 4319176 commit 1745eee
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 76 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
strategy:
fail-fast: true
matrix:
ps_version: ["1.6.1.24", "1.7.6.9", "1.7.7.8", "1.7.8.11", "8.1.4"]
ps_version: ["1.6.1.24", "1.7.6.9", "1.7.7.8", "1.7.8.11", "8.1.7"]
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down Expand Up @@ -138,7 +138,7 @@ jobs:
strategy:
fail-fast: true
matrix:
ps_version: ["1.6.1.24", "1.7.6.9", "1.7.7.8", "1.7.8.11", "8.1.4"]
ps_version: ["1.6.1.24", "1.7.6.9", "1.7.7.8", "1.7.8.11", "8.1.7"]
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -154,6 +154,11 @@ jobs:
env:
DOCKER_IMAGE: prestashop/prestashop-flashlight:${{ matrix.ps_version }}-debian

- name: The image has a PrestaShop console CLI
run: docker run --env PS_DOMAIN='localhost:80' --entrypoint /bin/ls $DOCKER_IMAGE bin/console
env:
DOCKER_IMAGE: prestashop/prestashop-flashlight:${{ matrix.ps_version }}-debian

docker_build_nightly:
name: "Docker build: PS nightly"
runs-on: ubuntu-latest
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ sonar-project.properties
PrestaShop/
prestashop/
vendor/
.prestashop-tags
.prestashop-minor-tags

# cloudflared credentials
credentials.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ On error, PrestaShop Flashlight can quit with these exit codes:

## Does Flashlight support PrestaShop 1.6?

Partially yes. As there is no console within the sources, the modules cannot be automatically installed right now. Feel free to contribute!
Yes. A polyfill is provided for the PrestaShop console, officially introduced in 1.7.

## Developing a module with RW (known Linux issue)

Expand Down
2 changes: 1 addition & 1 deletion assets/alpine-base-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -eu

# Install base tools, PHP requirements and dev-tools
apk --no-cache add -U \
bash less vim geoip git tzdata zip curl jq autoconf \
bash less vim geoip git tzdata zip curl jq autoconf findutils \
nginx nginx-mod-http-headers-more nginx-mod-http-geoip \
nginx-mod-stream nginx-mod-stream-geoip ca-certificates \
php-common php-iconv php-gd mariadb-client sudo libjpeg libxml2 \
Expand Down
10 changes: 9 additions & 1 deletion assets/patch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ set -eu
PS_FOLDER=${PS_FOLDER:?missing PS_FOLDER}
PS_VERSION=$(awk 'NR==1{print $2}' "$PS_FOLDER/VERSION")

add_polyfill_console () {
mkdir -p "$PS_FOLDER/bin"
touch "$PS_FOLDER/bin/console"
mv /tmp/ps-console-polyfill.php "$PS_FOLDER/bin/console"
chmod +x "$PS_FOLDER/bin/console"
}

patch_1_6 () {
echo "✅ Add a robots file for PrestaShop 1.6"
echo "User-agent: *" > "$PS_FOLDER/admin/robots.txt"
echo "Disallow: /" >> "$PS_FOLDER/admin/robots.txt"
add_polyfill_console
}

patch_1_7_6 () {
Expand All @@ -20,7 +28,7 @@ patch_1_7_6 () {
}

patch_other () {
cat /dev/null # Nothing to do
rm -f /tmp/ps-console-polyfill.php
}

if echo "$PS_VERSION" | grep "^1.6" > /dev/null; then
Expand Down
59 changes: 59 additions & 0 deletions assets/ps-console-polyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/local/bin/php -d memory_limit=-1
<?php
if (!defined('_PS_ADMIN_DIR_')) { define('_PS_ADMIN_DIR_', '/admin-dev'); }
if (!defined('_PS_MODE_DEV_')) { define('_PS_MODE_DEV_', true); }
$rootDirectory = getenv('_PS_ROOT_DIR_') ?: '/var/www/html';
require_once $rootDirectory . '/config/config.inc.php';

function getModuleName($args) {
for ($i = 0; $i < count($args); $i++) {
if ($args[$i] === 'install') {
if (isset($args[$i + 1])) {
return $args[$i + 1];
}
}
}
throw new Exception('Module name not found');
}

function installModule($args) {
$moduleName = getModuleName($args);
if (version_compare(_PS_VERSION_, '1.7', '>=')) {
global $kernel;
if(!$kernel){
require_once _PS_ROOT_DIR_.'/app/AppKernel.php';
$kernel = new \AppKernel('dev', true);
$kernel->boot();
}
}
$module = Module::getInstanceByName($moduleName);
$module->install();
}

function clearCache() {
$cacheDirs = [
_PS_CACHEFS_DIRECTORY_ . '/smarty/compile',
_PS_CACHEFS_DIRECTORY_ . '/smarty/cache',
_PS_IMG_DIR_ . '/tmp',
];
foreach ($cacheDirs as $dir) {
$files = glob($dir . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
}

switch ($argv[1]) {
case 'prestashop:module':
installModule($argv);
break;
case 'cache:clear':
clearCache();
break;
default:
throw new Exception('This command is not supported by prestashop-flashlight\'s polyfill');
}

26 changes: 11 additions & 15 deletions assets/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,17 @@ fi
# Eventually install some modules
if [ ! -f $MODULES_INSTALLED_LOCK ] || [ "$INSTALL_MODULES_ON_RESTART" = "true" ]; then
if [ -d "$INSTALL_MODULES_DIR" ]; then
if [ -f "$PS_FOLDER/bin/console" ]; then
for file in "$INSTALL_MODULES_DIR"/*.zip; do
module=$(unzip -l "$file" | awk 'NR==4{print $4}' | sed 's/\/$//' | tr "-" "\n" | head -n 1)
echo "--> Unzipping and installing $module from $file..."
rm -rf "/var/www/html/modules/${module:-something-at-least}"
unzip -qq "$file" -d /var/www/html/modules
if [ "$ON_INSTALL_MODULES_FAILURE" = "continue" ]; then
(php -d memory_limit=-1 "$PS_FOLDER/bin/console" prestashop:module --no-interaction install "$module") || { echo "x module installation failed. Skipping."; }
else
(php -d memory_limit=-1 "$PS_FOLDER/bin/console" prestashop:module --no-interaction install "$module") || { echo "x module installation failed. Sleep and exit."; sleep 10; exit 6; }
fi
done;
else
echo "Auto-installing modules with PrestaShop v1.6 is not yet supported";
fi
for file in "$INSTALL_MODULES_DIR"/*.zip; do
module=$(unzip -l "$file" | awk 'NR==4{print $4}' | sed 's/\/$//' | tr "-" "\n" | head -n 1)
echo "--> Unzipping and installing $module from $file..."
rm -rf "/var/www/html/modules/${module:-something-at-least}"
unzip -qq "$file" -d /var/www/html/modules
if [ "$ON_INSTALL_MODULES_FAILURE" = "continue" ]; then
(php -d memory_limit=-1 "$PS_FOLDER/bin/console" prestashop:module --no-interaction install "$module") || { echo "x module installation failed. Skipping."; }
else
(php -d memory_limit=-1 "$PS_FOLDER/bin/console" prestashop:module --no-interaction install "$module") || { echo "x module installation failed. Sleep and exit."; sleep 10; exit 6; }
fi
done;
fi
touch $MODULES_INSTALLED_LOCK
else
Expand Down
1 change: 1 addition & 0 deletions docker/alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ COPY ./assets/nginx.conf /etc/nginx/nginx.conf
COPY ./php-flavours.json /tmp
COPY ./assets/php-configuration.sh /tmp/
COPY ./assets/alpine-base-install.sh /tmp/
COPY ./assets/ps-console-polyfill.php /tmp/
COPY ./assets/coding-standards /var/opt/prestashop/coding-standards

RUN /tmp/alpine-base-install.sh \
Expand Down
1 change: 1 addition & 0 deletions docker/debian.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ COPY ./assets/nginx.conf /etc/nginx/nginx.conf
COPY ./php-flavours.json /tmp
COPY ./assets/php-configuration.sh /tmp/
COPY ./assets/debian-base-install.sh /tmp/
COPY ./assets/ps-console-polyfill.php /tmp/
COPY ./assets/coding-standards /var/opt/prestashop/coding-standards

RUN /tmp/debian-base-install.sh \
Expand Down
10 changes: 3 additions & 7 deletions examples/auto-install-modules/init-scripts/install-mock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
#
set -eu

run_user () {
sudo -g www-data -u www-data -- "$@"
}

ps_accounts_mock_install() {
echo "* [ps_accounts_mock] downloading..."
wget -q -O /tmp/ps_accounts.zip "https://github.com/PrestaShopCorp/ps_accounts_mock/releases/download/v1.0.0/ps_accounts.zip"
wget -q -O /tmp/ps_accounts.zip "https://github.com/PrestaShopCorp/ps_accounts_mock/releases/download/v7.0.2/ps_accounts.zip"
echo "* [ps_accounts_mock] unzipping..."
run_user unzip -qq /tmp/ps_accounts.zip -d /var/www/html/modules
unzip -qq /tmp/ps_accounts.zip -d /var/www/html/modules
echo "* [ps_accounts_mock] installing the module..."
cd "$PS_FOLDER"
run_user php -d memory_limit=-1 bin/console prestashop:module --no-interaction install "ps_accounts"
php -d memory_limit=-1 bin/console prestashop:module --no-interaction install "ps_accounts"
}

ps_accounts_mock_install
Binary file modified examples/auto-install-modules/modules/psalsace-v1.0.0.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions examples/develop-a-module/modules/testmodule/testmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function __construct()
$this->bootstrap = false;

parent::__construct();
$this->displayName = $this->trans('Test Module', [], 'Modules.Mymodule.Admin');
$this->description = $this->trans('Test Module', [], 'Modules.Mymodule.Admin');
$this->confirmUninstall = $this->trans('Are you sure you want to quit ModuleA?', [], 'Modules.Mymodule.Admin');
$this->displayName = $this->l('Test Module', [], 'Modules.Mymodule.Admin');
$this->description = $this->l('Test Module', [], 'Modules.Mymodule.Admin');
$this->confirmUninstall = $this->l('Are you sure you want to quit ModuleA?', [], 'Modules.Mymodule.Admin');

require_once __DIR__ . '/vendor/autoload.php';
}
Expand Down
6 changes: 3 additions & 3 deletions examples/module-coding-standard/testmodule/testmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function __construct()
$this->bootstrap = false;

parent::__construct();
$this->displayName = $this->trans('Test Module', [], 'Modules.Mymodule.Admin');
$this->description = $this->trans('Test Module', [], 'Modules.Mymodule.Admin');
$this->confirmUninstall = $this->trans('Are you sure you want to quit ModuleA?', [], 'Modules.Mymodule.Admin');
$this->displayName = $this->l('Test Module', [], 'Modules.Mymodule.Admin');
$this->description = $this->l('Test Module', [], 'Modules.Mymodule.Admin');
$this->confirmUninstall = $this->l('Are you sure you want to quit ModuleA?', [], 'Modules.Mymodule.Admin');

require_once __DIR__ . '/vendor/autoload.php';
}
Expand Down
79 changes: 38 additions & 41 deletions release.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
#!/bin/sh
set -eu
PRESTASHOP_TAGS=.prestashop-tags
PRESTASHOP_MINOR_TAGS=.prestashop-minor-tags

get_prestashop_tags() {
git ls-remote --tags [email protected]:PrestaShop/PrestaShop.git \
| cut -f2 \
| grep -Ev '\/1.5|\/1.6.0|beta|rc|RC|\^' \
| cut -d '/' -f3 \
| sort -r -V > "$PRESTASHOP_TAGS"
}

get_prestashop_minor_tags() {
printf "" > "$PRESTASHOP_MINOR_TAGS"
while IFS= read -r version; do
major_minor=$(echo "$version" | cut -d. -f1-2)
major_minor_patch=$(echo "$version" | cut -d. -f1-3)
criteria=$major_minor
# shellcheck disable=SC3010
[[ "$major_minor" == 1* ]] && criteria=$major_minor_patch
if ! grep -q "^$criteria" "$PRESTASHOP_MINOR_TAGS"; then
echo "$version" >> "$PRESTASHOP_MINOR_TAGS"
fi
done < "$PRESTASHOP_TAGS"
}
EXCLUDED_TAGS='\/1.5|\/1.6.0|alpha|beta|rc|RC|\^'
PRESTASHOP_TAGS=$(git ls-remote --tags [email protected]:PrestaShop/PrestaShop.git | cut -f2 | grep -Ev $EXCLUDED_TAGS | cut -d '/' -f3 | sort -r -V)
# PRESTASHOP_MAJOR_TAGS=$(
# MAJOR_TAGS=""
# for VERSION in $PRESTASHOP_TAGS; do
# CRITERIA=$(echo "$VERSION" | cut -d. -f1)
# # shellcheck disable=SC3010
# if [[ "$CRITERIA" == 1* ]]; then
# CRITERIA=$(echo "$VERSION" | cut -d. -f1-2)
# fi
# if ! echo "$MAJOR_TAGS" | grep -q "^$CRITERIA"; then
# MAJOR_TAGS="$MAJOR_TAGS\n$VERSION";
# fi
# done
# echo "$MAJOR_TAGS"
# )
# PRESTASHOP_MINOR_TAGS=$(
# MINOR_TAGS=""
# for VERSION in $PRESTASHOP_TAGS; do
# CRITERIA=$(echo "$VERSION" | cut -d. -f1-2)
# # shellcheck disable=SC3010
# if [[ "$CRITERIA" == 1* ]]; then
# CRITERIA=$(echo "$VERSION" | cut -d. -f1-3)
# fi
# if ! echo "$MINOR_TAGS" | grep -q "^$CRITERIA"; then
# MINOR_TAGS="$MINOR_TAGS\n$VERSION";
# fi
# done
# echo "$MINOR_TAGS"
# )

get_compatible_php_version() {
REGEXP_LIST=$(jq -r 'keys_unsorted | .[]' <prestashop-versions.json)
REGEXP_LIST=$(echo "$PRESTASHOP_TAGS" | jq -r 'keys_unsorted | .[]')
while IFS= read -r regExp; do
# shellcheck disable=SC3010
if [[ $1 =~ $regExp ]]; then
jq -r '."'"${regExp}"'".php.compatible[]' <prestashop-versions.json
echo "$PRESTASHOP_TAGS" | jq -r '."'"${regExp}"'".php.compatible[]'
break;
fi
done <<EOF
Expand All @@ -44,30 +50,21 @@ publish() {
--field target_platforms=linux/amd64,linux/arm64 "$@"
}

get_prestashop_tags
get_prestashop_minor_tags

# Latest
publish --field ps_version=latest
publish --field ps_version=latest --field os_flavour=debian

# Recommended PHP for some minor tag
# while IFS= read -r PS_VERSION; do
# publish --field ps_version="$PS_VERSION"
# publish --field ps_version="$PS_VERSION" --field os_flavour=debian
# done < "$PRESTASHOP_MINOR_TAGS"

# Recommended PHP for every tag
while IFS= read -r PS_VERSION; do
# Build & publish every prestashop version with recommended PHP version
for PS_VERSION in $PRESTASHOP_TAGS; do
publish --field ps_version="$PS_VERSION"
publish --field ps_version="$PS_VERSION" --field os_flavour=debian
done < "$PRESTASHOP_TAGS"
done

# Compatible PHP for every minor tag (alpine only)
# while IFS= read -r PS_VERSION; do
# Build & publish every prestashop minor version with all compatible PHP versions (alpine only)
# for PS_VERSION in $PRESTASHOP_MINOR_TAGS; do
# while IFS= read -r PHP_VERSION; do
# publish --field ps_version="$PS_VERSION" --field php_version="$PHP_VERSION"
# done <<EOF
# $(get_compatible_php_version "$PS_VERSION")
# EOF
# done < "$PRESTASHOP_MINOR_TAGS"
# done

0 comments on commit 1745eee

Please sign in to comment.