|
| 1 | +/** |
| 2 | + * SysML 2 Pilot Implementation |
| 3 | + * Copyright (C) 2025 Model Driven Solutions, Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + * @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later> |
| 19 | + * |
| 20 | + * Contributors: |
| 21 | + * Laszlo Gati, MDS |
| 22 | + */ |
| 23 | +package org.omg.sysml.xtext.ui.handlers; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.Properties; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import org.eclipse.core.commands.AbstractHandler; |
| 35 | +import org.eclipse.core.commands.ExecutionEvent; |
| 36 | +import org.eclipse.core.commands.ExecutionException; |
| 37 | +import org.eclipse.core.resources.IContainer; |
| 38 | +import org.eclipse.core.resources.IFile; |
| 39 | +import org.eclipse.core.resources.IProject; |
| 40 | +import org.eclipse.core.resources.IResource; |
| 41 | +import org.eclipse.core.resources.WorkspaceJob; |
| 42 | +import org.eclipse.core.runtime.CoreException; |
| 43 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 44 | +import org.eclipse.core.runtime.IStatus; |
| 45 | +import org.eclipse.core.runtime.Status; |
| 46 | +import org.eclipse.emf.common.util.URI; |
| 47 | +import org.eclipse.emf.ecore.EObject; |
| 48 | +import org.eclipse.emf.ecore.resource.ResourceSet; |
| 49 | +import org.eclipse.jface.viewers.ISelection; |
| 50 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 51 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 52 | +import org.eclipse.xtext.ui.resource.IResourceSetProvider; |
| 53 | +import org.omg.sysml.lang.sysml.Element; |
| 54 | +import org.omg.sysml.util.repository.ProjectDelta; |
| 55 | +import org.omg.sysml.util.repository.RepositoryContentFetcher; |
| 56 | +import org.omg.sysml.util.repository.RepositoryProject; |
| 57 | +import org.omg.sysml.util.traversal.Traversal; |
| 58 | +import org.omg.sysml.util.traversal.facade.impl.ElementIdProcessingFacade; |
| 59 | + |
| 60 | +import com.google.inject.Inject; |
| 61 | + |
| 62 | +public class PullRepositoryProject extends AbstractHandler { |
| 63 | + |
| 64 | + private static final Set<String> FILE_EXTENSIONS = Set.of("sysml", "kerml", "sysmlx", "kermlx"); |
| 65 | + |
| 66 | + @Inject |
| 67 | + private IResourceSetProvider resourceSetProvider; |
| 68 | + |
| 69 | + @Override |
| 70 | + public Object execute(ExecutionEvent event) throws ExecutionException { |
| 71 | + |
| 72 | + ISelection selection = HandlerUtil.getCurrentSelection(event); |
| 73 | + |
| 74 | + if (selection != null && selection instanceof IStructuredSelection) { |
| 75 | + IStructuredSelection structuredSelection = (IStructuredSelection) selection; |
| 76 | + structuredSelection.stream() |
| 77 | + .filter(IProject.class::isInstance) |
| 78 | + .map(IProject.class::cast).findFirst().ifPresent(this::tryPullRemoteFor);; |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + private void tryPullRemoteFor(IProject project) { |
| 86 | + WorkspaceJob job = new WorkspaceJob("Pulling from remote") { |
| 87 | + |
| 88 | + @Override |
| 89 | + public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { |
| 90 | + |
| 91 | + tryPullRemoteFor(project, monitor); |
| 92 | + return Status.OK_STATUS; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + job.setUser(true); |
| 97 | + job.setRule(project.getWorkspace().getRoot()); |
| 98 | + job.schedule(); |
| 99 | + } |
| 100 | + |
| 101 | + private void tryPullRemoteFor(IProject project, IProgressMonitor monitor) { |
| 102 | + try { |
| 103 | + IProject[] referencedProjects = project.getDescription().getReferencedProjects(); |
| 104 | + Optional<IProject> libraryProject = Stream.of(referencedProjects).filter(rp -> rp.getName().contains("sysml.library")).findFirst(); |
| 105 | + |
| 106 | + if (libraryProject.isPresent()) { |
| 107 | + Properties properties = new Properties(); |
| 108 | + IFile repositoryPropertiesFile = project.getFile(".settings/org.omg.sysml.remote.properties"); |
| 109 | + |
| 110 | + properties.load(repositoryPropertiesFile.getContents()); |
| 111 | + |
| 112 | + String repositoryUrl = properties.getProperty("base.url"); |
| 113 | + String projectName = properties.getProperty("remote.projectId"); |
| 114 | + String targetPath = project.getFullPath().toString(); |
| 115 | + |
| 116 | + Set<IFile> libraryResources = new HashSet<>(); |
| 117 | + collectFiles(libraryProject.get(), libraryResources); |
| 118 | + |
| 119 | + ResourceSet resourceSet = resourceSetProvider.get(project); |
| 120 | + loadResources(resourceSet, libraryProject.get(), libraryResources); |
| 121 | + |
| 122 | + //collect ids from library |
| 123 | + ElementIdProcessingFacade idProcessingFacade = new ElementIdProcessingFacade(); |
| 124 | + var traversal = new Traversal(idProcessingFacade, true); |
| 125 | + |
| 126 | + resourceSet.getResources().forEach(res -> { |
| 127 | + if (!res.getContents().isEmpty()) { |
| 128 | + traversal.visit((Element) res.getContents().get(0)); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + Map<Object, EObject> uuidToElementMap = idProcessingFacade.getUUIDToElementMap(); |
| 133 | + |
| 134 | + RepositoryProject repositoryProject = new RepositoryProject(repositoryUrl, projectName); |
| 135 | + RepositoryContentFetcher repositoryFetcher = new RepositoryContentFetcher(repositoryProject, uuidToElementMap); |
| 136 | + ProjectDelta delta = repositoryFetcher.fetch(); |
| 137 | + delta.save(resourceSet, targetPath); |
| 138 | + } |
| 139 | + } catch (IOException | CoreException e) { |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private static void loadResources(ResourceSet rs, IProject project, Collection<IFile> files) { |
| 145 | + |
| 146 | + for (IFile file: files) { |
| 147 | + String projectRelativeString = file.getProjectRelativePath().toString(); |
| 148 | + URI platformResourceUri = URI |
| 149 | + .createPlatformResourceURI("/" + project.getName() + "/" + projectRelativeString, true); |
| 150 | + rs.getResource(platformResourceUri, true); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static void collectFiles(IContainer container, Collection<IFile> files) throws CoreException { |
| 155 | + IResource[] members = container.members(); |
| 156 | + for (int i = 0; i < members.length; i++) { |
| 157 | + if (members[i] instanceof IContainer) { |
| 158 | + collectFiles((IContainer) members[i], files); |
| 159 | + } else if (members[i] instanceof IFile) { |
| 160 | + IFile file = (IFile) members[i]; |
| 161 | + if (FILE_EXTENSIONS.contains(file.getFileExtension())) { |
| 162 | + files.add(file); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments