[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/build-script/ -> yourls-build.sh (source)

   1  #!/bin/bash
   2  
   3  ####################################################################
   4  # This file is part of YOURLS
   5  #
   6  # List (and remove) unneeded files for production
   7  #
   8  # Run this script when adding, updating or removing a 3rd party
   9  # library that goes in the `vendor` directory.
  10  #
  11  # Typical use:
  12  #
  13  # $ composer update --no-dev --prefer-dist
  14  # $ ./includes/vendor/build-script/yourls-build.sh ./includes/vendor
  15  # $ commit & push
  16  #
  17  ####################################################################
  18  
  19  
  20  ## OPTIONS ##########################################################
  21  
  22  # This directories in /vendor won't be cleaned up
  23  # Must be explicit names, case sensitive, no wildcard eg "README.*"
  24  #
  25  PRESERVE_IN_VENDOR=(
  26      'composer'
  27      'build-script'
  28      'symfony'
  29  )
  30  
  31  # Files & dirs to keep in each library directory
  32  # Must be explicit names, case sensitive, no wildcard eg "README.*"
  33  #
  34  PRESERVE_IN_LIB=(
  35      'src'
  36      'library'
  37      'lib'
  38      'Psr'
  39      'README.md'
  40      'readme.md'
  41      'certificates'
  42  )
  43  
  44  # Nothing to edit past this line !
  45  
  46  
  47  ## VARS #############################################################
  48  
  49  # Default values.
  50  TESTRUN=true
  51  
  52  # Colors and fancyness
  53  RED='\033[0;31m'
  54  NORM='\033[0m'
  55  BOLD='\033[1m'
  56  GREEN='\033[0;32m'
  57  PURPLE='\033[0;35m'
  58  
  59  # Set Script Name variable
  60  SCRIPT=`basename $BASH_SOURCE[0]}`
  61  
  62  
  63  ## FUNCS ############################################################
  64  
  65  # Print help
  66  rtfm () {
  67      echo -e "\nUsage: "
  68      echo -e "   $BOLD}$SCRIPT}$NORM} [-dh] <directory to cleanup>"
  69      echo -e ""
  70      echo -e "Examples: "
  71      echo -e "   $BOLD}$SCRIPT}$NORM} [-dh] ."
  72      echo -e "   $BOLD}$SCRIPT}$NORM} [-dh] /some/path/to/clean"
  73      echo -e ""
  74      echo -e "Options:"
  75      echo -e "  $BOLD}-h$NORM}           Display this $BOLD}H$NORM}elp message"
  76      echo -e "  $BOLD}-d$NORM}           Actually $BOLD}D$NORM}elete files flagged by this script"
  77      echo -e ""
  78      exit 1
  79  }
  80  
  81  
  82  # in_array NEEDLE HAYSTACK
  83  # Return 0/1
  84  in_array () {
  85    local e
  86    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  87    return 1
  88  }
  89  
  90  
  91  # Cleans the mess
  92  cleanup () {
  93      # Return if function called with no parameter
  94      if [ -z "$1" ]
  95      then
  96          return
  97      fi
  98  
  99      # Directory we are in
 100      CUR=$1
 101  
 102      # Loop over each file and delete those we don't want to keep
 103      echo -e "$PURPLE}Cleaning: $(basename $(dirname "$CUR"))/$(basename "$CUR") $NORM}"
 104      for FILE in $(ls -A $CUR)
 105      do
 106  
 107          if in_array $FILE "$PRESERVE_IN_LIB[@]}"
 108          then
 109              echo -e "$GREEN}+$NORM} KEEP: $FILE"
 110          else
 111              echo -e "$RED}-$NORM} DEL : $FILE"
 112              maybe_delete "$CUR}$FILE}"
 113          fi
 114  
 115      done;
 116  
 117      # If directory is empty, delete
 118      if [ ! "$(ls -A $CUR)" ]
 119      then
 120          echo -e "$RED}-$NORM} del : $(basename "$CUR") (empty dir)"
 121          maybe_delete "$CUR"
 122      fi
 123  
 124      echo ""
 125  
 126  }
 127  
 128  # Delete file if not in test run
 129  maybe_delete () {
 130      if [ "$TESTRUN" = false ]
 131      then
 132          rm -rf "$1"
 133      fi
 134  }
 135  
 136  # Check the number of arguments. If none are passed, print help and exit.
 137  args_or_die () {
 138      if [ $1 -eq 0 ]; then
 139          rtfm
 140      fi
 141  }
 142  
 143  
 144  ## WORK #############################################################
 145  
 146  # We should have some arguments
 147  args_or_die "$#"
 148  
 149  # Check options
 150  while getopts "dh" opt; do
 151    case $opt in
 152      d)
 153        TESTRUN=false
 154        ;;
 155      h)
 156        rtfm
 157        ;;
 158      \?)
 159        rtfm
 160        ;;
 161    esac
 162  done
 163  
 164  shift $((OPTIND-1))  #This tells getopts to move on to the next argument.
 165  
 166  # Again, we should have some arguments after dealing with options if any
 167  # Yes, this isn't perfect, there should be one test. Will do.
 168  args_or_die "$#"
 169  
 170  # Check for valid dir
 171  if [ ! -d "$1" ]
 172  then
 173      echo -e "Need a valid directory, '$RED}$1$NORM}' is not."
 174      rtfm
 175  else
 176      # Resolve directory (expand '.' or '../stuff' as full path)
 177      TARGETDIR=$(cd "$1"; pwd)
 178  fi
 179  
 180  # Dry run notice if applicable
 181  if [ "$TESTRUN" = true ]
 182  then
 183      echo -e "Test mode. $RED}Nothing will be deleted$NORM}.\n"
 184      echo -e "Use with $RED}-d$NORM} if you are OK with the proposed changes (or clean up manually).\n"
 185  fi
 186  
 187  
 188  # 1. Get list of all directories in target directory, except the one
 189  #    listed in PRESERVE_IN_VENDOR that we don't want to touch
 190  #
 191  VENDORS=($(ls -d $TARGETDIR/*/))
 192  TEMP=(${VENDORS[@]})
 193  
 194  for (( i=0; i<${#VENDORS[@]}; i++ ))
 195  do
 196      DIR=$(basename "${VENDORS[i]}")
 197      if in_array "$DIR" "${PRESERVE_IN_VENDOR[@]}"
 198      then
 199          unset TEMP[$i]
 200      fi
 201  done
 202  
 203  VENDORS=(${TEMP[@]})
 204  
 205  # 2. Loop over each directory and clean up
 206  #
 207  for DIR in ${VENDORS[@]}
 208  do
 209      SUBDIRS=$(ls -d $DIR*/ 2>/dev/null)
 210      if [ ! -z "$SUBDIRS" ]
 211      then
 212          # This VENDORS directory has subdirectory: process each subdir
 213          for SUBDIR in $SUBDIRS
 214          do
 215              cleanup $SUBDIR
 216          done;
 217      else
 218          # This directory contains no subdirectory
 219          cleanup $DIR
 220      fi
 221  done
 222  
 223  # Exit reminder
 224  if [ "$TESTRUN" = true ]
 225  then
 226      echo -e "($GREEN}Nothing has been deleted$NORM})\n"
 227  else
 228      echo -e "... all done $GREEN ;) $NORM\n"
 229  fi
 230  
 231  
 232  


Generated: Thu Sep 19 05:10:04 2024 Cross-referenced by PHPXref 0.7.1