|
7 | 7 | import java.io.BufferedReader; |
8 | 8 | import java.io.File; |
9 | 9 | import java.io.InputStreamReader; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
10 | 12 | import java.util.ArrayList; |
11 | 13 | import java.util.List; |
| 14 | +import java.util.regex.Matcher; |
| 15 | +import java.util.regex.Pattern; |
12 | 16 |
|
13 | 17 | import org.eclipse.core.runtime.Platform; |
14 | 18 |
|
|
25 | 29 | */ |
26 | 30 | public class ToolsUtility |
27 | 31 | { |
| 32 | + private static final Pattern IDF_VERSION_MAJOR_PATTERN = Pattern.compile("IDF_VERSION_MAJOR\\s+(\\d+)"); //$NON-NLS-1$ |
| 33 | + private static final Pattern IDF_VERSION_MINOR_PATTERN = Pattern.compile("IDF_VERSION_MINOR\\s+(\\d+)"); //$NON-NLS-1$ |
| 34 | + private static final Pattern IDF_VERSION_PATCH_PATTERN = Pattern.compile("IDF_VERSION_PATCH\\s+(\\d+)"); //$NON-NLS-1$ |
| 35 | + |
| 36 | + /** |
| 37 | + * Detects the ESP-IDF version for the given installation. |
| 38 | + * <p> |
| 39 | + * Prefers the full {@code MAJOR.MINOR.PATCH} read directly from {@code tools/cmake/version.cmake} |
| 40 | + * under the installation's {@code IDF_PATH}. This is intentionally not derived from the |
| 41 | + * {@code ESP_IDF_VERSION} environment variable: ESP-IDF and EIM define that variable as |
| 42 | + * {@code MAJOR.MINOR} only (components consume it in Kconfig, e.g. {@code Kconfig.idf_v5.5.in}), so |
| 43 | + * it never carries the patch component. When {@code version.cmake} cannot be read, falls back to the |
| 44 | + * {@code ESP_IDF_VERSION} value printed by the activation script. |
| 45 | + */ |
28 | 46 | public static String getIdfVersion(EimInstallationModel installation) |
29 | 47 | { |
| 48 | + String versionFromCMake = readIdfVersionFromCMake(installation.getPath()); |
| 49 | + if (!StringUtil.isEmpty(versionFromCMake)) |
| 50 | + { |
| 51 | + return versionFromCMake; |
| 52 | + } |
| 53 | + |
30 | 54 | return installation.getActivationScript().map(ToolsUtility::readIdfVersionFromScript) |
31 | 55 | .orElse(StringUtil.EMPTY); |
32 | 56 | } |
33 | 57 |
|
| 58 | + private static String readIdfVersionFromCMake(String idfPath) |
| 59 | + { |
| 60 | + if (StringUtil.isEmpty(idfPath)) |
| 61 | + { |
| 62 | + return StringUtil.EMPTY; |
| 63 | + } |
| 64 | + |
| 65 | + Path versionCMakeFile = Path.of(idfPath, "tools", "cmake", "version.cmake"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 66 | + return parseVersionCMake(versionCMakeFile); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Parses {@code version.cmake} and returns {@code MAJOR.MINOR.PATCH} (or {@code MAJOR.MINOR} when |
| 71 | + * the patch entry is absent). Returns an empty string when the file is missing or the required |
| 72 | + * major/minor entries cannot be found. |
| 73 | + */ |
| 74 | + public static String parseVersionCMake(Path versionCMakeFile) |
| 75 | + { |
| 76 | + if (versionCMakeFile == null || !Files.isRegularFile(versionCMakeFile)) |
| 77 | + { |
| 78 | + return StringUtil.EMPTY; |
| 79 | + } |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + String content = Files.readString(versionCMakeFile); |
| 84 | + String major = firstGroup(IDF_VERSION_MAJOR_PATTERN, content); |
| 85 | + String minor = firstGroup(IDF_VERSION_MINOR_PATTERN, content); |
| 86 | + if (major == null || minor == null) |
| 87 | + { |
| 88 | + return StringUtil.EMPTY; |
| 89 | + } |
| 90 | + |
| 91 | + String patch = firstGroup(IDF_VERSION_PATCH_PATTERN, content); |
| 92 | + if (patch == null) |
| 93 | + { |
| 94 | + return major + "." + minor; //$NON-NLS-1$ |
| 95 | + } |
| 96 | + |
| 97 | + return major + "." + minor + "." + patch; //$NON-NLS-1$ //$NON-NLS-2$ |
| 98 | + } |
| 99 | + catch (Exception e) |
| 100 | + { |
| 101 | + Logger.log(e); |
| 102 | + return StringUtil.EMPTY; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static String firstGroup(Pattern pattern, String content) |
| 107 | + { |
| 108 | + Matcher matcher = pattern.matcher(content); |
| 109 | + return matcher.find() ? matcher.group(1) : null; |
| 110 | + } |
| 111 | + |
34 | 112 | private static String readIdfVersionFromScript(String activationScript) |
35 | 113 | { |
36 | 114 | String espIdfVersion = StringUtil.EMPTY; |
|
0 commit comments