2222import com .espressif .idf .core .IDFCorePlugin ;
2323import com .espressif .idf .core .IDFEnvironmentVariables ;
2424import com .espressif .idf .core .ProcessBuilderFactory ;
25+ import com .espressif .idf .core .SystemExecutableFinder ;
2526import com .espressif .idf .core .logging .Logger ;
2627import com .espressif .idf .core .tools .exceptions .EimVersionMismatchException ;
2728import com .espressif .idf .core .tools .vo .EimJson ;
@@ -48,16 +49,59 @@ public ToolInitializer(Preferences preferences)
4849
4950 public boolean isEimInstalled ()
5051 {
52+ return !StringUtil .isEmpty (resolveEimExecutablePath (null ));
53+ }
54+
55+ /**
56+ * Looks for an {@code eim} executable on the process {@code PATH}, using the same rules as other tools in this
57+ * plugin ({@link SystemExecutableFinder}: PATHEXT on Windows, plain name on Linux/macOS).
58+ *
59+ * @return absolute path to the executable, or empty if not found
60+ */
61+ private String findEimOnSystemPath ()
62+ {
63+ IPath eimPath = new SystemExecutableFinder ().find ("eim" ); //$NON-NLS-1$
64+ return eimPath != null ? eimPath .toOSString () : StringUtil .EMPTY ;
65+ }
66+
67+ /**
68+ * Resolves the EIM executable path: <strong>system {@code PATH} first</strong>, then {@code eimPath} from
69+ * {@code eim_idf.json} when the path exists on disk, then {@code EIM_PATH} env variable, then
70+ * {@link #getDefaultEimPath()} (existence-checked).
71+ *
72+ * @param eimJson parsed JSON or {@code null}
73+ * @return resolved absolute path string, or empty if nothing could be resolved
74+ */
75+ public String resolveEimExecutablePath (EimJson eimJson )
76+ {
77+ String fromPath = findEimOnSystemPath ();
78+ if (!StringUtil .isEmpty (fromPath ))
79+ {
80+ return fromPath ;
81+ }
82+
83+ if (eimJson != null && !StringUtil .isEmpty (eimJson .getEimPath ()))
84+ {
85+ String jsonPath = eimJson .getEimPath ();
86+ if (Files .exists (Paths .get (jsonPath )))
87+ {
88+ return jsonPath ;
89+ }
90+ }
91+
5192 String eimExePathEnv = idfEnvironmentVariables .getEnvValue (IDFEnvironmentVariables .EIM_PATH );
52- boolean exists = !StringUtil .isEmpty (eimExePathEnv ) && Files .exists (Paths .get (eimExePathEnv ));
53- if (!exists )
93+ if (!StringUtil .isEmpty (eimExePathEnv ) && Files .exists (Paths .get (eimExePathEnv )))
94+ {
95+ return eimExePathEnv ;
96+ }
97+
98+ Path defaultEimPath = getDefaultEimPath ();
99+ if (defaultEimPath != null && Files .exists (defaultEimPath ))
54100 {
55- // Fallback: check in user home .espressif/eim_gui folder
56- Path defaultEimPath = getDefaultEimPath ();
57- if (defaultEimPath != null )
58- exists = Files .exists (defaultEimPath );
101+ return defaultEimPath .toString ();
59102 }
60- return exists ;
103+
104+ return StringUtil .EMPTY ;
61105 }
62106
63107 public boolean isEimIdfJsonPresent ()
@@ -138,38 +182,50 @@ public boolean isEspIdfSet()
138182 public Path getDefaultEimPath ()
139183 {
140184 String userHome = System .getProperty ("user.home" ); //$NON-NLS-1$
141- Path defaultEimPath ;
142- String os = Platform .getOS ();
143- if (os .equals (Platform .OS_WIN32 ))
144- {
145- defaultEimPath = Paths .get (userHome , ".espressif" , "eim_gui" , //$NON-NLS-1$//$NON-NLS-2$
146- "eim.exe" ); //$NON-NLS-1$
147- }
148- else if (os .equals (Platform .OS_MACOSX ))
149- {
150- defaultEimPath = Paths .get ("/Applications" , //$NON-NLS-1$
151- "eim.app" , "Contents" , //$NON-NLS-1$//$NON-NLS-2$
152- "MacOS" , "eim" ); //$NON-NLS-1$ //$NON-NLS-2$
153- }
154- else
155- {
156- defaultEimPath = Paths .get (userHome , ".espressif" , //$NON-NLS-1$
157- "eim_gui" , "eim" ); //$NON-NLS-1$//$NON-NLS-2$
158- }
159-
160- return defaultEimPath ;
161- }
162-
163- public void findAndSetEimPath ()
164- {
165- Path defaultEimPath = getDefaultEimPath ();
166-
167- if (defaultEimPath != null )
168- setEimPathInEnvVar (defaultEimPath .toString ());
185+ Path defaultEimPath ;
186+ String os = Platform .getOS ();
187+ if (os .equals (Platform .OS_WIN32 ))
188+ {
189+ defaultEimPath = Paths .get (userHome , ".espressif" , "eim_gui" , //$NON-NLS-1$//$NON-NLS-2$
190+ "eim.exe" ); //$NON-NLS-1$
191+ if (!Files .exists (defaultEimPath ))
192+ {
193+ Path eimGuiDir = Paths .get (userHome , ".espressif" , "eim_gui" ); //$NON-NLS-1$ //$NON-NLS-2$
194+ if (Files .isDirectory (eimGuiDir ))
195+ {
196+ try (var entries = Files .list (eimGuiDir ))
197+ {
198+ Path found = entries
199+ .filter (Files ::isRegularFile )
200+ .filter (p -> p .getFileName ().toString ().toLowerCase ().startsWith ("eim" ) //$NON-NLS-1$
201+ && p .getFileName ().toString ().toLowerCase ().endsWith (".exe" )) //$NON-NLS-1$
202+ .findFirst ()
203+ .orElse (null );
204+ if (found != null )
205+ {
206+ return found ;
207+ }
208+ }
209+ catch (IOException e )
210+ {
211+ Logger .log (e );
212+ }
213+ }
214+ }
215+ }
216+ else if (os .equals (Platform .OS_MACOSX ))
217+ {
218+ defaultEimPath = Paths .get ("/Applications" , //$NON-NLS-1$
219+ "eim.app" , "Contents" , //$NON-NLS-1$//$NON-NLS-2$
220+ "MacOS" , "eim" ); //$NON-NLS-1$ //$NON-NLS-2$
221+ }
222+ else
223+ {
224+ defaultEimPath = Paths .get (userHome , ".espressif" , //$NON-NLS-1$
225+ "eim_gui" , "eim" ); //$NON-NLS-1$//$NON-NLS-2$
226+ }
227+
228+ return defaultEimPath ;
169229 }
170230
171- private void setEimPathInEnvVar (String eimPath )
172- {
173- idfEnvironmentVariables .addEnvVariable (IDFEnvironmentVariables .EIM_PATH , eimPath );
174- }
175231}
0 commit comments