Skip to content

Commit

Permalink
Improve renaming feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ronilaukkarinen committed Aug 18, 2022
1 parent bee6697 commit 50a2436
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 1.1.8: 2022-08-18

* Add support for renaming ready-made blocks
* `newblock`: Add hero-big-image

### 1.1.7: 2022-08-18

Expand Down
37 changes: 37 additions & 0 deletions bin/blocks/hero-big-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
# @Author: Roni Laukkarinen
# @Date: 2022-08-18 13:02:37
# @Last Modified by: Roni Laukkarinen
# @Last Modified time: 2022-08-18 13:10:23

# // New files/Dependencies (this file will install them):
# // ├── sass/gutenberg/blocks/_hero-big-image.scss (automatic from get-block.sh)
# // ├── sass/components/_content-box.scss
# // └── svg/block-icons/hero-big-image.svg

# // Changes to files/folders:
# // ├── sass/components/_components.scss
# // ├── sass/gutenberg/_blocks.scss
# // ├── acf-json/
# // └── functions.php

# Block specific variables
export BLOCK_ACF_JSON_FILE="group_62022454b648d.json"
export BLOCK_ACF_JSON_PATH="${AIRBLOCKS_THEME_PATH}/acf-json/${BLOCK_ACF_JSON_FILE}"

# Style component dependencies
cp -nv ${AIRBLOCKS_THEME_PATH}/sass/components/_content-box.scss ${PROJECT_THEME_PATH}/sass/components/

# Import components right after the last default component in the _components.scss file
sed -e "/\@import \'link\'\;/a\\
@import 'content-box';" < ${PROJECT_THEME_PATH}/sass/components/_components.scss > ${PROJECT_THEME_PATH}/sass/components/_components_with_changes.scss
rm ${PROJECT_THEME_PATH}/sass/components/_components.scss
mv ${PROJECT_THEME_PATH}/sass/components/_components_with_changes.scss ${PROJECT_THEME_PATH}/sass/components/_components.scss

# Register ACF block in functions.php
# Please note: The title of the block will be translated in localization.sh if en is selected
sed -e "/\'acf_blocks\' \=\> \[/a\\
[|\
'name' => 'hero-big-image',|\
'title' => 'Sivun yläosa isolla taustakuvalla',|\
],\\" < ${PROJECT_THEME_PATH}/functions.php | tr '|' '\n' > ${PROJECT_THEME_PATH}/tmpfile
6 changes: 6 additions & 0 deletions bin/tasks/footer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
# @Date: 2021-11-23 18:28:35
# @Last Modified by: Roni Laukkarinen
# @Last Modified time: 2021-11-23 18:42:39

# Check if renamed block
if [[ $IS_RENAMED_BLOCK =~ "yes" ]]; then
export BLOCK_NAME=$BLOCK_NAME_TO_RENAME_TO
fi

echo ""
echo "${BOLDGREEN}All done!${TXTRESET} Block created: ${BLOCK_NAME}"
echo ""
Expand Down
27 changes: 14 additions & 13 deletions bin/tasks/get-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ else

# All the typical tasks that we do in every single block
# Checking if block already exists
if [[ -f "${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME}.php" && -f "${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME_TO_RENAME_TO}.php" ]]; then
if [[ -f "${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME}.php" && -f "${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME_TO_RENAME_TO}.php" ]] ; then
echo "
${RED}Block already exists. The newtheme script will now quit...${TXTRESET}
"
Expand All @@ -46,31 +46,32 @@ ${RED}Block already exists. The newtheme script will now quit...${TXTRESET}
# Block icon
mkdir -p ${PROJECT_THEME_PATH}/svg/block-icons
cp -nv ${AIRBLOCKS_THEME_PATH}/svg/block-icons/${BLOCK_NAME}.svg ${PROJECT_THEME_PATH}/svg/block-icons/

if [[ $IS_RENAMED_BLOCK =~ "yes" ]] ; then
echo "${YELLOW}Renaming block...${TXTRESET}"
mv ${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME}.php ${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME_TO_RENAME_TO}.php
mv ${PROJECT_THEME_PATH}/sass/gutenberg/blocks/${BLOCK_NAME}.scss ${PROJECT_THEME_PATH}/sass/gutenberg/blocks/${BLOCK_NAME_TO_RENAME_TO}.scss
mv ${PROJECT_THEME_PATH}/svg/block-icons/${BLOCK_NAME}.svg ${PROJECT_THEME_PATH}/svg/block-icons/${BLOCK_NAME_TO_RENAME_TO}.svg
else
echo ""
fi

fi
fi

# Start block import script
if [ -f "${SCRIPTS_LOCATION}/blocks/${BLOCK_NAME}.sh" ]; then
source ${SCRIPTS_LOCATION}/blocks/${BLOCK_NAME}.sh
else
echo "Block called $BLOCK_NAME does not exist (yet) in the importer script. Or perhaps you mistyped it?"
echo "
${RED}Block called $BLOCK_NAME does not exist (yet) in the importer script. Or perhaps you mistyped it?"
exit
fi
fi

if [[ ${AIR_BLOCKS_LANG} = "en" ]]; then
# Run localization task
source ${SCRIPTS_LOCATION}/tasks/localization.sh
fi

# Tasks that should be run after block-specific tasks
if [[ $IS_NEW_BLOCK =~ "yes" ]] ; then
source ${SCRIPTS_LOCATION}/tasks/post-block-empty-block.sh
else
source ${SCRIPTS_LOCATION}/tasks/post-block.sh
fi

# Check if renamed block
if [[ $IS_RENAMED_BLOCK =~ "yes" ]]; then
# Run rename task
source ${SCRIPTS_LOCATION}/tasks/rename.sh
fi
19 changes: 19 additions & 0 deletions bin/tasks/rename.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
echo "${YELLOW}Renaming block to '${BLOCK_NAME_TO_RENAME_TO}'...${TXTRESET}"
mv ${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME}.php ${PROJECT_THEME_PATH}/template-parts/blocks/${BLOCK_NAME_TO_RENAME_TO}.php
mv ${PROJECT_THEME_PATH}/sass/gutenberg/blocks/_${BLOCK_NAME}.scss ${PROJECT_THEME_PATH}/sass/gutenberg/blocks/_${BLOCK_NAME_TO_RENAME_TO}.scss
mv ${PROJECT_THEME_PATH}/svg/block-icons/${BLOCK_NAME}.svg ${PROJECT_THEME_PATH}/svg/block-icons/${BLOCK_NAME_TO_RENAME_TO}.svg

# Renaming block names in files
sed -i '' -e "s;${BLOCK_NAME};${BLOCK_NAME_TO_RENAME_TO};" ${PROJECT_THEME_PATH}/sass/gutenberg/_blocks.scss
sed -i '' -e "s;${BLOCK_NAME};${BLOCK_NAME_TO_RENAME_TO};" ${PROJECT_THEME_PATH}/functions.php

# Renaming block UI label
sed -i '' -e "s/'title' \=\> '.*',/'title' \=\> '${BLOCK_UI_TITLE_TO_RENAME_TO}',/" ${PROJECT_THEME_PATH}/functions.php

# Renaming block UI name in ACF
if [[ ${AIR_BLOCKS_LANG} = "fi" ]]; then
sed -i '' -e "s/\"title\"\: \"Lohko\: .*\"\,/\"title\"\: \"Lohko\: ${BLOCK_UI_TITLE_TO_RENAME_TO}\"\,/" ${PROJECT_THEME_PATH}/acf-json/${BLOCK_ACF_JSON_FILE}
else
sed -i '' -e "s/\"title\"\: \"Block\: .*\"\,/\"title\"\: \"Block\: ${BLOCK_UI_TITLE_TO_RENAME_TO}\"\,/" ${PROJECT_THEME_PATH}/acf-json/${BLOCK_ACF_JSON_FILE}
fi
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@
"config": {
"preferred-install": "dist",
"generate-salts": true,
"secure-http": true
"secure-http": true,
"allow-plugins": {
"pivvenit/acf-pro-installer": true,
"ffraenz/private-composer-installer": true,
"gotoandplay/gravityforms-composer-installer": true,
"composer/installers": true,
"johnpbloch/wordpress-core-installer": true,
"koodimonni/composer-dropin-installer": true
}
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 50a2436

Please sign in to comment.