Add commands to workspace when importing project samples (#2976)

Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com>
6.19.x
Kaloyan Raev 2017-01-27 17:51:07 +02:00 committed by Vitalii Parfonov
parent 6d459be382
commit a652db2c46
4 changed files with 110 additions and 3 deletions

View File

@ -15,6 +15,7 @@ import com.google.common.annotations.Beta;
import org.eclipse.che.api.core.model.project.NewProjectConfig;
import org.eclipse.che.api.core.model.project.ProjectConfig;
import org.eclipse.che.api.core.model.project.SourceStorage;
import org.eclipse.che.api.machine.shared.dto.CommandDto;
import java.util.List;
import java.util.Map;
@ -37,6 +38,7 @@ public class MutableProjectConfig implements ProjectConfig {
private MutableSourceStorage sourceStorage;
private Map<String, String> options;
private List<NewProjectConfig> projects;
private List<CommandDto> commands;
public MutableProjectConfig(ProjectConfig source) {
name = source.getName();
@ -136,6 +138,17 @@ public class MutableProjectConfig implements ProjectConfig {
public void setOptions(Map<String, String> options) {
this.options = options;
}
public List<CommandDto> getCommands() {
if (commands == null) {
commands = newArrayList();
}
return commands;
}
public void setCommands(List<CommandDto> commands) {
this.commands = commands;
}
/**
* Returns the list of configurations to creating projects

View File

@ -14,6 +14,7 @@ import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.eclipse.che.api.machine.shared.dto.CommandDto;
import org.eclipse.che.api.promises.client.Function;
import org.eclipse.che.api.promises.client.FunctionException;
import org.eclipse.che.api.promises.client.Operation;
@ -21,6 +22,8 @@ import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.PromiseError;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.command.CommandImpl;
import org.eclipse.che.ide.api.command.CommandManager;
import org.eclipse.che.ide.api.project.MutableProjectConfig;
import org.eclipse.che.ide.api.project.type.wizard.ProjectWizardMode;
import org.eclipse.che.ide.api.resources.Container;
@ -49,17 +52,22 @@ import static org.eclipse.che.ide.api.resources.Resource.PROJECT;
* @author Valeriy Svydenko
*/
public class ProjectWizard extends AbstractWizard<MutableProjectConfig> {
private final static String PROJECT_PATH_MACRO_REGEX = "\\$\\{current.project.path\\}";
private final ProjectWizardMode mode;
private final AppContext appContext;
private final CommandManager commandManager;
@Inject
public ProjectWizard(@Assisted MutableProjectConfig dataObject,
@Assisted ProjectWizardMode mode,
AppContext appContext) {
AppContext appContext,
CommandManager commandManager) {
super(dataObject);
this.mode = mode;
this.appContext = appContext;
this.commandManager = commandManager;
context.put(WIZARD_MODE_KEY, mode.toString());
context.put(PROJECT_NAME_KEY, dataObject.getName());
@ -108,11 +116,50 @@ public class ProjectWizard extends AbstractWizard<MutableProjectConfig> {
return project.update().withBody(dataObject).send();
}
})
.then(onComplete(callback))
.then(addCommands(callback))
.catchError(onFailure(callback));
}
}
private Operation<Project> addCommands(final CompleteCallback callback) {
return new Operation<Project>() {
@Override
public void apply(final Project project) throws OperationException {
Promise<CommandImpl> chain = null;
for (final CommandDto command : dataObject.getCommands()) {
if (chain == null) {
chain = addCommand(project, command);
} else {
chain = chain.thenPromise(new Function<CommandImpl, Promise<CommandImpl>>() {
@Override
public Promise<CommandImpl> apply(CommandImpl ignored) throws FunctionException {
return addCommand(project, command);
}
});
}
}
if (chain == null) {
callback.onCompleted();
} else {
chain.then(new Operation<CommandImpl>() {
@Override
public void apply(CommandImpl ignored) throws OperationException {
callback.onCompleted();
}
}).catchError(onFailure(callback));
}
}
};
}
private Promise<CommandImpl> addCommand(Project project, CommandDto command) {
String name = project.getName() + ": " + command.getName();
String projectPath = appContext.getProjectsRoot().append(project.getPath()).toString();
String commandLine = command.getCommandLine().replaceAll(PROJECT_PATH_MACRO_REGEX, projectPath);
return commandManager.create(name, commandLine, command.getType(), command.getAttributes());
}
private Operation<Project> onComplete(final CompleteCallback callback) {
return new Operation<Project>() {
@Override

View File

@ -218,6 +218,7 @@ public class ProjectWizardPresenter implements Wizard.UpdateDelegate,
dataObject.setSource(newProjectConfig.getSource());
dataObject.setAttributes(newProjectConfig.getAttributes());
dataObject.setOptions(newProjectConfig.getOptions());
dataObject.setCommands(projectTemplate.getCommands());
}
/** Creates or returns project wizard for the specified projectType with the given dataObject. */

View File

@ -13,11 +13,14 @@ package org.eclipse.che.ide.projecttype.wizard;
import com.google.common.base.Optional;
import org.eclipse.che.api.core.model.project.ProjectConfig;
import org.eclipse.che.api.machine.shared.dto.CommandDto;
import org.eclipse.che.api.promises.client.Function;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.PromiseError;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.command.CommandImpl;
import org.eclipse.che.ide.api.command.CommandManager;
import org.eclipse.che.ide.api.project.MutableProjectConfig;
import org.eclipse.che.ide.api.project.type.wizard.ProjectWizardMode;
import org.eclipse.che.ide.api.resources.Container;
@ -34,6 +37,9 @@ import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import java.util.Map;
import static org.eclipse.che.ide.api.project.type.wizard.ProjectWizardMode.CREATE;
import static org.eclipse.che.ide.api.project.type.wizard.ProjectWizardMode.IMPORT;
import static org.eclipse.che.ide.api.project.type.wizard.ProjectWizardMode.UPDATE;
@ -59,6 +65,8 @@ public class ProjectWizardTest {
@Mock
private AppContext appContext;
@Mock
private CommandManager commandManager;
@Mock
private Container workspaceRoot;
@Mock
private Project.ProjectRequest createProjectRequest;
@ -67,6 +75,12 @@ public class ProjectWizardTest {
@Mock
private Project createdProject;
@Mock
private CommandDto command;
@Mock
private Promise<CommandImpl> createCommandPromise;
@Mock
private CommandImpl createdCommand;
@Mock
private Promise<Optional<Container>> optionalContainerPromise;
@Mock
private Project projectToUpdate;
@ -81,6 +95,8 @@ public class ProjectWizardTest {
@Captor
private ArgumentCaptor<Operation<Project>> completeOperationCaptor;
@Captor
private ArgumentCaptor<Operation<CommandImpl>> completeAddCommandsOperationCaptor;
@Captor
private ArgumentCaptor<Operation<PromiseError>> failedOperationCaptor;
@Captor
private ArgumentCaptor<Operation<Optional<Container>>> optionalContainerCaptor;
@ -90,7 +106,9 @@ public class ProjectWizardTest {
@Before
public void setUp() {
when(appContext.getWorkspaceRoot()).thenReturn(workspaceRoot);
when(appContext.getProjectsRoot()).thenReturn(Path.valueOf("/projects"));
when(dataObject.getPath()).thenReturn(Path.valueOf(PROJECT_NAME).toString());
when(createdProject.getPath()).thenReturn(PROJECT_NAME);
}
@Test
@ -112,7 +130,7 @@ public class ProjectWizardTest {
}
private void prepareWizard(ProjectWizardMode mode) {
wizard = new ProjectWizard(dataObject, mode, appContext);
wizard = new ProjectWizard(dataObject, mode, appContext, commandManager);
}
@Test
@ -155,6 +173,34 @@ public class ProjectWizardTest {
verify(completeCallback).onCompleted();
}
@Test
public void shouldImportProjectWithCommandSuccessfully() throws Exception {
prepareWizard(IMPORT);
when(workspaceRoot.importProject()).thenReturn(createProjectRequest);
when(createProjectRequest.withBody(any(ProjectConfig.class))).thenReturn(createProjectRequest);
when(createProjectRequest.send()).thenReturn(createProjectPromise);
when(createProjectPromise.then(any(Operation.class))).thenReturn(createProjectPromise);
when(createProjectPromise.thenPromise(any(Function.class))).thenReturn(createProjectPromise);
when(createProjectPromise.catchError(any(Operation.class))).thenReturn(createProjectPromise);
when(promiseError.getCause()).thenReturn(exception);
when(dataObject.getCommands()).thenReturn(Collections.singletonList(command));
when(command.getCommandLine()).thenReturn("echo 'Hello'");
when(commandManager.create(any(String.class), any(String.class), any(String.class), any(Map.class))).thenReturn(createCommandPromise);
when(createCommandPromise.then(any(Operation.class))).thenReturn(createCommandPromise);
when(createCommandPromise.catchError(any(Operation.class))).thenReturn(createCommandPromise);
wizard.complete(completeCallback);
verify(createProjectPromise).then(completeOperationCaptor.capture());
completeOperationCaptor.getValue().apply(createdProject);
verify(createCommandPromise).then(completeAddCommandsOperationCaptor.capture());
completeAddCommandsOperationCaptor.getValue().apply(createdCommand);
verify(completeCallback).onCompleted();
}
@Test
public void shouldFailOnImportProject() throws Exception {
prepareWizard(IMPORT);