Remove outdated files during module upgrade process

This commit is contained in:
max-baranikov 2022-03-04 13:14:32 +03:00 committed by GitHub
parent 805e2414a0
commit cf97a5e3db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 374 additions and 1 deletions

2
.gitignore vendored
View File

@ -7,5 +7,7 @@ retailcrm/views/css/*.map
retailcrm/views/js/*.map retailcrm/views/js/*.map
retailcrm/config*.xml retailcrm/config*.xml
retailcrm/custom retailcrm/custom
upgrade/upgrade-*.php
!upgrade/upgrade-sample.php
coverage.xml coverage.xml
.php-cs-fixer.cache .php-cs-fixer.cache

View File

@ -368,6 +368,51 @@ class RetailCRM extends Module
); );
} }
/**
* Remove files that was deleted\moved\renamed in a newer version and currently are outdated
*
* @param array $files File paths relative to the `modules/` directory
*
* @return bool
*/
public function removeOldFiles($files)
{
foreach ($files as $file) {
try {
if (0 !== strpos($file, 'retailcrm/')) {
continue;
}
$relativePath = str_replace('retailcrm/', '', $file);
$fullPath = sprintf(
'%s/%s', __DIR__, $relativePath
);
if (!file_exists($fullPath)) {
continue;
}
RetailcrmLogger::writeCaller(
__METHOD__, sprintf('Remove `%s`', $file)
);
unlink($fullPath); // todo maybe check and remove empty directories
} catch (Exception $e) {
RetailcrmLogger::writeCaller(
__METHOD__,
sprintf('Error removing `%s`: %s', $file, $e->getMessage())
);
} catch (Error $e) {
RetailcrmLogger::writeCaller(
__METHOD__,
sprintf('Error removing `%s`: %s', $file, $e->getMessage())
);
}
}
return true;
}
public function getContent() public function getContent()
{ {
$output = null; $output = null;

View File

@ -53,5 +53,77 @@ function upgrade_module_3_0_2($module)
return false; return false;
} }
return $module->registerHook('actionCarrierUpdate'); return $module->registerHook('actionCarrierUpdate')
&& upgrade_module_3_0_2_remove_old_files([
'retailcrm/job/abandonedCarts.php',
'retailcrm/job/export.php',
'retailcrm/job/icml.php',
'retailcrm/job/index.php',
'retailcrm/job/inventories.php',
'retailcrm/job/jobs.php',
'retailcrm/job/missing.php',
'retailcrm/job/sync.php',
'retailcrm/lib/CurlException.php',
'retailcrm/lib/InvalidJsonException.php',
'retailcrm/lib/JobManager.php',
'retailcrm/lib/RetailcrmApiClient.php',
'retailcrm/lib/RetailcrmApiClientV4.php',
'retailcrm/lib/RetailcrmApiClientV5.php',
'retailcrm/lib/RetailcrmApiErrors.php',
'retailcrm/lib/RetailcrmApiResponse.php',
'retailcrm/lib/RetailcrmDaemonCollector.php',
'retailcrm/lib/RetailcrmHttpClient.php',
'retailcrm/lib/RetailcrmInventories.php',
'retailcrm/lib/RetailcrmProxy.php',
'retailcrm/lib/RetailcrmService.php',
'retailcrm/public/css/.gitignore',
'retailcrm/public/css/retailcrm-upload.css',
'retailcrm/public/js/.gitignore',
'retailcrm/public/js/exec-jobs.js',
'retailcrm/public/js/retailcrm-upload.js',
]);
}
/**
* Remove files that was deleted\moved\renamed in new version and currently outdated
*
* @param array $files File paths relative to the `modules/` directory
*/
function upgrade_module_3_0_2_remove_old_files($files)
{
try {
foreach ($files as $file) {
if (0 !== strpos($file, 'retailcrm/')) {
continue;
}
$fullPath = sprintf(
'%s/../%s', __DIR__, str_replace('retailcrm/', '', $file)
);
if (!file_exists($fullPath)) {
continue;
}
RetailcrmLogger::writeCaller(
__METHOD__, sprintf('Remove `%s`', $file)
);
unlink($fullPath);
}
return true;
} catch (Exception $e) {
RetailcrmLogger::writeCaller(
__METHOD__,
sprintf('Error removing `%s`: %s', $file, $e->getMessage())
);
} catch (Error $e) {
RetailcrmLogger::writeCaller(
__METHOD__,
sprintf('Error removing `%s`: %s', $file, $e->getMessage())
);
}
return false;
} }

View File

@ -0,0 +1,84 @@
<?php
/**
* MIT License
*
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
* @license https://opensource.org/licenses/MIT The MIT License
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/
if (!defined('_PS_VERSION_')) {
exit;
}
/**
* Upgrade module to version 3.3.6
*
* @param \RetailCRM $module
*
* @return bool
*/
function upgrade_module_3_3_6($module)
{
if ('retailcrm' != $module->name) {
return false;
}
return $module->removeOldFiles([
'retailcrm/job/abandonedCarts.php',
'retailcrm/job/export.php',
'retailcrm/job/icml.php',
'retailcrm/job/index.php',
'retailcrm/job/inventories.php',
'retailcrm/job/jobs.php',
'retailcrm/job/missing.php',
'retailcrm/job/sync.php',
'retailcrm/lib/CurlException.php',
'retailcrm/lib/InvalidJsonException.php',
'retailcrm/lib/JobManager.php',
'retailcrm/lib/RetailcrmApiClient.php',
'retailcrm/lib/RetailcrmApiClientV4.php',
'retailcrm/lib/RetailcrmApiClientV5.php',
'retailcrm/lib/RetailcrmApiErrors.php',
'retailcrm/lib/RetailcrmApiResponse.php',
'retailcrm/lib/RetailcrmDaemonCollector.php',
'retailcrm/lib/RetailcrmHttpClient.php',
'retailcrm/lib/RetailcrmInventories.php',
'retailcrm/lib/RetailcrmProxy.php',
'retailcrm/lib/RetailcrmService.php',
'retailcrm/public/css/.gitignore',
'retailcrm/public/css/retailcrm-upload.css',
'retailcrm/public/js/.gitignore',
'retailcrm/public/js/exec-jobs.js',
'retailcrm/public/js/retailcrm-upload.js',
]);
}

113
upgrade/upgrade-build Executable file
View File

@ -0,0 +1,113 @@
#! /bin/bash
# params
DIFF_FILTER=DR
# functions
build-diff()
{
DIFF_TAG_FROM=$1
DIFF_TAG_TO=${2//v/}
DIFF_PATH_TO=$3 # todo use $2 by default
if [ ! -f upgrade/upgrade-sample.php ]; then
echo "Sample file not found"
exit 1
fi
if [ "$DIFF_PATH_TO" == "" ]; then
CURRENT_DIFF=$(git diff --name-status --diff-filter=$DIFF_FILTER "$DIFF_TAG_FROM" | grep "\tretailcrm/" | grep --invert-match "\tretailcrm/config.*.xml" | awk '{print $2}')
else
CURRENT_DIFF=$(git diff --name-status --diff-filter=$DIFF_FILTER "$DIFF_TAG_FROM" "$DIFF_PATH_TO" | grep "\tretailcrm/" | grep --invert-match "\tretailcrm/config.*.xml" | awk '{print $2}')
fi
if [ "$CURRENT_DIFF" != "" ]; then
echo "$CURRENT_DIFF" >> upgrade/tmp-diff
# echo "$CURRENT_DIFF" > "upgrade/tmp-diff ($DIFF_TAG_FROM to $DIFF_TAG_TO)"
FILES_TO_DELETE=$(echo "$CURRENT_DIFF" | awk 'BEGIN {FS=" "; printf "[\\n"} {printf "\t\t'\''%s'\',\\\\n' ", $1} END {print "\t]" }' )
DIFF_TAG_TO_UND=$(echo "$DIFF_TAG_TO" | sed 's/\./_/g' )
DIFF_TAG_TO_ESC=$(echo "$DIFF_TAG_TO" | sed 's/\./\\\./g' )
UPGRADE_SCRIPT=$(cat upgrade/upgrade-sample.php)
UPGRADE_SCRIPT=$(echo "$UPGRADE_SCRIPT" | sed "s=\['sample'\]=$FILES_TO_DELETE=")
UPGRADE_SCRIPT=$(echo "$UPGRADE_SCRIPT" | sed "s/upgrade_module_sample/upgrade_module_$DIFF_TAG_TO_UND/")
UPGRADE_SCRIPT=$(echo "$UPGRADE_SCRIPT" | sed "s/to version sample/to version $DIFF_TAG_TO_ESC/")
echo "$UPGRADE_SCRIPT" > upgrade/upgrade-"$DIFF_TAG_TO".php
# if [ ! -f retailcrm/upgrade/upgrade-"$DIFF_TAG_TO".php ]; then
# echo "$UPGRADE_SCRIPT" > retailcrm/upgrade/upgrade-"$DIFF_TAG_TO".php
# fi
fi
}
# get count of found files and remove temp files
build-diff-count()
{
if test -f upgrade/tmp-diff; then
sort upgrade/tmp-diff | uniq > upgrade/diff
echo "Found $(wc -l < upgrade/diff) files"
rm upgrade/tmp-diff
rm upgrade/diff
fi
}
# compare current index with latest tag
build-diff-latest-tag()
{
CURRENT_TAG=$(git tag | tail -n 1)
NEW_TAG=$(cat VERSION)
if [ "$CURRENT_TAG" == "v$NEW_TAG" ] && [ "$1" != "force" ]; then
echo "You should update module version at 'VERSION' first"
echo "Or use 'force' argument"
exit 0
fi
echo "Diff from $CURRENT_TAG to $NEW_TAG"
build-diff "$CURRENT_TAG" "$NEW_TAG"
}
# get all tags
build-diff-all-tags()
{
OLDEST_TAG=$(git tag | head -n 1)
NEWEST_TAG=$(git tag | tail -n 1)
ALL_TAGS=$(git tag | cat)
PREVIOUS_TAG=$OLDEST_TAG
echo "Diff from $OLDEST_TAG to $NEWEST_TAG"
for CURRENT_TAG in $ALL_TAGS ; do
if [ "$CURRENT_TAG" == "$PREVIOUS_TAG" ]; then
continue;
fi
build-diff "$PREVIOUS_TAG" "$CURRENT_TAG" "$CURRENT_TAG"
if [ "$CURRENT_TAG" == "$NEWEST_TAG" ]; then
break;
fi
PREVIOUS_TAG=$CURRENT_TAG
done
}
# go to root
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR/../" || exit
# todo fetch tags
if [ "$1" == "all" ]; then
build-diff-all-tags
else
build-diff-latest-tag "$1"
fi
build-diff-count
echo 'Done'

View File

@ -0,0 +1,57 @@
<?php
/**
* MIT License
*
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
* @license https://opensource.org/licenses/MIT The MIT License
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/
if (!defined('_PS_VERSION_')) {
exit;
}
/**
* Upgrade module to version sample
*
* @param \RetailCRM $module
*
* @return bool
*/
function upgrade_module_sample($module)
{
if ('retailcrm' != $module->name) {
return false;
}
return $module->removeOldFiles(['sample']);
}