Search in sources :

Example 1 with GradleInstallationManager

use of org.jetbrains.plugins.gradle.service.GradleInstallationManager in project intellij-community by JetBrains.

the class GradleScriptType method getRunner.

@Override
public GroovyScriptRunner getRunner() {
    return new GroovyScriptRunner() {

        @Override
        public boolean shouldRefreshAfterFinish() {
            return true;
        }

        @Override
        public boolean isValidModule(@NotNull Module module) {
            GradleInstallationManager libraryManager = ServiceManager.getService(GradleInstallationManager.class);
            return libraryManager.isGradleSdk(OrderEnumerator.orderEntries(module).getAllLibrariesAndSdkClassesRoots());
        }

        @Override
        public void ensureRunnerConfigured(@NotNull GroovyScriptRunConfiguration configuration) {
            String parameters = configuration.getProgramParameters();
            if (parameters != null) {
            // TODO den implement
            //            GradleTasksList list = GradleUtil.getToolWindowElement(GradleTasksList.class, project, ExternalSystemDataKeys.RECENT_TASKS_LIST);
            //            if (list != null) {
            //              ExternalSystemTaskDescriptor descriptor = new ExternalSystemTaskDescriptor(parameters, null);
            //              descriptor.setExecutorId(executor.getId());
            //              list.setFirst(descriptor);
            //              GradleLocalSettings.getInstance(project).setRecentTasks(list.getModel().getTasks());
            //            }
            }
            final GradleInstallationManager libraryManager = ServiceManager.getService(GradleInstallationManager.class);
        // TODO den implement
        //if (libraryManager.getGradleHome(module, project) == null) {
        //  int result = 0;
        //          int result = Messages.showOkCancelDialog(
        //            ExternalSystemBundle.message("gradle.run.no.sdk.text"),
        //            ExternalSystemBundle.message("gradle.run.no.sdk.title"),
        //            GradleIcons.Gradle
        //          );
        //          if (result == 0) {
        //            ShowSettingsUtil.getInstance().editConfigurable(project, new AbstractExternalProjectConfigurable(project));
        //          }
        //          if (libraryManager.getGradleHome(module, project) == null) {
        //            return false;
        //          }
        //        }
        }

        @Override
        public void configureCommandLine(JavaParameters params, @Nullable Module module, boolean tests, VirtualFile script, GroovyScriptRunConfiguration configuration) throws CantRunException {
            final Project project = configuration.getProject();
            final GradleInstallationManager libraryManager = ServiceManager.getService(GradleInstallationManager.class);
            if (module == null) {
                throw new CantRunException("Target module is undefined");
            }
            String rootProjectPath = module.getOptionValue(ExternalSystemConstants.ROOT_PROJECT_PATH_KEY);
            if (StringUtil.isEmpty(rootProjectPath)) {
                throw new CantRunException(String.format("Module '%s' is not backed by gradle", module.getName()));
            }
            final VirtualFile gradleHome = libraryManager.getGradleHome(module, project, rootProjectPath);
            if (gradleHome == null) {
                throw new CantRunException("Gradle home can not be found");
            }
            params.setMainClass(findMainClass(gradleHome, script, project));
            final File[] groovyJars = GroovyConfigUtils.getGroovyAllJars(gradleHome.getPath() + "/lib/");
            if (groovyJars.length > 0) {
                params.getClassPath().add(groovyJars[0].getAbsolutePath());
            } else {
                final VirtualFile groovyJar = findGroovyJar(module);
                if (groovyJar != null) {
                    params.getClassPath().add(groovyJar);
                }
            }
            final String userDefinedClasspath = System.getProperty("gradle.launcher.classpath");
            if (StringUtil.isNotEmpty(userDefinedClasspath)) {
                params.getClassPath().add(userDefinedClasspath);
            } else {
                final Collection<VirtualFile> roots = libraryManager.getClassRoots(project);
                if (roots != null) {
                    params.getClassPath().addVirtualFiles(roots);
                }
            }
            params.getVMParametersList().addParametersString(configuration.getVMParameters());
            params.getVMParametersList().add("-Dgradle.home=" + FileUtil.toSystemDependentName(gradleHome.getPath()));
            setToolsJar(params);
            final String scriptPath = configuration.getScriptPath();
            if (scriptPath == null) {
                throw new CantRunException("Target script or gradle project path is undefined");
            }
            if (new File(scriptPath).isFile()) {
                params.getProgramParametersList().add("--build-file");
            } else {
                params.getProgramParametersList().add("--project-dir");
            }
            params.getProgramParametersList().add(FileUtil.toSystemDependentName(scriptPath));
            params.getProgramParametersList().addParametersString(configuration.getProgramParameters());
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleInstallationManager(org.jetbrains.plugins.gradle.service.GradleInstallationManager) NotNull(org.jetbrains.annotations.NotNull) GroovyScriptRunConfiguration(org.jetbrains.plugins.groovy.runner.GroovyScriptRunConfiguration) Project(com.intellij.openapi.project.Project) CantRunException(com.intellij.execution.CantRunException) GroovyScriptRunner(org.jetbrains.plugins.groovy.runner.GroovyScriptRunner) JavaParameters(com.intellij.execution.configurations.JavaParameters) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GradleInstallationManager

use of org.jetbrains.plugins.gradle.service.GradleInstallationManager in project intellij-community by JetBrains.

the class BuildClasspathModuleGradleDataService method importData.

@Override
public void importData(@NotNull final Collection<DataNode<BuildScriptClasspathData>> toImport, @Nullable final ProjectData projectData, @NotNull final Project project, @NotNull final IdeModifiableModelsProvider modelsProvider) {
    if (projectData == null || toImport.isEmpty()) {
        return;
    }
    final GradleInstallationManager gradleInstallationManager = ServiceManager.getService(GradleInstallationManager.class);
    ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(GradleConstants.SYSTEM_ID);
    assert manager != null;
    AbstractExternalSystemLocalSettings localSettings = manager.getLocalSettingsProvider().fun(project);
    final String linkedExternalProjectPath = projectData.getLinkedExternalProjectPath();
    final File gradleHomeDir = toImport.iterator().next().getData().getGradleHomeDir();
    final GradleLocalSettings gradleLocalSettings = GradleLocalSettings.getInstance(project);
    if (gradleHomeDir != null) {
        gradleLocalSettings.setGradleHome(linkedExternalProjectPath, gradleHomeDir.getPath());
    }
    final GradleProjectSettings settings = GradleSettings.getInstance(project).getLinkedProjectSettings(linkedExternalProjectPath);
    final NotNullLazyValue<Set<String>> externalProjectGradleSdkLibs = new NotNullLazyValue<Set<String>>() {

        @NotNull
        @Override
        protected Set<String> compute() {
            final Set<String> gradleSdkLibraries = ContainerUtil.newLinkedHashSet();
            File gradleHome = gradleInstallationManager.getGradleHome(project, linkedExternalProjectPath);
            if (gradleHome != null && gradleHome.isDirectory()) {
                final Collection<File> libraries = gradleInstallationManager.getClassRoots(project, linkedExternalProjectPath);
                if (libraries != null) {
                    for (File library : libraries) {
                        gradleSdkLibraries.add(FileUtil.toCanonicalPath(library.getPath()));
                    }
                }
            }
            return gradleSdkLibraries;
        }
    };
    final NotNullLazyValue<Set<String>> buildSrcProjectsRoots = new NotNullLazyValue<Set<String>>() {

        @NotNull
        @Override
        protected Set<String> compute() {
            Set<String> result = new LinkedHashSet<>();
            //// add main java root of buildSrc project
            result.add(linkedExternalProjectPath + "/buildSrc/src/main/java");
            //// add main groovy root of buildSrc project
            result.add(linkedExternalProjectPath + "/buildSrc/src/main/groovy");
            for (Module module : modelsProvider.getModules(projectData)) {
                final String projectPath = ExternalSystemApiUtil.getExternalProjectPath(module);
                if (projectPath != null && StringUtil.startsWith(projectPath, linkedExternalProjectPath + "/buildSrc")) {
                    final List<String> sourceRoots = ContainerUtil.map(modelsProvider.getSourceRoots(module, false), VirtualFile::getPath);
                    result.addAll(sourceRoots);
                }
            }
            return result;
        }
    };
    final Map<String, ExternalProjectBuildClasspathPojo> localProjectBuildClasspath = ContainerUtil.newHashMap(localSettings.getProjectBuildClasspath());
    for (final DataNode<BuildScriptClasspathData> node : toImport) {
        if (GradleConstants.SYSTEM_ID.equals(node.getData().getOwner())) {
            DataNode<ModuleData> moduleDataNode = ExternalSystemApiUtil.findParent(node, ProjectKeys.MODULE);
            if (moduleDataNode == null)
                continue;
            String externalModulePath = moduleDataNode.getData().getLinkedExternalProjectPath();
            if (settings == null || settings.getDistributionType() == null) {
                LOG.warn("Gradle SDK distribution type was not configured for the project at " + linkedExternalProjectPath);
            }
            final Set<String> buildClasspath = ContainerUtil.newLinkedHashSet();
            BuildScriptClasspathData buildScriptClasspathData = node.getData();
            for (BuildScriptClasspathData.ClasspathEntry classpathEntry : buildScriptClasspathData.getClasspathEntries()) {
                for (String path : classpathEntry.getSourcesFile()) {
                    buildClasspath.add(FileUtil.toCanonicalPath(path));
                }
                for (String path : classpathEntry.getClassesFile()) {
                    buildClasspath.add(FileUtil.toCanonicalPath(path));
                }
            }
            ExternalProjectBuildClasspathPojo projectBuildClasspathPojo = localProjectBuildClasspath.get(linkedExternalProjectPath);
            if (projectBuildClasspathPojo == null) {
                projectBuildClasspathPojo = new ExternalProjectBuildClasspathPojo(moduleDataNode.getData().getExternalName(), ContainerUtil.newArrayList(), ContainerUtil.newHashMap());
                localProjectBuildClasspath.put(linkedExternalProjectPath, projectBuildClasspathPojo);
            }
            List<String> projectBuildClasspath = ContainerUtil.newArrayList(externalProjectGradleSdkLibs.getValue());
            projectBuildClasspath.addAll(buildSrcProjectsRoots.getValue());
            projectBuildClasspathPojo.setProjectBuildClasspath(projectBuildClasspath);
            projectBuildClasspathPojo.getModulesBuildClasspath().put(externalModulePath, new ExternalModuleBuildClasspathPojo(externalModulePath, ContainerUtil.newArrayList(buildClasspath)));
        }
    }
    localSettings.setProjectBuildClasspath(localProjectBuildClasspath);
    if (!project.isDisposed()) {
        GradleBuildClasspathManager.getInstance(project).reload();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AbstractExternalSystemLocalSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemLocalSettings) NotNullLazyValue(com.intellij.openapi.util.NotNullLazyValue) GradleProjectSettings(org.jetbrains.plugins.gradle.settings.GradleProjectSettings) GradleLocalSettings(org.jetbrains.plugins.gradle.settings.GradleLocalSettings) GradleInstallationManager(org.jetbrains.plugins.gradle.service.GradleInstallationManager) ExternalProjectBuildClasspathPojo(com.intellij.openapi.externalSystem.model.project.ExternalProjectBuildClasspathPojo) ExternalModuleBuildClasspathPojo(com.intellij.openapi.externalSystem.model.project.ExternalModuleBuildClasspathPojo) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) BuildScriptClasspathData(org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

Module (com.intellij.openapi.module.Module)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 GradleInstallationManager (org.jetbrains.plugins.gradle.service.GradleInstallationManager)2 CantRunException (com.intellij.execution.CantRunException)1 JavaParameters (com.intellij.execution.configurations.JavaParameters)1 ExternalModuleBuildClasspathPojo (com.intellij.openapi.externalSystem.model.project.ExternalModuleBuildClasspathPojo)1 ExternalProjectBuildClasspathPojo (com.intellij.openapi.externalSystem.model.project.ExternalProjectBuildClasspathPojo)1 ModuleData (com.intellij.openapi.externalSystem.model.project.ModuleData)1 AbstractExternalSystemLocalSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemLocalSettings)1 Project (com.intellij.openapi.project.Project)1 NotNullLazyValue (com.intellij.openapi.util.NotNullLazyValue)1 PsiFile (com.intellij.psi.PsiFile)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 BuildScriptClasspathData (org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData)1 GradleLocalSettings (org.jetbrains.plugins.gradle.settings.GradleLocalSettings)1 GradleProjectSettings (org.jetbrains.plugins.gradle.settings.GradleProjectSettings)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GroovyScriptRunConfiguration (org.jetbrains.plugins.groovy.runner.GroovyScriptRunConfiguration)1