Search in sources :

Example 1 with ModulesProvider

use of com.intellij.openapi.roots.ui.configuration.ModulesProvider in project intellij-community by JetBrains.

the class JarFromModulesTemplate method doCreateArtifact.

@Nullable
public NewArtifactConfiguration doCreateArtifact(final Module[] modules, final String mainClassName, final String directoryForManifest, final boolean extractLibrariesToJar, final boolean includeTests) {
    VirtualFile manifestFile = null;
    final Project project = myContext.getProject();
    if (mainClassName != null && !mainClassName.isEmpty() || !extractLibrariesToJar) {
        final VirtualFile directory;
        try {
            directory = ApplicationManager.getApplication().runWriteAction(new ThrowableComputable<VirtualFile, IOException>() {

                @Override
                public VirtualFile compute() throws IOException {
                    return VfsUtil.createDirectoryIfMissing(directoryForManifest);
                }
            });
        } catch (IOException e) {
            LOG.info(e);
            Messages.showErrorDialog(project, "Cannot create directory '" + directoryForManifest + "': " + e.getMessage(), CommonBundle.getErrorTitle());
            return null;
        }
        if (directory == null)
            return null;
        manifestFile = ManifestFileUtil.createManifestFile(directory, project);
        if (manifestFile == null) {
            return null;
        }
        ManifestFileUtil.updateManifest(manifestFile, mainClassName, null, true);
    }
    String name = modules.length == 1 ? modules[0].getName() : project.getName();
    final PackagingElementFactory factory = PackagingElementFactory.getInstance();
    final CompositePackagingElement<?> archive = factory.createArchive(ArtifactUtil.suggestArtifactFileName(name) + ".jar");
    OrderEnumerator orderEnumerator = ProjectRootManager.getInstance(project).orderEntries(Arrays.asList(modules));
    final Set<Library> libraries = new THashSet<>();
    if (!includeTests) {
        orderEnumerator = orderEnumerator.productionOnly();
    }
    final ModulesProvider modulesProvider = myContext.getModulesProvider();
    final OrderEnumerator enumerator = orderEnumerator.using(modulesProvider).withoutSdk().runtimeOnly().recursively();
    enumerator.forEachLibrary(new CommonProcessors.CollectProcessor<>(libraries));
    enumerator.forEachModule(module -> {
        if (ProductionModuleOutputElementType.ELEMENT_TYPE.isSuitableModule(modulesProvider, module)) {
            archive.addOrFindChild(factory.createModuleOutput(module));
        }
        if (includeTests && TestModuleOutputElementType.ELEMENT_TYPE.isSuitableModule(modulesProvider, module)) {
            archive.addOrFindChild(factory.createTestModuleOutput(module));
        }
        return true;
    });
    final JarArtifactType jarArtifactType = JarArtifactType.getInstance();
    if (manifestFile != null && !manifestFile.equals(ManifestFileUtil.findManifestFile(archive, myContext, jarArtifactType))) {
        archive.addFirstChild(factory.createFileCopyWithParentDirectories(manifestFile.getPath(), ManifestFileUtil.MANIFEST_DIR_NAME));
    }
    final String artifactName = name + ":jar";
    if (extractLibrariesToJar) {
        addExtractedLibrariesToJar(archive, factory, libraries);
        return new NewArtifactConfiguration(archive, artifactName, jarArtifactType);
    } else {
        final ArtifactRootElement<?> root = factory.createArtifactRootElement();
        List<String> classpath = new ArrayList<>();
        root.addOrFindChild(archive);
        addLibraries(libraries, root, archive, classpath);
        ManifestFileUtil.updateManifest(manifestFile, mainClassName, classpath, true);
        return new NewArtifactConfiguration(root, artifactName, PlainArtifactType.getInstance());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) ThrowableComputable(com.intellij.openapi.util.ThrowableComputable) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Library(com.intellij.openapi.roots.libraries.Library) CommonProcessors(com.intellij.util.CommonProcessors) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ModulesProvider

use of com.intellij.openapi.roots.ui.configuration.ModulesProvider in project intellij-community by JetBrains.

the class ModuleOutputPackagingElementBase method findModule.

@Override
@Nullable
public Module findModule(PackagingElementResolvingContext context) {
    if (myModulePointer != null) {
        final Module module = myModulePointer.getModule();
        final ModulesProvider modulesProvider = context.getModulesProvider();
        if (module != null) {
            if (//optimization
            modulesProvider instanceof DefaultModulesProvider || ArrayUtil.contains(module, modulesProvider.getModules())) {
                return module;
            }
        }
        return modulesProvider.getModule(myModulePointer.getModuleName());
    }
    return null;
}
Also used : ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) DefaultModulesProvider(com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider) DefaultModulesProvider(com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ModulesProvider

use of com.intellij.openapi.roots.ui.configuration.ModulesProvider in project intellij-community by JetBrains.

the class CloudModuleBuilderSourceContribution method detectModuleStructure.

private void detectModuleStructure(Module module, final String contentPath) {
    final Project project = module.getProject();
    final CreateFromSourcesMode mode = new CreateFromSourcesMode() {

        @Override
        public boolean isAvailable(WizardContext context) {
            return true;
        }

        @Override
        public void addSteps(WizardContext context, ModulesProvider modulesProvider, StepSequence sequence, String specific) {
            super.addSteps(context, modulesProvider, sequence, specific);
            myProjectBuilder.setFileToImport(contentPath);
        }
    };
    final WizardContext context = new WizardContext(project);
    final StepSequence stepSequence = mode.getSteps(context, DefaultModulesProvider.createForProject(context.getProject()));
    if (stepSequence == null) {
        return;
    }
    Disposer.register(project, new Disposable() {

        @Override
        public void dispose() {
            for (ModuleWizardStep step : stepSequence.getAllSteps()) {
                step.disposeUIResources();
            }
        }
    });
    ProgressManager.getInstance().run(new Task.Backgroundable(project, CloudBundle.getText("detect.module.structure", getCloudType().getPresentableName()), false) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            for (ModuleWizardStep step = ContainerUtil.getFirstItem(stepSequence.getSelectedSteps()); step != null; step = stepSequence.getNextStep(step)) {
                if (step instanceof AbstractStepWithProgress<?>) {
                    ((AbstractStepWithProgress) step).performStep();
                } else {
                    step.updateDataModel();
                }
            }
            CloudAccountSelectionEditor.unsetAccountOnContext(context, getCloudType());
        }

        @Override
        public boolean shouldStartInBackground() {
            return false;
        }

        @Override
        public void onSuccess() {
            ProjectBuilder moduleBuilder = mode.getModuleBuilder();
            if (moduleBuilder == null) {
                return;
            }
            moduleBuilder.commit(project);
            getNotifier().showMessage(CloudBundle.getText("cloud.support.added", getCloudType().getPresentableName()), MessageType.INFO);
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) DefaultModulesProvider(com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider) Task(com.intellij.openapi.progress.Task) StepSequence(com.intellij.ide.util.newProjectWizard.StepSequence) CreateFromSourcesMode(com.intellij.ide.util.newProjectWizard.modes.CreateFromSourcesMode) Project(com.intellij.openapi.project.Project) ModuleWizardStep(com.intellij.ide.util.projectWizard.ModuleWizardStep) ProjectBuilder(com.intellij.ide.util.projectWizard.ProjectBuilder) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) WizardContext(com.intellij.ide.util.projectWizard.WizardContext)

Example 4 with ModulesProvider

use of com.intellij.openapi.roots.ui.configuration.ModulesProvider in project intellij-community by JetBrains.

the class ProjectFromSourcesBuilderImpl method commit.

@Override
public List<Module> commit(@NotNull final Project project, final ModifiableModuleModel model, final ModulesProvider modulesProvider) {
    final boolean fromProjectStructure = model != null;
    ModifiableModelsProvider modelsProvider = new IdeaModifiableModelsProvider();
    final LibraryTable.ModifiableModel projectLibraryTable = modelsProvider.getLibraryTableModifiableModel(project);
    final Map<LibraryDescriptor, Library> projectLibs = new HashMap<>();
    final List<Module> result = new ArrayList<>();
    try {
        WriteAction.run(() -> {
            // create project-level libraries
            for (ProjectDescriptor projectDescriptor : getSelectedDescriptors()) {
                for (LibraryDescriptor lib : projectDescriptor.getLibraries()) {
                    final Collection<File> files = lib.getJars();
                    final Library projectLib = projectLibraryTable.createLibrary(lib.getName());
                    final Library.ModifiableModel libraryModel = projectLib.getModifiableModel();
                    for (File file : files) {
                        libraryModel.addRoot(VfsUtil.getUrlForLibraryRoot(file), OrderRootType.CLASSES);
                    }
                    libraryModel.commit();
                    projectLibs.put(lib, projectLib);
                }
            }
            if (!fromProjectStructure) {
                projectLibraryTable.commit();
            }
        });
    } catch (Exception e) {
        LOG.info(e);
        Messages.showErrorDialog(IdeBundle.message("error.adding.module.to.project", e.getMessage()), IdeBundle.message("title.add.module"));
    }
    final Map<ModuleDescriptor, Module> descriptorToModuleMap = new HashMap<>();
    try {
        WriteAction.run(() -> {
            final ModifiableModuleModel moduleModel = fromProjectStructure ? model : ModuleManager.getInstance(project).getModifiableModel();
            for (ProjectDescriptor descriptor : getSelectedDescriptors()) {
                for (final ModuleDescriptor moduleDescriptor : descriptor.getModules()) {
                    final Module module;
                    if (moduleDescriptor.isReuseExistingElement()) {
                        final ExistingModuleLoader moduleLoader = ExistingModuleLoader.setUpLoader(FileUtil.toSystemIndependentName(moduleDescriptor.computeModuleFilePath()));
                        module = moduleLoader.createModule(moduleModel);
                    } else {
                        module = createModule(descriptor, moduleDescriptor, projectLibs, moduleModel);
                    }
                    result.add(module);
                    descriptorToModuleMap.put(moduleDescriptor, module);
                }
            }
            if (!fromProjectStructure) {
                moduleModel.commit();
            }
        });
    } catch (Exception e) {
        LOG.info(e);
        Messages.showErrorDialog(IdeBundle.message("error.adding.module.to.project", e.getMessage()), IdeBundle.message("title.add.module"));
    }
    // setup dependencies between modules
    try {
        WriteAction.run(() -> {
            for (ProjectDescriptor data : getSelectedDescriptors()) {
                for (final ModuleDescriptor descriptor : data.getModules()) {
                    final Module module = descriptorToModuleMap.get(descriptor);
                    if (module == null) {
                        continue;
                    }
                    final Set<ModuleDescriptor> deps = descriptor.getDependencies();
                    if (deps.size() == 0) {
                        continue;
                    }
                    final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
                    for (ModuleDescriptor dependentDescriptor : deps) {
                        final Module dependentModule = descriptorToModuleMap.get(dependentDescriptor);
                        if (dependentModule != null) {
                            rootModel.addModuleOrderEntry(dependentModule);
                        }
                    }
                    rootModel.commit();
                }
            }
        });
    } catch (Exception e) {
        LOG.info(e);
        Messages.showErrorDialog(IdeBundle.message("error.adding.module.to.project", e.getMessage()), IdeBundle.message("title.add.module"));
    }
    WriteAction.run(() -> {
        ModulesProvider updatedModulesProvider = fromProjectStructure ? modulesProvider : new DefaultModulesProvider(project);
        for (ProjectConfigurationUpdater updater : myUpdaters) {
            updater.updateProject(project, modelsProvider, updatedModulesProvider);
        }
    });
    return result;
}
Also used : ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) DefaultModulesProvider(com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider) DefaultModulesProvider(com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider) InvalidDataException(com.intellij.openapi.util.InvalidDataException) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) ConfigurationException(com.intellij.openapi.options.ConfigurationException) ExistingModuleLoader(com.intellij.ide.util.projectWizard.ExistingModuleLoader) ModuleDescriptor(com.intellij.ide.util.importProject.ModuleDescriptor) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ProjectDescriptor(com.intellij.ide.util.importProject.ProjectDescriptor) Library(com.intellij.openapi.roots.libraries.Library) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) LibraryDescriptor(com.intellij.ide.util.importProject.LibraryDescriptor)

Example 5 with ModulesProvider

use of com.intellij.openapi.roots.ui.configuration.ModulesProvider in project intellij-community by JetBrains.

the class LibraryPackagingElement method findLibrary.

@Nullable
public Library findLibrary(@NotNull PackagingElementResolvingContext context) {
    if (myModuleName == null) {
        return context.findLibrary(myLevel, myLibraryName);
    }
    final ModulesProvider modulesProvider = context.getModulesProvider();
    final Module module = modulesProvider.getModule(myModuleName);
    if (module != null) {
        for (OrderEntry entry : modulesProvider.getRootModel(module).getOrderEntries()) {
            if (entry instanceof LibraryOrderEntry) {
                final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) entry;
                if (libraryEntry.isModuleLevel()) {
                    final String libraryName = libraryEntry.getLibraryName();
                    if (libraryName != null && libraryName.equals(myLibraryName)) {
                        return libraryEntry.getLibrary();
                    }
                }
            }
        }
    }
    return null;
}
Also used : ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ModulesProvider (com.intellij.openapi.roots.ui.configuration.ModulesProvider)7 Module (com.intellij.openapi.module.Module)4 Nullable (org.jetbrains.annotations.Nullable)4 Project (com.intellij.openapi.project.Project)3 Library (com.intellij.openapi.roots.libraries.Library)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 IOException (java.io.IOException)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)2 DefaultModulesProvider (com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider)2 File (java.io.File)2 JDOMException (org.jdom.JDOMException)2 LibraryDescriptor (com.intellij.ide.util.importProject.LibraryDescriptor)1 ModuleDescriptor (com.intellij.ide.util.importProject.ModuleDescriptor)1 ProjectDescriptor (com.intellij.ide.util.importProject.ProjectDescriptor)1 StepSequence (com.intellij.ide.util.newProjectWizard.StepSequence)1 CreateFromSourcesMode (com.intellij.ide.util.newProjectWizard.modes.CreateFromSourcesMode)1 ExistingModuleLoader (com.intellij.ide.util.projectWizard.ExistingModuleLoader)1 ModuleWizardStep (com.intellij.ide.util.projectWizard.ModuleWizardStep)1