assets.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. SOURCE_FILE=${1}
  3. TARGET_FILE=${2}
  4. # Check if source file is set
  5. if [[ "${SOURCE_FILE}" = "" ]]; then
  6. echo "Source file is not set"
  7. exit 1
  8. fi
  9. # Check if target file is set
  10. if [[ "${TARGET_FILE}" = "" ]]; then
  11. # Try to generate from source file name
  12. TARGET_FILE=$(echo "${SOURCE_FILE}" | sed 's/\.dev\.\(css\|js\)$/.\1/')
  13. if [[ "${TARGET_FILE}" = "" ]]; then
  14. echo "Target file is not set"
  15. exit 1
  16. fi
  17. fi
  18. # Check if source file exists
  19. if [[ ! -f "${SOURCE_FILE}" ]]; then
  20. echo "Source file does not exists: ${SOURCE_FILE}"
  21. exit 1
  22. fi
  23. # Re-create empty target file
  24. if [[ -f "${TARGET_FILE}" ]]; then
  25. rm ${TARGET_FILE}
  26. fi
  27. touch ${TARGET_FILE}
  28. IFS=""
  29. while read line; do
  30. if [[ ${line} == "/*"* ]]; then
  31. if [[ ${line} == *"*/" ]]; then
  32. if [[ ${line} == *"import("* ]]; then
  33. FILE_TO_IMPORT=$(echo "${line}" | grep -oP "/*\s?import\(\K[^)]+")
  34. DATA_TO_IMPORT=$(curl -s "${FILE_TO_IMPORT}")
  35. echo "${DATA_TO_IMPORT}" >> ${TARGET_FILE}
  36. else
  37. echo "${line}" >> ${TARGET_FILE}
  38. fi
  39. else
  40. echo "${line}" >> ${TARGET_FILE}
  41. fi
  42. else
  43. echo "${line}" >> ${TARGET_FILE}
  44. fi
  45. done < ${SOURCE_FILE}
  46. # Minify target file (CSS, JS)
  47. # Install yui-compressor by command:
  48. # sudo apt-get install yui-compressor
  49. CHECK_YUI_COMPRESSOR=$(command -v yui-compressor 2> /dev/null)
  50. if [[ "${CHECK_YUI_COMPRESSOR}" != "" ]]; then
  51. yui-compressor ${TARGET_FILE} -o ${TARGET_FILE}
  52. fi