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());
}
}
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;
}
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);
}
});
}
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;
}
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;
}
Aggregations