setup_esp_idf__latest_stable__linux_macos.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env bash
  2. SHELL_RC="$HOME/.bashrc"
  3. # Step 1: Check if curl and git are installed
  4. echo "============== Step 1: Checking if dependencies are installed =============="
  5. if ! command -v curl &> /dev/null; then
  6. echo "curl is not installed. Please install curl."
  7. exit 1
  8. fi
  9. if ! command -v git &> /dev/null; then
  10. echo "git is not installed. Please install git."
  11. exit 1
  12. fi
  13. echo "All required dependencies are installed."
  14. # Step 2: Fetch the branches from the GitHub API and find the latest stable release branch
  15. echo "============== Step 2: Fetching branch list from ESP-IDF GitHub API =============="
  16. LATEST_BRANCH=$(curl -s https://api.github.com/repos/espressif/esp-idf/branches | grep -o '"name": "release/[^"]*' | awk -F'"' '{print $4}' | sort -V | tail -n 1)
  17. echo "Latest stable branch found: $LATEST_BRANCH"
  18. # Step 3: Clone or update the ESP-IDF repository
  19. if [ ! -d "$HOME/esp-idf" ]; then
  20. echo "========= Step 3: Cloning the ESP-IDF repository (takes 3-4 mins) ============="
  21. git clone -b "$LATEST_BRANCH" --recursive --depth 1 https://github.com/espressif/esp-idf.git "$HOME/esp-idf"
  22. else
  23. echo "ESP-IDF repository already exists. Updating..."
  24. cd "$HOME/esp-idf" || exit
  25. git checkout "$LATEST_BRANCH"
  26. git pull --recurse-submodules
  27. fi
  28. # Log the current commit hash
  29. cd "$HOME/esp-idf" || exit
  30. IDF_COMMIT=$(git rev-parse HEAD)
  31. echo "<< ESP-IDF is set to commit: $IDF_COMMIT >>"
  32. # Step 4: Set up the ESP-IDF environment
  33. echo "============== Step 4: Setting up the ESP-IDF environment ================"
  34. "$HOME/esp-idf/install.sh"
  35. source "$HOME/esp-idf/export.sh"
  36. # Step 5: Optionally add an alias to shell configuration for easy setup
  37. echo "============== Step 5: Adding alias to shell configuration ==============="
  38. if ! grep -q "alias get-idf" "$SHELL_RC"; then
  39. read -p "Do you want to add the alias 'get-idf' to $SHELL_RC? [yes/no] " -r
  40. if [[ $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
  41. echo "alias get-idf='source $HOME/esp-idf/export.sh'" >> "$SHELL_RC"
  42. echo "ESP-IDF setup alias added to $SHELL_RC. Run 'get-idf' to configure your environment."
  43. else
  44. echo "Alias not added. You can manually add 'alias get-idf=\"source $HOME/esp-idf/export.sh\"' to $SHELL_RC."
  45. fi
  46. else
  47. echo "ESP-IDF setup alias already exists in $SHELL_RC."
  48. fi
  49. # Step 6: Inform the user to reload the shell
  50. echo "============== Step 6: Informing user to reload shell ==============="
  51. echo "\nPlease run 'source $SHELL_RC' to reload the shell with the new alias."
  52. echo "\nIn a new shell, run 'get-idf' to enable the ESP-IDF environment."