mftest 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. #
  3. # mftest [name] [index]
  4. #
  5. # Set configuration options based on a test
  6. # By default it will do megaatmega2560
  7. # Use 'mftest -' to pick from the list.
  8. #
  9. MFINFO=$(mfinfo) || exit 1
  10. [[ -d Marlin/src ]] || { echo "Please 'cd' up to repo root." ; exit 1 ; }
  11. TESTPATH=buildroot/share/tests
  12. STATE_FILE=$( echo ./.pio/.mftestrc )
  13. shopt -s extglob nocasematch
  14. # Get the environment and test number from the command
  15. TESTENV=${1:-'-'}
  16. CHOICE=${2:-0}
  17. # Allow shorthand for test name
  18. case $TESTENV in
  19. tree) platformio run --project-dir . -e include_tree ; exit 1 ;;
  20. due) TESTENV='DUE' ;;
  21. esp) TESTENV='esp32' ;;
  22. lin*) TESTENV='linux_native' ;;
  23. lpc?(8)) TESTENV='LPC1768' ;;
  24. lpc9) TESTENV='LPC1769' ;;
  25. m128) TESTENV='megaatmega1280' ;;
  26. m256) TESTENV='megaatmega2560' ;;
  27. mega) TESTENV='megaatmega2560' ;;
  28. stm) TESTENV='STM32F103RE' ;;
  29. f1) TESTENV='STM32F103RE' ;;
  30. f4) TESTENV='STM32F4' ;;
  31. f7) TESTENV='STM32F7' ;;
  32. s6) TESTENV='FYSETC_S6' ;;
  33. teensy) TESTENV='teensy31' ;;
  34. t31) TESTENV='teensy31' ;;
  35. t32) TESTENV='teensy31' ;;
  36. t35) TESTENV='teensy35' ;;
  37. t36) TESTENV='teensy35' ;;
  38. # Build with the last-built env
  39. -r) [[ -f "$STATE_FILE" ]] || { echo "No previous (-r) build state found." ; exit 1 ; }
  40. read TESTENV <"$STATE_FILE"
  41. platformio run --project-dir . -e $TESTENV
  42. exit
  43. ;;
  44. # A -y may come first
  45. -y) TESTENV=${2:-'-'} ; CHOICE=${3:-0} ;;
  46. -[a-z]) echo "Unknown flag $TESTENV" ; exit 1 ;;
  47. -) ;;
  48. esac
  49. # Matching patterns
  50. ISNUM='^[0-9]+$'
  51. ISCMD='^(restore|opt|exec|use|pins|env)_'
  52. ISEXEC='^exec_'
  53. ISCONT='\\ *$'
  54. # List available tests and ask for selection
  55. if [[ $TESTENV == '-' ]]; then
  56. IND=0
  57. NAMES=()
  58. for FILE in $( ls -1 $TESTPATH/*-tests )
  59. do
  60. let IND++
  61. TNAME=${FILE/-tests/}
  62. TNAME=${TNAME/$TESTPATH\//}
  63. NAMES+=($TNAME)
  64. (( IND < 10 )) && echo -n " "
  65. echo " $IND) $TNAME"
  66. done
  67. echo
  68. for (( ; ; ))
  69. do
  70. read -p "Select a test to apply (1-$IND) : " NAMEIND
  71. [[ -z "$NAMEIND" ]] && { echo '(canceled)' ; exit 1 ; }
  72. [[ $NAMEIND =~ $ISNUM ]] && ((NAMEIND >= 1 && NAMEIND <= IND)) && { TESTENV=${NAMES[$NAMEIND-1]} ; echo ; break ; }
  73. echo "Invalid selection."
  74. done
  75. fi
  76. # Get the contents of the test file
  77. OUT=$( cat $TESTPATH/$TESTENV-tests 2>/dev/null ) || { echo "Can't find test '$TESTENV'." ; exit 1 ; }
  78. # Count up the number of tests
  79. # TODO: List test descriptions with numbers
  80. TESTCOUNT=$( awk "/$ISEXEC/{a++}END{print a}" <<<"$OUT" )
  81. # User entered a number?
  82. (( CHOICE && CHOICE > TESTCOUNT )) && { echo "Invalid test index '$CHOICE' (1-$TESTCOUNT)." ; exit 1 ; }
  83. if [[ $CHOICE == 0 ]]; then
  84. # List test descriptions with numbers
  85. echo "Available '$TESTENV' tests:" ; echo "$OUT" | {
  86. IND=0
  87. SED=$(which gsed || which sed)
  88. while IFS= read -r LINE
  89. do
  90. if [[ $LINE =~ $ISEXEC ]]; then
  91. DESC=$( "$SED" -E 's/^.+"(.*)".*$/\1/g' <<<"$LINE" )
  92. (( ++IND < 10 )) && echo -n " "
  93. echo " $IND) $DESC"
  94. fi
  95. done
  96. }
  97. CHOICE=1
  98. if [[ $TESTCOUNT > 1 ]]; then
  99. for (( ; ; ))
  100. do
  101. read -p "Select a '$TESTENV' test (1-$TESTCOUNT) : " CHOICE
  102. [[ -z "$CHOICE" ]] && { echo '(canceled)' ; exit 1 ; }
  103. [[ $CHOICE =~ $ISNUM ]] && ((CHOICE >= 1 && CHOICE <= TESTCOUNT)) && break
  104. echo ">>> Invalid test index '$CHOICE'."
  105. done
  106. fi
  107. fi
  108. # Finally, run the specified test lines
  109. echo "$OUT" | {
  110. IND=0
  111. GOTX=0
  112. CMD=""
  113. while IFS= read -r LINE
  114. do
  115. if [[ $LINE =~ $ISCMD || $GOTX == 1 ]]; then
  116. ((!IND)) && let IND++
  117. if [[ $LINE =~ $ISEXEC ]]; then
  118. ((IND++ > CHOICE)) && break
  119. else
  120. ((!HEADER)) && {
  121. HEADER=1
  122. echo -e "\n#\n# Test $TESTENV ($CHOICE) $DESC\n#"
  123. }
  124. ((IND == CHOICE)) && {
  125. GOTX=1
  126. [[ $CMD == "" ]] && CMD="$LINE" || CMD=$( echo -e "$CMD$LINE" | sed -e 's/\\//g' )
  127. [[ $LINE =~ $ISCONT ]] || { echo $CMD ; eval "$CMD" ; CMD="" ; }
  128. }
  129. fi
  130. fi
  131. done
  132. }
  133. # Get a -y parameter the lazy way
  134. [[ "$2" == "-y" || "$3" == "-y" ]] && BUILD_YES='Y'
  135. # Build the test too?
  136. if [[ $BUILD_YES != 'Y' ]]; then
  137. echo
  138. read -p "Build $TESTENV test #$CHOICE (y/N) ? " BUILD_YES
  139. fi
  140. [[ $BUILD_YES == 'Y' || $BUILD_YES == 'Yes' ]] && {
  141. platformio run --project-dir . -e $TESTENV
  142. echo "$TESTENV" >"$STATE_FILE"
  143. }