Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;

import com.espressif.idf.core.util.IDFUtil;

/**
* @author Kondal Kolipaka
*
Expand All @@ -31,6 +34,10 @@ public class IDFCorePreferenceConstants
public static final String IDF_GITHUB_ASSETS_DEFAULT = "dl.espressif.com/github_assets"; //$NON-NLS-1$
public static final String PIP_EXTRA_INDEX_URL = "PIP_EXTRA_INDEX_URL"; //$NON-NLS-1$
public static final String PIP_EXTRA_INDEX_URL_DEFAULT = "https://dl.espressif.com/pypi"; //$NON-NLS-1$
public static final String IDF_TOOLS_PATH = "IDF_TOOLS_PATH"; //$NON-NLS-1$
public static final String IDF_TOOLS_PATH_DEFAULT = Platform.getOS().equals(Platform.OS_WIN32)
? IDFUtil.resolveEnvVariable("%USERPROFILE%\\.espressif") //$NON-NLS-1$
: IDFUtil.resolveEnvVariable("$HOME/.espressif"); //$NON-NLS-1$
/**
* Returns the node in the preference in the given context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import org.eclipse.cdt.managedbuilder.envvar.IBuildEnvironmentVariable;

import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.core.util.StringUtil;

/**
* @author Kondal Kolipaka <kondal.kolipaka@espressif.com>
*
*/
@SuppressWarnings("restriction") //$NON-NLS-1$
@SuppressWarnings("restriction")
public class IDFEnvironmentVariables
{
/**
Expand Down Expand Up @@ -127,17 +128,21 @@ public Map<String, String> getSystemEnvMap()
IEnvironmentVariableManager buildEnvironmentManager = CCorePlugin.getDefault().getBuildEnvironmentManager();
IEnvironmentVariable[] variables = buildEnvironmentManager.getVariables((ICConfigurationDescription) null,
true);
Map<String, String> envMap = new HashMap<>();
Map<String, String> envMap = IDFUtil.getSystemEnv();
if (variables != null)
{
for (IEnvironmentVariable iEnvironmentVariable : variables)
{
String key = iEnvironmentVariable.getName();
if (key.equals(IDFCorePreferenceConstants.IDF_TOOLS_PATH))
{
continue;
}
String value = iEnvironmentVariable.getValue();
envMap.put(key, value);
}
}

return envMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ private void runCmakeBuildCommand(IConsole console, IProgressMonitor monitor, IP
}
}
}


envVars.add(new EnvironmentVariable(IDFCorePreferenceConstants.IDF_TOOLS_PATH,
IDFUtil.getIDFToolsPathFromPreferences()));

String buildCommand = getProperty(BUILD_COMMAND);
if (buildCommand.isBlank())
{
Expand Down Expand Up @@ -521,7 +524,7 @@ private void runCmakeCommand(IConsole console, IProgressMonitor monitor, IProjec
{
command.add("-DIDF_TOOLCHAIN=clang"); //$NON-NLS-1$
}

String userArgs = getProperty(CMAKE_ARGUMENTS);
if (userArgs != null && !userArgs.isBlank())
{
Expand All @@ -539,8 +542,9 @@ private void runCmakeCommand(IConsole console, IProgressMonitor monitor, IProjec
// Set PYTHONUNBUFFERED to 1/TRUE to dump the messages back immediately without
// buffering
IEnvironmentVariable bufferEnvVar = new EnvironmentVariable("PYTHONUNBUFFERED", "1"); //$NON-NLS-1$ //$NON-NLS-2$
IEnvironmentVariable idfToolsPathEnvVar = new EnvironmentVariable(IDFCorePreferenceConstants.IDF_TOOLS_PATH, IDFUtil.getIDFToolsPathFromPreferences());

Process p = startBuildProcess(command, new IEnvironmentVariable[] { bufferEnvVar }, workingDir, errConsole,
Process p = startBuildProcess(command, new IEnvironmentVariable[] { bufferEnvVar, idfToolsPathEnvVar }, workingDir, errConsole,
monitor);
if (p == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ protected String getIdfToolsExportPath()
Logger.log(commands.toString());

IStatus idfToolsExportStatus = new ProcessBuilderFactory().runInBackground(commands,
org.eclipse.core.runtime.Path.ROOT, System.getenv());
org.eclipse.core.runtime.Path.ROOT, IDFUtil.getSystemEnv());
if (idfToolsExportStatus != null && idfToolsExportStatus.isOK())
{
String message = idfToolsExportStatus.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.espressif.idf.core.tools.ToolsSystemWrapper;
import com.espressif.idf.core.tools.vo.ToolsVO;
import com.espressif.idf.core.util.FileUtil;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.core.util.StringUtil;

/**
Expand Down Expand Up @@ -318,7 +319,7 @@ public static IPath findAbsoluteToolPath(String toolName, String path)
{
if (StringUtil.isEmpty(path))
{
Map<String, String> env = System.getenv();
Map<String, String> env = IDFUtil.getSystemEnv();
if (env.containsKey(IDFEnvironmentVariables.PATH))
path = env.get(IDFEnvironmentVariables.PATH);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@ public class EspToolCommands
public Process chipInformation(String port) throws Exception
{
destroyAnyChipInfoProcess();
chipInfoProcess = new ProcessBuilder(getChipInfoCommand(port)).start();
ProcessBuilder processBuilder = new ProcessBuilder(getChipInfoCommand(port));
processBuilder.environment().putAll(IDFUtil.getSystemEnv());
chipInfoProcess = processBuilder.start();
return chipInfoProcess;
}

public Process eraseFlash(String port) throws Exception
{
destroyAnyChipInfoProcess();
flashEraseProcess = new ProcessBuilder(getFlashEraseCommand(port)).start();
ProcessBuilder processBuilder = new ProcessBuilder(getFlashEraseCommand(port));
processBuilder.environment().putAll(IDFUtil.getSystemEnv());
flashEraseProcess = processBuilder.start();
return flashEraseProcess;
}

public Process writeFlash(String port, String path, String offset) throws IOException
{
destroyAnyChipInfoProcess();
writeFlashProcess = new ProcessBuilder(getWriteFlashCommand(port, path, offset)).start();
ProcessBuilder processBuilder = new ProcessBuilder(getWriteFlashCommand(port, path, offset));
processBuilder.environment().putAll(IDFUtil.getSystemEnv());
writeFlashProcess = processBuilder.start();
return writeFlashProcess;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import com.espressif.idf.core.IDFConstants;
import com.espressif.idf.core.IDFCorePlugin;
import com.espressif.idf.core.IDFCorePreferenceConstants;
import com.espressif.idf.core.IDFEnvironmentVariables;
import com.espressif.idf.core.ProcessBuilderFactory;
import com.espressif.idf.core.SystemExecutableFinder;
Expand Down Expand Up @@ -728,7 +729,7 @@ public static String getGitExecutablePathFromSystem()
arguments.add("whereis"); //$NON-NLS-1$
arguments.add("git"); //$NON-NLS-1$

Map<String, String> environment = new HashMap<>(System.getenv());
Map<String, String> environment = new HashMap<>(getSystemEnv());

IStatus status = processRunner.runInBackground(arguments, org.eclipse.core.runtime.Path.ROOT,
environment);
Expand Down Expand Up @@ -760,4 +761,56 @@ public static boolean isReparseTag(File file)
}
return false;
}

public static String resolveEnvVariable(String path)
{
Pattern winEnvPattern = Pattern.compile("%(\\w+)%"); //$NON-NLS-1$
Pattern unixEnvPattern = Pattern.compile("\\$(\\w+)"); //$NON-NLS-1$
Matcher matcher;
if (Platform.getOS().equals(Platform.OS_WIN32))
{
matcher = winEnvPattern.matcher(path);
}
else
{
matcher = unixEnvPattern.matcher(path);
}

StringBuffer resolvedPath = new StringBuffer();
while (matcher.find())
{
String envVarName = matcher.group(1);
String envVarValue = System.getenv(envVarName);

if (envVarValue != null)
{
matcher.appendReplacement(resolvedPath, envVarValue.replace("\\", "\\\\")); //$NON-NLS-1$ //$NON-NLS-2$
}
else
{
// If the environment variable is not found, keep the original
matcher.appendReplacement(resolvedPath, matcher.group(0));
}
}
matcher.appendTail(resolvedPath);

return resolvedPath.toString();

}

public static Map<String, String> getSystemEnv()
{
Map<String, String> env = new HashMap<String, String>(System.getenv());
String idfToolsPath = Platform.getPreferencesService().getString(IDFCorePlugin.PLUGIN_ID,
IDFCorePreferenceConstants.IDF_TOOLS_PATH, IDFCorePreferenceConstants.IDF_TOOLS_PATH_DEFAULT, null);
env.put(IDFCorePreferenceConstants.IDF_TOOLS_PATH, idfToolsPath);
return env;
}

public static String getIDFToolsPathFromPreferences()
{
String idfToolsPath = Platform.getPreferencesService().getString(IDFCorePlugin.PLUGIN_ID,
IDFCorePreferenceConstants.IDF_TOOLS_PATH, IDFCorePreferenceConstants.IDF_TOOLS_PATH_DEFAULT, null);
return idfToolsPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Process start() throws Exception
idfEnvMap.put("PYTHONUNBUFFERED", "1"); //$NON-NLS-1$ //$NON-NLS-2$

// Update with the CDT build environment variables
Map<String, String> environment = new HashMap<>(System.getenv());
Map<String, String> environment = new HashMap<>(IDFUtil.getSystemEnv());
environment.putAll(idfEnvMap);

Logger.log(environment.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private String buildProjectDescriptionPath()

private void runEspIdfSbomCommand()
{
Map<String, String> environment = new HashMap<>(System.getenv());
Map<String, String> environment = new HashMap<>(IDFUtil.getSystemEnv());
List<String> arguments = new ArrayList<>();
final String pythonEnvPath = IDFUtil.getIDFPythonEnvPath();
arguments.add(pythonEnvPath);
Expand Down Expand Up @@ -435,7 +435,7 @@ private boolean checkIfFileIsNotWritable(java.nio.file.Path pathToFile)

private void installEspIdfSbom()
{
Map<String, String> environment = new HashMap<>(System.getenv());
Map<String, String> environment = new HashMap<>(IDFUtil.getSystemEnv());
List<String> arguments = new ArrayList<>();
final String pythonEnvPath = IDFUtil.getIDFPythonEnvPath();
arguments.add(pythonEnvPath);
Expand All @@ -450,7 +450,7 @@ private void installEspIdfSbom()

private boolean getEspIdfSbomInstalledStatus()
{
Map<String, String> environment = new HashMap<>(System.getenv());
Map<String, String> environment = new HashMap<>(IDFUtil.getSystemEnv());
List<String> arguments = new ArrayList<>();
final String pythonEnvPath = IDFUtil.getIDFPythonEnvPath();
arguments.add(pythonEnvPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private String getPythonExeVersion(String pythonExePath)
List<String> commands = new ArrayList<>();
commands.add(pythonExePath);
commands.add("--version"); //$NON-NLS-1$
return pythonExePath != null ? runCommand(commands, System.getenv()) : null;
return pythonExePath != null ? runCommand(commands, IDFUtil.getSystemEnv()) : null;
}

private String runCommand(List<String> arguments, Map<String, String> env)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
Expand Down Expand Up @@ -38,6 +41,7 @@ public class EspresssifPreferencesPage extends PreferencePage implements IWorkbe

private Text gitAssetsText;
private Text pythonWheelText;
private Text idfToolsPathText;

public EspresssifPreferencesPage()
{
Expand Down Expand Up @@ -83,7 +87,7 @@ private void addToolsInstallationSettings(Composite mainComposite)
{
Group toolsInstallationGroup = new Group(mainComposite, SWT.SHADOW_ETCHED_IN);
toolsInstallationGroup.setText(Messages.EspressifPreferencesPage_ToolsInstallationGrpTxt);
toolsInstallationGroup.setLayout(new GridLayout(2, false));
toolsInstallationGroup.setLayout(new GridLayout(3, false));

Label githubAssetsLabel = new Label(toolsInstallationGroup, SWT.NONE);
githubAssetsLabel.setText(Messages.EspressifPreferencesPage_ToolsInstallationGitAssetUrlLabel);
Expand All @@ -92,24 +96,51 @@ private void addToolsInstallationSettings(Composite mainComposite)
Label pythonWheelsLabel = new Label(toolsInstallationGroup, SWT.NONE);
pythonWheelsLabel.setText(Messages.EspressifPreferencesPage_ToolsInstallationPythonPyWheelUrlLabel);
pythonWheelText = new Text(toolsInstallationGroup, SWT.SINGLE | SWT.NONE);

GridData gitTextGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
GridData gitTextGridData = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
gitTextGridData.widthHint = 200;
GridData pythonTextGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
GridData pythonTextGridData = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
pythonTextGridData.widthHint = 200;
gitAssetsText.setLayoutData(gitTextGridData);
pythonWheelText.setLayoutData(pythonTextGridData);

Label idfToolsPathLabel = new Label(toolsInstallationGroup, SWT.NONE);
idfToolsPathLabel.setText(Messages.EspressifPreferencesPage_EspIdfToolsInstallationDirectoryLabel);
idfToolsPathText = new Text(toolsInstallationGroup, SWT.SINGLE | SWT.NONE);
GridData idfToolsPathTextGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
idfToolsPathTextGridData.widthHint = 200;
idfToolsPathText.setLayoutData(idfToolsPathTextGridData);
Comment on lines +109 to +112
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also provide a Browse button to choose a directory?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

// Add browse button
Button browseButtonIdfToolsPath = new Button(toolsInstallationGroup, SWT.PUSH);
browseButtonIdfToolsPath.setText(Messages.EspressifPreferencesPage_DirectorySelectionIDFToolsPathBrowseButton);
browseButtonIdfToolsPath.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(mainComposite.getShell());
dialog.setText(Messages.EspressifPreferencesPage_DirectorySelectionIDFToolsPathTitle);
dialog.setMessage(Messages.EspressifPreferencesPage_DirectorySelectionIDFToolsPathMessage);
String dir = dialog.open();
if (dir != null) {
idfToolsPathText.setText(dir);
}
}
});

String gitUrl = getPreferenceStore().getString(IDFCorePreferenceConstants.IDF_GITHUB_ASSETS);
String pyWheelUrl = getPreferenceStore().getString(IDFCorePreferenceConstants.PIP_EXTRA_INDEX_URL);
String idfToolsPath = getPreferenceStore().getString(IDFCorePreferenceConstants.IDF_TOOLS_PATH);
gitUrl = StringUtil.isEmpty(gitUrl)
? getPreferenceStore().getDefaultString(IDFCorePreferenceConstants.IDF_GITHUB_ASSETS)
: gitUrl;
pyWheelUrl = StringUtil.isEmpty(pyWheelUrl)
? getPreferenceStore().getDefaultString(IDFCorePreferenceConstants.PIP_EXTRA_INDEX_URL)
: pyWheelUrl;
idfToolsPath = StringUtil.isEmpty(idfToolsPath)
? getPreferenceStore().getDefaultString(IDFCorePreferenceConstants.IDF_TOOLS_PATH)
: idfToolsPath;
gitAssetsText.setText(gitUrl);
pythonWheelText.setText(pyWheelUrl);
idfToolsPathText.setText(idfToolsPath);
}

private void addBuildSettings(Composite mainComposite)
Expand Down Expand Up @@ -214,6 +245,8 @@ public boolean performOk()
getPreferenceStore().setValue(IDFCorePreferenceConstants.IDF_GITHUB_ASSETS, gitAssetsText.getText());

getPreferenceStore().setValue(IDFCorePreferenceConstants.PIP_EXTRA_INDEX_URL, pythonWheelText.getText());

getPreferenceStore().setValue(IDFCorePreferenceConstants.IDF_TOOLS_PATH, idfToolsPathText.getText());
}
catch (Exception e)
{
Expand All @@ -237,6 +270,7 @@ protected void performDefaults()
.setSelection(getPreferenceStore().getBoolean(IDFCorePreferenceConstants.HIDE_ERRORS_IDF_COMPONENTS));
gitAssetsText.setText(getPreferenceStore().getDefaultString(IDFCorePreferenceConstants.IDF_GITHUB_ASSETS));
pythonWheelText.setText(getPreferenceStore().getDefaultString(IDFCorePreferenceConstants.PIP_EXTRA_INDEX_URL));
idfToolsPathText.setText(getPreferenceStore().getDefaultString(IDFCorePreferenceConstants.IDF_TOOLS_PATH));
}

private void initializeDefaults()
Expand All @@ -253,5 +287,6 @@ private void initializeDefaults()

getPreferenceStore().setDefault(IDFCorePreferenceConstants.IDF_GITHUB_ASSETS, IDFCorePreferenceConstants.IDF_GITHUB_ASSETS_DEFAULT);
getPreferenceStore().setDefault(IDFCorePreferenceConstants.PIP_EXTRA_INDEX_URL, IDFCorePreferenceConstants.PIP_EXTRA_INDEX_URL_DEFAULT);
getPreferenceStore().setDefault(IDFCorePreferenceConstants.IDF_TOOLS_PATH, IDFCorePreferenceConstants.IDF_TOOLS_PATH_DEFAULT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class Messages extends NLS
public static String EspressifPreferencesPage_ToolsInstallationGrpTxt;
public static String EspressifPreferencesPage_ToolsInstallationGitAssetUrlLabel;
public static String EspressifPreferencesPage_ToolsInstallationPythonPyWheelUrlLabel;
public static String EspressifPreferencesPage_EspIdfToolsInstallationDirectoryLabel;
public static String EspressifPreferencesPage_DirectorySelectionIDFToolsPathMessage;
public static String EspressifPreferencesPage_DirectorySelectionIDFToolsPathTitle;
public static String EspressifPreferencesPage_DirectorySelectionIDFToolsPathBrowseButton;

static
{
Expand Down
Loading