Search in sources :

Example 1 with WrapperGradleDistribution

use of org.eclipse.buildship.core.WrapperGradleDistribution in project eclipse.jdt.ls by eclipse.

the class GradleProjectImporter method getBuildConfiguration.

public static BuildConfiguration getBuildConfiguration(Path rootFolder) {
    GradleDistribution distribution = getGradleDistribution(rootFolder);
    Preferences preferences = getPreferences();
    File javaHome = getJavaHome(preferences);
    File gradleUserHome = getGradleUserHomeFile();
    List<String> gradleArguments = preferences.getGradleArguments();
    List<String> gradleJvmArguments = preferences.getGradleJvmArguments();
    boolean offlineMode = preferences.isImportGradleOfflineEnabled();
    boolean overrideWorkspaceConfiguration = !(distribution instanceof WrapperGradleDistribution) || offlineMode || (gradleArguments != null && !gradleArguments.isEmpty()) || (gradleJvmArguments != null && !gradleJvmArguments.isEmpty()) || gradleUserHome != null || javaHome != null;
    // @formatter:off
    BuildConfiguration build = BuildConfiguration.forRootProjectDirectory(rootFolder.toFile()).overrideWorkspaceConfiguration(overrideWorkspaceConfiguration).gradleDistribution(distribution).javaHome(javaHome).arguments(gradleArguments).gradleUserHome(gradleUserHome).jvmArguments(gradleJvmArguments).offlineMode(offlineMode).build();
    // @formatter:on
    return build;
}
Also used : GradleDistribution(org.eclipse.buildship.core.GradleDistribution) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) File(java.io.File)

Example 2 with WrapperGradleDistribution

use of org.eclipse.buildship.core.WrapperGradleDistribution in project eclipse.jdt.ls by eclipse.

the class GradlePreferenceChangeListener method updateProject.

private void updateProject(ProjectsManager projectsManager, IProject project, boolean gradleJavaHomeChanged) {
    String projectDir = project.getLocation().toFile().getAbsolutePath();
    Path projectPath = Paths.get(projectDir);
    if (gradleJavaHomeChanged || Files.exists(projectPath.resolve("gradlew"))) {
        ProjectConfiguration configuration = CorePlugin.configurationManager().loadProjectConfiguration(project);
        GradleDistribution distribution = configuration.getBuildConfiguration().getGradleDistribution();
        if (gradleJavaHomeChanged || !(distribution instanceof WrapperGradleDistribution)) {
            projectsManager.updateProject(project, true);
        } else {
            try {
                ValidationResult result = new WrapperValidator().checkWrapper(projectDir);
                if (!result.isValid()) {
                    projectsManager.updateProject(project, true);
                }
            } catch (CoreException e) {
                JavaLanguageServerPlugin.logException(e.getMessage(), e);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) GradleDistribution(org.eclipse.buildship.core.GradleDistribution) CoreException(org.eclipse.core.runtime.CoreException) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) WrapperValidator(org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator) ValidationResult(org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult) ProjectConfiguration(org.eclipse.buildship.core.internal.configuration.ProjectConfiguration)

Example 3 with WrapperGradleDistribution

use of org.eclipse.buildship.core.WrapperGradleDistribution in project eclipse.jdt.ls by eclipse.

the class GradleProjectImporter method getGradleDistribution.

public static GradleDistribution getGradleDistribution(Path rootFolder) {
    PreferenceManager preferencesManager = JavaLanguageServerPlugin.getPreferencesManager();
    Preferences preferences = getPreferences();
    if (preferencesManager != null && preferences.isGradleWrapperEnabled() && Files.exists(rootFolder.resolve("gradlew"))) {
        WrapperValidator validator = new WrapperValidator();
        try {
            ValidationResult result = validator.checkWrapper(rootFolder.toFile().getAbsolutePath());
            if (result.isValid()) {
                WrapperGradleDistribution gradleDistribution = GradleDistribution.fromBuild();
                return gradleDistribution;
            } else {
                if (!WrapperValidator.contains(result.getChecksum())) {
                    ProjectsManager pm = JavaLanguageServerPlugin.getProjectsManager();
                    if (pm != null && pm.getConnection() != null) {
                        if (preferencesManager.getClientPreferences().isGradleChecksumWrapperPromptSupport()) {
                            String id = "gradle/checksum/prompt";
                            ExecuteCommandParams params = new ExecuteCommandParams(id, asList(result.getWrapperJar(), result.getChecksum()));
                            pm.getConnection().sendNotification(params);
                        } else {
                            // @formatter:off
                            String message = GRADLE_WRAPPER_CHEKSUM_WARNING_TEMPLATE.replaceAll("@wrapper@", result.getWrapperJar()).replaceAll("@checksum@", result.getChecksum());
                            // @formatter:on
                            pm.getConnection().showMessage(new MessageParams(MessageType.Error, message));
                        }
                    }
                }
            }
        } catch (CoreException e) {
            JavaLanguageServerPlugin.logException(e.getMessage(), e);
        }
    }
    if (StringUtils.isNotBlank(preferences.getGradleVersion())) {
        List<GradleVersion> versions = CorePlugin.publishedGradleVersions().getVersions();
        GradleVersion gradleVersion = null;
        String versionString = preferences.getGradleVersion();
        GradleVersion requiredVersion = GradleVersion.version(versionString);
        for (GradleVersion version : versions) {
            if (version.compareTo(requiredVersion) == 0) {
                gradleVersion = version;
            }
        }
        if (gradleVersion != null) {
            return GradleDistribution.forVersion(gradleVersion.getVersion());
        } else {
            JavaLanguageServerPlugin.logInfo("Invalid gradle version" + versionString);
        }
    }
    File gradleHomeFile = getGradleHomeFile();
    if (gradleHomeFile != null) {
        return GradleDistribution.forLocalInstallation(gradleHomeFile);
    }
    return DEFAULT_DISTRIBUTION;
}
Also used : WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) WrapperValidator(org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator) MessageParams(org.eclipse.lsp4j.MessageParams) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) ValidationResult(org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CoreException(org.eclipse.core.runtime.CoreException) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) GradleVersion(org.eclipse.buildship.core.internal.util.gradle.GradleVersion) File(java.io.File)

Example 4 with WrapperGradleDistribution

use of org.eclipse.buildship.core.WrapperGradleDistribution in project eclipse.jdt.ls by eclipse.

the class GradleProjectImporterTest method testDisableGradleWrapper.

@Test
public void testDisableGradleWrapper() throws Exception {
    boolean enabled = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isGradleWrapperEnabled();
    String gradleVersion = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleVersion();
    File file = new File(getSourceProjectDirectory(), "gradle/simple-gradle");
    assertTrue(file.isDirectory());
    try {
        GradleDistribution distribution = GradleProjectImporter.getGradleDistribution(file.toPath());
        assertTrue(distribution instanceof WrapperGradleDistribution);
        JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleWrapperEnabled(false);
        distribution = GradleProjectImporter.getGradleDistribution(file.toPath());
        if (GradleProjectImporter.getGradleHomeFile() != null) {
            assertEquals(distribution.getClass(), LocalGradleDistribution.class);
        } else {
            assertSame(distribution, GradleProjectImporter.DEFAULT_DISTRIBUTION);
        }
        String requiredVersion = "5.2.1";
        JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleVersion(requiredVersion);
        distribution = GradleProjectImporter.getGradleDistribution(file.toPath());
        assertEquals(distribution.getClass(), FixedVersionGradleDistribution.class);
        assertEquals(((FixedVersionGradleDistribution) distribution).getVersion(), requiredVersion);
        List<IProject> projects = importProjects("eclipse/eclipsegradle");
        // default + 1 eclipse projects
        assertEquals(2, projects.size());
        IProject eclipse = WorkspaceHelper.getProject("eclipsegradle");
        assertNotNull(eclipse);
        assertTrue(eclipse.getName() + " does not have the Gradle nature", ProjectUtils.isGradleProject(eclipse));
    } finally {
        JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleWrapperEnabled(enabled);
        JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleVersion(gradleVersion);
    }
}
Also used : FixedVersionGradleDistribution(org.eclipse.buildship.core.FixedVersionGradleDistribution) GradleDistribution(org.eclipse.buildship.core.GradleDistribution) LocalGradleDistribution(org.eclipse.buildship.core.LocalGradleDistribution) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test)

Aggregations

WrapperGradleDistribution (org.eclipse.buildship.core.WrapperGradleDistribution)4 File (java.io.File)3 GradleDistribution (org.eclipse.buildship.core.GradleDistribution)3 CoreException (org.eclipse.core.runtime.CoreException)2 Preferences (org.eclipse.jdt.ls.core.internal.preferences.Preferences)2 ValidationResult (org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult)2 WrapperValidator (org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator)2 Path (java.nio.file.Path)1 BuildConfiguration (org.eclipse.buildship.core.BuildConfiguration)1 FixedVersionGradleDistribution (org.eclipse.buildship.core.FixedVersionGradleDistribution)1 LocalGradleDistribution (org.eclipse.buildship.core.LocalGradleDistribution)1 ProjectConfiguration (org.eclipse.buildship.core.internal.configuration.ProjectConfiguration)1 GradleVersion (org.eclipse.buildship.core.internal.util.gradle.GradleVersion)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 PreferenceManager (org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager)1 ExecuteCommandParams (org.eclipse.lsp4j.ExecuteCommandParams)1 MessageParams (org.eclipse.lsp4j.MessageParams)1 Test (org.junit.Test)1