Search in sources :

Example 6 with GradleBuildFile

use of com.android.tools.idea.gradle.parser.GradleBuildFile in project android by JetBrains.

the class ArchiveToGradleModuleModelTest method assertSourceProjectCorrectlyModified.

private void assertSourceProjectCorrectlyModified(@NotNull String expectedBuildGradle, @NotNull String sourceModuleFilePath) throws IOException {
    Project project = getProject();
    VirtualFile gradleBuildFile = project.getBaseDir().findFileByRelativePath(sourceModuleFilePath + "/" + SdkConstants.FN_BUILD_GRADLE);
    assertThat(gradleBuildFile).isNotNull();
    GradleBuildFile buildModel = new GradleBuildFile(gradleBuildFile, project);
    assertThat(expectedBuildGradle).isEqualTo(buildModel.getPsiFile().getText());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile)

Example 7 with GradleBuildFile

use of com.android.tools.idea.gradle.parser.GradleBuildFile in project freeline by alibaba.

the class FreelineUtil method getFreelineStatus.

/**
     * 获取freeline安装状态
     *
     * @param project
     * @return
     */
public static FreelineStatus getFreelineStatus(@NotNull Project project) {
    FreelineStatus status = new FreelineStatus();
    Collection<VirtualFile> gradleFiles = GradleUtil.getAllGradleFile(project);
    status.setGradleBuildFiles(gradleFiles);
    for (VirtualFile file : gradleFiles) {
        if (!status.isExistClasspath()) {
            GradleBuildModel model = GradleBuildModel.parseBuildFile(file, project);
            if (model != null) {
                List<ArtifactDependencyModel> classPaths = model.buildscript().dependencies().artifacts();
                for (ArtifactDependencyModel classpath : classPaths) {
                    ArtifactDependencyModelWrapper wrapper = new ArtifactDependencyModelWrapper(classpath);
                    if (wrapper.group().equals(Constant.FREELINE_CLASSPATH_GROUP) && wrapper.name().equals(Constant.FREELINE_CLASSPATH_ARTIFACT)) {
                        status.setClasspathFile(file);
                        break;
                    }
                }
            }
        }
        // 正则二次判断是否存在Freeline classpath
        if (!status.isExistClasspath() && regularExistFreelineClassPath(file)) {
            status.setClasspathFile(file);
        }
        if (!status.isExistPlugin()) {
            GradleBuildFile gradleBuildFile = new GradleBuildFile(file, project);
            if (gradleBuildFile != null) {
                List<String> plugins = gradleBuildFile.getPlugins();
                if (plugins.contains(Constant.FREELINE_PLUGIN_ID)) {
                    status.setPluginFile(file);
                }
            }
        }
        if (status.isExistClasspath() && status.isExistPlugin()) {
            break;
        }
    }
    File baseFile = new File(project.getBasePath());
    if (new File(baseFile, Constant.FREELINE_ROOT_FOLDER).exists() && new File(baseFile, Constant.FREELINE_PYTHON).exists()) {
        if (SystemInfo.isWindows) {
            if (new File(baseFile, Constant.FREELINE_ROOT_FOLDER_CORE).exists()) {
                status.setExistFreelineCore(true);
            }
        } else {
            status.setExistFreelineCore(true);
        }
    }
    return status;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) File(java.io.File)

Example 8 with GradleBuildFile

use of com.android.tools.idea.gradle.parser.GradleBuildFile in project android by JetBrains.

the class ManifestPanel method describePosition.

private void describePosition(@NotNull HtmlBuilder sb, @NotNull AndroidFacet facet, @NotNull SourceFilePosition sourceFilePosition) {
    SourceFile sourceFile = sourceFilePosition.getFile();
    SourcePosition sourcePosition = sourceFilePosition.getPosition();
    File file = sourceFile.getSourceFile();
    if (file == GRADLE_MODEL_MARKER_FILE) {
        VirtualFile gradleBuildFile = GradleUtil.getGradleBuildFile(facet.getModule());
        if (gradleBuildFile != null) {
            file = VfsUtilCore.virtualToIoFile(gradleBuildFile);
            sb.addHtml("<a href=\"");
            sb.add(file.toURI().toString());
            sb.addHtml("\">");
            sb.add(file.getName());
            sb.addHtml("</a>");
            sb.add(" injection");
        } else {
            sb.add("build.gradle injection (source location unknown)");
        }
        return;
    }
    AndroidLibrary library;
    if (file != null) {
        String source = null;
        Module libraryModule = null;
        Module[] modules = ModuleManager.getInstance(facet.getModule().getProject()).getModules();
        VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(file);
        if (vFile != null) {
            Module module = ModuleUtilCore.findModuleForFile(vFile, facet.getModule().getProject());
            if (module != null) {
                if (modules.length >= 2) {
                    source = module.getName();
                }
                // AAR Library?
                if (file.getPath().contains(EXPLODED_AAR)) {
                    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
                    if (androidModel != null) {
                        library = GradleUtil.findLibrary(file.getParentFile(), androidModel.getSelectedVariant(), androidModel.getModelVersion());
                        if (library != null) {
                            if (library.getProject() != null) {
                                libraryModule = GradleUtil.findModuleByGradlePath(facet.getModule().getProject(), library.getProject());
                                if (libraryModule != null) {
                                    module = libraryModule;
                                    source = module.getName();
                                } else {
                                    source = library.getProject();
                                    source = StringUtil.trimStart(source, ":");
                                }
                            } else {
                                MavenCoordinates coordinates = library.getResolvedCoordinates();
                                source = /*coordinates.getGroupId() + ":" +*/
                                coordinates.getArtifactId() + ":" + coordinates.getVersion();
                            }
                        }
                    }
                }
            }
            IdeaSourceProvider provider = ManifestUtils.findManifestSourceProvider(facet, vFile);
            if (provider != null) /*&& !provider.equals(facet.getMainIdeaSourceProvider())*/
            {
                String providerName = provider.getName();
                if (source == null) {
                    source = providerName;
                } else {
                    // "the app main manifest" - "app" is the module name, "main" is the source provider name
                    source = source + " " + providerName;
                }
            }
        }
        if (source == null) {
            source = file.getName();
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                source += ":" + String.valueOf(sourcePosition);
            }
        }
        sb.addHtml("<a href=\"");
        boolean redirected = false;
        if (libraryModule != null) {
            AndroidFacet libraryFacet = AndroidFacet.getInstance(libraryModule);
            if (libraryFacet != null) {
                File manifestFile = libraryFacet.getMainSourceProvider().getManifestFile();
                if (manifestFile.exists()) {
                    sb.add(manifestFile.toURI().toString());
                    redirected = true;
                    // Line numbers probably aren't right
                    sourcePosition = SourcePosition.UNKNOWN;
                // TODO: Set URL which points to the element/attribute path
                }
            }
        }
        if (!redirected) {
            sb.add(file.toURI().toString());
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartLine()));
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartColumn()));
            }
        }
        sb.addHtml("\">");
        sb.add(source);
        sb.addHtml("</a>");
        sb.add(" manifest");
        if (FileUtil.filesEqual(file, VfsUtilCore.virtualToIoFile(myFile))) {
            sb.add(" (this file)");
        }
        if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
            sb.add(", line ");
            sb.add(Integer.toString(sourcePosition.getStartLine()));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) MavenCoordinates(com.android.builder.model.MavenCoordinates) AndroidLibrary(com.android.builder.model.AndroidLibrary) SourcePosition(com.android.ide.common.blame.SourcePosition) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) SourceFile(com.android.ide.common.blame.SourceFile) Module(com.intellij.openapi.module.Module) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFile(com.android.ide.common.blame.SourceFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File)

Aggregations

GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)8 Module (com.intellij.openapi.module.Module)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 File (java.io.File)4 Project (com.intellij.openapi.project.Project)3 PsiFile (com.intellij.psi.PsiFile)3 SourceFile (com.android.ide.common.blame.SourceFile)2 NamedObject (com.android.tools.idea.gradle.parser.NamedObject)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 DialogBuilder (com.intellij.openapi.ui.DialogBuilder)2 Pair (com.intellij.openapi.util.Pair)2 XmlFile (com.intellij.psi.xml.XmlFile)2 ActionEvent (java.awt.event.ActionEvent)2 IdeaSourceProvider (org.jetbrains.android.facet.IdeaSourceProvider)2 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 MavenCoordinates (com.android.builder.model.MavenCoordinates)1 SourcePosition (com.android.ide.common.blame.SourcePosition)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)1 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)1