Преглед изворни кода

tools: allow alternative spellings of target name (ESP32-S2, ESP32S2)

by ignoring character case and hyphens in target name.
Ivan Grokhotkov пре 5 година
родитељ
комит
7b79b52062

+ 7 - 0
tools/ci/test_build_system_cmake.sh

@@ -353,6 +353,13 @@ function run_tests()
     grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target"
     grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target"
 
+    print_status "idf.py understands alternative target names"
+    clean_build_dir
+    rm sdkconfig
+    idf.py set-target ESP32-S2
+    grep "CONFIG_IDF_TARGET=\"${other_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target"
+    grep "IDF_TARGET:STRING=${other_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target"
+
     print_status "Can guess target from sdkconfig, if CMakeCache does not exist"
     idf.py fullclean || failure "Failed to clean the build directory"
     idf.py reconfigure || failure "Failed to reconfigure after fullclean"

+ 2 - 2
tools/idf_py_actions/core_ext.py

@@ -8,7 +8,7 @@ import click
 from idf_py_actions.constants import GENERATORS, SUPPORTED_TARGETS
 from idf_py_actions.errors import FatalError
 from idf_py_actions.global_options import global_options
-from idf_py_actions.tools import ensure_build_directory, idf_version, merge_action_lists, realpath, run_target
+from idf_py_actions.tools import ensure_build_directory, idf_version, merge_action_lists, realpath, run_target, TargetChoice
 
 
 def action_extensions(base_actions, project_path):
@@ -379,7 +379,7 @@ def action_extensions(base_actions, project_path):
                     {
                         "names": ["idf-target"],
                         "nargs": 1,
-                        "type": click.Choice(SUPPORTED_TARGETS),
+                        "type": TargetChoice(SUPPORTED_TARGETS),
                     },
                 ],
                 "dependencies": ["fullclean"],

+ 23 - 0
tools/idf_py_actions/tools.py

@@ -1,3 +1,4 @@
+import click
 import os
 import re
 import subprocess
@@ -314,3 +315,25 @@ def _guess_or_check_idf_target(args, prog_name, cache):
                          "To keep the setting in sdkconfig ({t_conf}) and re-generate CMakeCache.txt, run '{prog} fullclean'. "
                          "To re-generate sdkconfig for '{t_cache}' target, run '{prog} set-target {t_cache}'."
                          .format(t_conf=idf_target_from_sdkconfig, t_cache=idf_target_from_cache, prog=prog_name))
+
+
+class TargetChoice(click.Choice):
+    """
+    A version of click.Choice with two special features:
+    - ignores hyphens
+    - not case sensitive
+    """
+    def __init__(self, choices):
+        super(TargetChoice, self).__init__(choices, case_sensitive=False)
+
+    def convert(self, value, param, ctx):
+        def normalize(str):
+            return str.lower().replace("-", "")
+
+        saved_token_normalize_func = ctx.token_normalize_func
+        ctx.token_normalize_func = normalize
+
+        try:
+            return super(TargetChoice, self).convert(value, param, ctx)
+        finally:
+            ctx.token_normalize_func = saved_token_normalize_func