Search in sources :

Example 1 with GenerateIdeaModule

use of org.gradle.plugins.ide.idea.GenerateIdeaModule in project gradle-apt-plugin by tbroyer.

the class AptIdeaPlugin method configureIdeaModule.

private void configureIdeaModule(Project project, final SourceSet mainSourceSet, final SourceSet testSourceSet) {
    final IdeaModule ideaModule = project.getExtensions().getByType(IdeaModel.class).getModule();
    final ModuleApt apt = new ModuleApt();
    new DslObject(ideaModule).getConvention().getPlugins().put("net.ltgt.apt-idea", new ModuleAptConvention(apt));
    project.afterEvaluate(new Action<Project>() {

        @Override
        public void execute(Project project) {
            if (apt.isAddGeneratedSourcesDirs()) {
                Set<File> excl = new LinkedHashSet<>();
                for (SourceSet sourceSet : new SourceSet[] { mainSourceSet, testSourceSet }) {
                    File generatedSourcesDir = new DslObject(sourceSet.getOutput()).getConvention().getPlugin(AptPlugin.AptSourceSetOutputConvention.class).getGeneratedSourcesDir();
                    for (File f = generatedSourcesDir; f != null && !f.equals(project.getProjectDir()); f = f.getParentFile()) {
                        excl.add(f);
                    }
                }
                // For some reason, modifying the existing collections doesn't work.
                // We need to copy the values and then assign it back.
                Set<File> excludeDirs = new LinkedHashSet<>(ideaModule.getExcludeDirs());
                if (excl.contains(project.getBuildDir()) && excludeDirs.contains(project.getBuildDir())) {
                    excludeDirs.remove(project.getBuildDir());
                    // Race condition: many of these will actually be created afterwards…
                    File[] subdirs = project.getBuildDir().listFiles(new FileFilter() {

                        @Override
                        public boolean accept(File pathname) {
                            return pathname.isDirectory();
                        }
                    });
                    if (subdirs != null) {
                        excludeDirs.addAll(Arrays.asList(subdirs));
                    }
                }
                excludeDirs.removeAll(excl);
                ideaModule.setExcludeDirs(excludeDirs);
                File mainGeneratedSourcesDir = new DslObject(mainSourceSet.getOutput()).getConvention().getPlugin(AptPlugin.AptSourceSetOutputConvention.class).getGeneratedSourcesDir();
                File testGeneratedSourcesDir = new DslObject(testSourceSet.getOutput()).getConvention().getPlugin(AptPlugin.AptSourceSetOutputConvention.class).getGeneratedSourcesDir();
                // For some reason, modifying the existing collections doesn't work.
                // We need to copy the values and then assign it back.
                ideaModule.setSourceDirs(addToSet(ideaModule.getSourceDirs(), mainGeneratedSourcesDir));
                ideaModule.setTestSourceDirs(addToSet(ideaModule.getTestSourceDirs(), testGeneratedSourcesDir));
                ideaModule.setGeneratedSourceDirs(addToSet(ideaModule.getGeneratedSourceDirs(), mainGeneratedSourcesDir, testGeneratedSourcesDir));
            }
            if (apt.isAddCompileOnlyDependencies() || apt.isAddAptDependencies()) {
                final AptPlugin.AptSourceSetConvention mainSourceSetConvention = new DslObject(mainSourceSet).getConvention().getPlugin(AptPlugin.AptSourceSetConvention.class);
                final AptPlugin.AptSourceSetConvention testSourceSetConvention = new DslObject(testSourceSet).getConvention().getPlugin(AptPlugin.AptSourceSetConvention.class);
                final List<Configuration> mainConfigurations = new ArrayList<>();
                final List<Configuration> testConfigurations = new ArrayList<>();
                if (apt.isAddCompileOnlyDependencies()) {
                    mainConfigurations.add(project.getConfigurations().getByName(mainSourceSetConvention.getCompileOnlyConfigurationName()));
                    testConfigurations.add(project.getConfigurations().getByName(testSourceSetConvention.getCompileOnlyConfigurationName()));
                }
                if (apt.isAddAptDependencies()) {
                    mainConfigurations.add(project.getConfigurations().getByName(mainSourceSetConvention.getAnnotationProcessorConfigurationName()));
                    testConfigurations.add(project.getConfigurations().getByName(testSourceSetConvention.getAnnotationProcessorConfigurationName()));
                }
                ideaModule.getScopes().get(apt.getMainDependenciesScope()).get("plus").addAll(mainConfigurations);
                ideaModule.getScopes().get("TEST").get("plus").addAll(testConfigurations);
                project.getTasks().withType(GenerateIdeaModule.class, new Action<GenerateIdeaModule>() {

                    @Override
                    public void execute(GenerateIdeaModule generateIdeaModule) {
                        generateIdeaModule.dependsOn(mainConfigurations.toArray());
                        generateIdeaModule.dependsOn(testConfigurations.toArray());
                    }
                });
            }
        }

        private Set<File> addToSet(Set<File> sourceDirs, File... dirs) {
            Set<File> newSet = new LinkedHashSet<>(sourceDirs);
            newSet.addAll(Arrays.asList(dirs));
            return newSet;
        }
    });
}
Also used : Action(org.gradle.api.Action) SourceSet(org.gradle.api.tasks.SourceSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) DslObject(org.gradle.api.internal.plugins.DslObject) IdeaProject(org.gradle.plugins.ide.idea.model.IdeaProject) Project(org.gradle.api.Project) SourceSet(org.gradle.api.tasks.SourceSet) GenerateIdeaModule(org.gradle.plugins.ide.idea.GenerateIdeaModule) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) GenerateIdeaModule(org.gradle.plugins.ide.idea.GenerateIdeaModule) IdeaModel(org.gradle.plugins.ide.idea.model.IdeaModel) NodeList(groovy.util.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) FileFilter(java.io.FileFilter) File(java.io.File)

Example 2 with GenerateIdeaModule

use of org.gradle.plugins.ide.idea.GenerateIdeaModule in project gradle by gradle.

the class PlayIdeaPlugin method configureIdeaModule.

@Mutate
public void configureIdeaModule(@Path("tasks.ideaModule") GenerateIdeaModule ideaModule, @Path("binaries.playBinary") final PlayApplicationBinarySpec playApplicationBinarySpec, @Path("buildDir") final File buildDir, ConfigurationContainer configurations, final FileResolver fileResolver) {
    IdeaModule module = ideaModule.getModule();
    module.setScopes(buildScopes(configurations));
    ConventionMapping conventionMapping = conventionMappingFor(module);
    conventionMapping.map("sourceDirs", new Callable<Set<File>>() {

        @Override
        public Set<File> call() throws Exception {
            // TODO: Assets should probably be a source set too
            Set<File> sourceDirs = Sets.newHashSet(playApplicationBinarySpec.getAssets().getAssetDirs());
            return CollectionUtils.inject(sourceDirs, playApplicationBinarySpec.getInputs(), new Action<CollectionUtils.InjectionStep<Set<File>, LanguageSourceSet>>() {

                @Override
                public void execute(CollectionUtils.InjectionStep<Set<File>, LanguageSourceSet> step) {
                    step.getTarget().addAll(step.getItem().getSource().getSrcDirs());
                }
            });
        }
    });
    conventionMapping.map("testSourceDirs", new Callable<Set<File>>() {

        @Override
        public Set<File> call() throws Exception {
            // TODO: This should be modeled as a source set
            return Collections.singleton(fileResolver.resolve("test"));
        }
    });
    conventionMapping.map("singleEntryLibraries", new Callable<Map<String, Iterable<File>>>() {

        @Override
        public Map<String, Iterable<File>> call() throws Exception {
            return ImmutableMap.<String, Iterable<File>>builder().put("COMPILE", Collections.singleton(playApplicationBinarySpec.getClasses().getClassesDir())).put("RUNTIME", playApplicationBinarySpec.getClasses().getResourceDirs()).put("TEST", Collections.singleton(new File(buildDir, "playBinary/testClasses"))).build();
        }
    });
    module.setScalaPlatform(playApplicationBinarySpec.getTargetPlatform().getScalaPlatform());
    conventionMapping.map("targetBytecodeVersion", new Callable<JavaVersion>() {

        @Override
        public JavaVersion call() throws Exception {
            return getTargetJavaVersion(playApplicationBinarySpec);
        }
    });
    conventionMapping.map("languageLevel", new Callable<IdeaLanguageLevel>() {

        @Override
        public IdeaLanguageLevel call() throws Exception {
            return new IdeaLanguageLevel(getTargetJavaVersion(playApplicationBinarySpec));
        }
    });
    ideaModule.dependsOn(playApplicationBinarySpec.getInputs());
    ideaModule.dependsOn(playApplicationBinarySpec.getAssets());
}
Also used : Action(org.gradle.api.Action) LanguageSourceSet(org.gradle.language.base.LanguageSourceSet) Set(java.util.Set) CollectionUtils(org.gradle.util.CollectionUtils) IdeaLanguageLevel(org.gradle.plugins.ide.idea.model.IdeaLanguageLevel) JavaVersion(org.gradle.api.JavaVersion) ConventionMapping(org.gradle.api.internal.ConventionMapping) GenerateIdeaModule(org.gradle.plugins.ide.idea.GenerateIdeaModule) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) LanguageSourceSet(org.gradle.language.base.LanguageSourceSet) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Mutate(org.gradle.model.Mutate)

Aggregations

File (java.io.File)2 Set (java.util.Set)2 Action (org.gradle.api.Action)2 GenerateIdeaModule (org.gradle.plugins.ide.idea.GenerateIdeaModule)2 IdeaModule (org.gradle.plugins.ide.idea.model.IdeaModule)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 NodeList (groovy.util.NodeList)1 FileFilter (java.io.FileFilter)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 JavaVersion (org.gradle.api.JavaVersion)1 Project (org.gradle.api.Project)1 ConventionMapping (org.gradle.api.internal.ConventionMapping)1 DslObject (org.gradle.api.internal.plugins.DslObject)1 SourceSet (org.gradle.api.tasks.SourceSet)1 LanguageSourceSet (org.gradle.language.base.LanguageSourceSet)1 Mutate (org.gradle.model.Mutate)1 IdeaLanguageLevel (org.gradle.plugins.ide.idea.model.IdeaLanguageLevel)1