|
| 1 | +package org.nasdanika.launcher; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Collection; |
| 5 | + |
| 6 | +import org.eclipse.emf.common.util.URI; |
| 7 | +import org.eclipse.emf.ecore.EModelElement; |
| 8 | +import org.eclipse.emf.ecore.resource.Resource; |
| 9 | +import org.eclipse.emf.ecore.util.EcoreUtil; |
| 10 | +import org.eclipse.emf.ecore.xcore.XcoreStandaloneSetup; |
| 11 | +import org.eclipse.xtext.resource.XtextResourceSet; |
| 12 | +import org.nasdanika.capability.CapabilityLoader; |
| 13 | +import org.nasdanika.cli.CommandGroup; |
| 14 | +import org.nasdanika.cli.ParentCommands; |
| 15 | +import org.nasdanika.cli.ResourceSetMixIn; |
| 16 | +import org.nasdanika.cli.RootCommand; |
| 17 | +import org.nasdanika.common.Description; |
| 18 | +import org.nasdanika.common.EModelElementSupplier; |
| 19 | +import org.nasdanika.common.ProgressMonitor; |
| 20 | + |
| 21 | +import com.google.inject.Injector; |
| 22 | + |
| 23 | +import picocli.CommandLine.Command; |
| 24 | +import picocli.CommandLine.Mixin; |
| 25 | +import picocli.CommandLine.Option; |
| 26 | +import picocli.CommandLine.Parameters; |
| 27 | + |
| 28 | +@Command( |
| 29 | + description = "Loads Ecore models from an Xcore URI or file", |
| 30 | + versionProvider = ModuleVersionProvider.class, |
| 31 | + mixinStandardHelpOptions = true, |
| 32 | + name = "xcore") |
| 33 | +@ParentCommands(RootCommand.class) |
| 34 | +@Description(icon = "https://docs.nasdanika.org/images/xcore-logo-g.svg") |
| 35 | +public class XcoreCommand extends CommandGroup implements EModelElementSupplier<EModelElement> { |
| 36 | + |
| 37 | + protected XcoreCommand() { |
| 38 | + super(); |
| 39 | + } |
| 40 | + |
| 41 | + protected XcoreCommand(CapabilityLoader capabilityLoader) { |
| 42 | + super(capabilityLoader); |
| 43 | + } |
| 44 | + |
| 45 | + @Parameters( |
| 46 | + index = "0", |
| 47 | + arity = "1", |
| 48 | + description = { |
| 49 | + "Xcore resource URI or file path", |
| 50 | + "resolved relative to the current directory" |
| 51 | + }) |
| 52 | + private String uri; |
| 53 | + |
| 54 | + @Option( |
| 55 | + names = {"-f", "--file"}, |
| 56 | + description = "URI parameter is a file path") |
| 57 | + private boolean isFile; |
| 58 | + |
| 59 | + @Override |
| 60 | + public Collection<EModelElement> getEObjects(ProgressMonitor progressMonitor) { |
| 61 | + URI theUri = isFile ? URI.createFileURI(new File(uri).getAbsolutePath()) : URI.createURI(uri).resolve(URI.createFileURI(new File(".").getAbsolutePath()).appendSegment("")); |
| 62 | + |
| 63 | + Injector injector = new XcoreStandaloneSetup().createInjectorAndDoEMFRegistration(); |
| 64 | + XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); |
| 65 | + Resource resource = resourceSet.getResource(theUri, true); |
| 66 | + EcoreUtil.resolveAll(resource); |
| 67 | + |
| 68 | + return resource.getContents() |
| 69 | + .stream() |
| 70 | + .filter(EModelElement.class::isInstance) |
| 71 | + .map(EModelElement.class::cast) |
| 72 | + .toList(); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments