|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | +import re |
| 5 | +import sys |
| 6 | + |
| 7 | +MANIFEST_DATA = { |
| 8 | + "name": "framework-arduinoespressif32-libs", |
| 9 | + "description": "Precompiled libraries for Arduino Wiring-based Framework for the Espressif ESP32 series of SoCs", |
| 10 | + "keywords": ["framework", "arduino", "espressif", "esp32"], |
| 11 | + "license": "LGPL-2.1-or-later", |
| 12 | + "repository": { |
| 13 | + "type": "git", |
| 14 | + "url": "https://github.com/espressif/esp32-arduino-libs", |
| 15 | + }, |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def convert_version(version_line): |
| 20 | + """A helper function that converts a custom IDF version string |
| 21 | + to a suitable SemVer alternative. For example: |
| 22 | + 'release/v5.1 420ebd208a' becomes '5.1.0+sha.420ebd208a' |
| 23 | + """ |
| 24 | + |
| 25 | + regex_pattern = r"^esp-idf:\s*release\/v(?P<IDF_VERSION>[\d\.]{3,5})\s*(?P<COMMIT_HASH>[0-9a-f]{5,40})" |
| 26 | + match = re.search(regex_pattern, version_line) |
| 27 | + if not match: |
| 28 | + sys.stderr.write( |
| 29 | + f"Failed to find a regex match for '{regex_pattern}' in '{version_line}'\n" |
| 30 | + ) |
| 31 | + return "" |
| 32 | + |
| 33 | + version = match.group("IDF_VERSION") |
| 34 | + commit = match.group("COMMIT_HASH") |
| 35 | + |
| 36 | + assert version, f"Failed to parse version value from '{version_line}'" |
| 37 | + assert commit, f"Failed to parse commit hash value from '{version_line}'" |
| 38 | + |
| 39 | + if version.count(".") < 2: |
| 40 | + # The most basic casting to a SemVer with three digits |
| 41 | + version = version + ".0" |
| 42 | + |
| 43 | + return f"{version}+sha.{commit}" |
| 44 | + |
| 45 | + |
| 46 | +def main(dst_dir): |
| 47 | + # The "version.txt" file is expected to contain IDF version in the following format |
| 48 | + # "esp-idf: release/v$VERSION COMMIT_HASH". |
| 49 | + version_file = os.path.join("version.txt") |
| 50 | + |
| 51 | + if not os.path.isfile(version_file): |
| 52 | + sys.stderr.write("Missing the 'version.txt' file.\n") |
| 53 | + return -1 |
| 54 | + |
| 55 | + version_line = "" |
| 56 | + with open(version_file, encoding="utf8") as fp: |
| 57 | + for line in fp.readlines(): |
| 58 | + if not line.startswith("esp-idf"): |
| 59 | + continue |
| 60 | + version_line = line.strip() |
| 61 | + |
| 62 | + if not version_line: |
| 63 | + sys.stderr.write("Failed to find ESP-IDF version in the 'version.txt' file!\n") |
| 64 | + return -1 |
| 65 | + |
| 66 | + converted_version = convert_version(version_line) |
| 67 | + if not converted_version: |
| 68 | + sys.stderr.write( |
| 69 | + f"Failed to convert version '{version_line}' from version.txt\n" |
| 70 | + ) |
| 71 | + return -1 |
| 72 | + |
| 73 | + manifest_file_path = os.path.join(dst_dir, "package.json") |
| 74 | + with open(manifest_file_path, "w", encoding="utf8") as fp: |
| 75 | + MANIFEST_DATA["version"] = converted_version |
| 76 | + json.dump(MANIFEST_DATA, fp, indent=2) |
| 77 | + |
| 78 | + print( |
| 79 | + f"Generated PlatformIO manifest file '{manifest_file_path}' with '{converted_version}' version" |
| 80 | + ) |
| 81 | + return 0 |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + parser = argparse.ArgumentParser() |
| 86 | + parser.add_argument( |
| 87 | + "-o", |
| 88 | + "--dst-dir", |
| 89 | + dest="dst_dir", |
| 90 | + required=True, |
| 91 | + help="Destination folder where the 'package.json' manifest will be located", |
| 92 | + ) |
| 93 | + args = parser.parse_args() |
| 94 | + |
| 95 | + sys.exit(main(args.dst_dir)) |
0 commit comments