blob: 0f2cc6bb38cf4e74ac783bb7c0d26126ad681822 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2014 Bosch Software Innovations GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Bosch Software Innovations GmbH - Please refer to git log
*
*******************************************************************************/
package org.eclipse.vorto.codegen.api.tasks.eclipse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.vorto.codegen.api.IProjectGenerator;
import org.eclipse.vorto.codegen.api.tasks.ICodeGeneratorTask;
import org.eclipse.vorto.codegen.api.tasks.eclipse.ProjectFileOutputter;
import org.eclipse.vorto.functionblock.FunctionblockModel;
/**
* Code Generator which generates an Eclipse project
*
*
*
*/
public class EclipseProjectGenerator implements IProjectGenerator {
private ConfigurationContainer configuration;
private List<ICodeGeneratorTask> tasks = new ArrayList<ICodeGeneratorTask>();
private IProject project;
private LocationWrapper locationW;
public EclipseProjectGenerator(LocationWrapper location,
ConfigurationContainer configuration, ICodeGeneratorTask... tasks) {
this.locationW = location;
this.configuration = configuration;
this.tasks.addAll(Arrays.asList(tasks));
}
public EclipseProjectGenerator(String projectName,
ConfigurationContainer configuration, ICodeGeneratorTask[] tasks) {
String rootWorkspace = ResourcesPlugin.getWorkspace().getRoot()
.getLocation().toString();
this.locationW = new LocationWrapper(rootWorkspace, projectName);
this.configuration = configuration;
this.tasks.addAll(Arrays.asList(tasks));
}
public void generate(FunctionblockModel model, IProgressMonitor monitor) {
project = getWorkspace().getProject(locationW.getProjectName());
if (!project.exists()) {
try {
project = createProjectInWorkspace(monitor);
project.open(monitor);
configuration.configure(project);
this.tasks.addAll(configuration.getGeneratorTasks());
} catch (CoreException e) {
e.printStackTrace();
}
}
if (!project.isOpen()) {
try {
project.open(monitor);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (ICodeGeneratorTask task : tasks) {
task.generate(model, new ProjectFileOutputter(project));
}
}
private IProject createProjectInWorkspace(IProgressMonitor monitor)
throws CoreException {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProjectDescription desc = workspace.newProjectDescription(locationW
.getProjectName());
desc.setLocation(locationW.getValidPath());
if (!isDefaultWorkLocation(workspace)) {
project.create(desc, new SubProgressMonitor(monitor, 1));
} else {
project.create(new SubProgressMonitor(monitor, 1));
}
return project;
}
private boolean isDefaultWorkLocation(IWorkspace workspace) {
String defaultWorkspace = workspace.getRoot().getLocation().toString();
return StringUtils.equals(defaultWorkspace, locationW.getPath());
}
@Override
public IProject getProject() {
return project;
}
private IWorkspaceRoot getWorkspace() {
return ResourcesPlugin.getWorkspace().getRoot();
}
}