diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/.classpath b/plugins/org.etsi.mts.tdl.openapi.generator.ui/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..e801ebfb4680123285c15553dc70584276fe0057 --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/.project b/plugins/org.etsi.mts.tdl.openapi.generator.ui/.project new file mode 100644 index 0000000000000000000000000000000000000000..91de69b2746fd27596b052ece51cc0bea8fc1ee5 --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/.project @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.etsi.mts.tdl.openapi.generator.ui</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/.settings/org.eclipse.jdt.core.prefs b/plugins/org.etsi.mts.tdl.openapi.generator.ui/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000000000000000000000000000000000..7adc0fb9a0d32bd6b4e3ce6f305ab7165208865c --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,10 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/META-INF/MANIFEST.MF b/plugins/org.etsi.mts.tdl.openapi.generator.ui/META-INF/MANIFEST.MF new file mode 100644 index 0000000000000000000000000000000000000000..38138a204367b1b265d956248af57b42cd88de9f --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/META-INF/MANIFEST.MF @@ -0,0 +1,15 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: OpenAPI Generator Wrapper UI +Bundle-SymbolicName: org.etsi.mts.tdl.openapi.generator.ui;singleton:=true +Bundle-Version: 1.0.0.qualifier +Bundle-Activator: org.etsi.mts.tdl.openapi.generator.ui.Activator +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime, + org.eclipse.core.resources, + org.eclipse.ui.editors;bundle-version="3.5.0", + org.eclipse.ui.ide;bundle-version="3.5.0", + org.eclipse.ui.workbench, + org.etsi.mts.tdl.openapi.generator.wrapper +Bundle-ActivationPolicy: lazy +Import-Package: org.eclipse.emf.common.util diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/build.properties b/plugins/org.etsi.mts.tdl.openapi.generator.ui/build.properties new file mode 100644 index 0000000000000000000000000000000000000000..0d3d3a745d49c12500fb77bdc372b76e8bf624de --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/build.properties @@ -0,0 +1,6 @@ +source.. = src/ +output.. = bin/ +bin.includes = plugin.xml,\ + META-INF/,\ + .,\ + icons/ diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/TransformIcon.png b/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/TransformIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..6b23bd6a60b0c2362a07da1a44e16f8c6a66926c Binary files /dev/null and b/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/TransformIcon.png differ diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/TranslateIcon.png b/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/TranslateIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..555d9f2a5ad5bd079bb41a2d952dc7adfb8939e1 Binary files /dev/null and b/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/TranslateIcon.png differ diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/sample.gif b/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/sample.gif new file mode 100644 index 0000000000000000000000000000000000000000..34fb3c9d8cb7d489681b7f7aee4bdcd7eaf53610 Binary files /dev/null and b/plugins/org.etsi.mts.tdl.openapi.generator.ui/icons/sample.gif differ diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/plugin.xml b/plugins/org.etsi.mts.tdl.openapi.generator.ui/plugin.xml new file mode 100644 index 0000000000000000000000000000000000000000..701e6dee7064bc6ba51b422b9e2bfe423abf6fd1 --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/plugin.xml @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.4"?> +<plugin> + + <extension + point="org.eclipse.ui.commands"> + <category + name="TDL" + id="org.etsi.mts.tdl.openapi.generator.ui.commands.category"> + </category> + <command + name="OpenAPI Generate target implementation" + categoryId="org.etsi.mts.tdl.openapi.generator.ui.commands.category" + id="org.etsi.mts.tdl.openapi.generator.ui.commands.generateCommand"> + </command> + </extension> + <extension + point="org.eclipse.ui.handlers"> + <handler + commandId="org.etsi.mts.tdl.openapi.generator.ui.commands.generateCommand" + class="org.etsi.mts.tdl.openapi.generator.ui.handlers.GeneratorHandler"> + </handler> + </extension> + <extension + point="org.eclipse.ui.menus"> + <menuContribution + locationURI="menu:org.eclipse.ui.main.menu?after=additions"> + <menu + label="TDL" + mnemonic="X" + id="org.etsi.mts.tdl.tools.menus.TDLMenu"> + <command + commandId="org.etsi.mts.tdl.openapi.generator.ui.commands.generateCommand" + icon="icons/TransformIcon.png" + mnemonic="X" + id="org.etsi.mts.tdl.openapi.generator.ui.menus.generateCommand"> + </command> + </menu> + </menuContribution> + <menuContribution + locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions"> + <toolbar + id="org.etsi.mts.tdl.openapi.generator.ui.toolbars.generate"> + <command + commandId="org.etsi.mts.tdl.openapi.generator.ui.commands.generateCommand" + icon="icons/TransformIcon.png" + name="OpenAPI Generate target implementation" + id="org.etsi.mts.tdl.openapi.generator.ui.toolbars.generateCommand"> + </command> + </toolbar> + </menuContribution> + </extension> + +</plugin> diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/Activator.java b/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/Activator.java new file mode 100644 index 0000000000000000000000000000000000000000..0905c40699ed8d4b63abc265d201b459685841b1 --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/Activator.java @@ -0,0 +1,61 @@ +package org.etsi.mts.tdl.openapi.generator.ui; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.etsi.mts.tdl.openapi.generator.ui"; //$NON-NLS-1$ + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + + /** + * Returns an image descriptor for the image file at the given + * plug-in relative path + * + * @param path the path + * @return the image descriptor + */ + public static ImageDescriptor getImageDescriptor(String path) { + return imageDescriptorFromPlugin(PLUGIN_ID, path); + } +} diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/handlers/GeneratorHandler.java b/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/handlers/GeneratorHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..234b3a65e739f20283a920814f08b0fa828d3386 --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/handlers/GeneratorHandler.java @@ -0,0 +1,111 @@ +package org.etsi.mts.tdl.openapi.generator.ui.handlers; + +import java.util.Arrays; +import java.util.LinkedHashMap; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IFile; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.window.Window; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.dialogs.ElementListSelectionDialog; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.part.FileEditorInput; + +/** + * Our sample handler extends AbstractHandler, an IHandler base class. + * @see org.eclipse.core.commands.IHandler + * @see org.eclipse.core.commands.AbstractHandler + */ +public class GeneratorHandler extends AbstractHandler { + + LinkedHashMap<String, String> targetFormats = new LinkedHashMap<>(); + + public static String translationTarget = "java"; + + private IWorkbenchWindow window; + + /** + * The constructor. + */ + public GeneratorHandler() { + init(); + } + + private void init() { + //TODO: update rt.ui as well + //TODO: do not reload after first init + if (!targetFormats.isEmpty()) { + return; + } + targetFormats.clear(); + + for (String gen : OpenAPIGeneratorLauncher.listGenerators()) { + targetFormats.put(gen, gen); + } + } + + /** + * the command has been executed, so extract extract the needed information + * from the application context. + */ + public Object execute(ExecutionEvent event) throws ExecutionException { + init(); + ISelection selection = HandlerUtil.getCurrentSelection(event); + IEditorInput input = HandlerUtil.getActiveEditorInput(event); + IFile file = null; + if (input != null && input instanceof FileEditorInput && ((FileEditorInput) input).getFile().getName().endsWith("yaml")) { + file = ((FileEditorInput) input).getFile(); + } else if (selection !=null && selection instanceof IStructuredSelection) { + IStructuredSelection structuredSelection = (IStructuredSelection) selection; + Object firstElement = structuredSelection.getFirstElement(); + if (firstElement instanceof IFile) { + file = (IFile) firstElement; + } + } + + if (file !=null && file.getName().endsWith("yaml")) { + + ElementListSelectionDialog dialog = new ElementListSelectionDialog(Display.getDefault().getActiveShell(), new LabelProvider()); + dialog.setTitle("OpenAPI Generator Target Selection"); + dialog.setMessage("Generating assets from "+file.getName() + +"\n\nSelect the target format"); + dialog.setElements(targetFormats.keySet().toArray()); + + dialog.setInitialElementSelections(Arrays.asList(new String[] {GeneratorHandler.translationTarget})); + //TODO: add option to skip validation + //TODO: add step to validate + + // user pressed cancel + if (dialog.open() != Window.OK) { + return false; + } else { + //TODO: enable validation once it does not exit +// OpenAPIGeneratorLauncher.validate(file.getLocation().toFile().getAbsolutePath()); +// OpenAPIGeneratorLauncher.parse(file.getLocation().toFile().getAbsolutePath()); + + Object[] result = dialog.getResult(); + String selected = (String)result[0]; + translationTarget = selected; + OpenAPIGeneratorLauncher.launch(file.getLocation().toFile().getAbsolutePath(), selected); + } + } + return null; + } + + public void init(IWorkbenchWindow window) { + this.window = window; + } + + @Override + public boolean isEnabled() { + return true; + } + +} diff --git a/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/handlers/OpenAPIGeneratorLauncher.java b/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/handlers/OpenAPIGeneratorLauncher.java new file mode 100644 index 0000000000000000000000000000000000000000..1c13cf35b10dd23b6e280100e1f3882de2ffe645 --- /dev/null +++ b/plugins/org.etsi.mts.tdl.openapi.generator.ui/src/org/etsi/mts/tdl/openapi/generator/ui/handlers/OpenAPIGeneratorLauncher.java @@ -0,0 +1,79 @@ +package org.etsi.mts.tdl.openapi.generator.ui.handlers; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.CodegenConfigLoader; +import org.openapitools.codegen.OpenAPIGenerator; +import org.openapitools.codegen.meta.GeneratorMetadata; +import org.openapitools.codegen.utils.ModelUtils; + +import io.swagger.parser.OpenAPIParser; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.parser.core.models.ParseOptions; + +public class OpenAPIGeneratorLauncher { + + public static void main(String[] args) { +// launch(input, target); + listGenerators(); +// parse(input); + } + + public static List<String> listGenerators() { + List<CodegenConfig> generators = new ArrayList<>(); + List<String> generatorNames = new ArrayList<>(); + for (CodegenConfig codegenConfig : CodegenConfigLoader.getAll()) { + GeneratorMetadata meta = codegenConfig.getGeneratorMetadata(); + if (meta != null) { + generators.add(codegenConfig); + generatorNames.add(codegenConfig.getName()); + } + } + return generatorNames; + } + + public static void launch(String input, String target) { + launch(input, target, true); + } + + public static void launch(String input, String target, boolean skipValidation) { + String inputFolder = new File(input).getParentFile().getAbsolutePath(); + String output = inputFolder+"/generated/"+target+"/"; + new File(output).mkdirs(); + String skip = ""; + if (skipValidation) { + skip = "--skip-validate-spec"; + } + OpenAPIGenerator.main(new String[] {"generate", "-g", target, "-i", input, "-o", output, skip}); + } + + public static void validate(String input) { + //TODO: adapt from source so that it does not exit or run in a separate process? + OpenAPIGenerator.main(new String[] {"validate", "-i", input}); + } + + public static void parse(String input) { + System.out.println("Parsing "+input); + OpenAPI openAPI = new OpenAPIParser().readLocation(input, null, new ParseOptions()).getOpenAPI(); + Components components = openAPI.getComponents(); + Map<String, Schema> schemas = components.getSchemas(); + ArraySchema object = (ArraySchema) schemas.get("Library").getProperties().get("books"); + System.out.println(object.getItems().get$ref()); +// System.out.println(schemas.get("Library").getName()); + System.out.println("MU:"+ModelUtils.getReferencedSchema(openAPI, object.getItems())); + schemas.forEach((k,v) -> System.out.println(k)); + // Invoke helper function to get the original swagger version. + // See https://github.com/swagger-api/swagger-parser/pull/1374 + // Also see https://github.com/swagger-api/swagger-parser/issues/1369. + ModelUtils.getOpenApiVersion(openAPI, input, null); + + } + +}