Search in sources :

Example 1 with GradleBuildFile

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

the class FreelineUtil method checkInstall.

/**
     * 检查是否需要载入Freeline
     *
     * @param project
     * @return
     */
public static boolean checkInstall(@NotNull final Project project) {
    final FreelineStatus status = getFreelineStatus(project);
    if (GradleUtil.isSyncInProgress(project)) {
        NotificationUtils.errorMsgDialog("Waiting for sync project to complete");
        return false;
    }
    if (status.hasInitFreeline()) {
        return true;
    }
    if (status.getGradleBuildFiles().size() < 1) {
        NotificationUtils.errorMsgDialog("It's not an Android Gradle project Currently?");
        return false;
    }
    if (status.isExistClasspath() && status.isExistPlugin() && !status.isExistFreelineCore()) {
        NotificationUtils.errorNotification("Execute task initFreeline and download freeline dependencies...");
        initFreeline(project);
        return false;
    }
    if (DialogUtil.createDialog("Detected that you did not installFreeline Freeline, Whether installFreeline Automatically?", "Install Freeline Automatically", "Cancel")) {
        Module[] modules = ModuleManager.getInstance(project).getModules();
        List<Pair<Module, PsiFile>> selectModulesList = new ArrayList<Pair<Module, PsiFile>>();
        for (Module module : modules) {
            GradleBuildFile file = GradleBuildFile.get(module);
            if (file != null && !GradleUtil.isLibrary(file)) {
                selectModulesList.add(Pair.create(module, file.getPsiFile()));
            }
        }
        // 多个app模块的情况
        if (selectModulesList.size() > 1) {
            final DialogBuilder builder = new DialogBuilder();
            builder.setTitle("Install Freeline");
            builder.resizable(false);
            builder.setCenterPanel(new JLabel("There are multiple application modules, Please select the module to be installed Freeline.", Messages.getInformationIcon(), SwingConstants.CENTER));
            builder.addOkAction().setText("Cancel");
            for (final Pair<Module, PsiFile> pair : selectModulesList) {
                builder.addAction(new AbstractAction(":" + pair.first.getName()) {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        builder.getDialogWrapper().close(DialogWrapper.CANCEL_EXIT_CODE);
                        installFreeline(project, status, pair.getSecond());
                    }
                });
            }
            if (builder.show() > -1) {
                return false;
            }
        } else if (selectModulesList.size() == 1) {
            installFreeline(project, status, selectModulesList.get(0).getSecond());
        } else {
            NotificationUtils.errorMsgDialog("Can not found Application Module! Please Sync Project.");
            return false;
        }
    }
    return false;
}
Also used : GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) ActionEvent(java.awt.event.ActionEvent) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) DialogBuilder(com.intellij.openapi.ui.DialogBuilder) Pair(com.intellij.openapi.util.Pair)

Example 2 with GradleBuildFile

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

the class UsingReportAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent event) {
    final Project project = event.getProject();
    Module[] modules = ModuleManager.getInstance(project).getModules();
    List<Pair<Module, PsiFile>> selectModulesList = new ArrayList<Pair<Module, PsiFile>>();
    for (Module module : modules) {
        GradleBuildFile file = GradleBuildFile.get(module);
        if (file != null && !GradleUtil.isLibrary(file)) {
            selectModulesList.add(Pair.create(module, file.getPsiFile()));
        }
    }
    if (selectModulesList.size() > 1) {
        final DialogBuilder builder = new DialogBuilder();
        builder.setTitle("Freeline Reporter");
        builder.resizable(false);
        builder.setCenterPanel(new JLabel("There are multiple application modules, Please select the exact one.", Messages.getInformationIcon(), SwingConstants.CENTER));
        builder.addOkAction().setText("Cancel");
        for (final Pair<Module, PsiFile> pair : selectModulesList) {
            builder.addAction(new AbstractAction(":" + pair.first.getName()) {

                @Override
                public void actionPerformed(ActionEvent e) {
                    builder.getDialogWrapper().close(DialogWrapper.CANCEL_EXIT_CODE);
                    report(project, pair.getSecond());
                }
            });
        }
        if (builder.show() > -1) {
        //return false;
        }
    } else if (selectModulesList.size() == 1) {
        report(project, selectModulesList.get(0).getSecond());
    }
}
Also used : GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) ActionEvent(java.awt.event.ActionEvent) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) DialogBuilder(com.intellij.openapi.ui.DialogBuilder) Pair(com.intellij.openapi.util.Pair)

Example 3 with GradleBuildFile

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

the class GradleSyncTest method sdkCreationForAddons.

// https://code.google.com/p/android/issues/detail?id=185313
@Test
public void sdkCreationForAddons() throws IOException {
    guiTest.importSimpleApplication();
    IdeFrameFixture ideFrame = guiTest.ideFrame();
    Project project = ideFrame.getProject();
    Module appModule = ideFrame.getModule("app");
    GradleBuildFile buildFile = GradleBuildFile.get(appModule);
    execute(new GuiTask() {

        @Override
        protected void executeInEDT() throws Throwable {
            runWriteCommandAction(project, () -> buildFile.setValue(BuildFileKey.COMPILE_SDK_VERSION, "Google Inc.:Google APIs:24"));
        }
    });
    ideFrame.requestProjectSync().waitForGradleProjectSyncToFinish();
    Sdk sdk = ModuleRootManager.getInstance(appModule).getSdk();
    AndroidSdkData sdkData = AndroidSdkData.getSdkData(sdk);
    SdkAdditionalData data = sdk.getSdkAdditionalData();
    assertThat(data).isInstanceOf(AndroidSdkAdditionalData.class);
    AndroidSdkAdditionalData androidSdkData = (AndroidSdkAdditionalData) data;
    IAndroidTarget buildTarget = androidSdkData.getBuildTarget(sdkData);
    // By checking that there are no additional libraries in the SDK, we are verifying that an additional SDK was not created for add-ons.
    assertThat(buildTarget.getAdditionalLibraries()).hasSize(0);
}
Also used : GuiTask(org.fest.swing.edt.GuiTask) Project(com.intellij.openapi.project.Project) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) GradleUtil.getGradleBuildFile(com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData) Test(org.junit.Test)

Example 4 with GradleBuildFile

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

the class KeystoreUtils method getGradleDebugKeystore.

/**
   * Gets a custom debug keystore defined in the build.gradle file for this module
   *
   * @return null if there is no custom debug keystore configured, or if the project is not a Gradle project.
   */
@Nullable
private static File getGradleDebugKeystore(@NotNull AndroidFacet facet) {
    GradleSettingsFile gradleSettingsFile = GradleSettingsFile.get(facet.getModule().getProject());
    if (gradleSettingsFile == null) {
        return null;
    }
    String modulePath = GradleSettingsFile.getModuleGradlePath(facet.getModule());
    if (modulePath == null) {
        return null;
    }
    final GradleBuildFile moduleBuildFile = gradleSettingsFile.getModuleBuildFile(modulePath);
    if (moduleBuildFile == null) {
        return null;
    }
    Iterable<NamedObject> signingConfigs = ApplicationManager.getApplication().runReadAction(new Computable<Iterable<NamedObject>>() {

        @Override
        @SuppressWarnings("unchecked")
        public Iterable<NamedObject> compute() {
            return (Iterable<NamedObject>) moduleBuildFile.getValue(BuildFileKey.SIGNING_CONFIGS);
        }
    });
    if (signingConfigs == null) {
        return null;
    }
    for (NamedObject namedObject : signingConfigs) {
        if (!"debug".equals(namedObject.getName())) {
            continue;
        }
        File debugKey = (File) namedObject.getValue(BuildFileKey.STORE_FILE);
        if (debugKey == null) {
            continue;
        }
        // NOTE: debugKey.getParent() is the current working directory.
        return new File(ModuleUtilCore.getModuleDirPath(facet.getModule()), debugKey.getPath());
    }
    return null;
}
Also used : GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GradleBuildFile

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

the class ManifestPanel method getErrorRemoveHtml.

@NotNull
private static String getErrorRemoveHtml(@NotNull final AndroidFacet facet, @NotNull String message, @NotNull final SourceFilePosition position, @NotNull HtmlLinkManager htmlLinkManager, @Nullable final VirtualFile currentlyOpenFile) {
    /*
    Example Input:
    ERROR Overlay manifest:package attribute declared at AndroidManifest.xml:3:5-49
    value=(com.foo.manifestapplication.debug) has a different value=(com.foo.manifestapplication)
    declared in main manifest at AndroidManifest.xml:5:5-43 Suggestion: remove the overlay
    declaration at AndroidManifest.xml and place it in the build.gradle: flavorName
    { applicationId = "com.foo.manifestapplication.debug" } AndroidManifest.xml (debug)
     */
    HtmlBuilder sb = new HtmlBuilder();
    int start = message.indexOf('{');
    int end = message.indexOf('}', start + 1);
    final String declaration = message.substring(start + 1, end).trim();
    if (!declaration.startsWith("applicationId")) {
        throw new IllegalArgumentException("unexpected remove suggestion format " + message);
    }
    final GradleBuildFile buildFile = GradleBuildFile.get(facet.getModule());
    Runnable link = null;
    if (buildFile != null) {
        final String applicationId = declaration.substring(declaration.indexOf('"') + 1, declaration.lastIndexOf('"'));
        final File manifestOverlayFile = position.getFile().getSourceFile();
        assert manifestOverlayFile != null;
        VirtualFile manifestOverlayVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(manifestOverlayFile);
        assert manifestOverlayVirtualFile != null;
        IdeaSourceProvider sourceProvider = ManifestUtils.findManifestSourceProvider(facet, manifestOverlayVirtualFile);
        assert sourceProvider != null;
        final String name = sourceProvider.getName();
        AndroidModuleModel androidModuleModel = AndroidModuleModel.get(facet.getModule());
        assert androidModuleModel != null;
        final XmlFile manifestOverlayPsiFile = (XmlFile) PsiManager.getInstance(facet.getModule().getProject()).findFile(manifestOverlayVirtualFile);
        assert manifestOverlayPsiFile != null;
        if (androidModuleModel.getBuildTypeNames().contains(name)) {
            final String packageName = MergedManifest.get(facet).getPackage();
            assert packageName != null;
            if (applicationId.startsWith(packageName)) {
                link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {

                    @Override
                    protected void run() throws Throwable {
                        if (currentlyOpenFile != null) {
                            // We mark this action as affecting the currently open file, so the Undo is available in this editor
                            CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
                        }
                        removePackageAttribute(manifestOverlayPsiFile);
                        final String applicationIdSuffix = applicationId.substring(packageName.length());
                        @SuppressWarnings("unchecked") List<NamedObject> buildTypes = (List<NamedObject>) buildFile.getValue(BuildFileKey.BUILD_TYPES);
                        if (buildTypes == null) {
                            buildTypes = new ArrayList<>();
                        }
                        NamedObject buildType = find(buildTypes, name);
                        if (buildType == null) {
                            buildType = new NamedObject(name);
                            buildTypes.add(buildType);
                        }
                        buildType.setValue(BuildFileKey.APPLICATION_ID_SUFFIX, applicationIdSuffix);
                        buildFile.setValue(BuildFileKey.BUILD_TYPES, buildTypes);
                        GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
                    }
                }.execute();
            }
        } else if (androidModuleModel.getProductFlavorNames().contains(name)) {
            link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {

                @Override
                protected void run() throws Throwable {
                    if (currentlyOpenFile != null) {
                        // We mark this action as affecting the currently open file, so the Undo is available in this editor
                        CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
                    }
                    removePackageAttribute(manifestOverlayPsiFile);
                    @SuppressWarnings("unchecked") List<NamedObject> flavors = (List<NamedObject>) buildFile.getValue(BuildFileKey.FLAVORS);
                    assert flavors != null;
                    NamedObject flavor = find(flavors, name);
                    assert flavor != null;
                    flavor.setValue(BuildFileKey.APPLICATION_ID, applicationId);
                    buildFile.setValue(BuildFileKey.FLAVORS, flavors);
                    GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
                }
            }.execute();
        }
    }
    if (link != null) {
        sb.addLink(message.substring(0, end + 1), htmlLinkManager.createRunnableLink(link));
        sb.add(message.substring(end + 1));
    } else {
        sb.add(message);
    }
    return sb.getHtml();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) XmlFile(com.intellij.psi.xml.XmlFile) HtmlBuilder(com.android.utils.HtmlBuilder) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) List(java.util.List) 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) NotNull(org.jetbrains.annotations.NotNull)

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