Skip to content

Instantly share code, notes, and snippets.

@nielslange
Created April 5, 2025 13:09
Show Gist options
  • Save nielslange/ce2340724445bf0fe52a9d0851ce095e to your computer and use it in GitHub Desktop.
Save nielslange/ce2340724445bf0fe52a9d0851ce095e to your computer and use it in GitHub Desktop.
SMNTCS plugin scripts
#! /usr/bin/env bash
# Check if running with bash
if [ -z "$BASH_VERSION" ]; then
echo "This script requires bash. Please run it with: bash $0"
exit 1
fi
# ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Arrays to store dependency information
declare -a composer_deps=()
declare -a composer_versions=()
declare -a npm_deps=()
declare -a npm_versions=()
declare -a projects_with_composer=()
declare -a projects_with_npm=()
# Function to compare versions
compare_versions() {
local v1=$1
local v2=$2
# Remove any non-numeric and non-dot characters
v1=$(echo "$v1" | sed 's/[^0-9.]//g')
v2=$(echo "$v2" | sed 's/[^0-9.]//g')
if [ "$v1" = "$v2" ]; then
return 0
fi
local IFS=.
local i ver1=($v1) ver2=($v2)
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
return 2
fi
done
return 0
}
# Function to get latest version from packagist
get_latest_composer_version() {
local package=$1
curl -s "https://repo.packagist.org/p2/$package.json" | jq -r '.packages[][0].version' | head -n1
}
# Function to get latest version from pnpm
get_latest_npm_version() {
local package=$1
pnpm info "$package" version 2>/dev/null
}
# Function to add or update a composer dependency
add_composer_dependency() {
local key=$1
local value=$2
local project=$3
# Check if dependency already exists
local found=0
for i in "${!composer_deps[@]}"; do
if [ "${composer_deps[i]}" = "$key" ]; then
found=1
break
fi
done
if [ $found -eq 0 ]; then
composer_deps+=("$key")
fi
# Add version information
composer_versions+=("$key:$project:$value")
}
# Function to add or update an npm dependency
add_npm_dependency() {
local key=$1
local value=$2
local project=$3
# Check if dependency already exists
local found=0
for i in "${!npm_deps[@]}"; do
if [ "${npm_deps[i]}" = "$key" ]; then
found=1
break
fi
done
if [ $found -eq 0 ]; then
npm_deps+=("$key")
fi
# Add version information
npm_versions+=("$key:$project:$value")
}
# Function to extract dependencies from composer.json
extract_composer_deps() {
local file=$1
local project=$2
if [ -f "$file" ]; then
projects_with_composer+=("$project")
while IFS=':' read -r key value; do
# Remove quotes and whitespace
key=$(echo "$key" | tr -d '", ' | sed 's/require//')
value=$(echo "$value" | tr -d '", ')
if [ -n "$key" ] && [ -n "$value" ]; then
add_composer_dependency "$key" "$value" "$project"
fi
done < <(jq -r '.require | to_entries[] | "\(.key):\(.value)"' "$file" 2>/dev/null)
fi
}
# Function to extract dependencies from package.json
extract_npm_deps() {
local file=$1
local project=$2
if [ -f "$file" ]; then
projects_with_npm+=("$project")
while IFS=':' read -r key value; do
# Remove quotes and whitespace
key=$(echo "$key" | tr -d '", ')
value=$(echo "$value" | tr -d '", ')
if [ -n "$key" ] && [ -n "$value" ]; then
add_npm_dependency "$key" "$value" "$project"
fi
done < <(jq -r '.dependencies | to_entries[] | "\(.key):\(.value)"' "$file" 2>/dev/null)
fi
}
# Function to get versions for a composer dependency
get_composer_versions() {
local dep=$1
local -a result=()
for version in "${composer_versions[@]}"; do
if [[ $version == "$dep:"* ]]; then
result+=("$version")
fi
done
echo "${result[@]}"
}
# Function to get versions for an npm dependency
get_npm_versions() {
local dep=$1
local -a result=()
for version in "${npm_versions[@]}"; do
if [[ $version == "$dep:"* ]]; then
result+=("$version")
fi
done
echo "${result[@]}"
}
# Print headers
printf "${BLUE}┌──────────────────────────────────────────────┐${NC}\n"
printf "${BLUE}│ Dependency Sync Analysis │${NC}\n"
printf "${BLUE}└──────────────────────────────────────────────┘${NC}\n"
printf "\n"
# Process all projects
for dir in ~/plugins/smntcs/*; do
if [ -d "$dir" ]; then
project_name=$(basename "$dir")
printf "${YELLOW}Analyzing: ${BLUE}$project_name${NC}\n"
# Check for composer.json
if [ -f "$dir/composer.json" ]; then
printf " Found composer.json\n"
extract_composer_deps "$dir/composer.json" "$project_name"
fi
# Check for package.json
if [ -f "$dir/package.json" ]; then
printf " Found package.json\n"
extract_npm_deps "$dir/package.json" "$project_name"
fi
fi
done
# Print Composer dependency analysis with version comparison
if [ ${#projects_with_composer[@]} -gt 0 ]; then
printf "\n${GREEN}Composer Dependencies:${NC}\n"
for dep in "${composer_deps[@]}"; do
printf " ${BLUE}$dep${NC}:\n"
# Get all versions for this dependency
versions=($(get_composer_versions "$dep"))
declare -a unique_versions=()
for version in "${versions[@]}"; do
IFS=':' read -r _ project ver <<< "$version"
printf " - $project: $ver\n"
# Add to unique versions if not already present
if [[ ! " ${unique_versions[@]} " =~ " $ver " ]]; then
unique_versions+=("$ver")
fi
done
# Check for version inconsistencies
if [ ${#unique_versions[@]} -gt 1 ]; then
printf " ${RED}⚠ Version inconsistency detected!${NC}\n"
fi
# Get latest version and suggest update
latest_version=$(get_latest_composer_version "$dep")
if [ -n "$latest_version" ]; then
# Use the first version found as current
IFS=':' read -r _ _ current_version <<< "${versions[0]}"
compare_versions "$current_version" "$latest_version"
case $? in
2) printf " ${YELLOW}↑ Update available: $latest_version${NC}\n";;
0) printf " ${GREEN}✓ Up to date${NC}\n";;
1) printf " ${BLUE}ℹ Using newer version than latest${NC}\n";;
esac
fi
printf "\n"
done
fi
# Print npm dependency analysis with version comparison
if [ ${#projects_with_npm[@]} -gt 0 ]; then
printf "\n${GREEN}pnpm Dependencies:${NC}\n"
for dep in "${npm_deps[@]}"; do
printf " ${BLUE}$dep${NC}:\n"
# Get all versions for this dependency
versions=($(get_npm_versions "$dep"))
declare -a unique_versions=()
for version in "${versions[@]}"; do
IFS=':' read -r _ project ver <<< "$version"
printf " - $project: $ver\n"
# Add to unique versions if not already present
if [[ ! " ${unique_versions[@]} " =~ " $ver " ]]; then
unique_versions+=("$ver")
fi
done
# Check for version inconsistencies
if [ ${#unique_versions[@]} -gt 1 ]; then
printf " ${RED}⚠ Version inconsistency detected!${NC}\n"
fi
# Get latest version and suggest update
latest_version=$(get_latest_npm_version "$dep")
if [ -n "$latest_version" ]; then
# Use the first version found as current
IFS=':' read -r _ _ current_version <<< "${versions[0]}"
compare_versions "$current_version" "$latest_version"
case $? in
2) printf " ${YELLOW}↑ Update available: $latest_version${NC}\n";;
0) printf " ${GREEN}✓ Up to date${NC}\n";;
1) printf " ${BLUE}ℹ Using newer version than latest${NC}\n";;
esac
fi
printf "\n"
done
fi
# Print project summary
printf "\n${GREEN}Project Summary:${NC}\n"
printf " Projects with Composer: ${#projects_with_composer[@]}\n"
printf " Projects with pnpm: ${#projects_with_npm[@]}\n"
# Check if jq is installed
if ! command -v jq &> /dev/null; then
printf "\n${RED}Warning:${NC} jq is not installed. Please install it for better dependency analysis:\n"
printf " macOS: brew install jq\n"
printf " Linux: sudo apt-get install jq\n"
fi
#! /bin/bash
# ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Arrays to track results
declare -a failed_plugins=()
declare -a successful_plugins=()
printf "${BLUE}┌──────────────────────────────────────────────┐${NC}\n"
printf "${BLUE}│ WordPress Plugin Sync │${NC}\n"
printf "${BLUE}└──────────────────────────────────────────────┘${NC}\n"
printf "\n"
for dir in ~/plugins/smntcs/*; do
if [ -d "$dir/.git" ]; then
plugin_name=$(basename "$dir")
printf "${YELLOW}Syncing: ${BLUE}$plugin_name${NC}\n"
cd "$dir" || {
printf "${RED}Failed to change directory to $dir${NC}\n"
failed_plugins+=("$plugin_name (Directory access failed)")
continue
}
if git pull; then
printf "${GREEN}✓ Successfully synced $plugin_name${NC}\n"
successful_plugins+=("$plugin_name")
else
printf "${RED}✗ Failed to sync $plugin_name${NC}\n"
failed_plugins+=("$plugin_name (Git pull failed)")
fi
cd - > /dev/null
printf "\n"
fi
done
# Print summary
printf "${BLUE}┌──────────────────────────────────────────────┐${NC}\n"
printf "${BLUE}│ Sync Summary │${NC}\n"
printf "${BLUE}└──────────────────────────────────────────────┘${NC}\n"
printf "\n"
if [ ${#successful_plugins[@]} -gt 0 ]; then
printf "${GREEN}Successfully synced:${NC}\n"
for plugin in "${successful_plugins[@]}"; do
printf "$plugin\n"
done
printf "\n"
fi
if [ ${#failed_plugins[@]} -gt 0 ]; then
printf "${RED}Failed to sync:${NC}\n"
for plugin in "${failed_plugins[@]}"; do
printf "$plugin\n"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment