assets.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. CURRENT_DIR="$(dirname "$0")"
  3. SOURCE_FILE=${1}
  4. TARGET_FILE=${2}
  5. # Check if source file is set
  6. if [[ "${SOURCE_FILE}" = "" ]]; then
  7. echo "Source file is not set"
  8. exit 1
  9. fi
  10. # Check if target file is set
  11. if [[ "${TARGET_FILE}" = "" ]]; then
  12. # Try to generate from source file name
  13. TARGET_FILE=$(echo "${SOURCE_FILE}" | sed 's/\.dev\.\(css\|js\)$/.\1/')
  14. if [[ "${TARGET_FILE}" = "" ]]; then
  15. echo "Target file is not set"
  16. exit 1
  17. fi
  18. fi
  19. # Check if source file exists
  20. if [[ ! -f "${SOURCE_FILE}" ]]; then
  21. echo "Source file does not exists: ${SOURCE_FILE}"
  22. exit 1
  23. fi
  24. # Re-create empty target file
  25. if [[ -f "${TARGET_FILE}" ]]; then
  26. rm ${TARGET_FILE}
  27. fi
  28. touch ${TARGET_FILE}
  29. # Create cache dir if not exists
  30. if [[ ! -d "${CURRENT_DIR}/.cache" ]]; then
  31. mkdir "${CURRENT_DIR}/.cache"
  32. fi
  33. IFS=""
  34. while read -r line; do
  35. if [[ ${line} == "/*"* ]]; then
  36. if [[ ${line} == *"*/" ]]; then
  37. if [[ ${line} == *"import("* ]]; then
  38. FILE_TO_IMPORT=$(echo "${line}" | grep -oP "/*\s?import\(\K[^)]+")
  39. CACHE_NAME=$(echo "${FILE_TO_IMPORT}" | md5sum | cut -f1 -d" ")
  40. CACHE_NAME=$(echo "${CACHE_NAME}-$(basename ${FILE_TO_IMPORT})")
  41. CACHE_FILE="${CURRENT_DIR}/.cache/${CACHE_NAME}"
  42. DATA_TO_IMPORT=""
  43. # Local import
  44. if [[ ${FILE_TO_IMPORT} == "./"* ]]; then
  45. FILE_TO_IMPORT="$(dirname "${SOURCE_FILE}")$(echo "${FILE_TO_IMPORT}" | sed 's/^\.//')"
  46. if [[ -f "${FILE_TO_IMPORT}" ]]; then
  47. DATA_TO_IMPORT=$(cat "${FILE_TO_IMPORT}")
  48. fi
  49. fi
  50. # Remote import
  51. if [[ ${FILE_TO_IMPORT} == "http"* ]]; then
  52. if [[ ! -f "${CACHE_FILE}" ]]; then
  53. DATA_TO_IMPORT=$(curl -s "${FILE_TO_IMPORT}")
  54. echo "${DATA_TO_IMPORT}" > ${CACHE_FILE}
  55. else
  56. DATA_TO_IMPORT=$(cat ${CACHE_FILE})
  57. fi
  58. fi
  59. echo "${DATA_TO_IMPORT}" >> ${TARGET_FILE}
  60. else
  61. echo "${line}" >> ${TARGET_FILE}
  62. fi
  63. else
  64. echo "${line}" >> ${TARGET_FILE}
  65. fi
  66. else
  67. echo "${line}" >> ${TARGET_FILE}
  68. fi
  69. done < ${SOURCE_FILE}
  70. # Minify target file (CSS, JS)
  71. # Install yui-compressor by command:
  72. # sudo apt-get install yui-compressor
  73. CHECK_YUI_COMPRESSOR=$(command -v yui-compressor 2> /dev/null)
  74. if [[ "${CHECK_YUI_COMPRESSOR}" != "" ]]; then
  75. yui-compressor ${TARGET_FILE} -o ${TARGET_FILE}
  76. fi