Search in sources :

Example 1 with BuildScriptClasspathData

use of org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData in project intellij-community by JetBrains.

the class BaseGradleProjectResolverExtension method populateModuleExtraModels.

@Override
public void populateModuleExtraModels(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule) {
    final BuildScriptClasspathModel buildScriptClasspathModel = resolverCtx.getExtraProject(gradleModule, BuildScriptClasspathModel.class);
    final List<BuildScriptClasspathData.ClasspathEntry> classpathEntries;
    if (buildScriptClasspathModel != null) {
        classpathEntries = ContainerUtil.map(buildScriptClasspathModel.getClasspath(), new Function<ClasspathEntryModel, BuildScriptClasspathData.ClasspathEntry>() {

            @Override
            public BuildScriptClasspathData.ClasspathEntry fun(ClasspathEntryModel model) {
                return new BuildScriptClasspathData.ClasspathEntry(model.getClasses(), model.getSources(), model.getJavadoc());
            }
        });
    } else {
        classpathEntries = ContainerUtil.emptyList();
    }
    BuildScriptClasspathData buildScriptClasspathData = new BuildScriptClasspathData(GradleConstants.SYSTEM_ID, classpathEntries);
    buildScriptClasspathData.setGradleHomeDir(buildScriptClasspathModel != null ? buildScriptClasspathModel.getGradleHomeDir() : null);
    ideModule.createChild(BuildScriptClasspathData.KEY, buildScriptClasspathData);
    GradleExtensions gradleExtensions = resolverCtx.getExtraProject(gradleModule, GradleExtensions.class);
    if (gradleExtensions != null) {
        DefaultGradleExtensions extensions = new DefaultGradleExtensions(gradleExtensions);
        ExternalProject externalProject = resolverCtx.getExtraProject(gradleModule, ExternalProject.class);
        if (externalProject != null) {
            extensions.getTasks().addAll(externalProject.getTasks().values());
        }
        ideModule.createChild(GradleExtensionsDataService.KEY, extensions);
    }
}
Also used : BuildScriptClasspathData(org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData)

Example 2 with BuildScriptClasspathData

use of org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData 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

BuildScriptClasspathData (org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData)2 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 Module (com.intellij.openapi.module.Module)1 NotNullLazyValue (com.intellij.openapi.util.NotNullLazyValue)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 File (java.io.File)1 GradleInstallationManager (org.jetbrains.plugins.gradle.service.GradleInstallationManager)1 GradleLocalSettings (org.jetbrains.plugins.gradle.settings.GradleLocalSettings)1 GradleProjectSettings (org.jetbrains.plugins.gradle.settings.GradleProjectSettings)1