|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function: Check version format |
| 4 | +# Input parameters: $1 The version number |
| 5 | +# Return value: 0 if the version numbers are correct, 1 if the first version is incorrect, |
| 6 | +check_version_format() { |
| 7 | + version_regex="^v[0-9]+\.[0-9]+\.[0-9]+$" |
| 8 | + |
| 9 | + if [[ ! $1 =~ $version_regex ]]; then |
| 10 | + return 1 |
| 11 | + fi |
| 12 | + |
| 13 | + return 0 |
| 14 | +} |
| 15 | + |
| 16 | +if [ $# -lt 1 ]; then |
| 17 | + latest_version="0.0.0" |
| 18 | + echo "Don't get the lastest version, use \"0.0.0\" as default" |
| 19 | +else |
| 20 | + # Get the first input parameter as the version to be compared |
| 21 | + latest_version="$1" |
| 22 | + # Check the version format |
| 23 | + check_version_format "${latest_version}" |
| 24 | + result=$? |
| 25 | + if [ ${result} -ne 0 ]; then |
| 26 | + echo "The latest release version (${latest_version}) format is incorrect." |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | +fi |
| 30 | + |
| 31 | +# Specify the directory path |
| 32 | +target_directory="./" |
| 33 | + |
| 34 | +echo "Checking directory: ${target_directory}" |
| 35 | + |
| 36 | +# Function: Check if a file exists |
| 37 | +# Input parameters: $1 The file to check |
| 38 | +# Return value: 0 if the file exists, 1 if the file does not exist |
| 39 | +check_file_exists() { |
| 40 | + if [ ! -f "$1" ]; then |
| 41 | + echo "File '$1' not found." |
| 42 | + return 1 |
| 43 | + fi |
| 44 | + return 0 |
| 45 | +} |
| 46 | + |
| 47 | +# Function: Compare version numbers |
| 48 | +# Input parameters: $1 The first version number, $2 The second version number |
| 49 | +# Return value: 0 if the version numbers are equal, 1 if the first version is greater, |
| 50 | +# 2 if the second version is greater, |
| 51 | +compare_versions() { |
| 52 | + version_regex="^v[0-9]+\.[0-9]+\.[0-9]+$" |
| 53 | + |
| 54 | + version1=$(echo "$1" | cut -c 2-) # Remove the 'v' at the beginning of the version number |
| 55 | + version2=$(echo "$2" | cut -c 2-) |
| 56 | + |
| 57 | + IFS='.' read -ra v1_parts <<< "$version1" |
| 58 | + IFS='.' read -ra v2_parts <<< "$version2" |
| 59 | + |
| 60 | + for ((i=0; i<${#v1_parts[@]}; i++)); do |
| 61 | + if [[ "${v1_parts[$i]}" -lt "${v2_parts[$i]}" ]]; then |
| 62 | + return 2 |
| 63 | + elif [[ "${v1_parts[$i]}" -gt "${v2_parts[$i]}" ]]; then |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + done |
| 67 | + |
| 68 | + return 0 |
| 69 | +} |
| 70 | + |
| 71 | +echo "Checking file: library.properties" |
| 72 | +# Check if "library.properties" file exists |
| 73 | +check_file_exists "${target_directory}/library.properties" |
| 74 | +if [ $? -ne 0 ]; then |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | +# Read the version information from the file |
| 78 | +arduino_version=v$(grep -E '^version=' "${target_directory}/library.properties" | cut -d '=' -f 2) |
| 79 | +echo "Get Arduino version: ${arduino_version}" |
| 80 | +# Check the version format |
| 81 | +check_version_format "${arduino_version}" |
| 82 | +result=$? |
| 83 | +if [ ${result} -ne 0 ]; then |
| 84 | + echo "Arduino version (${arduino_version}) format is incorrect." |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +# Compare Arduino Library version with the latest release version |
| 89 | +compare_versions "${arduino_version}" "${latest_version}" |
| 90 | +result=$? |
| 91 | +if [ ${result} -ne 1 ]; then |
| 92 | + if [ ${result} -eq 3 ]; then |
| 93 | + echo "Arduino version (${arduino_version}) is incorrect." |
| 94 | + else |
| 95 | + echo "Arduino version (${arduino_version}) is not greater than the latest release version (${latest_version})." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +fi |
| 99 | + |
| 100 | +echo "Checking file: idf_component.yml" |
| 101 | +# Check if "idf_component.yml" file exists |
| 102 | +check_file_exists "${target_directory}/idf_component.yml" |
| 103 | +if [ $? -eq 0 ]; then |
| 104 | + # Read the version information from the file |
| 105 | + idf_version=v$(grep -E '^version:' "${target_directory}/idf_component.yml" | awk -F'"' '{print $2}') |
| 106 | + echo "Get IDF component version: ${idf_version}" |
| 107 | + # Check the version format |
| 108 | + check_version_format "${idf_version}" |
| 109 | + result=$? |
| 110 | + if [ ${result} -ne 0 ]; then |
| 111 | + echo "IDF component (${idf_version}) format is incorrect." |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | + # Compare IDF Component version with Arduino Library version |
| 115 | + compare_versions ${idf_version} ${arduino_version} |
| 116 | + result=$? |
| 117 | + if [ ${result} -ne 0 ]; then |
| 118 | + if [ ${result} -eq 3 ]; then |
| 119 | + echo "IDF component version (${idf_version}) is incorrect." |
| 120 | + else |
| 121 | + echo "IDF component version (${idf_version}) is not equal to the Arduino version (${arduino_version})." |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + fi |
| 125 | + # Compare IDF Component version with the latest release version |
| 126 | + compare_versions ${idf_version} ${latest_version} |
| 127 | + result=$? |
| 128 | + if [ ${result} -ne 1 ]; then |
| 129 | + if [ ${result} -eq 3 ]; then |
| 130 | + echo "IDF component version (${idf_version}) is incorrect." |
| 131 | + else |
| 132 | + echo "IDF component version (${idf_version}) is not greater than the latest release version (${latest_version})." |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + fi |
| 136 | +fi |
0 commit comments