Skip to content

Instantly share code, notes, and snippets.

@IlanVivanco
Last active April 8, 2025 18:48
Show Gist options
  • Save IlanVivanco/3c8dceb7b3966dfeb8bea16940f26a7b to your computer and use it in GitHub Desktop.
Save IlanVivanco/3c8dceb7b3966dfeb8bea16940f26a7b to your computer and use it in GitHub Desktop.
Creates a signoff folder based on a git branch
#!/bin/bash
# Get current branch name
current_branch=$(git branch --show-current | sed 's|/|-|g')
# Create signoff folder
signoff_dir="signoff"
mkdir -p "$signoff_dir"
# Always use current directory as source
src_dir="."
# Create list of files to include
include_files=(
"classes"
"composer.json"
"composer.lock"
"datos-salesforce.php"
"public"
"templates"
)
# Check if the source directory contains any required files
found_files=0
for file in "${include_files[@]}"; do
if [ -e "$src_dir/$file" ]; then
found_files=$((found_files + 1))
fi
done
if [ $found_files -eq 0 ]; then
echo "❌ Error: No files found in current directory"
exit 1
fi
# Create a temporary directory for the package contents
temp_dir=$(mktemp -d)
# Copy files to temp directory
for file in "${include_files[@]}"; do
if [ -e "$src_dir/$file" ]; then
cp -r "$src_dir/$file" "$temp_dir/"
else
echo "Warning: $file not found in source directory"
fi
done
# Remove node_modules and vendor directories
if [ -d "$temp_dir/node_modules" ]; then
rm -rf "$temp_dir/node_modules"
fi
if [ -d "$temp_dir/inc/vendor" ]; then
rm -rf "$temp_dir/inc/vendor"
fi
if [ -d "$temp_dir/inc/imports" ]; then
rm -rf "$temp_dir/inc/imports"
fi
# Remove specified imports from functions.php
if [ -f "$temp_dir/functions.php" ]; then
# Create a temp file
temp_functions=$(mktemp)
# Create a temporary pattern file for the exact text to remove
pattern_file=$(mktemp)
cat >"$pattern_file" <<'EOF'
/**
* Imports config.
*/
require get_stylesheet_directory() . '/inc/imports/actions.php';
require get_stylesheet_directory() . '/inc/imports/utils.php';
EOF
# Remove the imports block using grep
grep -v -F -f "$pattern_file" "$temp_dir/functions.php" >"$temp_functions"
# Replace the original file
mv "$temp_functions" "$temp_dir/functions.php"
# cat "$temp_dir/functions.php"
# Clean up the pattern file
rm -f "$pattern_file"
fi
# Create the tar file
tar_filename="$signoff_dir/my-plugin-${current_branch}.tgz"
tar -czf "$tar_filename" -C "$temp_dir" .
# List the contents of the package and save to a file
contents_file="$signoff_dir/my-plugin-${current_branch}-contents.txt"
tar -tf "$tar_filename" | sort >"$contents_file"
total_files=$(tar -tf "$tar_filename" | wc -l)
size=$(du -h "$tar_filename" | cut -f1)
echo "=== Package contents summary ==="
# echo "-------------------------"
# tar -tf "$tar_filename" | sort
# echo "-------------------------"
echo ""
echo "🌿 Current branch: $current_branch"
echo "πŸ“ Created signoff directory: $signoff_dir"
echo "πŸ“ Using source directory: $src_dir"
echo ""
echo "βœ… Package created successfully!"
echo "πŸ“’ Total files: $total_files"
echo "πŸ“ Size: $size"
echo "πŸ“¦ Package: $tar_filename"
echo "πŸ“¦ Contents: $contents_file"
# Clean up
rm -rf "$temp_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment