Search in sources :

Example 6 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project intellij-community by JetBrains.

the class RootsChangedTest method testModuleJdkEditing.

public void testModuleJdkEditing() throws Exception {
    final File tempDirectory = createTempDirectory();
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Module moduleA = createModule("a.iml");
        final Module moduleB = createModule("b.iml");
        assertEventsCount(2);
        final Sdk jdk;
        try {
            jdk = (Sdk) IdeaTestUtil.getMockJdk17().clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        ProjectJdkTable.getInstance().addJdk(jdk);
        assertEventsCount(0);
        final ModifiableRootModel rootModelA = ModuleRootManager.getInstance(moduleA).getModifiableModel();
        final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
        rootModelA.setSdk(jdk);
        rootModelB.setSdk(jdk);
        ModifiableRootModel[] rootModels = { rootModelA, rootModelB };
        ModifiableModelCommitter.multiCommit(rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel());
        assertEventsCount(1);
        final SdkModificator sdkModificator = jdk.getSdkModificator();
        sdkModificator.addRoot(getVirtualFile(tempDirectory), OrderRootType.CLASSES);
        sdkModificator.commitChanges();
        assertEventsCount(1);
        ProjectJdkTable.getInstance().removeJdk(jdk);
        assertEventsCount(1);
    });
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 7 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project intellij-community by JetBrains.

the class RootsChangedTest method testInheritedJdkEditing.

public void testInheritedJdkEditing() throws Exception {
    final File tempDirectory = createTempDirectory();
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Module moduleA = createModule("a.iml");
        final Module moduleB = createModule("b.iml");
        assertEventsCount(2);
        final Sdk jdk;
        final Sdk jdkBBB;
        try {
            jdk = (Sdk) IdeaTestUtil.getMockJdk17("AAA").clone();
            ProjectJdkTable.getInstance().addJdk(jdk);
            assertEventsCount(0);
            jdkBBB = (Sdk) IdeaTestUtil.getMockJdk17("BBB").clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        ProjectJdkTable.getInstance().addJdk(jdk);
        assertEventsCount(0);
        ProjectRootManager.getInstance(myProject).setProjectSdk(jdkBBB);
        assertEventsCount(0);
        final ModifiableRootModel rootModelA = ModuleRootManager.getInstance(moduleA).getModifiableModel();
        final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
        rootModelA.inheritSdk();
        rootModelB.inheritSdk();
        ModifiableRootModel[] rootModels = { rootModelA, rootModelB };
        if (rootModels.length > 0) {
            ModifiableModelCommitter.multiCommit(rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel());
        }
        assertEventsCount(1);
        ProjectRootManager.getInstance(myProject).setProjectSdk(jdk);
        assertEventsCount(1);
        final SdkModificator sdkModificator = jdk.getSdkModificator();
        sdkModificator.addRoot(getVirtualFile(tempDirectory), OrderRootType.CLASSES);
        sdkModificator.commitChanges();
        assertEventsCount(1);
        ProjectJdkTable.getInstance().removeJdk(jdk);
        assertEventsCount(1);
    });
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 8 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project intellij-community by JetBrains.

the class BytecodeAnalysisIntegrationTest method setUpExternalUpAnnotations.

private void setUpExternalUpAnnotations() {
    String annotationsPath = PathManagerEx.getTestDataPath() + "/codeInspection/bytecodeAnalysis/annotations";
    VirtualFile annotationsDir = LocalFileSystem.getInstance().refreshAndFindFileByPath(annotationsPath);
    assertNotNull(annotationsDir);
    ModuleRootModificationUtil.updateModel(myModule, new AsynchConsumer<ModifiableRootModel>() {

        @Override
        public void finished() {
        }

        @Override
        public void consume(ModifiableRootModel modifiableRootModel) {
            LibraryTable libraryTable = modifiableRootModel.getModuleLibraryTable();
            Library[] libs = libraryTable.getLibraries();
            for (Library library : libs) {
                Library.ModifiableModel libraryModel = library.getModifiableModel();
                libraryModel.addRoot(annotationsDir, AnnotationOrderRootType.getInstance());
                libraryModel.commit();
            }
            Sdk sdk = modifiableRootModel.getSdk();
            if (sdk != null) {
                Sdk clone;
                try {
                    clone = (Sdk) sdk.clone();
                } catch (CloneNotSupportedException e) {
                    throw new RuntimeException(e);
                }
                SdkModificator sdkModificator = clone.getSdkModificator();
                sdkModificator.addRoot(annotationsDir, AnnotationOrderRootType.getInstance());
                sdkModificator.commitChanges();
                modifiableRootModel.setSdk(clone);
            }
        }
    });
    VfsUtilCore.visitChildrenRecursively(annotationsDir, new VirtualFileVisitor() {
    });
    annotationsDir.refresh(false, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 9 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class AndroidSdks method setUpSdk.

public void setUpSdk(@NotNull Sdk androidSdk, @NotNull IAndroidTarget target, @NotNull String sdkName, @NotNull Collection<Sdk> allSdks, @Nullable Sdk jdk, boolean addRoots) {
    SdkModificator sdkModificator = androidSdk.getSdkModificator();
    setUpSdk(androidSdk, sdkModificator, target, sdkName, allSdks, jdk, addRoots);
    sdkModificator.commitChanges();
}
Also used : SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 10 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class AndroidSdks method replaceLibraries.

@VisibleForTesting
void replaceLibraries(@NotNull Sdk sdk, @NotNull VirtualFile[] libraries) {
    SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.removeRoots(CLASSES);
    for (VirtualFile library : libraries) {
        sdkModificator.addRoot(library, CLASSES);
    }
    sdkModificator.commitChanges();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)51 Sdk (com.intellij.openapi.projectRoots.Sdk)32 VirtualFile (com.intellij.openapi.vfs.VirtualFile)30 File (java.io.File)8 Module (com.intellij.openapi.module.Module)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)6 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)5 NotNull (org.jetbrains.annotations.NotNull)5 ProjectJdkTable (com.intellij.openapi.projectRoots.ProjectJdkTable)4 Library (com.intellij.openapi.roots.libraries.Library)4 Nullable (org.jetbrains.annotations.Nullable)4 JSTestOptions (com.intellij.lang.javascript.JSTestOptions)3 Project (com.intellij.openapi.project.Project)3 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)3 FirstRunWizardMode (com.android.tools.idea.welcome.config.FirstRunWizardMode)2 FlexSdkType2 (com.intellij.lang.javascript.flex.sdk.FlexSdkType2)2 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)2 IOException (java.io.IOException)2 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)2 Contract (org.jetbrains.annotations.Contract)2