Search in sources :

Example 51 with SdkModificator

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

the class PythonMockSdk method create.

public static Sdk create(final String version, @NotNull final VirtualFile... additionalRoots) {
    final String mock_path = PythonTestUtil.getTestDataPath() + "/MockSdk" + version + "/";
    String sdkHome = new File(mock_path, "bin/python" + version).getPath();
    SdkType sdkType = PythonSdkType.getInstance();
    final Sdk sdk = new ProjectJdkImpl(MOCK_SDK_NAME + " " + version, sdkType) {

        @Override
        public String getVersionString() {
            return "Python " + version + " Mock SDK";
        }
    };
    final SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.setHomePath(sdkHome);
    File libPath = new File(mock_path, "Lib");
    if (libPath.exists()) {
        sdkModificator.addRoot(LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libPath), OrderRootType.CLASSES);
    }
    sdkModificator.addRoot(PyUserSkeletonsUtil.getUserSkeletonsDirectory(), OrderRootType.CLASSES);
    final LanguageLevel level = LanguageLevel.fromPythonVersion(version);
    final VirtualFile typeShedDir = PyTypeShed.INSTANCE.getDirectory();
    PyTypeShed.INSTANCE.findRootsForLanguageLevel(level).forEach(path -> {
        final VirtualFile file = typeShedDir.findFileByRelativePath(path);
        if (file != null) {
            sdkModificator.addRoot(file, OrderRootType.CLASSES);
        }
    });
    String mock_stubs_path = mock_path + PythonSdkType.SKELETON_DIR_NAME;
    sdkModificator.addRoot(LocalFileSystem.getInstance().refreshAndFindFileByPath(mock_stubs_path), PythonSdkType.BUILTIN_ROOT_TYPE);
    for (final VirtualFile root : additionalRoots) {
        sdkModificator.addRoot(root, OrderRootType.CLASSES);
    }
    sdkModificator.commitChanges();
    final FileBasedIndex index = FileBasedIndex.getInstance();
    index.requestRebuild(StubUpdatingIndex.INDEX_ID);
    index.requestRebuild(PyModuleNameIndex.NAME);
    return sdk;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) SdkType(com.intellij.openapi.projectRoots.SdkType) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

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