Search in sources :

Example 26 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project android by JetBrains.

the class PostSyncProjectSetup method setUpProject.

/**
   * Invoked after a project has been synced with Gradle.
   */
public void setUpProject(@NotNull Request request, @Nullable ProgressIndicator progressIndicator) {
    // Force a refresh after a sync.
    // https://code.google.com/p/android/issues/detail?id=229633
    ApplicationManager.getApplication().runWriteAction(() -> ProjectRootManagerEx.getInstanceEx(myProject).makeRootsChange(EmptyRunnable.INSTANCE, false, true));
    boolean syncFailed = mySyncState.lastSyncFailedOrHasIssues();
    if (syncFailed && request.isUsingCachedGradleModels()) {
        onCachedModelsSetupFailure(request);
        return;
    }
    myDependencySetupErrors.reportErrors();
    myVersionCompatibilityChecker.checkAndReportComponentIncompatibilities(myProject);
    CommonModuleValidator moduleValidator = myModuleValidatorFactory.create(myProject);
    ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    for (Module module : moduleManager.getModules()) {
        moduleValidator.validate(module);
    }
    moduleValidator.fixAndReportFoundIssues();
    if (syncFailed) {
        myProjectSetup.setUpProject(progressIndicator, true);
        // Notify "sync end" event first, to register the timestamp. Otherwise the cache (GradleProjectSyncData) will store the date of the
        // previous sync, and not the one from the sync that just ended.
        mySyncState.syncEnded();
        return;
    }
    if (myPluginVersionUpgrade.checkAndPerformUpgrade()) {
        // Plugin version was upgraded and a sync was triggered.
        return;
    }
    new ProjectStructureUsageTracker(myProject).trackProjectStructure();
    DisposedModules.getInstance(myProject).deleteImlFilesForDisposedModules();
    removeAllModuleCompiledArtifacts(myProject);
    AndroidGradleProjectComponent.getInstance(myProject).checkForSupportedModules();
    findAndShowVariantConflicts();
    myProjectSetup.setUpProject(progressIndicator, false);
    // For Android Studio, use "Gradle-Aware Make" to run JUnit tests.
    // For IDEA, use regular "Make".
    boolean androidStudio = myIdeInfo.isAndroidStudio();
    String taskName = androidStudio ? MakeBeforeRunTaskProvider.TASK_NAME : ExecutionBundle.message("before.launch.compile.step");
    setMakeStepInJunitRunConfigurations(taskName);
    notifySyncFinished(request);
    attemptToGenerateSources(request);
    TemplateManager.getInstance().refreshDynamicTemplateMenu(myProject);
    myModuleSetup.setUpModules(null);
}
Also used : CommonModuleValidator(com.android.tools.idea.gradle.project.sync.validation.common.CommonModuleValidator) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Example 27 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project android by JetBrains.

the class ProjectProfileSelectionDialog method createProjectStructureTree.

private void createProjectStructureTree() {
    CheckboxTree.CheckboxTreeCellRenderer renderer = new CheckboxTree.CheckboxTreeCellRenderer() {

        @Override
        public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            if (value instanceof DefaultMutableTreeNode) {
                Object data = ((DefaultMutableTreeNode) value).getUserObject();
                ColoredTreeCellRenderer textRenderer = getTextRenderer();
                if (data instanceof ModuleTreeElement) {
                    ModuleTreeElement moduleElement = (ModuleTreeElement) data;
                    textRenderer.append(moduleElement.myModule.getName());
                    if (!moduleElement.myConflicts.isEmpty()) {
                        boolean allResolved = true;
                        for (Conflict conflict : moduleElement.myConflicts) {
                            if (!conflict.isResolved()) {
                                allResolved = false;
                                break;
                            }
                        }
                        SimpleTextAttributes attributes = allResolved ? UNRESOLVED_ATTRIBUTES : SimpleTextAttributes.GRAY_ATTRIBUTES;
                        textRenderer.append(" ");
                        textRenderer.append(myConflicts.size() == 1 ? "[Conflict]" : "[Conflicts]", attributes);
                    }
                    textRenderer.setIcon(AllIcons.Actions.Module);
                } else if (data instanceof String) {
                    textRenderer.append((String) data, SimpleTextAttributes.REGULAR_ITALIC_ATTRIBUTES);
                    textRenderer.setIcon(AndroidIcons.Variant);
                } else if (data instanceof DependencyTreeElement) {
                    DependencyTreeElement dependency = (DependencyTreeElement) data;
                    textRenderer.append(dependency.myModule.getName());
                    if (!StringUtil.isEmpty(dependency.myVariant)) {
                        textRenderer.append(" (" + dependency.myVariant + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
                    }
                    Icon icon = dependency.myConflict != null ? AllIcons.RunConfigurations.TestFailed : AllIcons.RunConfigurations.TestPassed;
                    textRenderer.setIcon(icon);
                }
            }
        }
    };
    CheckedTreeNode rootNode = new FilterAwareCheckedTreeNode(null);
    ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    Module[] modules = moduleManager.getModules();
    Arrays.sort(modules, ModulesAlphaComparator.INSTANCE);
    Map<String, Module> modulesByGradlePath = Maps.newHashMap();
    for (Module module : modules) {
        String gradlePath = GradleUtil.getGradlePath(module);
        if (StringUtil.isEmpty(gradlePath)) {
            // We always want to include it, therefore we don't give users a chance to uncheck it in the "Project Structure" pane.
            continue;
        }
        modulesByGradlePath.put(gradlePath, module);
        ModuleTreeElement moduleElement = new ModuleTreeElement(module);
        CheckedTreeNode moduleNode = new FilterAwareCheckedTreeNode(moduleElement);
        rootNode.add(moduleNode);
        AndroidModuleModel androidModel = AndroidModuleModel.get(module);
        if (androidModel == null) {
            continue;
        }
        Multimap<String, DependencyTreeElement> dependenciesByVariant = HashMultimap.create();
        for (Variant variant : androidModel.getAndroidProject().getVariants()) {
            for (AndroidLibrary library : getDirectLibraryDependencies(variant, androidModel)) {
                gradlePath = library.getProject();
                if (gradlePath == null) {
                    continue;
                }
                Module dependency = modulesByGradlePath.get(gradlePath);
                if (dependency == null) {
                    dependency = GradleUtil.findModuleByGradlePath(myProject, gradlePath);
                }
                if (dependency == null) {
                    continue;
                }
                Conflict conflict = getConflict(dependency);
                modulesByGradlePath.put(gradlePath, dependency);
                DependencyTreeElement dependencyElement = new DependencyTreeElement(dependency, gradlePath, library.getProjectVariant(), conflict);
                dependenciesByVariant.put(variant.getName(), dependencyElement);
            }
        }
        List<String> variantNames = Lists.newArrayList(dependenciesByVariant.keySet());
        Collections.sort(variantNames);
        List<String> consolidatedVariants = Lists.newArrayList();
        List<String> variantsToSkip = Lists.newArrayList();
        int variantCount = variantNames.size();
        for (int i = 0; i < variantCount; i++) {
            String variant1 = variantNames.get(i);
            if (variantsToSkip.contains(variant1)) {
                continue;
            }
            Collection<DependencyTreeElement> set1 = dependenciesByVariant.get(variant1);
            for (int j = i + 1; j < variantCount; j++) {
                String variant2 = variantNames.get(j);
                Collection<DependencyTreeElement> set2 = dependenciesByVariant.get(variant2);
                if (set1.equals(set2)) {
                    variantsToSkip.add(variant2);
                    if (!consolidatedVariants.contains(variant1)) {
                        consolidatedVariants.add(variant1);
                    }
                    consolidatedVariants.add(variant2);
                }
            }
            String variantName = variant1;
            if (!consolidatedVariants.isEmpty()) {
                variantName = Joiner.on(", ").join(consolidatedVariants);
            }
            DefaultMutableTreeNode variantNode = new DefaultMutableTreeNode(variantName);
            moduleNode.add(variantNode);
            List<DependencyTreeElement> dependencyElements = Lists.newArrayList(set1);
            Collections.sort(dependencyElements);
            for (DependencyTreeElement dependencyElement : dependencyElements) {
                if (dependencyElement.myConflict != null) {
                    moduleElement.addConflict(dependencyElement.myConflict);
                }
                variantNode.add(new DefaultMutableTreeNode(dependencyElement));
            }
            consolidatedVariants.clear();
        }
    }
    myProjectStructureTree = new CheckboxTreeView(renderer, rootNode) {

        @Override
        protected void onNodeStateChanged(@NotNull CheckedTreeNode node) {
            Module module = null;
            Object data = node.getUserObject();
            if (data instanceof ModuleTreeElement) {
                module = ((ModuleTreeElement) data).myModule;
            }
            if (module == null) {
                return;
            }
            boolean updated = false;
            Enumeration variantNodes = myConflictTree.myRoot.children();
            while (variantNodes.hasMoreElements()) {
                Object child = variantNodes.nextElement();
                if (!(child instanceof CheckedTreeNode)) {
                    continue;
                }
                CheckedTreeNode variantNode = (CheckedTreeNode) child;
                Enumeration moduleNodes = variantNode.children();
                while (moduleNodes.hasMoreElements()) {
                    child = moduleNodes.nextElement();
                    if (!(child instanceof CheckedTreeNode)) {
                        continue;
                    }
                    CheckedTreeNode moduleNode = (CheckedTreeNode) child;
                    data = moduleNode.getUserObject();
                    if (!(data instanceof Conflict.AffectedModule)) {
                        continue;
                    }
                    Conflict.AffectedModule affected = (Conflict.AffectedModule) data;
                    boolean checked = node.isChecked();
                    if (module.equals(affected.getTarget()) && moduleNode.isChecked() != checked) {
                        affected.setSelected(checked);
                        moduleNode.setChecked(checked);
                        updated = true;
                    }
                }
            }
            if (updated) {
                repaintAll();
            }
        }
    };
    myProjectStructureTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    myProjectStructureTree.setRootVisible(false);
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ModuleManager(com.intellij.openapi.module.ModuleManager) Variant(com.android.builder.model.Variant) Conflict(com.android.tools.idea.gradle.variant.conflict.Conflict) AndroidLibrary(com.android.builder.model.AndroidLibrary) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module)

Example 28 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project android by JetBrains.

the class BuildProjectHyperlink method execute.

@Override
protected void execute(@NotNull Project project) {
    if (AndroidProjectInfo.getInstance(project).requiresAndroidModel() && isDirectGradleInvocationEnabled(project)) {
        ModuleManager moduleManager = ModuleManager.getInstance(project);
        GradleBuildInvoker.getInstance(project).compileJava(moduleManager.getModules(), GradleBuildInvoker.TestCompileType.NONE);
        return;
    }
    CompilerManager.getInstance(project).make(null);
}
Also used : ModuleManager(com.intellij.openapi.module.ModuleManager)

Example 29 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project intellij-community by JetBrains.

the class DirtyScopeHolder method compilerActivityFinished.

void compilerActivityFinished() {
    final List<Module> compiledModules = ReadAction.compute(() -> {
        final Project project = myService.getProject();
        if (project.isDisposed()) {
            return null;
        }
        final ModuleManager moduleManager = ModuleManager.getInstance(myService.getProject());
        return myCompilationAffectedModules.stream().map(moduleManager::findModuleByName).collect(Collectors.toList());
    });
    compilationFinished(() -> {
        if (compiledModules == null)
            return;
        myVFSChangedModules.removeAll(compiledModules);
    });
}
Also used : Project(com.intellij.openapi.project.Project) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Example 30 with ModuleManager

use of com.intellij.openapi.module.ModuleManager in project android by JetBrains.

the class AndroidCompileUtil method removeGenModule.

private static void removeGenModule(@NotNull final ModifiableRootModel model, @NotNull Ref<Boolean> modelChangedFlag) {
    final String genModuleName = getGenModuleName(model.getModule());
    final Project project = model.getProject();
    final ModuleManager moduleManager = ModuleManager.getInstance(project);
    final Module genModule = moduleManager.findModuleByName(genModuleName);
    if (genModule == null) {
        return;
    }
    for (OrderEntry entry : model.getOrderEntries()) {
        if (entry instanceof ModuleOrderEntry && genModuleName.equals(((ModuleOrderEntry) entry).getModuleName())) {
            model.removeOrderEntry(entry);
            modelChangedFlag.set(true);
        }
    }
    final VirtualFile moduleFile = genModule.getModuleFile();
    moduleManager.disposeModule(genModule);
    if (moduleFile != null) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {

            @Override
            public void run() {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            moduleFile.delete(project);
                        } catch (IOException e) {
                            LOG.error(e);
                        }
                    }
                });
            }
        });
    }
}
Also used : Project(com.intellij.openapi.project.Project) IOException(java.io.IOException) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Aggregations

ModuleManager (com.intellij.openapi.module.ModuleManager)51 Module (com.intellij.openapi.module.Module)40 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 Project (com.intellij.openapi.project.Project)10 NotNull (org.jetbrains.annotations.NotNull)8 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)7 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)7 File (java.io.File)7 GradleFacet (com.android.tools.idea.gradle.project.facet.gradle.GradleFacet)3 IOException (java.io.IOException)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 AndroidLibrary (com.android.builder.model.AndroidLibrary)2 Variant (com.android.builder.model.Variant)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 BuildMode (com.android.tools.idea.gradle.util.BuildMode)2 AccessToken (com.intellij.openapi.application.AccessToken)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Sdk (com.intellij.openapi.projectRoots.Sdk)2 ContentEntry (com.intellij.openapi.roots.ContentEntry)2 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)2