From 854875de76c5546220f913058b0c57fe9a38b367 Mon Sep 17 00:00:00 2001 From: Simon Graband Date: Thu, 17 Sep 2020 12:22:32 +0200 Subject: [PATCH] Cleanup Code + Remove data parameter Signed-off-by: Simon Graband --- .../src/node/ecore-file-generation.ts | 45 ++++++++------- .../backend/app/CreateEcoreApplication.java | 38 ++++++++++++- .../backend/app/GenModelApplication.java | 56 ++++++++++++++----- 3 files changed, 101 insertions(+), 38 deletions(-) diff --git a/client/theia-ecore/src/node/ecore-file-generation.ts b/client/theia-ecore/src/node/ecore-file-generation.ts index 027dae6..aa0f587 100644 --- a/client/theia-ecore/src/node/ecore-file-generation.ts +++ b/client/theia-ecore/src/node/ecore-file-generation.ts @@ -26,13 +26,7 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont @inject(ILogger) private readonly logger: ILogger) { } generateEcore(name: string, prefix: string, uri: string, workspacePath: string): Promise { - const jarPath = path.resolve(__dirname, "..", "..", "..", "..", - "server", "org.eclipse.emfcloud.ecore.backend-app", "org.eclipse.emfcloud.ecore.codegen.product", - "target", "products", "org.eclipse.emfcloud.ecore.codegen.product", "linux", "gtk", "x86_64", "plugins", - "org.eclipse.equinox.launcher_1.5.600.v20191014-2022.jar"); - if (jarPath.length === 0) { - throw new Error("The eclipse.equinox.launcher is not found. "); - } + const jarPath = this.getEclipseProductJar(); const command = "java"; const args: string[] = []; @@ -43,7 +37,6 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont args.push( "-cp", jarPath, "org.eclipse.equinox.launcher.Main", - "-data", workspacePath, "-application", "org.eclipse.emfcloud.ecore.backend.app.create-ecore", name, prefix, uri, platformWorkspacePath ); @@ -59,6 +52,10 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont process.process.on("exit", (code: any) => { switch (code) { case 0: resolve("OK"); break; + case -10: resolve("Name is missing"); break; + case -11: resolve("Prefix is missing"); break; + case -12: resolve("Uri is missing"); break; + case -13: resolve("Workspace Path is missing"); break; default: resolve("UNKNOWN ERROR"); break; } }); @@ -66,20 +63,13 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont } generateGenModel(workspacePath: string,ecorePath: string, customPackageName: string, folderName: string): Promise { - const jarPath = path.resolve(__dirname, "..", "..", "..", "..", - "server", "org.eclipse.emfcloud.ecore.backend-app", "org.eclipse.emfcloud.ecore.codegen.product", - "target", "products", "org.eclipse.emfcloud.ecore.codegen.product", "linux", "gtk", "x86_64", "plugins", - "org.eclipse.equinox.launcher_1.5.600.v20191014-2022.jar"); - if (jarPath.length === 0) { - throw new Error("The eclipse.equinox.launcher is not found. "); - } + const jarPath = this.getEclipseProductJar(); const command = "java"; const args: string[] = []; args.push( "-cp", jarPath, "org.eclipse.equinox.launcher.Main", - "-data", workspacePath, "-application", "org.eclipse.emfcloud.ecore.backend.app.create-genmodel", ecorePath, customPackageName, folderName ); @@ -101,6 +91,9 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont process.process.on("exit", (code: any) => { switch (code) { case 0: resolve("OK"); break; + case -10: resolve("Ecore File Path is missing"); break; + case -11: resolve("Custom Root Package is missing"); break; + case -12: resolve("Outputfolder is missing"); break; default: resolve("UNKNOWN ERROR " + code); break; } }); @@ -108,13 +101,7 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont } generateCode(genmodelPath: string, workspacePath: string): Promise { - const jarPath = path.resolve(__dirname, "..", "..", "..", "..", - "server", "org.eclipse.emfcloud.ecore.backend-app", "org.eclipse.emfcloud.ecore.codegen.product", - "target", "products", "org.eclipse.emfcloud.ecore.codegen.product", "linux", "gtk", "x86_64", "plugins", - "org.eclipse.equinox.launcher_1.5.600.v20191014-2022.jar"); - if (jarPath.length === 0) { - throw new Error("The eclipse.equinox.launcher is not found. "); - } + const jarPath = this.getEclipseProductJar(); const command = "java"; const args: string[] = []; @@ -155,6 +142,18 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont // do nothing } + private getEclipseProductJar(): string{ + const jarPath = path.resolve(__dirname, "..", "..", "..", "..", + "server", "org.eclipse.emfcloud.ecore.backend-app", "org.eclipse.emfcloud.ecore.codegen.product", + "target", "products", "org.eclipse.emfcloud.ecore.codegen.product", "linux", "gtk", "x86_64", "plugins", + "org.eclipse.equinox.launcher_1.5.600.v20191014-2022.jar"); + if (jarPath.length === 0) { + throw new Error("The eclipse.equinox.launcher is not found. "); + } + + return jarPath; + } + private spawnProcess(command: string, args?: string[]): RawProcess | undefined { const rawProcess = this.processFactory({ command, args }); if (rawProcess.process === undefined) { diff --git a/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/CreateEcoreApplication.java b/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/CreateEcoreApplication.java index 6209fd5..7a90c43 100644 --- a/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/CreateEcoreApplication.java +++ b/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/CreateEcoreApplication.java @@ -16,8 +16,42 @@ public class CreateEcoreApplication implements IApplication { @Override public Object start(IApplicationContext context) throws Exception { - String[] args = getArgs(context); - createEcore(args[0], args[1], args[2], Paths.get(args[3], args[0]+".ecore")); + String[] args = getArgs(context); + String name = null; + String prefix = null; + String uri = null; + String workspacePath = null; + for (int i = 0; i < args.length; i++) { + switch (i) { + case 0: + name = args[i]; + break; + case 1: + prefix = args[i]; + break; + case 2: + uri = args[i]; + break; + case 3: + workspacePath = args[i]; + break; + } + } + + if (name == null) { + System.exit(-10); + } + if (prefix == null) { + System.exit(-11); + } + if (uri == null) { + System.exit(-12); + } + if (workspacePath == null) { + System.exit(-13); + } + + createEcore(name, prefix, uri, Paths.get(workspacePath, name+".ecore")); return null; } diff --git a/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/GenModelApplication.java b/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/GenModelApplication.java index 389cedb..b86a20c 100644 --- a/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/GenModelApplication.java +++ b/server/org.eclipse.emfcloud.ecore.backend-app/org.eclipse.emfcloud.ecore.backend.app/src/org/eclipse/emfcloud/ecore/backend/app/GenModelApplication.java @@ -17,35 +17,65 @@ public class GenModelApplication implements IApplication { public Object start(IApplicationContext context) throws Exception { Generator codegen = new Generator(); String[] args = getArgs(context); + + String ecoreFilePath = null; + String customRootPackage = null; + String customOutputFolder = null; + + for (int i = 0; i < args.length; i++) { + switch (i) { + case 0: + ecoreFilePath = args[i]; + break; + case 1: + customRootPackage = args[i]; + break; + case 2: + customOutputFolder = args[i]; + break; + } + } + if (ecoreFilePath == null) { + System.exit(-10); + } + if (customRootPackage == null) { + System.exit(-11); + } + if (customOutputFolder == null) { + System.exit(-12); + } + ResourceSet resourceSet = new ResourceSetImpl(); - File ecoreFile = new File(args[0]); + File ecoreFile = new File(ecoreFilePath); if (!ecoreFile.exists()) { throw new RuntimeException("Ecore file not found: " + ecoreFile.getAbsolutePath()); } URI uri = URI.createFileURI(ecoreFile.getAbsolutePath()); Resource ecorePackageResource = resourceSet.getResource(uri, true); EPackage rootPackage = (EPackage) ecorePackageResource.getContents().get(0); - if(args[1].equals("") || args[2].equals("")){ - //get default values - if(args[1].equals("")){ - args[1] = rootPackage.getName(); - } - if(args[2].equals("")){ - args[2] = "src"; - } + + //get default values + if(customRootPackage.equals("")){ + customRootPackage = rootPackage.getName(); + } + if(customOutputFolder.equals("")){ + customOutputFolder = "src"; } + //remove leading / - if(args[2].charAt(0) == '/') args[2] = args[2].substring(1); + if(customOutputFolder.charAt(0) == '/') customOutputFolder = customOutputFolder.substring(1); //initialize args for genmodel creation - String[] genmodelArgs = new String[]{"-ecore2GenModel", args[0], args[1], rootPackage.getNsPrefix()}; + String[] genmodelArgs = new String[]{"-ecore2GenModel", ecoreFilePath, customRootPackage, rootPackage.getNsPrefix()}; codegen.run(genmodelArgs); - String genmodelPath = args[0].substring(0, args[0].lastIndexOf(".")) + ".genmodel"; + + String genmodelPath = ecoreFilePath.substring(0, ecoreFilePath.lastIndexOf(".")) + ".genmodel"; File genmodelFile = new File(genmodelPath); URI genmodelUri = URI.createFileURI(genmodelFile.getAbsolutePath()); Resource genmodelResource = resourceSet.getResource(genmodelUri, true); GenModel genmodel = (GenModel) genmodelResource.getContents().get(0); - genmodel.setModelDirectory("/" + args[2]); + genmodel.setModelDirectory("/" + customOutputFolder); genmodelResource.save(null); + return null; }