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.

189 lines
4.8 KiB

  1. #!/bin/bash
  2. set -e
  3. # Utility
  4. is_git() {
  5. command -v git >/dev/null 2>&1 || return 1
  6. command git rev-parse >/dev/null 2>&1 || return 1
  7. return 0
  8. }
  9. stat_bytes() {
  10. case "$(uname -s)" in
  11. Darwin) stat -f %z "$1";;
  12. *) stat -c %s "$1";;
  13. esac
  14. }
  15. # Script settings
  16. destination=../firmware
  17. version_file=espurna/config/version.h
  18. version=$(grep -E '^#define APP_VERSION' $version_file | awk '{print $3}' | sed 's/"//g')
  19. script_build_environments=true
  20. script_build_webui=true
  21. release_mode=false
  22. if ${TRAVIS:-false}; then
  23. git_revision=${TRAVIS_COMMIT::7}
  24. git_tag=${TRAVIS_TAG}
  25. elif is_git; then
  26. git_revision=$(git rev-parse --short HEAD)
  27. git_tag=$(git tag --contains HEAD)
  28. else
  29. git_revision=unknown
  30. git_tag=
  31. fi
  32. if [[ -n $git_tag ]]; then
  33. new_version=${version/-*}
  34. sed -i -e "s@$version@$new_version@" $version_file
  35. version=$new_version
  36. trap "git checkout -- $version_file" EXIT
  37. fi
  38. # Available environments
  39. list_envs() {
  40. grep -E '^\[env:' platformio.ini | sed 's/\[env:\(.*\)\]/\1/g'
  41. }
  42. available=$(list_envs | grep -Ev -- '-ota$|-ssl$|-secure-client.*$|^esp8266-.*base$' | sort)
  43. # Functions
  44. print_available() {
  45. echo "--------------------------------------------------------------"
  46. echo "Available environments:"
  47. for environment in $available; do
  48. echo "* $environment"
  49. done
  50. }
  51. print_environments() {
  52. echo "--------------------------------------------------------------"
  53. echo "Current environments:"
  54. for environment in $environments; do
  55. echo "* $environment"
  56. done
  57. }
  58. set_default_environments() {
  59. # Fallback to all available environments
  60. environments=$available
  61. }
  62. build_webui() {
  63. # Build system uses gulpscript.js to build web interface
  64. if [ ! -e node_modules/gulp/bin/gulp.js ]; then
  65. echo "--------------------------------------------------------------"
  66. echo "Installing dependencies..."
  67. npm install --only=dev
  68. fi
  69. # Recreate web interface (espurna/data/index.html.*.gz.h)
  70. echo "--------------------------------------------------------------"
  71. echo "Building web interface..."
  72. node node_modules/gulp/bin/gulp.js || exit
  73. # TODO: do something if webui files are different
  74. # for now, just print in travis log
  75. if ${TRAVIS:-false}; then
  76. git --no-pager diff --stat
  77. fi
  78. }
  79. build_release() {
  80. echo "--------------------------------------------------------------"
  81. echo "Building release images..."
  82. python scripts/generate_release_sh.py --ignore secure-client $version > release.sh
  83. bash release.sh
  84. echo "--------------------------------------------------------------"
  85. }
  86. build_environments() {
  87. echo "--------------------------------------------------------------"
  88. echo "Building firmware images..."
  89. mkdir -p $destination/espurna-$version
  90. for environment in $environments; do
  91. echo "* espurna-$version-$environment.bin"
  92. platformio run --silent --environment $environment || exit 1
  93. echo -n "SIZE: "
  94. stat_bytes .pio/build/$environment/firmware.bin
  95. [[ "${TRAVIS_BUILD_STAGE_NAME}" = "Test" ]] || \
  96. mv .pio/build/$environment/firmware.bin $destination/espurna-$version/espurna-$version-$environment.bin
  97. done
  98. echo "--------------------------------------------------------------"
  99. }
  100. # Parameters
  101. print_getopts_help() {
  102. cat <<EOF
  103. Usage: $(basename "$0") [OPTION] <ENVIRONMENT>...
  104. Where ENVIRONMENT is environment name(s) from platformio.ini
  105. Options:
  106. -f VALUE Filter build stage by name to skip it
  107. Supported VALUEs are "environments" and "webui"
  108. Can be specified multiple times.
  109. -r Release mode
  110. Generate build list through an external script.
  111. -l Print available environments
  112. -d VALUE Destination to move .bin files after building environments
  113. -h Display this message
  114. EOF
  115. }
  116. while getopts "f:lrpd:h" opt; do
  117. case $opt in
  118. f)
  119. case "$OPTARG" in
  120. webui) script_build_webui=false ;;
  121. environments) script_build_environments=false ;;
  122. esac
  123. ;;
  124. l)
  125. print_available
  126. exit
  127. ;;
  128. d)
  129. destination=$OPTARG
  130. ;;
  131. r)
  132. release_mode=true
  133. ;;
  134. h)
  135. print_getopts_help
  136. exit
  137. esac
  138. done
  139. shift $((OPTIND-1))
  140. # Welcome
  141. echo "--------------------------------------------------------------"
  142. echo "ESPURNA FIRMWARE BUILDER"
  143. echo "Building for version ${version}" ${git_revision:+($git_revision)}
  144. # Environments to build
  145. environments=$@
  146. if $script_build_webui ; then
  147. build_webui
  148. fi
  149. if $script_build_environments ; then
  150. if [ $# -eq 0 ]; then
  151. set_default_environments
  152. fi
  153. if $release_mode ; then
  154. build_release
  155. else
  156. build_environments
  157. fi
  158. fi