|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2023 Payara Foundation |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + ******************************************************************************/ |
| 9 | + |
| 10 | +package fish.payara.eclipse.tools.server.handlers; |
| 11 | + |
| 12 | +import java.io.BufferedReader; |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.core.commands.AbstractHandler; |
| 20 | +import org.eclipse.core.commands.ExecutionEvent; |
| 21 | +import org.eclipse.core.commands.ExecutionException; |
| 22 | +import org.eclipse.core.resources.IProject; |
| 23 | +import org.eclipse.core.resources.IResource; |
| 24 | +import org.eclipse.core.runtime.IPath; |
| 25 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 26 | +import org.eclipse.jdt.core.IJavaProject; |
| 27 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 28 | +import org.eclipse.jface.viewers.ISelection; |
| 29 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 30 | +import org.eclipse.swt.widgets.DirectoryDialog; |
| 31 | +import org.eclipse.swt.widgets.Shell; |
| 32 | +import org.eclipse.ui.PlatformUI; |
| 33 | +import org.eclipse.ui.console.ConsolePlugin; |
| 34 | +import org.eclipse.ui.console.IConsole; |
| 35 | +import org.eclipse.ui.console.MessageConsole; |
| 36 | +import org.eclipse.ui.console.MessageConsoleStream; |
| 37 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 38 | + |
| 39 | +public class MigrateHandler extends AbstractHandler { |
| 40 | + |
| 41 | + public static final String PAYARA_TRANSFORMER = "fish.payara.transformer"; |
| 42 | + public static final String PAYARA_TRANSFORMER_MAVEN = "fish.payara.transformer.maven"; |
| 43 | + public static final String PAYARA_TRANSFORMER_VERSION = "0.2.10"; |
| 44 | + |
| 45 | + @Override |
| 46 | + public Object execute(ExecutionEvent event) throws ExecutionException { |
| 47 | + Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell(); |
| 48 | + IStructuredSelection selection = getSelection(event); |
| 49 | + if (selection != null && !selection.isEmpty()) { |
| 50 | + Object firstElement = selection.getFirstElement(); |
| 51 | + IPath resourcePath = null; |
| 52 | + IPath projectPath = null; |
| 53 | + String name = ""; |
| 54 | + boolean isFile = false; |
| 55 | + if (firstElement instanceof IResource) { |
| 56 | + IResource resource = (IResource) firstElement; |
| 57 | + resourcePath = resource.getLocation(); |
| 58 | + projectPath = resourcePath; |
| 59 | + name = resource.getName(); |
| 60 | + } else if (firstElement instanceof IJavaProject) { |
| 61 | + IJavaProject javaProject = (IJavaProject) firstElement; |
| 62 | + IProject project = javaProject.getProject(); |
| 63 | + resourcePath = project.getLocation(); |
| 64 | + projectPath = resourcePath; |
| 65 | + name = project.getName(); |
| 66 | + } else if (firstElement instanceof ICompilationUnit) { |
| 67 | + ICompilationUnit file = (ICompilationUnit) firstElement; |
| 68 | + resourcePath = file.getResource().getLocation(); |
| 69 | + projectPath = file.getJavaProject().getProject().getLocation(); |
| 70 | + name = file.getElementName(); |
| 71 | + isFile = true; |
| 72 | + } |
| 73 | + if (resourcePath != null && projectPath != null && !"".equals(name)) { |
| 74 | + String srcPath = resourcePath.toOSString(); |
| 75 | + String srcProjectPath = projectPath.toOSString(); |
| 76 | + String destinationPath = chooseDestinationPath(srcProjectPath, name, isFile); |
| 77 | + if ("".equals(destinationPath)) return null; |
| 78 | + int exitCode = runMvnCommand(srcProjectPath, srcPath, destinationPath); |
| 79 | + if (exitCode == 0) { |
| 80 | + MessageDialog.openInformation(shell, "Success", "Project " + destinationPath + " created successfully."); |
| 81 | + } else { |
| 82 | + MessageDialog.openError(shell, "Error", "Maven command failed with exit code " + exitCode + "."); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + private String chooseDestinationPath(String srcPath, String name, boolean isFile) { |
| 90 | + Shell shell = new Shell(); |
| 91 | + DirectoryDialog dialog = new DirectoryDialog(shell); |
| 92 | + dialog.setText("Choose a destination Folder"); |
| 93 | + dialog.setMessage("Please select a Directory:"); |
| 94 | + dialog.setFilterPath(srcPath); |
| 95 | + String selectedDirectory = dialog.open(); |
| 96 | + if (selectedDirectory != null) { |
| 97 | + if (isFile) { |
| 98 | + return selectedDirectory + "/" + name; |
| 99 | + } |
| 100 | + return selectedDirectory + "/" + name + "-JakartaEE10"; |
| 101 | + } |
| 102 | + return ""; |
| 103 | + } |
| 104 | + |
| 105 | + private IStructuredSelection getSelection(ExecutionEvent event) { |
| 106 | + ISelection selection = HandlerUtil.getCurrentSelection(event); |
| 107 | + if (selection instanceof IStructuredSelection) { |
| 108 | + return (IStructuredSelection) selection; |
| 109 | + } |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + private int runMvnCommand(String srcProjectPath, String srcPath, String targetPath) { |
| 114 | + Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 115 | + |
| 116 | + MessageConsole console = new MessageConsole("Maven Command Console", null); |
| 117 | + ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{console}); |
| 118 | + |
| 119 | + MessageConsoleStream consoleStream = console.newMessageStream(); |
| 120 | + |
| 121 | + try { |
| 122 | + List<String> command = new ArrayList<>(); |
| 123 | + command.add(getMvnCommand()); |
| 124 | + command.add("package"); |
| 125 | + command.add(getTransformCommand()); |
| 126 | + command.add("-DselectedSource=" + srcPath); |
| 127 | + command.add("-DselectedTarget=" + targetPath); |
| 128 | + ProcessBuilder builder = new ProcessBuilder(command); |
| 129 | + builder.environment().put("PATH", System.getenv("PATH")); |
| 130 | + builder.directory(new File(srcProjectPath)); |
| 131 | + builder.redirectErrorStream(true); |
| 132 | + Process process = builder.start(); |
| 133 | + |
| 134 | + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 135 | + String line = null; |
| 136 | + while ((line = reader.readLine()) != null) { |
| 137 | + consoleStream.println(line); |
| 138 | + } |
| 139 | + |
| 140 | + return process.waitFor(); |
| 141 | + } catch (IOException | InterruptedException e) { |
| 142 | + MessageDialog.openError(shell, "Error", "An error occurred while running the Maven command: " + e.getMessage()); |
| 143 | + return -1; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private String getTransformCommand() { |
| 148 | + return String.format("%s:%s:%s:run", |
| 149 | + PAYARA_TRANSFORMER, PAYARA_TRANSFORMER_MAVEN, PAYARA_TRANSFORMER_VERSION); |
| 150 | + } |
| 151 | + |
| 152 | + private String getMvnCommand() { |
| 153 | + String osName = System.getProperty("os.name"); |
| 154 | + if (osName.contains("Windows")) { |
| 155 | + return "mvn.cmd"; |
| 156 | + } else { |
| 157 | + return "mvn"; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments