Search in sources :

Example 6 with GradleProject

use of org.gradle.tooling.model.GradleProject in project android by JetBrains.

the class AndroidGradleProjectResolver method populateModuleContentRoots.

@Override
public void populateModuleContentRoots(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule) {
    ImportedModule importedModule = new ImportedModule(gradleModule);
    ideModule.createChild(IMPORTED_MODULE, importedModule);
    GradleProject gradleProject = gradleModule.getGradleProject();
    GradleScript buildScript = null;
    try {
        buildScript = gradleProject.getBuildScript();
    } catch (UnsupportedOperationException ignore) {
    }
    if (buildScript == null || !isAndroidGradleProject()) {
        nextResolver.populateModuleContentRoots(gradleModule, ideModule);
        return;
    }
    // do not derive module root dir based on *.iml file location
    File moduleRootDirPath = new File(toSystemDependentName(ideModule.getData().getLinkedExternalProjectPath()));
    AndroidProject androidProject = resolverCtx.getExtraProject(gradleModule, AndroidProject.class);
    boolean androidProjectWithoutVariants = false;
    String moduleName = gradleModule.getName();
    if (androidProject != null) {
        Variant selectedVariant = myVariantSelector.findVariantToSelect(androidProject);
        if (selectedVariant == null) {
            // If an Android project does not have variants, it would be impossible to build. This is a possible but invalid use case.
            // For now we are going to treat this case as a Java library module, because everywhere in the IDE (e.g. run configurations,
            // editors, test support, variants tool window, project building, etc.) we have the assumption that there is at least one variant
            // per Android project, and changing that in the code base is too risky, for very little benefit.
            // See https://code.google.com/p/android/issues/detail?id=170722
            androidProjectWithoutVariants = true;
        } else {
            String variantName = selectedVariant.getName();
            AndroidModuleModel model = new AndroidModuleModel(moduleName, moduleRootDirPath, androidProject, variantName);
            ideModule.createChild(ANDROID_MODEL, model);
        }
    }
    NativeAndroidProject nativeAndroidProject = resolverCtx.getExtraProject(gradleModule, NativeAndroidProject.class);
    if (nativeAndroidProject != null) {
        NdkModuleModel ndkModuleModel = new NdkModuleModel(moduleName, moduleRootDirPath, nativeAndroidProject);
        ideModule.createChild(NDK_MODEL, ndkModuleModel);
    }
    File gradleSettingsFile = new File(moduleRootDirPath, FN_SETTINGS_GRADLE);
    if (gradleSettingsFile.isFile() && androidProject == null && nativeAndroidProject == null) {
        // This is just a root folder for a group of Gradle projects. We don't set an IdeaGradleProject so the JPS builder won't try to
        // compile it using Gradle. We still need to create the module to display files inside it.
        createJavaProject(gradleModule, ideModule, false);
        return;
    }
    BuildScriptClasspathModel buildScriptModel = resolverCtx.getExtraProject(BuildScriptClasspathModel.class);
    String gradleVersion = buildScriptModel != null ? buildScriptModel.getGradleVersion() : null;
    File buildFilePath = buildScript.getSourceFile();
    GradleModuleModel gradleModuleModel = new GradleModuleModel(moduleName, gradleProject, buildFilePath, gradleVersion);
    ideModule.createChild(GRADLE_MODULE_MODEL, gradleModuleModel);
    if (nativeAndroidProject == null && (androidProject == null || androidProjectWithoutVariants)) {
        // This is a Java lib module.
        createJavaProject(gradleModule, ideModule, androidProjectWithoutVariants);
    }
}
Also used : BuildScriptClasspathModel(org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel) ImportedModule(com.android.tools.idea.gradle.ImportedModule) AndroidProject(com.android.builder.model.AndroidProject) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) Variant(com.android.builder.model.Variant) GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) GradleScript(org.gradle.tooling.model.gradle.GradleScript) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) GradleProject(org.gradle.tooling.model.GradleProject) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) File(java.io.File)

Example 7 with GradleProject

use of org.gradle.tooling.model.GradleProject in project android by JetBrains.

the class GradleUtilIdeaTest method testGetGradleBuildFileFromModuleWithGradleFacet.

public void testGetGradleBuildFileFromModuleWithGradleFacet() {
    GradleProject project = createMock(GradleProject.class);
    expect(project.getPath()).andReturn(myModule.getName());
    //noinspection unchecked
    DomainObjectSet<? extends GradleTask> tasks = createMock(DomainObjectSet.class);
    project.getTasks();
    expectLastCall().andReturn(tasks);
    expect(tasks.isEmpty()).andReturn(true);
    replay(project, tasks);
    GradleModuleModel gradleModuleModel = new GradleModuleModel(myModule.getName(), project, myBuildFile, "2.2.1");
    GradleFacet facet = createAndAddGradleFacet(myModule);
    facet.setGradleModuleModel(gradleModuleModel);
    VirtualFile buildFile = GradleUtil.getGradleBuildFile(myModule);
    assertIsGradleBuildFile(buildFile);
    verify(project, tasks);
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Facets.createAndAddGradleFacet(com.android.tools.idea.testing.Facets.createAndAddGradleFacet) GradleProject(org.gradle.tooling.model.GradleProject)

Example 8 with GradleProject

use of org.gradle.tooling.model.GradleProject in project gradle by gradle.

the class GradleBuildAdapterProducer method produceModel.

public <T> T produceModel(Class<T> type, ConsumerOperationParameters operationParameters) {
    if (type.equals(GradleBuild.class)) {
        GradleProject gradleProject = delegate.produceModel(GradleProject.class, operationParameters);
        final DefaultGradleBuild convert = new GradleBuildConverter().convert(gradleProject);
        return mappingProvider.applyCompatibilityMapping(adapter.builder(type), operationParameters).build(convert);
    }
    return delegate.produceModel(type, operationParameters);
}
Also used : GradleBuildConverter(org.gradle.tooling.internal.consumer.converters.GradleBuildConverter) DefaultGradleBuild(org.gradle.tooling.internal.gradle.DefaultGradleBuild) GradleProject(org.gradle.tooling.model.GradleProject)

Example 9 with GradleProject

use of org.gradle.tooling.model.GradleProject in project gradle by gradle.

the class FetchIncludedGradleProjects method execute.

@Override
public List<GradleProject> execute(BuildController controller) {
    List<GradleProject> projects = new ArrayList<GradleProject>();
    GradleBuild build = controller.getBuildModel();
    for (GradleBuild includedBuild : build.getIncludedBuilds()) {
        projects.add(controller.getModel(includedBuild, GradleProject.class));
    }
    return projects;
}
Also used : ArrayList(java.util.ArrayList) GradleProject(org.gradle.tooling.model.GradleProject) GradleBuild(org.gradle.tooling.model.gradle.GradleBuild)

Example 10 with GradleProject

use of org.gradle.tooling.model.GradleProject in project intellij-community by JetBrains.

the class GradleProjectResolver method exposeCompositeBuild.

@NotNull
private static Collection<IdeaModule> exposeCompositeBuild(ProjectImportAction.AllModels allModels, DefaultProjectResolverContext resolverCtx, DataNode<ProjectData> projectDataNode) {
    if (resolverCtx.getSettings() != null && !resolverCtx.getSettings().getExecutionWorkspace().getBuildParticipants().isEmpty()) {
        return Collections.emptyList();
    }
    CompositeBuildData compositeBuildData;
    List<IdeaModule> gradleIncludedModules = new SmartList<>();
    List<IdeaProject> includedBuilds = allModels.getIncludedBuilds();
    if (!includedBuilds.isEmpty()) {
        ProjectData projectData = projectDataNode.getData();
        compositeBuildData = new CompositeBuildData(projectData.getLinkedExternalProjectPath());
        for (IdeaProject project : includedBuilds) {
            if (!project.getModules().isEmpty()) {
                String rootProjectName = project.getName();
                BuildParticipant buildParticipant = new BuildParticipant();
                gradleIncludedModules.addAll(project.getModules());
                GradleProject gradleProject = project.getModules().getAt(0).getGradleProject();
                String projectPath = null;
                do {
                    try {
                        projectPath = ExternalSystemApiUtil.toCanonicalPath(gradleProject.getProjectDirectory().getCanonicalPath());
                    } catch (IOException e) {
                        LOG.warn("construction of the canonical path for the module fails", e);
                    }
                } while ((gradleProject = gradleProject.getParent()) != null);
                if (projectPath != null) {
                    buildParticipant.setRootProjectName(rootProjectName);
                    buildParticipant.setRootPath(projectPath);
                    for (IdeaModule module : project.getModules()) {
                        try {
                            String modulePath = ExternalSystemApiUtil.toCanonicalPath(module.getGradleProject().getProjectDirectory().getCanonicalPath());
                            buildParticipant.getProjects().add(modulePath);
                        } catch (IOException e) {
                            LOG.warn("construction of the canonical path for the module fails", e);
                        }
                    }
                    compositeBuildData.getCompositeParticipants().add(buildParticipant);
                }
            }
        }
        projectDataNode.createChild(CompositeBuildData.KEY, compositeBuildData);
    }
    return gradleIncludedModules;
}
Also used : IdeaModule(org.gradle.tooling.model.idea.IdeaModule) IdeaProject(org.gradle.tooling.model.idea.IdeaProject) BasicIdeaProject(org.gradle.tooling.model.idea.BasicIdeaProject) CompositeBuildData(org.jetbrains.plugins.gradle.model.data.CompositeBuildData) BuildParticipant(org.jetbrains.plugins.gradle.model.data.BuildParticipant) GradleBuildParticipant(org.jetbrains.plugins.gradle.settings.GradleBuildParticipant) GradleProject(org.gradle.tooling.model.GradleProject) IOException(java.io.IOException) SmartList(com.intellij.util.SmartList) JavaProjectData(com.intellij.externalSystem.JavaProjectData) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GradleProject (org.gradle.tooling.model.GradleProject)12 NotNull (org.jetbrains.annotations.NotNull)5 File (java.io.File)4 GradleModuleModel (com.android.tools.idea.gradle.project.model.GradleModuleModel)3 GradleScript (org.gradle.tooling.model.gradle.GradleScript)3 IdeaModule (org.gradle.tooling.model.idea.IdeaModule)3 IdeaProject (org.gradle.tooling.model.idea.IdeaProject)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 GradleBuild (org.gradle.tooling.model.gradle.GradleBuild)2 BasicIdeaProject (org.gradle.tooling.model.idea.BasicIdeaProject)2 BuildScriptClasspathModel (org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel)2 AndroidProject (com.android.builder.model.AndroidProject)1 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)1 Variant (com.android.builder.model.Variant)1 ImportedModule (com.android.tools.idea.gradle.ImportedModule)1 GradleFacet (com.android.tools.idea.gradle.project.facet.gradle.GradleFacet)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)1 Facets.createAndAddGradleFacet (com.android.tools.idea.testing.Facets.createAndAddGradleFacet)1