Skip to content

Commit

Permalink
fix: fixing esp-idf terminal and idf.py reconfigure not working on wi…
Browse files Browse the repository at this point in the history
…ndows
  • Loading branch information
sigmaaa committed Nov 11, 2024
1 parent ebf8111 commit 1b1376a
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 131 deletions.
3 changes: 2 additions & 1 deletion bundles/com.espressif.idf.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ Bundle-ClassPath: .,
lib/commons-compress-1.21.jar,
lib/xz-1.9.jar
Import-Package: org.eclipse.embedcdt.core,
org.eclipse.launchbar.ui.target
org.eclipse.launchbar.ui.target,
org.eclipse.ui.console
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public Process run(List<String> commands, IPath workingDirectory, Map<String, St
if (environment != null && !environment.isEmpty())
{
processBuilder.environment().putAll(environment);
// Removing Path, because we are using PATH
processBuilder.environment().remove("Path"); //$NON-NLS-1$
}
// let's merge the error stream with the standard output

processBuilder.redirectErrorStream(true);
return processBuilder.start();
}
Expand All @@ -46,8 +48,7 @@ public IStatus runInBackground(List<String> commands, IPath workingDirectory, Ma
throws IOException
{
Process process = run(commands, workingDirectory, environment);
return processData(process.getInputStream(), process.getErrorStream(), process.getOutputStream(),
process);
return processData(process.getInputStream(), process.getErrorStream(), process.getOutputStream(), process);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright 2024 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/

package com.espressif.idf.core.util;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;

public class ConsoleManager
{

private ConsoleManager()
{
}

public static MessageConsole getConsole(String consoleName)
{
MessageConsole console = findConsole(consoleName);
if (console == null)
{
console = new MessageConsole(consoleName, null);
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
}
ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
return console;
}

private static MessageConsole findConsole(String consoleName)
{
for (IConsole existing : ConsolePlugin.getDefault().getConsoleManager().getConsoles())
{
if (consoleName.equals(existing.getName()))
{
return (MessageConsole) existing;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*******************************************************************************
* Copyright 2021 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/

package com.espressif.idf.core.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import com.espressif.idf.core.IDFCorePlugin;
import com.espressif.idf.core.IDFEnvironmentVariables;
import com.espressif.idf.core.ProcessBuilderFactory;
import com.espressif.idf.core.logging.Logger;

public class IdfCommandExecutor
{

private final String target;
private final MessageConsole console;

public IdfCommandExecutor(String target, MessageConsole console)
{
this.target = target;
this.console = console;
}

public IStatus executeReconfigure(IProject project)
{
console.activate();
return runIdfReconfigureCommand(project);
}

private IStatus runIdfReconfigureCommand(IProject project)
{
ProcessBuilderFactory processRunner = new ProcessBuilderFactory();
List<String> arguments = prepareArguments();
Map<String, String> environment = new HashMap<>(new IDFEnvironmentVariables().getEnvMap());

try (MessageConsoleStream messageConsoleStream = console.newMessageStream())
{
return runProcess(arguments, environment, processRunner, project, messageConsoleStream);
}
catch (IOException e1)
{
Logger.log(e1);
return IDFCorePlugin.errorStatus(e1.getMessage(), e1);
}
}

private List<String> prepareArguments()
{
List<String> arguments = new ArrayList<>();
arguments.add(IDFUtil.getIDFPythonEnvPath());
arguments.add(IDFUtil.getIDFPythonScriptFile().getAbsolutePath());
arguments.add("-DIDF_TARGET=" + target); //$NON-NLS-1$
arguments.add("reconfigure"); //$NON-NLS-1$
return arguments;
}

private IStatus runProcess(List<String> arguments, Map<String, String> environment,
ProcessBuilderFactory processRunner, IProject project, MessageConsoleStream messageConsoleStream)
{
StringBuilder output = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
processRunner.run(arguments, project.getLocation(), environment).getInputStream())))
{
String line;
while ((line = reader.readLine()) != null)
{
output.append(line).append(System.lineSeparator());
messageConsoleStream.println(line);
}
return new Status(IStatus.OK, IDFCorePlugin.PLUGIN_ID, output.toString());
}
catch (Exception e)
{
Logger.log(e);
return IDFCorePlugin.errorStatus(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ public ITerminalConnector createTerminalConnector(Map<String, Object> properties
}
envpList.add(envKey + "=" + envValue); //$NON-NLS-1$
}

//Removing path, since we are using PATH
envMap.remove("Path"); //$NON-NLS-1$
// Convert back into a string array
envp = envpList.toArray(new String[envpList.size()]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* @author Kondal Kolipaka <[email protected]>
*
*/
public class Messages extends NLS {
public class Messages extends NLS
{
private static final String BUNDLE_NAME = "com.espressif.idf.ui.wizard.messages"; //$NON-NLS-1$
public static String ImportIDFProjectWizard_0;
public static String ImportIDFProjectWizardPage_0;
Expand Down Expand Up @@ -41,12 +42,15 @@ public class Messages extends NLS {
public static String NewComponentWizardPage_CantCreateCompErr;
public static String NewComponentWizardPage_ProjectDoesntExistErr;
public static String NewComponentWizardPage_ProjectNameLbl;

static {
public static String IdfReconfigureJobName;

static
{
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}

private Messages() {
private Messages()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
*******************************************************************************/
package com.espressif.idf.ui.wizard;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.debug.internal.core.InternalDebugCoreMessages;
import org.eclipse.core.resources.IProject;
Expand All @@ -19,7 +13,6 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugPlugin;
Expand All @@ -42,10 +35,7 @@
import org.eclipse.tools.templates.ui.TemplateWizard;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;

import com.espressif.idf.core.IDFConstants;
Expand All @@ -57,7 +47,8 @@
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.ClangFormatFileHandler;
import com.espressif.idf.core.util.ClangdConfigFileHandler;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.core.util.ConsoleManager;
import com.espressif.idf.core.util.IdfCommandExecutor;
import com.espressif.idf.core.util.LaunchUtil;
import com.espressif.idf.ui.UIPlugin;
import com.espressif.idf.ui.handlers.EclipseHandler;
Expand Down Expand Up @@ -157,12 +148,14 @@ public boolean performFinish()
{
Logger.log(e);
}
Job job = new Job("Running idf.py reconfigure command...")
Job job = new Job(Messages.IdfReconfigureJobName)
{

protected IStatus run(IProgressMonitor monitor)
{
IStatus status = runCommandIdfPyInIdfEnv(target);
IdfCommandExecutor executor = new IdfCommandExecutor(target,
ConsoleManager.getConsole("CDT Build Console")); //$NON-NLS-1$
IStatus status = executor.executeReconfigure(project);
try
{
IDEWorkbenchPlugin.getPluginWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
Expand All @@ -179,114 +172,6 @@ protected IStatus run(IProgressMonitor monitor)
return performFinish;
}

protected IStatus runCommandIdfPyInIdfEnv(String target)
{
openConsole("CDT Build Console"); //$NON-NLS-1$
console.activate();
MessageConsoleStream messageConsoleStream = console.newMessageStream();
ProcessBuilderFactory processRunner = new ProcessBuilderFactory();
StringBuilder output = new StringBuilder();
int waitCount = 10;
List<String> arguments = new ArrayList<String>();
try
{
arguments.add(0, pythonVirtualExecutablePath());
arguments.add(1, IDFUtil.getIDFPythonScriptFile().getAbsolutePath());
arguments.add("-DIDF_TARGET=" + target); //$NON-NLS-1$
arguments.add("reconfigure"); //$NON-NLS-1$

Map<String, String> environment = new HashMap<>(new IDFEnvironmentVariables().getEnvMap());
Logger.log(environment.toString());
Process process = processRunner.run(arguments, project.getLocation(), environment);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
output.append(line).append(System.lineSeparator());
messageConsoleStream.println(line);
messageConsoleStream.flush();
}

while (process.isAlive() && waitCount > 0)
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
Logger.log(e);
}
waitCount--;
}

if (waitCount == 0)
{
messageConsoleStream.println("Process possibly stuck"); //$NON-NLS-1$
Logger.log("Process possibly stuck"); //$NON-NLS-1$
return Status.CANCEL_STATUS;
}

IStatus status = new Status(process.exitValue() == 0 ? IStatus.OK : IStatus.ERROR, UIPlugin.PLUGIN_ID,
process.exitValue(), output.toString(), null);
messageConsoleStream.flush();
return status;
}
catch (Exception e1)
{
Logger.log(IDFCorePlugin.getPlugin(), e1);
return IDFCorePlugin.errorStatus(e1.getMessage(), e1);
}
}

private void openConsole(String consoleName)
{
// add it if necessary
boolean found = false;

IConsole[] consoles = ConsolePlugin.getDefault().getConsoleManager().getConsoles();
for (int i = 0; i < consoles.length; i++)
{
if (consoleName.equals(consoles[i].getName()))
{
console = (MessageConsole) consoles[i];
found = true;
break;
}
}

if (!found)
{
console = new MessageConsole(consoleName, null);
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
}

ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
}

protected String pythonVirtualExecutablePath()
{
String pythonVirtualPath = new IDFEnvironmentVariables()
.getEnvValue(IDFEnvironmentVariables.IDF_PYTHON_ENV_PATH);
StringBuilder pythonVirtualExePath = new StringBuilder();
pythonVirtualExePath.append(pythonVirtualPath);
pythonVirtualExePath.append("/"); //$NON-NLS-1$
if (Platform.getOS().equals(Platform.OS_WIN32))
{
pythonVirtualExePath.append("Scripts"); //$NON-NLS-1$
pythonVirtualExePath.append("/"); //$NON-NLS-1$
pythonVirtualExePath.append("python.exe"); //$NON-NLS-1$
}
else
{
pythonVirtualExePath.append("bin"); //$NON-NLS-1$
pythonVirtualExePath.append("/"); //$NON-NLS-1$
pythonVirtualExePath.append("python"); //$NON-NLS-1$
}

return pythonVirtualExePath.toString();
}

private void updateClangFiles(IProject project)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ NewIdfComponentWizard_NameCantBeEmptyErr=A component name can't be empty
NewIdfComponentWizard_NameAlreadyExistsErr=A component with such a name is already exists
NewComponentWizardPage_CantCreateCompErr=Can not create a component without a project selection
NewComponentWizardPage_ProjectDoesntExistErr=Project doesn't exist
NewComponentWizardPage_ProjectNameLbl=Project name:
NewComponentWizardPage_ProjectNameLbl=Project name:
IdfReconfigureJobName=Running idf.py reconfigure command...

0 comments on commit 1b1376a

Please sign in to comment.