Search in sources :

Example 1 with GradleProject

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

the class GradleBuildConverter method convertChildren.

private void convertChildren(DefaultGradleBuild gradleBuild, PartialBasicGradleProject rootProject, GradleProject project) {
    final List<? extends GradleProject> childProjects = new ArrayList<GradleProject>(project.getChildren());
    Collections.sort(childProjects, new Comparator<GradleProject>() {

        public int compare(GradleProject gp1, GradleProject gp2) {
            return gp1.getName().compareTo(gp2.getName());
        }
    });
    for (GradleProject childProject : childProjects) {
        PartialBasicGradleProject basicGradleProject = toPartialGradleProject(childProject);
        gradleBuild.addProject(basicGradleProject);
        basicGradleProject.setParent(rootProject);
        rootProject.addChild(basicGradleProject);
        convertChildren(gradleBuild, basicGradleProject, childProject);
    }
}
Also used : ArrayList(java.util.ArrayList) PartialBasicGradleProject(org.gradle.tooling.internal.gradle.PartialBasicGradleProject) GradleProject(org.gradle.tooling.model.GradleProject) PartialBasicGradleProject(org.gradle.tooling.internal.gradle.PartialBasicGradleProject)

Example 2 with GradleProject

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

the class UseOtherTypesToFetchProjectModel method execute.

public Map<String, CustomModel> execute(BuildController controller) {
    // Use an IdeaModule to reference a project
    IdeaProject ideaProject = controller.getModel(IdeaProject.class);
    for (IdeaModule ideaModule : ideaProject.getModules()) {
        visit(ideaModule, controller, new HashMap<String, CustomModel>());
    }
    // Use an EclipseProject to reference a project
    EclipseProject eclipseProject = controller.getModel(EclipseProject.class);
    visit(eclipseProject, controller, new HashMap<String, CustomModel>());
    // Use a GradleProject to reference a project
    GradleProject rootProject = controller.getModel(GradleProject.class);
    Map<String, CustomModel> projects = new HashMap<String, CustomModel>();
    visit(rootProject, controller, projects);
    return projects;
}
Also used : EclipseProject(org.gradle.tooling.model.eclipse.EclipseProject) IdeaProject(org.gradle.tooling.model.idea.IdeaProject) IdeaModule(org.gradle.tooling.model.idea.IdeaModule) HashMap(java.util.HashMap) GradleProject(org.gradle.tooling.model.GradleProject) CustomModel(org.gradle.integtests.tooling.r16.CustomModel)

Example 3 with GradleProject

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

the class GradleUtil method getConfigPath.

/**
   * Allows to build file system path to the target gradle sub-project given the root project path.
   *
   * @param subProject       target sub-project which config path we're interested in
   * @param rootProjectPath  path to root project's directory which contains 'build.gradle'
   * @return                 path to the given sub-project's directory which contains 'build.gradle'
   */
@NotNull
public static String getConfigPath(@NotNull GradleProject subProject, @NotNull String rootProjectPath) {
    try {
        GradleScript script = subProject.getBuildScript();
        if (script != null) {
            File file = script.getSourceFile();
            if (file != null) {
                if (!file.isDirectory()) {
                    // The file points to 'build.gradle' at the moment but we keep it's parent dir path instead.
                    file = file.getParentFile();
                }
                return ExternalSystemApiUtil.toCanonicalPath(file.getCanonicalPath());
            }
        }
    } catch (Exception e) {
    // As said by gradle team: 'One thing I'm interested in is whether you have any thoughts about how the tooling API should
    // deal with missing details from the model - for example, when asking for details about the build scripts when using
    // a version of Gradle that does not supply that information. Currently, you'll get a `UnsupportedOperationException`
    // when you call the `getBuildScript()` method'.
    //
    // So, just ignore it and assume that the user didn't define any custom build file name.
    }
    File rootProjectParent = new File(rootProjectPath);
    StringBuilder buffer = new StringBuilder(FileUtil.toCanonicalPath(rootProjectParent.getAbsolutePath()));
    Stack<String> stack = ContainerUtilRt.newStack();
    for (GradleProject p = subProject; p != null; p = p.getParent()) {
        stack.push(p.getName());
    }
    // pop root project
    stack.pop();
    while (!stack.isEmpty()) {
        buffer.append(ExternalSystemConstants.PATH_SEPARATOR).append(stack.pop());
    }
    return buffer.toString();
}
Also used : GradleScript(org.gradle.tooling.model.gradle.GradleScript) GradleProject(org.gradle.tooling.model.GradleProject) File(java.io.File) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GradleProject

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

the class GradleProjectResolver method extractExternalProjectModels.

private static void extractExternalProjectModels(@NotNull ProjectImportAction.AllModels models, @NotNull ProjectResolverContext resolverCtx) {
    resolverCtx.setModels(models);
    final Class<? extends ExternalProject> modelClazz = resolverCtx.isPreviewMode() ? ExternalProjectPreview.class : ExternalProject.class;
    final ExternalProject externalRootProject = models.getExtraProject((IdeaModule) null, modelClazz);
    if (externalRootProject == null)
        return;
    final DefaultExternalProject wrappedExternalRootProject = new DefaultExternalProject(externalRootProject);
    models.addExtraProject(wrappedExternalRootProject, ExternalProject.class);
    final Map<String, ExternalProject> externalProjectsMap = createExternalProjectsMap(null, wrappedExternalRootProject);
    DomainObjectSet<? extends IdeaModule> gradleModules = models.getIdeaProject().getModules();
    if (gradleModules != null && !gradleModules.isEmpty()) {
        for (IdeaModule ideaModule : gradleModules) {
            final ExternalProject externalProject = externalProjectsMap.get(getModuleId(resolverCtx, ideaModule));
            if (externalProject != null) {
                models.addExtraProject(externalProject, ExternalProject.class, ideaModule.getGradleProject());
            }
        }
    }
    for (IdeaProject project : models.getIncludedBuilds()) {
        DomainObjectSet<? extends IdeaModule> ideaModules = project.getModules();
        if (ideaModules.isEmpty())
            continue;
        GradleProject gradleProject = ideaModules.getAt(0).getGradleProject();
        while (gradleProject.getParent() != null) {
            gradleProject = gradleProject.getParent();
        }
        final ExternalProject externalIncludedRootProject = models.getExtraProject(gradleProject, modelClazz);
        if (externalIncludedRootProject == null)
            continue;
        final DefaultExternalProject wrappedExternalIncludedRootProject = new DefaultExternalProject(externalIncludedRootProject);
        wrappedExternalRootProject.getChildProjects().put(wrappedExternalIncludedRootProject.getName(), wrappedExternalIncludedRootProject);
        String compositePrefix = project.getName();
        final Map<String, ExternalProject> externalIncludedProjectsMap = createExternalProjectsMap(compositePrefix, wrappedExternalIncludedRootProject);
        for (IdeaModule ideaModule : ideaModules) {
            final ExternalProject externalProject = externalIncludedProjectsMap.get(getModuleId(resolverCtx, ideaModule));
            if (externalProject != null) {
                models.addExtraProject(externalProject, ExternalProject.class, ideaModule.getGradleProject());
            }
        }
    }
}
Also used : IdeaModule(org.gradle.tooling.model.idea.IdeaModule) IdeaProject(org.gradle.tooling.model.idea.IdeaProject) BasicIdeaProject(org.gradle.tooling.model.idea.BasicIdeaProject) GradleProject(org.gradle.tooling.model.GradleProject)

Example 5 with GradleProject

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

the class GradleModuleSetup method createGradleModel.

@NotNull
private static GradleModuleModel createGradleModel(@NotNull Module module, @NotNull SyncAction.ModuleModels models) {
    GradleProject gradleProject = models.getModule().getGradleProject();
    GradleScript buildScript = null;
    try {
        buildScript = gradleProject.getBuildScript();
    } catch (Throwable e) {
    // Ignored. We got here because the project is using Gradle 1.8 or older.
    }
    File buildFilePath = buildScript != null ? buildScript.getSourceFile() : null;
    BuildScriptClasspathModel classpathModel = models.findModel(BuildScriptClasspathModel.class);
    String gradleVersion = classpathModel != null ? classpathModel.getGradleVersion() : null;
    return new GradleModuleModel(module.getName(), gradleProject, buildFilePath, gradleVersion);
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) BuildScriptClasspathModel(org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel) GradleScript(org.gradle.tooling.model.gradle.GradleScript) GradleProject(org.gradle.tooling.model.GradleProject) File(java.io.File) 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