Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class IDFCorePreferenceConstants
public static final String AUTOMATE_BUILD_HINTS_STATUS = "automateHintsStatus"; //$NON-NLS-1$
public static final String HIDE_ERRORS_IDF_COMPONENTS = "hideErrorsOnIdfDerivedFiles"; //$NON-NLS-1$
public static final String AUTOMATE_CLANGD_FORMAT_FILE = "automateClangFormatFileCreation"; //$NON-NLS-1$
public static final String EIM_IDF_JSON_PATH = "eimIdfJsonPath"; //$NON-NLS-1$
public static final boolean AUTOMATE_CLANGD_FORMAT_FILE_DEFAULT = true;
public static final boolean CMAKE_CCACHE_DEFAULT_STATUS = true;
public static final boolean AUTOMATE_BUILD_HINTS_DEFAULT_STATUS = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.core.runtime.Platform;
import java.nio.file.Path;

import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.tools.exceptions.EimVersionMismatchException;
Expand All @@ -25,9 +24,8 @@ public EimIdfConfiguratinParser()

private void load() throws IOException, EimVersionMismatchException
{
String path = Platform.getOS().equals(Platform.OS_WIN32) ? EimConstants.EIM_WIN_PATH
: EimConstants.EIM_POSIX_PATH;

Path jsonPath = new EimIdfJsonPathResolver().resolveEimIdfJsonFile();
String path = jsonPath.toString();
File file = new File(path);
if (!file.exists())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.tools;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.InstanceScope;

import com.espressif.idf.core.IDFCorePlugin;
import com.espressif.idf.core.IDFCorePreferenceConstants;

/**
* @author Kondal Kolipaka <kondal.kolipaka@espressif.com>
*
*/
public class EimIdfJsonPathResolver
{
public Path resolveEimIdfJsonFile()
{
String custom = InstanceScope.INSTANCE.getNode(IDFCorePlugin.PLUGIN_ID)
.get(IDFCorePreferenceConstants.EIM_IDF_JSON_PATH, ""); //$NON-NLS-1$
return resolveEimIdfJsonFileFromPreferenceString(custom);
}

public Path resolveEimIdfJsonFileFromPreferenceString(String custom)
{
if (custom != null && !custom.isEmpty())
{
Path p = Paths.get(custom);
if (p.getFileName() != null
&& p.getFileName().toString().equals(EimConstants.EIM_JSON)
&& Files.isRegularFile(p))
{
return p;
}
}
return getDefaultEimIdfJsonFile();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

public String getDefaultEimIdfJsonPathString()
{
return Platform.getOS().equals(Platform.OS_WIN32) ? EimConstants.EIM_WIN_PATH : EimConstants.EIM_POSIX_PATH;
}

public Path getDefaultEimIdfJsonFile()
{
return Paths.get(getDefaultEimIdfJsonPathString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ protected IStatus run(IProgressMonitor monitor)
@Override
public void aboutToRun(IJobChangeEvent event)
{
EimJsonWatchService.getInstance().pauseListeners();
EimJsonWatchService ws = EimJsonWatchService.getInstance();
if (ws != null)
{
ws.pauseListeners();
}
}

@Override
Expand All @@ -162,7 +166,11 @@ public void done(IJobChangeEvent event)
callback.run();
}

EimJsonWatchService.getInstance().unpauseListeners();
EimJsonWatchService ws2 = EimJsonWatchService.getInstance();
if (ws2 != null)
{
ws2.unpauseListeners();
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public boolean isEimInstalled()

public boolean isEimIdfJsonPresent()
{
String path = Platform.getOS().equals(Platform.OS_WIN32) ? EimConstants.EIM_WIN_PATH
: EimConstants.EIM_POSIX_PATH;
return new File(path).exists();
Path path = new EimIdfJsonPathResolver().resolveEimIdfJsonFile();
return Files.isRegularFile(path) && Files.isReadable(path);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public EimJson loadEimJson() throws EimVersionMismatchException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

import java.io.File;

import org.eclipse.core.runtime.Platform;
import org.osgi.service.prefs.Preferences;

import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.tools.EimConstants;
import com.espressif.idf.core.tools.EimIdfJsonPathResolver;

/**
* Checks if eim_idf.json was changed while Eclipse was not running. Stores and compares last seen timestamp to file
Expand Down Expand Up @@ -62,6 +61,6 @@ public void updateLastSeenTimestamp()

private String getEimJsonPath()
{
return Platform.getOS().equals(Platform.OS_WIN32) ? EimConstants.EIM_WIN_PATH : EimConstants.EIM_POSIX_PATH;
return new EimIdfJsonPathResolver().resolveEimIdfJsonFile().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.runtime.Platform;

import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.tools.EimConstants;
import com.espressif.idf.core.tools.EimIdfJsonPathResolver;

/**
* eim_idf.json watch service. The service will only watch for changes. Any handling must be done by the listeners to
* this service.
*
* @author Ali Azam Rana <ali.azamrana@espressif.com>
*
*/
public class EimJsonWatchService extends Thread
{
Expand All @@ -41,29 +41,40 @@ public class EimJsonWatchService extends Thread

private EimJsonWatchService() throws IOException
{
String directoryPathString = Platform.getOS().equals(Platform.OS_WIN32) ? EimConstants.EIM_WIN_DIR
: EimConstants.EIM_POSIX_DIR;

watchDirectoryPath = Paths.get(directoryPathString);
EimIdfJsonPathResolver r = new EimIdfJsonPathResolver();
Path json = r.resolveEimIdfJsonFile();
Path def = r.getDefaultEimIdfJsonFile();
if (json.getParent() == null)
{
throw new IOException("Invalid eim_idf.json path"); //$NON-NLS-1$
}
watchDirectoryPath = json.getParent();
if (json.toAbsolutePath().normalize().equals(def.toAbsolutePath().normalize()))
{
if (!Files.exists(watchDirectoryPath))
{
Files.createDirectories(watchDirectoryPath);
}
}
if (!Files.exists(watchDirectoryPath))
{
Files.createDirectories(watchDirectoryPath);
throw new IOException("Directory for eim_idf.json does not exist: " + watchDirectoryPath); //$NON-NLS-1$
}
watchService = FileSystems.getDefault().newWatchService();
watchDirectoryPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);

Logger.log("Watcher added to the directory: " + directoryPathString); //$NON-NLS-1$
Logger.log("Watcher added to the directory: " + watchDirectoryPath); //$NON-NLS-1$
setName("EimJsonWatchService"); //$NON-NLS-1$
setDaemon(true);
start();
}

private static class Holder
{
private static EimJsonWatchService INSTANCE;
private static EimJsonWatchService INSTANCE;

static
public static synchronized EimJsonWatchService getInstance()
{
if (INSTANCE == null)
{
try
{
Expand All @@ -75,11 +86,52 @@ private static class Holder
Logger.log(e);
}
}
return INSTANCE;
}

public static synchronized void restartAfterEimIdfPathChange()
{
EimJsonWatchService old = INSTANCE;
List<EimJsonChangeListener> toCopy = (old == null) ? new ArrayList<>() : new ArrayList<>(old.eimJsonChangeListeners);
try
{
EimJsonWatchService fresh = new EimJsonWatchService();
for (EimJsonChangeListener l : toCopy)
{
fresh.addEimJsonChangeListener(l);
}
INSTANCE = fresh;
if (old != null)
{
old.requestStop();
}
}
catch (IOException e)
{
Logger.log("Failed to restart EimJsonWatchService"); //$NON-NLS-1$
Logger.log(e);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

public static EimJsonWatchService getInstance()
private void requestStop()
{
return Holder.INSTANCE;
running = false;
try
{
watchService.close();
}
catch (IOException e)
{
Logger.log(e);
}
try
{
join(10_000);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}

public void addEimJsonChangeListener(EimJsonChangeListener listener)
Expand All @@ -97,9 +149,21 @@ public void removeAllListeners()

public static void withPausedListeners(Runnable task)
{
EimJsonWatchService watchService = getInstance();
boolean wasPaused = watchService.paused;
watchService.pauseListeners();
EimJsonWatchService watch = getInstance();
if (watch == null)
{
try
{
task.run();
}
catch (Exception e)
{
Logger.log(e);
}
return;
}
boolean wasPaused = watch.paused;
watch.pauseListeners();

try
{
Expand All @@ -111,7 +175,7 @@ public static void withPausedListeners(Runnable task)
} finally
{
if (!wasPaused)
watchService.unpauseListeners();
watch.unpauseListeners();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.StreamSupport;

import org.eclipse.core.runtime.Platform;
import org.eclipse.terminal.connector.ITerminalControl;
import org.eclipse.terminal.connector.process.ProcessConnector;

import com.espressif.idf.core.IDFEnvironmentVariables;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.tools.EimConstants;
import com.espressif.idf.core.tools.EimIdfJsonPathResolver;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -59,14 +57,7 @@ private void sendCommand(OutputStream out, String command) {
}

private Optional<String> getActivationScriptPath() {
var envPath = Objects.equals(Platform.getOS(), Platform.OS_WIN32) ? EimConstants.EIM_WIN_PATH
: EimConstants.EIM_POSIX_PATH;

if (envPath == null) {
return Optional.empty();
}

var path = Path.of(envPath);
var path = new EimIdfJsonPathResolver().resolveEimIdfJsonFile();
if (!Files.exists(path)) {
return Optional.empty();
}
Expand Down
Loading
Loading