diff --git a/bundles/com.espressif.idf.core/META-INF/MANIFEST.MF b/bundles/com.espressif.idf.core/META-INF/MANIFEST.MF index 6298a9e93..521544273 100644 --- a/bundles/com.espressif.idf.core/META-INF/MANIFEST.MF +++ b/bundles/com.espressif.idf.core/META-INF/MANIFEST.MF @@ -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 diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ProcessBuilderFactory.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ProcessBuilderFactory.java index 0eba109ce..a9c1e9dcb 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ProcessBuilderFactory.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/ProcessBuilderFactory.java @@ -36,8 +36,10 @@ public Process run(List commands, IPath workingDirectory, Map 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); } /** diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ConsoleManager.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ConsoleManager.java new file mode 100644 index 000000000..03c9e518c --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ConsoleManager.java @@ -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; + } +} diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IdfCommandExecutor.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IdfCommandExecutor.java new file mode 100644 index 000000000..93ff26eff --- /dev/null +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/IdfCommandExecutor.java @@ -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 arguments = prepareArguments(); + Map 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 prepareArguments() + { + List 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 arguments, Map 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); + } + } +} diff --git a/bundles/com.espressif.idf.terminal.connector/src/com/espressif/idf/terminal/connector/launcher/IDFConsoleLauncherDelegate.java b/bundles/com.espressif.idf.terminal.connector/src/com/espressif/idf/terminal/connector/launcher/IDFConsoleLauncherDelegate.java index 26bc07285..c940b314c 100644 --- a/bundles/com.espressif.idf.terminal.connector/src/com/espressif/idf/terminal/connector/launcher/IDFConsoleLauncherDelegate.java +++ b/bundles/com.espressif.idf.terminal.connector/src/com/espressif/idf/terminal/connector/launcher/IDFConsoleLauncherDelegate.java @@ -397,7 +397,8 @@ public ITerminalConnector createTerminalConnector(Map 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()]); diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/Messages.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/Messages.java index 48ab9667e..bb1aa3f9c 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/Messages.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/Messages.java @@ -10,7 +10,8 @@ * @author Kondal Kolipaka * */ -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; @@ -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() + { } } diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/NewIDFProjectWizard.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/NewIDFProjectWizard.java index d2afdad6e..e65e4aa33 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/NewIDFProjectWizard.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/NewIDFProjectWizard.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -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); @@ -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 arguments = new ArrayList(); - 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 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 diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/messages.properties b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/messages.properties index 9bc7f0299..225d3a5fe 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/messages.properties +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/messages.properties @@ -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: \ No newline at end of file +NewComponentWizardPage_ProjectNameLbl=Project name: +IdfReconfigureJobName=Running idf.py reconfigure command...