Search in sources :

Example 46 with SdkModificator

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

the class PyOptimizeImportsTest method testLibraryRootInsideProject.

// PY-18970
public void testLibraryRootInsideProject() {
    final String testName = getTestName(true);
    myFixture.copyDirectoryToProject(testName, "");
    final VirtualFile libDir = myFixture.findFileInTempDir("lib");
    assertNotNull(libDir);
    final Sdk sdk = PythonSdkType.findPythonSdk(myFixture.getModule());
    assertNotNull(sdk);
    WriteAction.run(() -> {
        final SdkModificator modificator = sdk.getSdkModificator();
        assertNotNull(modificator);
        modificator.addRoot(libDir, OrderRootType.CLASSES);
        modificator.commitChanges();
    });
    try {
        myFixture.configureByFile("main.py");
        OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));
        myFixture.checkResultByFile(testName + "/main.after.py");
    } finally {
        //noinspection ThrowFromFinallyBlock
        WriteAction.run(() -> {
            final SdkModificator modificator = sdk.getSdkModificator();
            assertNotNull(modificator);
            modificator.removeRoot(libDir, OrderRootType.CLASSES);
            modificator.commitChanges();
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 47 with SdkModificator

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

the class KotlinAndroidTestCaseBase method createAndroidSdk.

public Sdk createAndroidSdk(String sdkPath, String platformDir) {
    Sdk sdk = ProjectJdkTable.getInstance().createSdk("android_test_sdk", AndroidSdkType.getInstance());
    SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.setHomePath(sdkPath);
    if (platformDir.equals(getDefaultPlatformDir())) {
        // Compatibility: the unit tests were using android.jar outside the sdk1.5 install;
        // we need to use that one, rather than the real one in sdk1.5, in order for the
        // tests to pass. Longer term, we should switch the unit tests over to all using
        // a valid SDK.
        String androidJarPath = sdkPath + "/../android.jar!/";
        androidJar = VirtualFileManager.getInstance().findFileByUrl("jar://" + androidJarPath);
    } else {
        androidJar = VirtualFileManager.getInstance().findFileByUrl("jar://" + sdkPath + "/platforms/" + platformDir + "/android.jar!/");
    }
    sdkModificator.addRoot(androidJar, OrderRootType.CLASSES);
    VirtualFile resFolder = LocalFileSystem.getInstance().findFileByPath(sdkPath + "/platforms/" + platformDir + "/data/res");
    sdkModificator.addRoot(resFolder, OrderRootType.CLASSES);
    VirtualFile docsFolder = LocalFileSystem.getInstance().findFileByPath(sdkPath + "/docs/reference");
    if (docsFolder != null) {
        sdkModificator.addRoot(docsFolder, JavadocOrderRootType.getInstance());
    }
    AndroidSdkAdditionalData data = new AndroidSdkAdditionalData(sdk);
    AndroidSdkData sdkData = AndroidSdkData.getSdkData(sdkPath);
    assertNotNull(sdkData);
    // TODO: Get rid of this hardcoded version number
    IAndroidTarget target = sdkData.findTargetByName("Android 5.0");
    if (target == null) {
        IAndroidTarget[] targets = sdkData.getTargets();
        for (IAndroidTarget t : targets) {
            if (t.getLocation().contains(platformDir)) {
                target = t;
                break;
            }
        }
        if (target == null && targets.length > 0) {
            target = targets[targets.length - 1];
        }
    }
    assertNotNull(target);
    data.setBuildTarget(target);
    sdkModificator.setSdkAdditionalData(data);
    sdkModificator.commitChanges();
    return sdk;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) Sdk(com.intellij.openapi.projectRoots.Sdk) IAndroidTarget(com.android.sdklib.IAndroidTarget) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 48 with SdkModificator

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

the class PythonSdkDetailsDialog method removeSdk.

private void removeSdk() {
    final Sdk currentSdk = getSelectedSdk();
    if (currentSdk != null) {
        final Sdk sdk = myProjectSdksModel.findSdk(currentSdk);
        SdkConfigurationUtil.removeSdk(sdk);
        myProjectSdksModel.removeSdk(sdk);
        myProjectSdksModel.removeSdk(currentSdk);
        if (myModificators.containsKey(currentSdk)) {
            SdkModificator modificator = myModificators.get(currentSdk);
            myModifiedModificators.remove(modificator);
            myModificators.remove(currentSdk);
        }
        refreshSdkList();
        mySdkListChanged = true;
        // TODO select initially selected SDK
        if (mySdkList.getSelectedIndex() < 0) {
            mySdkList.setSelectedIndex(0);
        }
    }
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 49 with SdkModificator

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

the class PythonSdkDetailsDialog method editSdk.

private void editSdk(final Sdk currentSdk) {
    final SdkModificator modificator = myModificators.get(currentSdk);
    final EditSdkDialog dialog = new EditSdkDialog(myProject, modificator, s -> {
        if (isDuplicateSdkName(s, currentSdk)) {
            return PyBundle.message("sdk.details.dialog.error.duplicate.name");
        }
        return null;
    });
    if (dialog.showAndGet()) {
        mySdkList.repaint();
        final boolean pathChanged = !Comparing.equal(currentSdk.getHomePath(), dialog.getHomePath());
        if (!modificator.getName().equals(dialog.getName()) || pathChanged || dialog.isAssociateChanged()) {
            myModifiedModificators.add(modificator);
            modificator.setName(dialog.getName());
            modificator.setHomePath(dialog.getHomePath());
            if (dialog.isAssociateChanged()) {
                setSdkAssociated(modificator, dialog.associateWithProject());
            }
            if (pathChanged) {
                reloadSdk(currentSdk);
            }
        }
    }
}
Also used : SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 50 with SdkModificator

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

the class PyTestSdkTools method generateTempSkeletonsOrPackages.

/**
   * Adds installed eggs to SDK, generates skeletons (optionally) and associates it with module.
   *
   * @param sdk          sdk to process
   * @param addSkeletons add skeletons or only packages
   * @param module       module to associate with (if provided)
   * @throws InvalidSdkException bas sdk
   * @throws IOException         failed to read eggs
   */
public static void generateTempSkeletonsOrPackages(@NotNull final Sdk sdk, final boolean addSkeletons, @Nullable final Module module) throws InvalidSdkException, IOException {
    Project project = null;
    if (module != null) {
        project = module.getProject();
        final Project finalProject = project;
        // Associate with module
        ModuleRootModificationUtil.setModuleSdk(module, sdk);
        EdtTestUtil.runInEdtAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> ProjectRootManager.getInstance(finalProject).setProjectSdk(sdk)));
    }
    final SdkModificator modificator = sdk.getSdkModificator();
    modificator.removeRoots(OrderRootType.CLASSES);
    modificator.setSdkAdditionalData(new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(sdk)));
    for (final String path : PythonSdkType.getSysPathsFromScript(sdk.getHomePath())) {
        addTestSdkRoot(modificator, path);
    }
    if (!addSkeletons) {
        EdtTestUtil.runInEdtAndWait(modificator::commitChanges);
        return;
    }
    final String skeletonsPath = PythonSdkType.getSkeletonsPath(PathManager.getSystemPath(), sdk.getHomePath());
    addTestSdkRoot(modificator, skeletonsPath);
    EdtTestUtil.runInEdtAndWait(modificator::commitChanges);
    PySkeletonRefresher.refreshSkeletonsOfSdk(project, null, skeletonsPath, sdk);
}
Also used : Project(com.intellij.openapi.project.Project) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

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