diff --git a/.gitignore b/.gitignore index ab6fba7..0c69bff 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,7 @@ retailcrm/views/css/*.map retailcrm/views/js/*.map retailcrm/config*.xml retailcrm/custom +upgrade/upgrade-*.php +!upgrade/upgrade-sample.php coverage.xml .php-cs-fixer.cache diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index d0a29a0..d1c2f63 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -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() { $output = null; diff --git a/retailcrm/upgrade/upgrade-3.0.2.php b/retailcrm/upgrade/upgrade-3.0.2.php index 0d1079c..337eebb 100644 --- a/retailcrm/upgrade/upgrade-3.0.2.php +++ b/retailcrm/upgrade/upgrade-3.0.2.php @@ -53,5 +53,77 @@ function upgrade_module_3_0_2($module) 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; } diff --git a/retailcrm/upgrade/upgrade-3.3.6.php b/retailcrm/upgrade/upgrade-3.3.6.php new file mode 100644 index 0000000..6914a9e --- /dev/null +++ b/retailcrm/upgrade/upgrade-3.3.6.php @@ -0,0 +1,84 @@ + + * @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', + ]); +} diff --git a/upgrade/upgrade-build b/upgrade/upgrade-build new file mode 100755 index 0000000..da3e7a9 --- /dev/null +++ b/upgrade/upgrade-build @@ -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' diff --git a/upgrade/upgrade-sample.php b/upgrade/upgrade-sample.php new file mode 100644 index 0000000..202715b --- /dev/null +++ b/upgrade/upgrade-sample.php @@ -0,0 +1,57 @@ + + * @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']); +}