SHARE
TWEET

Untitled

a guest Oct 20th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5. SELF_BASENAME=`basename $0`
  6. ROOT_DIR=`pwd`
  7.  
  8. usage(){
  9. cat << EOF
  10. usage: ${SELF_BASENAME} [options] <COMMAND>
  11.  
  12. Execute pub command in each child dir were "pubspec.yaml" will be found
  13.   "flutter pub <COMMAND>" will be executed if dependency to flutter will be found
  14.   "pub <COMMAND>" - in other cases
  15.  
  16.  
  17. OPTIONS:
  18.    -h     Show this help
  19.    -r     Execute command only in dirs where build_runner has dependencies
  20.    -f     Run command in foreground    
  21. EOF
  22. }
  23.  
  24. CURRENT_ACTION="print_version"
  25.  
  26. BUILD_RUNNER_ONLY=0
  27. RUN_IN_BACKGROUND=1
  28.  
  29. while getopts “:hrf” OPTION
  30. do
  31.      case $OPTION in
  32.          h)
  33.              usage
  34.              exit 1
  35.              ;;
  36.          r)
  37.              BUILD_RUNNER_ONLY=1
  38.              ;;
  39.          f)
  40.              RUN_IN_BACKGROUND=0
  41.              ;;
  42.          ?)
  43.              usage
  44.              exit 1
  45.              ;;
  46.      esac
  47.      shift
  48. done
  49.  
  50. COMMAND=$@
  51.  
  52.  
  53.  
  54. PUBSPEC_FILES=`find . -name pubspec.yaml | sed "s|^\./||"`
  55.  
  56.  
  57. execute_with_prefix() {
  58.   local prefix="$1"
  59.   shift
  60.   $@ > >(sed "s|^|[${prefix}]: |") 2> >(sed "s|^|[${prefix} (err)]:|" >&2)
  61. }
  62.  
  63.  
  64. #
  65. if [[ "${BUILD_RUNNER_ONLY}" -eq "1" ]]; then
  66.     echo "[${SELF_BASENAME}]Filtering with packages has build_runner dependencies"
  67.   NEW_PUBSPEC_FILES=""
  68.   for PUBSPEC_FILE in ${PUBSPEC_FILES}; do
  69.     if awk '!/^ *#/ && NF' "${ROOT_DIR}/${PUBSPEC_FILE}" | grep build_runner > /dev/null; then
  70.       echo "[${SELF_BASENAME}] Found build_runner in ${PUBSPEC_FILE}"
  71.       NEW_PUBSPEC_FILES="${NEW_PUBSPEC_FILES} ${PUBSPEC_FILE}"
  72.     else  
  73.       echo "[${SELF_BASENAME}] Not found build_runner in ${PUBSPEC_FILE}"
  74.     fi
  75.     PUBSPEC_FILES="${NEW_PUBSPEC_FILES}"
  76.   done 
  77. fi
  78.  
  79. for PUBSPEC_FILE in ${PUBSPEC_FILES}; do
  80.   PUBSPEC_FILE_DIR=`dirname ${PUBSPEC_FILE}`
  81.   cd ${ROOT_DIR}/${PUBSPEC_FILE_DIR};
  82.   echo "[${SELF_BASENAME}]==================================";
  83.   echo "[${SELF_BASENAME}]==>> ${PUBSPEC_FILE_DIR}"
  84.  
  85.   if awk '!/^ *#/ && NF' "${ROOT_DIR}/${PUBSPEC_FILE}" | grep flutter > /dev/null; then
  86.     echo "[${SELF_BASENAME}]==>> Flutter detected"
  87.     PUB_EXEC="flutter pub"
  88.   else
  89.   echo "[${SELF_BASENAME}]==>> Generic DART project"  
  90.     PUB_EXEC="pub"
  91.   fi
  92.  
  93.   FULL_COMMAND="${PUB_EXEC} ${COMMAND}"
  94.   if [[ ${RUN_IN_BACKGROUND} -eq 1 ]]; then
  95.     echo "[${SELF_BASENAME}]==>> Run in background: \"${FULL_COMMAND}\""
  96.     execute_with_prefix ${PUBSPEC_FILE_DIR} ${FULL_COMMAND} &
  97.   else
  98.     echo "[${SELF_BASENAME}]==>> Run in foreground: \"${FULL_COMMAND}\""
  99.     execute_with_prefix ${PUBSPEC_FILE_DIR} ${FULL_COMMAND}
  100.   fi   
  101.   echo "[${SELF_BASENAME}]==================================";  
  102. done;
  103.  
  104. if [[ ${RUN_IN_BACKGROUND} -eq 1 ]]; then
  105.     echo "[${SELF_BASENAME}]==================================";    
  106.     echo "[${SELF_BASENAME}]==>> Waiting for the background tasks to complete"
  107.     wait
  108.     echo "[${SELF_BASENAME}]==>> Background tasks done"
  109.     echo "[${SELF_BASENAME}]==================================";
  110. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top