mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
create module library from files: simplified, test added
This commit is contained in:
+29
-19
@@ -34,6 +34,7 @@ import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.*;
|
||||
@@ -78,12 +79,15 @@ public class CreateModuleLibraryChooser implements ClasspathElementChooser<Libra
|
||||
}
|
||||
}
|
||||
|
||||
private Library createLibraryFromRoots(List<OrderRoot> roots, @Nullable final LibraryType libraryType) {
|
||||
private static Library createLibraryFromRoots(@NotNull List<OrderRoot> roots,
|
||||
@Nullable final LibraryType libraryType,
|
||||
@NotNull LibraryTable.ModifiableModel moduleLibrariesModel,
|
||||
@Nullable Function<LibraryType, LibraryProperties> defaultPropertiesFactory) {
|
||||
final PersistentLibraryKind kind = libraryType == null ? null : libraryType.getKind();
|
||||
final Library library = myModuleLibrariesModel.createLibrary(null, kind);
|
||||
final Library library = moduleLibrariesModel.createLibrary(null, kind);
|
||||
final LibraryEx.ModifiableModelEx libModel = (LibraryEx.ModifiableModelEx)library.getModifiableModel();
|
||||
if (myDefaultPropertiesFactory != null) {
|
||||
libModel.setProperties(myDefaultPropertiesFactory.fun(libraryType));
|
||||
if (defaultPropertiesFactory != null) {
|
||||
libModel.setProperties(defaultPropertiesFactory.fun(libraryType));
|
||||
}
|
||||
for (OrderRoot root : roots) {
|
||||
if (root.isJarDirectory()) {
|
||||
@@ -97,30 +101,21 @@ public class CreateModuleLibraryChooser implements ClasspathElementChooser<Libra
|
||||
return library;
|
||||
}
|
||||
|
||||
private List<OrderRoot> filterAlreadyAdded(final List<OrderRoot> roots) {
|
||||
private static List<OrderRoot> filterAlreadyAdded(final List<OrderRoot> roots, LibraryTable.ModifiableModel moduleLibrariesModel) {
|
||||
if (roots == null || roots.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
final List<OrderRoot> result = new ArrayList<OrderRoot>();
|
||||
final Library[] libraries = myModuleLibrariesModel.getLibraries();
|
||||
final Library[] libraries = moduleLibrariesModel.getLibraries();
|
||||
for (OrderRoot root : roots) {
|
||||
if (!isIncluded(root, libraries)) {
|
||||
if (!Arrays.stream(libraries).anyMatch(library -> ArrayUtil.contains(root.getFile(), library.getFiles(root.getType())))) {
|
||||
result.add(root);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean isIncluded(OrderRoot root, Library[] libraries) {
|
||||
for (Library library : libraries) {
|
||||
if (ArrayUtil.contains(root.getFile(), library.getFiles(root.getType()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Library> chooseElements() {
|
||||
@@ -179,7 +174,21 @@ public class CreateModuleLibraryChooser implements ClasspathElementChooser<Libra
|
||||
}
|
||||
List<OrderRoot> chosenRoots = RootDetectionUtil.detectRoots(Arrays.asList(files), myParentComponent, project, rootsComponentDescriptor);
|
||||
|
||||
final List<OrderRoot> roots = filterAlreadyAdded(chosenRoots);
|
||||
return createLibrariesFromRoots(chosenRoots, libraryType, myModuleLibrariesModel, myDefaultPropertiesFactory);
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
@NotNull
|
||||
public static List<Library> createLibrariesFromRoots(List<OrderRoot> chosenRoots, LibraryTable.ModifiableModel moduleLibrariesModel) {
|
||||
return createLibrariesFromRoots(chosenRoots, null, moduleLibrariesModel, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<Library> createLibrariesFromRoots(@NotNull List<OrderRoot> chosenRoots,
|
||||
@Nullable LibraryType libraryType,
|
||||
@NotNull LibraryTable.ModifiableModel moduleLibrariesModel,
|
||||
@Nullable Function<LibraryType, LibraryProperties> defaultPropertiesFactory) {
|
||||
final List<OrderRoot> roots = filterAlreadyAdded(chosenRoots, moduleLibrariesModel);
|
||||
if (roots.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -191,11 +200,12 @@ public class CreateModuleLibraryChooser implements ClasspathElementChooser<Libra
|
||||
}
|
||||
if (onlyClasses) {
|
||||
for (OrderRoot root : roots) {
|
||||
addedLibraries.add(createLibraryFromRoots(Collections.singletonList(root), libraryType));
|
||||
addedLibraries.add(createLibraryFromRoots(Collections.singletonList(root), libraryType, moduleLibrariesModel,
|
||||
defaultPropertiesFactory));
|
||||
}
|
||||
}
|
||||
else {
|
||||
addedLibraries.add(createLibraryFromRoots(roots, libraryType));
|
||||
addedLibraries.add(createLibraryFromRoots(roots, libraryType, moduleLibrariesModel, defaultPropertiesFactory));
|
||||
}
|
||||
return addedLibraries;
|
||||
}
|
||||
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.roots.libraries;
|
||||
|
||||
import com.intellij.openapi.roots.ModifiableRootModel;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.roots.OrderRootType;
|
||||
import com.intellij.openapi.roots.libraries.Library;
|
||||
import com.intellij.openapi.roots.libraries.LibraryTable;
|
||||
import com.intellij.openapi.roots.libraries.ui.OrderRoot;
|
||||
import com.intellij.openapi.roots.ui.configuration.classpath.CreateModuleLibraryChooser;
|
||||
import com.intellij.roots.ModuleRootManagerTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class CreateModuleLibraryFromFilesTest extends ModuleRootManagerTestCase {
|
||||
private LibraryTable.ModifiableModel myModifiableModel;
|
||||
private ModifiableRootModel myModifiableRootModel;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myModifiableRootModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
|
||||
myModifiableModel = myModifiableRootModel.getModuleLibraryTable().getModifiableModel();
|
||||
}
|
||||
|
||||
public void testSingleJar() throws Exception {
|
||||
Library library = assertOneElement(createLibraries(new OrderRoot(getJDomJar(), OrderRootType.CLASSES)));
|
||||
assertNull(library.getName());
|
||||
assertSameElements(library.getFiles(OrderRootType.CLASSES), getJDomJar());
|
||||
assertEmpty(library.getFiles(OrderRootType.SOURCES));
|
||||
}
|
||||
|
||||
public void testTwoJars() throws Exception {
|
||||
List<Library> libraries = createLibraries(new OrderRoot(getJDomJar(), OrderRootType.CLASSES),
|
||||
new OrderRoot(getAsmJar(), OrderRootType.CLASSES));
|
||||
assertEquals(2, libraries.size());
|
||||
assertNull(libraries.get(0).getName());
|
||||
assertSameElements(libraries.get(0).getFiles(OrderRootType.CLASSES), getJDomJar());
|
||||
assertNull(libraries.get(1).getName());
|
||||
assertSameElements(libraries.get(1).getFiles(OrderRootType.CLASSES), getAsmJar());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<Library> createLibraries(OrderRoot... roots) {
|
||||
return CreateModuleLibraryChooser.createLibrariesFromRoots(Arrays.asList(roots), myModifiableModel);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
myModifiableRootModel.dispose();
|
||||
super.tearDown();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user