Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.build;

import java.util.Optional;
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.launchbar.core.ILaunchBarManager;

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

public class ActiveLaunchConfigurationProvider
{
private ILaunchBarManager launchBarManager;

public ActiveLaunchConfigurationProvider(ILaunchBarManager launchBarManager)
{
this.launchBarManager = launchBarManager;
}

public ActiveLaunchConfigurationProvider()
{
this(IDFCorePlugin.getService(ILaunchBarManager.class));
}

public ILaunchConfiguration getActiveLaunchConfiguration() throws CoreException
{
ILaunchConfiguration configuration = launchBarManager.getActiveLaunchConfiguration();

if (configuration == null)
{
Job[] jobs = Job.getJobManager().find(null);
@SuppressWarnings("restriction")
Optional<Job> launchBarInitJob = Stream.of(jobs)
.filter(job -> job.getName()
.equals(org.eclipse.launchbar.core.internal.Messages.LaunchBarManager_0))
.findAny();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can avoid referencing internal classes and messages if possible. In this case if we can copy the message and compare it directly

Copy link
Copy Markdown
Collaborator Author

@sigmaaa sigmaaa Aug 3, 2023

Choose a reason for hiding this comment

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

Hi @kolipakakondal, thanks for your feedback. In this case, we rely on this message because it's the name of the Job, so if this message is changed on the eclipse side, this code just won't work and we won't know about it

launchBarInitJob.ifPresent(job -> {
try
{
job.join();
}
catch (InterruptedException e)
{
Logger.log(e);
Thread.currentThread().interrupt();
}
});
configuration = launchBarManager.getActiveLaunchConfiguration();
}
return configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchMode;
import org.eclipse.launchbar.core.ILaunchBarManager;
Expand All @@ -114,6 +113,7 @@
public class IDFBuildConfiguration extends CBuildConfiguration
{

private static final ActiveLaunchConfigurationProvider LAUNCH_CONFIG_PROVIDER = new ActiveLaunchConfigurationProvider();
private static final String NINJA = "Ninja"; //$NON-NLS-1$
protected static final String COMPILE_COMMANDS_JSON = "compile_commands.json"; //$NON-NLS-1$
protected static final String COMPONENTS = "components"; //$NON-NLS-1$
Expand Down Expand Up @@ -248,15 +248,14 @@ public String getProperty(String name)
{
try
{
ILaunchConfiguration configuration = IDFCorePlugin.getService(ILaunchBarManager.class)
.getActiveLaunchConfiguration();
ILaunchConfigurationType debugConfigurationType = DebugPlugin.getDefault().getLaunchManager()
.getLaunchConfigurationType(IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE);
if (configuration.getType().equals(debugConfigurationType))
ILaunchConfiguration configuration = LAUNCH_CONFIG_PROVIDER.getActiveLaunchConfiguration();
if (configuration != null
&& configuration.getType().getIdentifier().equals(IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE))
{
configuration = getBoundConfiguration(configuration);
}
String property = configuration.getAttribute(name, StringUtil.EMPTY);
String property = configuration == null ? StringUtil.EMPTY
: configuration.getAttribute(name, StringUtil.EMPTY);
property = property.isBlank() ? getSettings().get(name, StringUtil.EMPTY) : property;
return property;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SerialFlashLaunchConfigProvider extends IDFCoreLaunchConfigProvider
@Override
public boolean supports(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException
{
return target.getTypeId().equals(IDFLaunchConstants.ESP_LAUNCH_TARGET_TYPE);
return target != null && target.getTypeId().equals(IDFLaunchConstants.ESP_LAUNCH_TARGET_TYPE);
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1",
junit-jupiter-api,
org.junit,
junit-jupiter-params
junit-jupiter-params,
org.eclipse.launchbar.core
Bundle-ClassPath: .,
lib/jmock-2.12.0.jar,
lib/commons-collections4-4.4.jar,
Expand Down Expand Up @@ -72,6 +73,7 @@ Export-Package: com.espressif.idf.core.test,
org.jmock.auto,
org.jmock.lib
Import-Package: org.eclipse.core.runtime,
org.eclipse.core.runtime.jobs,
org.eclipse.core.variables,
org.junit.jupiter.params,
org.junit.jupiter.params.provider,
org.eclipse.core.variables
org.junit.jupiter.params.provider
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.launchbar.core.ILaunchBarManager;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import com.espressif.idf.core.build.ActiveLaunchConfigurationProvider;
import com.espressif.idf.core.logging.Logger;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class ActiveLaunchConfigurationTest
{

private static final String EXPECTED_LAUNCH_CONFIG_NAME = "expected_launch_config_name";

@Test
void get_active_launch_configuration_returns_expected_config_when_init_launchbar_job_is_active()
throws CoreException
{
ILaunchBarManager launchBarManager = mock(ILaunchBarManager.class);
ILaunchConfiguration launchConfiguration = mock(ILaunchConfiguration.class);

when(launchConfiguration.getName()).thenReturn(EXPECTED_LAUNCH_CONFIG_NAME);
when(launchBarManager.getActiveLaunchConfiguration()).thenReturn(null);
ActiveLaunchConfigurationProvider provider = new ActiveLaunchConfigurationProvider(launchBarManager);
runInitLaunchBarJob(launchBarManager, launchConfiguration);
ILaunchConfiguration configuration = provider.getActiveLaunchConfiguration();

assertEquals(EXPECTED_LAUNCH_CONFIG_NAME, configuration.getName());
}

@Test
void get_active_launch_configuration_returns_expected_config_when_init_launchbar_job_is_not_active()
throws CoreException
{
ILaunchBarManager launchBarManager = mock(ILaunchBarManager.class);
ILaunchConfiguration launchConfiguration = mock(ILaunchConfiguration.class);

when(launchConfiguration.getName()).thenReturn(EXPECTED_LAUNCH_CONFIG_NAME);
when(launchBarManager.getActiveLaunchConfiguration()).thenReturn(launchConfiguration);
ActiveLaunchConfigurationProvider provider = new ActiveLaunchConfigurationProvider(launchBarManager);
ILaunchConfiguration configuration = provider.getActiveLaunchConfiguration();

assertEquals(EXPECTED_LAUNCH_CONFIG_NAME, configuration.getName());
}

private void runInitLaunchBarJob(ILaunchBarManager launchBarManager, ILaunchConfiguration launchConfiguration)
{
@SuppressWarnings("restriction")
Job job = new Job(org.eclipse.launchbar.core.internal.Messages.LaunchBarManager_0)
{

protected IStatus run(IProgressMonitor monitor)
{
try
{
when(launchBarManager.getActiveLaunchConfiguration()).thenReturn(launchConfiguration);
}
catch (CoreException e)
{
Logger.log(e);
}
return Status.OK_STATUS;
}
};
job.schedule();
}

}