Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.7 KiB

  1. #!/bin/bash
  2. set -e
  3. # Script settings
  4. version=$(grep APP_VERSION espurna/config/version.h | awk '{print $3}' | sed 's/"//g')
  5. (command -v git && git rev-parse --is-inside-work-tree) 2>&1>/dev/null
  6. if [ $? -eq 0 ]; then
  7. git_revision=$(git rev-parse --short HEAD)
  8. git_version=$(git describe --tags)
  9. else
  10. git_revision=
  11. git_version=$version
  12. fi
  13. par_build=0
  14. par_thread=${BUILDER_THREAD:-0}
  15. par_total_threads=${BUILDER_TOTAL_THREADS:-4}
  16. if [ ${par_thread} -ne ${par_thread} -o \
  17. ${par_total_threads} -ne ${par_total_threads} ]; then
  18. echo "Parallel threads should be a number."
  19. exit
  20. fi
  21. if [ ${par_thread} -ge ${par_total_threads} ]; then
  22. echo "Current thread is greater than total threads. Doesn't make sense"
  23. exit
  24. fi
  25. # Available environments
  26. travis=$(grep env: platformio.ini | grep travis | sed 's/\[env://' | sed 's/\]/ /' | sort)
  27. available=$(grep env: platformio.ini | grep -v ota | grep -v ssl | grep -v travis | sed 's/\[env://' | sed 's/\]/ /' | sort)
  28. # Build tools settings
  29. export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DAPP_REVISION='\"$git_revision\"'"
  30. # Functions
  31. print_available() {
  32. echo "--------------------------------------------------------------"
  33. echo "Available environments:"
  34. for environment in $available; do
  35. echo "* $environment"
  36. done
  37. }
  38. print_environments() {
  39. echo "--------------------------------------------------------------"
  40. echo "Current environments:"
  41. for environment in $environments; do
  42. echo "* $environment"
  43. done
  44. }
  45. set_default_environments() {
  46. # Hook to build in parallel when using travis
  47. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Release" ]] && [ ${par_build} ]; then
  48. environments=$(echo ${available} | \
  49. awk -v par_thread=${par_thread} -v par_total_threads=${par_total_threads} \
  50. '{ for (i = 1; i <= NF; i++) if (++j % par_total_threads == par_thread ) print $i; }')
  51. return
  52. fi
  53. # Only build travisN
  54. if [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]]; then
  55. environments=$travis
  56. return
  57. fi
  58. # Fallback to all available environments
  59. environments=$available
  60. }
  61. build_webui() {
  62. # Build system uses gulpscript.js to build web interface
  63. if [ ! -e node_modules/gulp/bin/gulp.js ]; then
  64. echo "--------------------------------------------------------------"
  65. echo "Installing dependencies..."
  66. npm install --only=dev
  67. fi
  68. # Recreate web interface (espurna/data/index.html.*.gz.h)
  69. echo "--------------------------------------------------------------"
  70. echo "Building web interface..."
  71. node node_modules/gulp/bin/gulp.js || exit
  72. }
  73. build_environments() {
  74. echo "--------------------------------------------------------------"
  75. echo "Building firmware images..."
  76. mkdir -p ../firmware/espurna-$version
  77. for environment in $environments; do
  78. echo "* espurna-$version-$environment.bin"
  79. platformio run --silent --environment $environment || exit 1
  80. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  81. mv .pioenvs/$environment/firmware.bin ../firmware/espurna-$version/espurna-$version-$environment.bin
  82. done
  83. echo "--------------------------------------------------------------"
  84. }
  85. # Parameters
  86. while getopts "lp" opt; do
  87. case $opt in
  88. l)
  89. print_available
  90. exit
  91. ;;
  92. p)
  93. par_build=1
  94. ;;
  95. esac
  96. done
  97. shift $((OPTIND-1))
  98. # Welcome
  99. echo "--------------------------------------------------------------"
  100. echo "ESPURNA FIRMWARE BUILDER"
  101. echo "Building for version ${git_version}"
  102. # Environments to build
  103. environments=$@
  104. if [ $# -eq 0 ]; then
  105. set_default_environments
  106. fi
  107. if [[ "${CI}" = true ]]; then
  108. print_environments
  109. fi
  110. build_webui
  111. build_environments