|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Broadcom, Inc. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Broadcom, Inc. - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.tooling.ls.eclipse.commons.commands; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.eclipse.buildship.core.internal.CorePlugin; |
| 19 | +import org.eclipse.buildship.core.internal.configuration.BuildConfiguration; |
| 20 | +import org.eclipse.buildship.core.internal.launch.GradleRunConfigurationAttributes; |
| 21 | +import org.eclipse.buildship.core.internal.util.variable.ExpressionUtils; |
| 22 | +import org.eclipse.core.commands.AbstractHandler; |
| 23 | +import org.eclipse.core.commands.ExecutionEvent; |
| 24 | +import org.eclipse.core.commands.ExecutionException; |
| 25 | +import org.eclipse.core.resources.IProject; |
| 26 | +import org.eclipse.core.resources.IResource; |
| 27 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 28 | +import org.eclipse.debug.core.ILaunchManager; |
| 29 | +import org.eclipse.debug.ui.DebugUITools; |
| 30 | +import org.eclipse.lsp4e.LSPEclipseUtils; |
| 31 | +import org.eclipse.lsp4e.command.LSPCommandHandler; |
| 32 | +import org.eclipse.lsp4j.Command; |
| 33 | + |
| 34 | +import com.google.common.base.Optional; |
| 35 | +import com.google.gson.Gson; |
| 36 | + |
| 37 | +@SuppressWarnings("restriction") |
| 38 | +public class ExecuteGradleTaskHandler extends AbstractHandler { |
| 39 | + |
| 40 | + @Override |
| 41 | + public Object execute(ExecutionEvent event) throws ExecutionException { |
| 42 | + Command cmd = new Gson().fromJson(event.getParameter(LSPCommandHandler.LSP_COMMAND_PARAMETER_ID), Command.class); |
| 43 | + if (cmd != null) { |
| 44 | + try { |
| 45 | + String projectPath = (String) cmd.getArguments().get(0); |
| 46 | + String task = (String) cmd.getArguments().get(1); |
| 47 | + System.out.println("Project: '%s', task: '%s'".formatted(projectPath, task)); |
| 48 | + |
| 49 | + IResource projectFolder = LSPEclipseUtils.findResourceFor(Paths.get(projectPath).toUri()); |
| 50 | + File workingFolder = projectFolder.getLocation().toFile(); |
| 51 | + GradleRunConfigurationAttributes configurationAttributes = getRunConfigurationAttributes(workingFolder, workingFolder, Arrays.asList(task.split("\\s+"))); |
| 52 | + |
| 53 | + // create/reuse a launch configuration for the given attributes |
| 54 | + ILaunchConfiguration launchConfig = CorePlugin.gradleLaunchConfigurationManager().getOrCreateRunConfiguration(configurationAttributes); |
| 55 | + |
| 56 | + DebugUITools.launch(launchConfig, ILaunchManager.RUN_MODE); |
| 57 | + } catch (Exception e) { |
| 58 | + throw new ExecutionException("Failed to execute Maven Goal command", e); |
| 59 | + } |
| 60 | + } |
| 61 | + throw new ExecutionException("Maven Goal Execution command is invalid"); |
| 62 | + } |
| 63 | + |
| 64 | + private static GradleRunConfigurationAttributes getRunConfigurationAttributes(File rootDir, File workingDir, List<String> tasks) { |
| 65 | + BuildConfiguration buildConfig = CorePlugin.configurationManager().loadBuildConfiguration(rootDir); |
| 66 | + return new GradleRunConfigurationAttributes(tasks, |
| 67 | + projectDirectoryExpression(workingDir), |
| 68 | + buildConfig.getGradleDistribution().toString(), |
| 69 | + gradleUserHomeExpression(buildConfig.getGradleUserHome()), |
| 70 | + javaHomeExpression(buildConfig.getJavaHome()), |
| 71 | + buildConfig.getJvmArguments(), |
| 72 | + buildConfig.getArguments(), |
| 73 | + buildConfig.isShowExecutionsView(), |
| 74 | + buildConfig.isShowExecutionsView(), |
| 75 | + buildConfig.isOverrideWorkspaceSettings(), |
| 76 | + buildConfig.isOfflineMode(), |
| 77 | + buildConfig.isBuildScansEnabled()); |
| 78 | + } |
| 79 | + |
| 80 | + private static String projectDirectoryExpression(File rootProjectDir) { |
| 81 | + // return the directory as an expression if the project is part of the workspace, otherwise |
| 82 | + // return the absolute path of the project directory available on the Eclipse project model |
| 83 | + Optional<IProject> project = CorePlugin.workspaceOperations().findProjectByLocation(rootProjectDir); |
| 84 | + if (project.isPresent()) { |
| 85 | + return ExpressionUtils.encodeWorkspaceLocation(project.get()); |
| 86 | + } else { |
| 87 | + return rootProjectDir.getAbsolutePath(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static String gradleUserHomeExpression(File gradleUserHome) { |
| 92 | + return gradleUserHome == null ? "" : gradleUserHome.getAbsolutePath(); |
| 93 | + } |
| 94 | + |
| 95 | + private static String javaHomeExpression(File javaHome) { |
| 96 | + return javaHome == null ? "" : javaHome.getAbsolutePath(); |
| 97 | + } |
| 98 | +} |
0 commit comments