| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.wizard.ui.basic; |
| |
| import java.util.List; |
| |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IResource; |
| 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.osbp.wizard.ui.Activator; |
| import org.eclipse.xpand2.XpandExecutionContextImpl; |
| import org.eclipse.xpand2.XpandFacade; |
| import org.eclipse.xpand2.output.Outlet; |
| import org.eclipse.xpand2.output.OutputImpl; |
| import org.eclipse.xtend.type.impl.java.JavaBeansMetaModel; |
| import org.osgi.framework.Version; |
| |
| |
| public abstract class BasicProjectCreator extends org.eclipse.xtext.ui.wizard.AbstractPluginProjectCreator { |
| |
| private String osbpBundleVersion = null; |
| |
| public BasicProjectCreator() { |
| super(); |
| // TODO Auto-generated constructor stub |
| } |
| |
| protected void enhanceProject(final IProject project, final IProgressMonitor monitor, String action) throws CoreException { |
| IPath absoluteLocation = project.getLocation().makeAbsolute(); |
| if (getProjectInfo() instanceof BasicProjectInfo) { |
| BasicProjectInfo projectInfo = (BasicProjectInfo) getProjectInfo(); |
| try { |
| projectInfo.setProjectLocation(absoluteLocation); |
| } |
| catch (Exception e) {} |
| } |
| OutputImpl output = new OutputImpl(); |
| output.addOutlet(new Outlet(false, getEncoding(), null, true, absoluteLocation.toOSString())); |
| |
| XpandExecutionContextImpl execCtx = new XpandExecutionContextImpl(output, null); |
| execCtx.getResourceManager().setFileEncoding("UTF-8"); |
| execCtx.registerMetaModel(new JavaBeansMetaModel()); |
| |
| XpandFacade facade = XpandFacade.create(execCtx); |
| Gofl.log(this, "enhanceProject() "+action+" ..."); |
| try { |
| facade.evaluate("org::eclipse::osbp::wizard::ui::basic::CommonNewProject::"+action, getProjectInfo()); |
| Gofl.log(this, "enhanceProject() "+action+" done!"); |
| } |
| catch (Exception e) { |
| Gofl.log(this, "enhanceProject() "+action+" caught exception!"); |
| Gofl.log(this, e); |
| } |
| |
| project.refreshLocal(IResource.DEPTH_INFINITE, monitor); |
| } |
| |
| @Override |
| protected String getPrimaryModelFileExtension() { |
| try { |
| return super.getPrimaryModelFileExtension(); |
| } |
| catch (Exception e) { |
| return "osbpBundle"; |
| } |
| } |
| |
| @Override |
| protected String getModelFolderName() { |
| // TODO Auto-generated method stub |
| return null; |
| } |
| |
| @Override |
| protected List<String> getAllFolders() { |
| // TODO Auto-generated method stub |
| return null; |
| } |
| |
| public enum ManifestAttribute { |
| EXPORTED_PACKAGE, |
| IMPORTED_PACKAGE, |
| REQUIRED_BUNDLE, |
| FACTORY_MODEL, |
| } |
| |
| protected String getObspBundleVersion() { |
| if (osbpBundleVersion == null) { |
| Version version = Activator.getInstance().getBundle().getVersion(); |
| osbpBundleVersion = String.format( |
| "[%d.%d.%d#%d.%d.%d)", |
| version.getMajor(), version.getMinor(), version.getMicro(), |
| version.getMajor(), version.getMinor()+1, 0); |
| } |
| return osbpBundleVersion; |
| } |
| |
| protected boolean isOsbpBundle(String bundlename) { |
| return bundlename.startsWith("org.eclipse.osbp.") && !bundlename.startsWith(((BasicProjectInfo) getProjectInfo()).getBasicSymbolicName()); |
| } |
| |
| protected final List<String> addFromMultiLine(List<String> result, ManifestAttribute manifestAttribute, String itemsText) { |
| itemsText += ((BasicProjectInfo) getProjectInfo()).getManifestTemplate(manifestAttribute); |
| String[] items = itemsText.split(","); |
| for (String item : items) { |
| item = item.trim(); |
| if (!item.isEmpty()) { |
| item = item.replace(BasicProjectInfo.CONTENT_BASIC_SYMBOLIC_NAME, ((BasicProjectInfo) getProjectInfo()).getBasicSymbolicName()); |
| item = item.replace(BasicProjectInfo.CONTENT_BASIC_PROJECT_NAME, ((BasicProjectInfo) getProjectInfo()).getBasicProjectName()); |
| item = item.replace(BasicProjectInfo.CONTENT_MODEL_SYMBOLIC_NAME, ((BasicProjectInfo) getProjectInfo()).getModelSymbolicName()); |
| item = item.replace(BasicProjectInfo.CONTENT_MODEL_PROJECT_NAME, ((BasicProjectInfo) getProjectInfo()).getModelProjectName()); |
| item = item.replace(BasicProjectInfo.CONTENT_MODEL_PROJECT_PACKAGE_NAME, ((BasicProjectInfo) getProjectInfo()).getModelProjectPackageName()); |
| switch (manifestAttribute) { |
| case EXPORTED_PACKAGE: |
| break; |
| case FACTORY_MODEL: |
| break; |
| case IMPORTED_PACKAGE: |
| /* |
| if (isOsbpBundle(item)) { |
| item = item.split(";")[0]+";version=\""+getObspBundleVersion()+"\""; |
| } |
| */ |
| break; |
| case REQUIRED_BUNDLE: |
| if (isOsbpBundle(item)) { |
| String bundleVersionPrefix = "bundle-version=\""; |
| String[] splitItem = item.split(";"); |
| boolean bundleVersionExists = false; |
| for (int i = 0; i < splitItem.length; i++) { |
| if (splitItem[i].startsWith(bundleVersionPrefix)) { |
| bundleVersionExists = true; |
| } |
| } |
| // Only for items without an already defined bundle version |
| if (!bundleVersionExists) { |
| StringBuffer strBuf = new StringBuffer(splitItem[0]+";"+bundleVersionPrefix+getObspBundleVersion()+"\""); |
| for (int i = 1; i < splitItem.length; i++) { |
| strBuf.append(";").append(splitItem[i]); |
| } |
| item = strBuf.toString(); |
| } |
| } |
| break; |
| } |
| result.add(item.replace('#',',')); |
| } |
| } |
| return result; |
| } |
| } |