| /******************************************************************************* |
| * 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.fbeditor.ui.api.project; |
| |
| import java.io.InputStream; |
| import java.lang.reflect.InvocationTargetException; |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| |
| import org.apache.commons.io.IOUtils; |
| import org.apache.commons.lang3.StringUtils; |
| import org.apache.log4j.Logger; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IFolder; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IProjectDescription; |
| import org.eclipse.core.resources.IResource; |
| 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.IPath; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.core.runtime.SubProgressMonitor; |
| import org.eclipse.core.runtime.URIUtil; |
| import org.eclipse.vorto.api.common.generation.FunctionBlockMetaData; |
| import org.eclipse.vorto.api.common.project.IoTModelProject; |
| import org.eclipse.vorto.api.common.project.nature.IoTProjectNature; |
| import org.eclipse.vorto.api.ui.progresstask.IProgressTask; |
| import org.eclipse.vorto.api.ui.progresstask.TaskParameter; |
| import org.eclipse.vorto.fbeditor.ui.internal.template.FbmodelTemplateFileContent; |
| |
| |
| public class ProjectCreationTask implements IProgressTask { |
| |
| public static final String SRC_MODELS = "src/models/"; |
| public static final String FUNCTION_BLOCK_META_DATA = "FunctionBlockMetaData"; |
| public static final String WORK_SPACE = "WorkSpace"; |
| public static final String PROJECT_NAME = "ProjectName"; |
| |
| private String errorMessage = ""; |
| |
| private String projectName; |
| private String workspaceLocation; |
| private FunctionBlockMetaData userInput; |
| private IWorkspace workspace; |
| |
| public static final String XTEXT_NATURE = "org.eclipse.xtext.ui.shared.xtextNature"; |
| public static final String JAVA_NATURE = "org.eclipse.jdt.core.javanature"; |
| public static final String TARGET_JAVA_CLASS_FOLDER = "target/classes"; |
| public static final String ADDITIONAL_SOURCES_FOLDER = "target/generated-sources"; |
| |
| private IoTModelProject iotproject; |
| |
| private static final Logger logger = Logger.getLogger(ProjectCreationTask.class); |
| |
| protected ProjectCreationTask() { |
| } |
| |
| public ProjectCreationTask(TaskParameter param) { |
| projectName = (String) param.get(PROJECT_NAME); |
| workspaceLocation = (String) param.get(WORK_SPACE); |
| userInput = (FunctionBlockMetaData) param.get(FUNCTION_BLOCK_META_DATA); |
| |
| workspace = ResourcesPlugin.getWorkspace(); |
| } |
| |
| @Override |
| public void run(IProgressMonitor monitor) throws InvocationTargetException, |
| InterruptedException { |
| try { |
| IProject project = createProjectInWorkspace(monitor); |
| project.open(new SubProgressMonitor(monitor, 1)); |
| setProjectNatures(project, monitor); |
| |
| monitor.beginTask("Generating project-specific resources", 2); |
| |
| generateFolders(project); |
| |
| project.refreshLocal(IResource.DEPTH_INFINITE, monitor); |
| |
| createTemplateFbmodel(project); |
| |
| monitor.worked(1); |
| |
| monitor.beginTask("Refreshing Project to reflect changes", 1); |
| monitor.worked(1); |
| |
| setIotproject(new IoTModelProject(project)); |
| |
| getIotproject().refresh(monitor); |
| |
| } catch (CoreException e) { |
| this.errorMessage = e.getMessage(); |
| throw new RuntimeException("Problem when creating project, error: " |
| + e.getMessage(), e); |
| } finally { |
| monitor.done(); |
| } |
| |
| } |
| |
| private void createTemplateFbmodel(IProject project) { |
| String fbfilename = userInput.getName() + ".fbmodel"; |
| |
| FbmodelTemplateFileContent gen = new FbmodelTemplateFileContent( |
| userInput); |
| InputStream ctInputStream = IOUtils.toInputStream(gen |
| .getFbModelContent()); |
| IFolder folder = project.getFolder(SRC_MODELS); |
| IFile file = folder.getFile(fbfilename); |
| |
| try { |
| file.create(ctInputStream, false, null); |
| } catch (CoreException e) { |
| e.printStackTrace(); |
| this.errorMessage = "fbmodel file creation error"; |
| } |
| } |
| |
| private void generateFolders(IProject project) throws CoreException { |
| String[] folders = { "src", SRC_MODELS }; |
| |
| for (String f : folders) { |
| createProjectFolders(f, project); |
| } |
| } |
| |
| protected IFolder createProjectFolders(String folderPath, IProject project) |
| throws CoreException { |
| IFolder folder = project.getFolder(folderPath); |
| if (!folder.exists()) { |
| boolean created = folder.getLocation().toFile().mkdirs(); |
| if(!created) { |
| logger.info("Folder not created at " + folder.getLocation().toFile().getPath()); |
| //throw exception here? |
| //this.errorMessage = "Folder not created at " + folder.getLocation().toFile().getPath(); |
| //throw new RuntimeException(errorMessage); |
| } |
| folder.refreshLocal(1, null); |
| folder = project.getFolder(folderPath); |
| } |
| return folder; |
| } |
| |
| private void setProjectNatures(IProject project, IProgressMonitor monitor) |
| throws CoreException { |
| monitor.beginTask("Setting Project Nature", 1); |
| IProjectDescription description = project.getDescription(); |
| description.setNatureIds(resolveProjectNatures()); |
| project.setDescription(description, monitor); |
| } |
| |
| private String[] resolveProjectNatures() { |
| return new String[] { XTEXT_NATURE, IoTProjectNature.NATURE_ID }; |
| } |
| |
| private IProject createProjectInWorkspace(IProgressMonitor monitor) |
| throws CoreException { |
| IWorkspaceRoot root = workspace.getRoot(); |
| IProject project = root.getProject(projectName); |
| IProjectDescription desc = workspace.newProjectDescription(projectName); |
| |
| desc.setLocation(getValidPath()); |
| |
| if (!isDefaultWorkLocation()) { |
| project.create(desc, new SubProgressMonitor(monitor, 1)); |
| } else { |
| project.create(new SubProgressMonitor(monitor, 1)); |
| } |
| return project; |
| } |
| |
| private IPath getValidPath() { |
| URI uri = null; |
| try { |
| uri = URIUtil.fromString(workspaceLocation + "/" + projectName); |
| |
| } catch (URISyntaxException e) { |
| throw new IllegalArgumentException( |
| "Invalid workspace location provided", e); |
| } |
| |
| return new Path(uri.toString()); |
| } |
| |
| private boolean isDefaultWorkLocation() { |
| String defaultWorkspace = workspace.getRoot().getLocation().toString(); |
| return StringUtils.equals(defaultWorkspace, workspaceLocation); |
| } |
| |
| @Override |
| public String getErrorMessage() { |
| return this.errorMessage; |
| } |
| |
| public IoTModelProject getIotproject() { |
| return iotproject; |
| } |
| |
| public void setIotproject(IoTModelProject iotproject) { |
| this.iotproject = iotproject; |
| } |
| |
| public void setErrorMessage(String errorMessage) { |
| this.errorMessage = errorMessage; |
| } |
| |
| public String getProjectName() { |
| return projectName; |
| } |
| |
| public void setProjectName(String projectName) { |
| this.projectName = projectName; |
| } |
| |
| public String getWorkspaceLocation() { |
| return workspaceLocation; |
| } |
| |
| public void setWorkspaceLocation(String workspaceLocation) { |
| this.workspaceLocation = workspaceLocation; |
| } |
| |
| public FunctionBlockMetaData getUserInput() { |
| return userInput; |
| } |
| |
| public void setUserInput(FunctionBlockMetaData userInput) { |
| this.userInput = userInput; |
| } |
| |
| public IWorkspace getWorkspace() { |
| return workspace; |
| } |
| |
| public void setWorkspace(IWorkspace workspace) { |
| this.workspace = workspace; |
| } |
| |
| } |