#!/bin/bash # Mage is a collection of easy commands and aliases for bin/magento # For those who hate typing long shell commands # Global variables RESET='\033[0m' BOLD='\033[1m' ITALIC='\033[3m' RED='\033[0;31m' GREEN='\033[1;32m' BLUE='\033[1;34m' GITNAME="$(git config --global --get user.name | head -n1 | cut -d " " -f1)" GITEMAIL="$(git config --global --get user.email)" ADMINNAME="$(echo "$GITNAME" | tr '[:upper:]' '[:lower:]')" ADMINEMAIL="${GITEMAIL}" ADMINPASS="m2_admin123$" WARDEN=0 MAGENTO_CLI="bin/magento" MAGERUN_CLI="" REDIS_CLI="redis-cli" VARNISH_CLI="varnishadm" # We never use varnish on a local machine, and prefer to never use it PHP_CLI="php" COMPOSER_CLI="composer" NODE_CLI="node" NPM_CLI="npm" PURGE_CLI="rm -rf" OPEN_CLI="xdg-open" # Linux GET_CLI="wget" # Linux # OSX if [[ "$OSTYPE" == "darwin"* ]]; then OPEN_CLI="open" GET_CLI="curl -O" fi # Check if this is the Magento 2 root if [[ ! -d app/etc ]]; then # Allow the following commands to run, even if the folder is not root Magento, # if else exit mage if [[ $1 != "-v" ]] && [[ $1 != "--version" ]] && [[ $1 != "version" ]] && [[ $1 != "help" ]] && [[ $1 != "self-update" ]] && [[ $1 != "install" ]] && [[ $1 != "create" ]]; then echo "This does not look like the Magento 2 root folder, aborting.." && exit 1 fi fi # Magerun NO_MAGERUN_MSG="Magerun2 is not installed" if command -v magerun2 &>/dev/null; then MAGERUN_CLI="magerun2" elif command -v n98-magerun2 &>/dev/null; then MAGERUN_CLI="n98-magerun2" fi # Warden Support if [ -f .env ] && grep -q "WARDEN_ENV_NAME" .env && [[ ! "$PATH" == /var/www/html* ]]; then WARDEN=1 MAGENTO_CLI="warden env exec php-fpm bin/magento" MAGERUN_CLI="warden env exec php-fpm n98-magerun" REDIS_CLI="warden env exec redis redis-cli" VARNISH_CLI="warden env exec -T varnish varnishadm" PHP_CLI="warden env exec php-fpm php" COMPOSER_CLI="warden env exec php-fpm composer" NODE_CLI="warden env exec php-fpm node" NPM_CLI="warden env exec php-fpm npm" # Run removal within environment, so that changes are in effect immediately. # Changes will get synced back to the host PURGE_CLI="warden env exec -T php-fpm rm -rf" fi # Helper to format language inputs function mage_lang_format_arguments() { formatted_args="" for arg in "$@"; do formatted_arg=$(tr '[a-z]' '[A-Z]' <<< "$arg") formatted_arg="${formatted_arg// /}" formatted_args="${formatted_args},${formatted_arg}" done formatted_args="${formatted_args:1}" echo "$formatted_args" } # Creates a file/folder and echo the contents in one command function mage_make_file() { touch $1 if [[ $2 == "rsync" ]]; then if [[ -d $3 ]]; then rsync -ah ${3}/ ${1} --exclude node_modules else echo -e "The folder '${3}' does not exists" fi else echo -e $2 >> $1 fi } # Convert string to kebab-case function mage_kebab_case() { echo "${@}" | sed 's/\([A-Z]\)/-\1/g' | tr '[:upper:]' '[:lower:]' | sed -e 's/^-*//' -e 's/-*$//' | tr -s '[:blank:]' '-' } # Get the Magento 2 Base Url function get_mage_base_uri() { local baseuri="$($MAGENTO_CLI config:show web/secure/base_url)" if [[ -z "${baseuri}" ]]; then local baseuri="$($MAGENTO_CLI config:show web/unsecure/base_url)" fi echo $baseuri } # Get specific Magento 2 Store Url function get_mage_store_uri() { local store_url="" if [[ -n "$MAGERUN_CLI" ]]; then if [[ "$1" == "admin" ]]; then store_url=$($MAGERUN_CLI sys:store:config:base-url:list --format csv | grep 1 -m 1 | head -1 | cut -d ',' -f3) else store_url=$($MAGERUN_CLI sys:store:config:base-url:list --format csv | grep $1 | cut -d ',' -f3) fi else store_url=$(get_mage_base_uri) fi echo $store_url } function get_composer_pkg_version() { echo -e $($COMPOSER_CLI show $1 | grep 'versions' | grep -o -E '\*\ .+' | awk '{print $2}' | cut -d',' -f1) } function mage_add_hyva() { local use_hyva_production=$1 if [[ $use_hyva_production =~ ^[yY]|[yY][eE][sS]$ ]]; then if [ ! -f "auth.json" ]; then read -e -p "No license found, add license? [Y/n] " echo "" if [[ ! $REPLY =~ ^[nN]|[nN][oO]$ ]]; then read -e -p "License key: " hyva_key && echo "" read -e -p "Packagist url: " hyva_url && echo "" $COMPOSER_CLI config --auth http-basic.hyva-themes.repo.packagist.com token $hyva_key $COMPOSER_CLI config repositories.private-packagist composer https://hyva-themes.repo.packagist.com/$hyva_url/ echo "Installing Hyva theme..." $COMPOSER_CLI require hyva-themes/magento2-default-theme else echo "Installing Hyva theme..." $COMPOSER_CLI require hyva-themes/magento2-default-theme fi else echo "Installing Hyva theme..." $COMPOSER_CLI require hyva-themes/magento2-default-theme fi else mage_add_hyva_dev fi; $MAGENTO_CLI config:set customer/captcha/enable 0 } function mage_add_hyva_dev() { echo "Adding repositories..." # Core Theme Deps $COMPOSER_CLI config repositories.hyva-themes/magento2-reset-theme git git@gitlab.hyva.io:hyva-themes/magento2-reset-theme.git $COMPOSER_CLI config repositories.hyva-themes/magento2-theme-module git git@gitlab.hyva.io:hyva-themes/magento2-theme-module.git $COMPOSER_CLI config repositories.hyva-themes/magento2-mollie-theme-bundle git git@gitlab.hyva.io:hyva-themes/hyva-compat/magento2-mollie-theme-bundle.git $COMPOSER_CLI config repositories.hyva-themes/magento2-email-module git git@gitlab.hyva.io:hyva-themes/magento2-email-module.git # magento2-graphql-tokens Found on: https://packagist.org/packages/hyva-themes/magento2-graphql-tokens # magento2-graphql-view-model Found on: https://packagist.org/packages/hyva-themes/magento2-graphql-view-model $COMPOSER_CLI config repositories.hyva-themes/magento2-order-cancellation-webapi git git@gitlab.hyva.io:hyva-themes/magento2-order-cancellation-webapi.git $COMPOSER_CLI config repositories.hyva-themes/magento2-default-theme git git@gitlab.hyva.io:hyva-themes/magento2-default-theme.git # Extra Deps $COMPOSER_CLI config repositories.hyva-themes/magento2-default-theme-csp git git@gitlab.hyva.io:hyva-themes/magento2-default-theme-csp.git $COMPOSER_CLI config repositories.hyva-themes/magento2-cms-tailwind-jit git git@gitlab.hyva.io:hyva-themes/magento2-cms-tailwind-jit.git $COMPOSER_CLI config repositories.hyva-themes/magento2-compat-module-fallback git git@gitlab.hyva.io:hyva-themes/magento2-compat-module-fallback.git $COMPOSER_CLI config repositories.hyva-themes/magento2-theme-fallback git git@gitlab.hyva.io:hyva-themes/magento2-theme-fallback.git $COMPOSER_CLI config repositories.hyva-themes/magento2-luma-checkout git git@gitlab.hyva.io:hyva-themes/magento2-luma-checkout.git # Checkout Deps $COMPOSER_CLI config repositories.hyva-themes/hyva-checkout git git@gitlab.hyva.io:hyva-checkout/checkout.git # Commerce Deps $COMPOSER_CLI config repositories.hyva-themes/commerce git git@gitlab.hyva.io:hyva-commerce/metapackage-commerce.git $COMPOSER_CLI config repositories.hyva-themes/commerce-module-commerce git git@gitlab.hyva.io:hyva-commerce/module-commerce.git $COMPOSER_CLI config repositories.hyva-themes/commerce-module-cms git git@gitlab.hyva.io:hyva-commerce/module-cms.git $COMPOSER_CLI config repositories.hyva-themes/commerce-module-image-editor git git@gitlab.hyva.io:hyva-commerce/module-image-editor.git $COMPOSER_CLI config repositories.hyva-themes/commerce-module-admin-theme git git@gitlab.hyva.io:hyva-commerce/module-admin-theme.git $COMPOSER_CLI config repositories.hyva-themes/commerce-theme-adminhtml git git@gitlab.hyva.io:hyva-commerce/theme-adminhtml.git $COMPOSER_CLI config repositories.hyva-themes/commerce-module-admin-dashboard git git@gitlab.hyva.io:hyva-commerce/module-admin-dashboard.git # Dev Deps $COMPOSER_CLI config repositories.hyva-themes/commerce-module-admin-dashboard-google-crux-history-widget git git@gitlab.hyva.io:hyva-commerce/module-admin-dashboard-google-crux-history-widget.git $COMPOSER_CLI config repositories.hyva-themes/commerce-module-media-optimization git git@gitlab.hyva.io:hyva-commerce/module-media-optimization.git echo "Installing Hyva theme..." $COMPOSER_CLI require hyva-themes/magento2-default-theme --prefer-source } function mage_add_hyva_checkout() { echo "Installing Hyva Checkout..." $COMPOSER_CLI require hyva-themes/magento2-hyva-checkout } function mage_add_hyva_commerce() { local use_hyva_production=$1 echo "Installing Hyva Commerce..." if [[ $use_hyva_production =~ ^[yY]|[yY][eE][sS]$ ]]; then $COMPOSER_CLI require hyva-themes/commerce else $COMPOSER_CLI require hyva-themes/commerce-module-cms $COMPOSER_CLI require hyva-themes/commerce-module-image-editor $COMPOSER_CLI require hyva-themes/commerce-theme-adminhtml $COMPOSER_CLI require hyva-themes/commerce-module-admin-dashboard fi } function mage_build_hyva() { if [ ! -d vendor/hyva-themes/magento2-default-theme/web/tailwind/node_modules ]; then $NPM_CLI --prefix vendor/hyva-themes/magento2-default-theme/web/tailwind install; fi $NPM_CLI --prefix vendor/hyva-themes/magento2-default-theme/web/tailwind run build; } MAGE_VERSION="2.3.4" function mage_version() { echo -e "\n${BOLD}Mage ${GREEN}${MAGE_VERSION}${RESET}" echo -e "${ITALIC}See https://github.com/GrimLink/mage for the last version${RESET}\n" } function mage_help_cmd() { echo -e "- ${BLUE}${1}${RESET} (${2})" } function mage_help() { echo -e "${BOLD}CMD Options:${RESET}" mage_help_cmd "self-update" "Update mage" mage_help_cmd "info" "Show base config for store" mage_help_cmd "start" "Open store and admin with code editor and git client" mage_help_cmd "open" "Open default store view" mage_help_cmd "open admin" "Open store admin" mage_help_cmd "open [storeview]" "Open specific store view" mage_help_cmd "stores" "Show all stores" mage_help_cmd "watch" "Run cache-clean.js from mageTV" mage_help_cmd "browser-sync" "Run browser-sync on default store view" mage_help_cmd "browser-sync [storeview]" "Run browser-sync on specific store view" mage_help_cmd "reindex" "Reindex all indexes and clear cache" mage_help_cmd "purge" "Purge all static assets" mage_help_cmd "new admin" "Create new admin user" mage_help_cmd "new customer" "Create new customer" mage_help_cmd "new theme" "Create new theme" mage_help_cmd "new module" "Create new module" mage_help_cmd "new patch" "Create new patch" mage_help_cmd "new i18n [src] | new translate [src]" "Create new translations" mage_help_cmd "add patch" "Add patch" mage_help_cmd "add sample" "Add sample data" mage_help_cmd "add hyva" "Add Hyva Theme" mage_help_cmd "add checkout" "Add Hyva Checkout" mage_help_cmd "add baldr" "Add Siteation Baldr" mage_help_cmd "set mage-os" "Replace Magento2 with Mage-OS distro" mage_help_cmd "set theme [theme]" "Run yireo theme:change" mage_help_cmd "set hyva" "Set theme: Hyva default" mage_help_cmd "set baldr" "Set theme: Siteation Baldr" mage_help_cmd "set mode [mode]" "Set deploy mode with admin settings for dev or prod" mage_help_cmd "set countries [list_countries]" "Set the store countries" mage_help_cmd "set fsp | set fpc default" "Set the full page cache to system" mage_help_cmd "set fpc varnish" "Set the full page cache to varnish" mage_help_cmd "log | log debug" "watch the debug log" mage_help_cmd "log exception" "watch the exception log" mage_help_cmd "log system" "watch the system log" mage_help_cmd "outdated" "Show all direct outdated composer dependencies" mage_help_cmd "build" "Run setup:static-content:deploy with common defaults" mage_help_cmd "run" "Run magerun2" echo -e "\n${ITALIC}Anything else will run ${BLUE}bin/magento${RESET}, ${ITALIC}To view this again, run ${BLUE}mage help${RESET}" } function mage_info() { echo -e "Magento: $GREEN$($MAGENTO_CLI --version | sed 's/Magento CLI //')$RESET" if $COMPOSER_CLI show hyva-themes/magento2-default-theme > /dev/null 2>&1; then local hyva_version="$(get_composer_pkg_version 'hyva-themes/magento2-theme-module')" local hyva_theme_version="$(get_composer_pkg_version 'hyva-themes/magento2-default-theme')" echo -e "Hyva: $GREEN$hyva_version$RESET" if [ "$hyva_version" != "$hyva_theme_version" ]; then echo -e " - Theme: $GREEN$hyva_theme_version$RESET" fi echo -e " - Modules: $($MAGENTO_CLI module:status | grep 'Hyva_' | sed 's/Hyva_//g' | paste -sd ',')" fi echo -e "PHP: $GREEN$($PHP_CLI --version | grep ^PHP | cut -d' ' -f2)$RESET" echo -e "NODE: $GREEN$($NODE_CLI --version | sed 's/v//')$RESET" echo -e "Base URI: $(get_mage_base_uri)" echo -e "Admin URI: $(grep frontName app/etc/env.php | tail -1 | cut -d ">" -f2 | cut -d "'" -f2)" echo -e "Database: $(grep dbname app/etc/env.php | tail -1 | cut -d ">" -f2 | cut -d "'" -f2)" $MAGENTO_CLI deploy:mode:show $MAGENTO_CLI maintenance:status if [[ -n "$MAGENTO_CLI config:show catalog/search/engine" ]]; then echo -e "Search Engine: $($MAGENTO_CLI config:show catalog/search/engine)" fi } function convert_to_mage_os() { if ! $COMPOSER_CLI show magento/product-community-edition >/dev/null 2>&1; then echo "This is not a Magento Community installation" && exit 1 fi if $COMPOSER_CLI show mage-os/product-community-edition >/dev/null 2>&1; then echo "Mage-OS already installed!" && exit 1 fi echo "Consider removing any composer replaces, before starting"; echo "Consider removing any 3de party moduels that could impact the conversion"; read -rsn1 -p "When ready, press any key to continue"; echo ""; # Setup $COMPOSER_CLI config repositories.0 composer https://repo.mage-os.org/ $COMPOSER_CLI require mage-os/product-community-edition --no-update $COMPOSER_CLI remove magento/product-community-edition magento/composer-dependency-version-audit-plugin magento/composer-root-update-plugin --no-update $COMPOSER_CLI remove sebastian/comparator --dev --no-update # remove if present $COMPOSER_CLI config allow-plugins.'mage-os/*' true rm -rf vendor # Install $COMPOSER_CLI update --no-plugins --with-all-dependencies mage_purge # Cleanup caches $MAGENTO_CLI s:up } function mage_new_in_folder() { if [[ ! -d package-source ]]; then mkdir package-source fi read -e -p "Create in package-source as local composer package? [y/N] " echo "" if [[ $REPLY =~ ^[yY]|[yY][eE][sS]$ ]]; then echo "package-source"; else echo $1 fi; } function mage_new_theme() { local application="frontend" local default_parrent_theme="Hyva/default" read -e -p "Is this a admin theme? [y/N] " echo "" if [[ $REPLY =~ ^[yY]|[yY][eE][sS]$ ]]; then local application="adminhtml" fi; local dest_path=$(mage_new_in_folder "app/design/${application}") read -e -p "Theme Name: " theme_name if [[ -z "$theme_name" ]]; then echo "The 'Name' can not be empty" && exit 1; fi if [[ "$theme_name" == */* ]]; then local theme_vendor="${theme_name%%/*}" local theme_name="${theme_name#*\/}" else read -e -p "Theme Vendor: " theme_vendor if [[ -z "$theme_vendor" ]]; then echo "The 'vendor' can not be empty" && exit 1; fi fi read -e -p "Parrent Theme ($default_parrent_theme): " parrent_theme if [[ -z "$parrent_theme" ]]; then parrent_theme=$default_parrent_theme; fi local theme_vendor="$(echo "$theme_vendor" | tr -d '[:blank:]')" local theme_name="$(echo "$theme_name" | tr -d '[:blank:]')" local folder_name="${theme_vendor}/$(mage_kebab_case "$theme_name")" local dest_path="$dest_path/$folder_name" local file_registration="\n\t