Search in sources :

Example 36 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class MavenModuleBuilderTest method testInheritJdkFromProject.

public void testInheritJdkFromProject() throws Exception {
    if (!hasMavenInstallation())
        return;
    createNewModule(new MavenId("org.foo", "module", "1.0"));
    ModuleRootManager manager = ModuleRootManager.getInstance(getModule("module"));
    assertTrue(manager.isSdkInherited());
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager)

Example 37 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class PluginRunConfiguration method getState.

@Override
public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) throws ExecutionException {
    final Module module = getModule();
    if (module == null) {
        throw new ExecutionException(DevKitBundle.message("run.configuration.no.module.specified"));
    }
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    final Sdk jdk = rootManager.getSdk();
    if (jdk == null) {
        throw CantRunException.noJdkForModule(module);
    }
    final Sdk ideaJdk = IdeaJdk.findIdeaJdk(jdk);
    if (ideaJdk == null) {
        throw new ExecutionException(DevKitBundle.message("sdk.type.incorrect.common"));
    }
    String sandboxHome = ((Sandbox) ideaJdk.getSdkAdditionalData()).getSandboxHome();
    if (sandboxHome == null) {
        throw new ExecutionException(DevKitBundle.message("sandbox.no.configured"));
    }
    try {
        sandboxHome = new File(sandboxHome).getCanonicalPath();
    } catch (IOException e) {
        throw new ExecutionException(DevKitBundle.message("sandbox.no.configured"));
    }
    final String canonicalSandbox = sandboxHome;
    //copy license from running instance of idea
    IdeaLicenseHelper.copyIDEALicense(sandboxHome);
    final JavaCommandLineState state = new JavaCommandLineState(env) {

        @Override
        protected JavaParameters createJavaParameters() throws ExecutionException {
            final JavaParameters params = new JavaParameters();
            ParametersList vm = params.getVMParametersList();
            fillParameterList(vm, VM_PARAMETERS);
            fillParameterList(params.getProgramParametersList(), PROGRAM_PARAMETERS);
            Sdk usedIdeaJdk = ideaJdk;
            String alternativeIdePath = getAlternativeJrePath();
            if (isAlternativeJreEnabled() && !StringUtil.isEmptyOrSpaces(alternativeIdePath)) {
                final Sdk configuredJdk = ProjectJdkTable.getInstance().findJdk(alternativeIdePath);
                if (configuredJdk != null) {
                    usedIdeaJdk = configuredJdk;
                } else {
                    try {
                        usedIdeaJdk = (Sdk) usedIdeaJdk.clone();
                    } catch (CloneNotSupportedException e) {
                        throw new ExecutionException(e.getMessage());
                    }
                    final SdkModificator sdkToSetUp = usedIdeaJdk.getSdkModificator();
                    sdkToSetUp.setHomePath(alternativeIdePath);
                    sdkToSetUp.commitChanges();
                }
            }
            String ideaJdkHome = usedIdeaJdk.getHomePath();
            boolean fromIdeaProject = IdeaJdk.isFromIDEAProject(ideaJdkHome);
            if (!fromIdeaProject) {
                String bootPath = "/lib/boot.jar";
                vm.add("-Xbootclasspath/a:" + ideaJdkHome + toSystemDependentName(bootPath));
            }
            vm.defineProperty("idea.config.path", canonicalSandbox + File.separator + "config");
            vm.defineProperty("idea.system.path", canonicalSandbox + File.separator + "system");
            vm.defineProperty("idea.plugins.path", canonicalSandbox + File.separator + "plugins");
            vm.defineProperty("idea.classpath.index.enabled", "false");
            if (!vm.hasProperty(JetBrainsProtocolHandler.REQUIRED_PLUGINS_KEY) && PluginModuleType.isOfType(module)) {
                final String id = DescriptorUtil.getPluginId(module);
                if (id != null) {
                    vm.defineProperty(JetBrainsProtocolHandler.REQUIRED_PLUGINS_KEY, id);
                }
            }
            if (SystemInfo.isMac) {
                vm.defineProperty("idea.smooth.progress", "false");
                vm.defineProperty("apple.laf.useScreenMenuBar", "true");
                vm.defineProperty("apple.awt.fileDialogForDirectories", "true");
            }
            if (SystemInfo.isXWindow) {
                if (VM_PARAMETERS == null || !VM_PARAMETERS.contains("-Dsun.awt.disablegrab")) {
                    // See http://devnet.jetbrains.net/docs/DOC-1142
                    vm.defineProperty("sun.awt.disablegrab", "true");
                }
            }
            if (!vm.hasProperty(PlatformUtils.PLATFORM_PREFIX_KEY)) {
                String buildNumber = IdeaJdk.getBuildNumber(ideaJdkHome);
                if (buildNumber != null) {
                    String prefix = IntelliJPlatformProduct.fromBuildNumber(buildNumber).getPlatformPrefix();
                    if (prefix != null) {
                        vm.defineProperty(PlatformUtils.PLATFORM_PREFIX_KEY, prefix);
                    }
                }
            }
            params.setWorkingDirectory(ideaJdkHome + File.separator + "bin" + File.separator);
            params.setJdk(usedIdeaJdk);
            if (fromIdeaProject) {
                OrderEnumerator enumerator = OrderEnumerator.orderEntries(module).recursively();
                for (VirtualFile file : enumerator.getAllLibrariesAndSdkClassesRoots()) {
                    params.getClassPath().add(file);
                }
            } else {
                for (String path : Arrays.asList("log4j.jar", "jdom.jar", "trove4j.jar", "openapi.jar", "util.jar", "extensions.jar", "bootstrap.jar", "idea_rt.jar", "idea.jar")) {
                    params.getClassPath().add(ideaJdkHome + toSystemDependentName("/lib/" + path));
                }
            }
            params.getClassPath().addFirst(((JavaSdkType) usedIdeaJdk.getSdkType()).getToolsPath(usedIdeaJdk));
            params.setMainClass("com.intellij.idea.Main");
            return params;
        }
    };
    return state;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) IOException(java.io.IOException) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) Sandbox(org.jetbrains.idea.devkit.projectRoots.Sandbox) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 38 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class ExecutionTestCase method setUpModule.

@Override
protected void setUpModule() {
    super.setUpModule();
    ApplicationManager.getApplication().runWriteAction(() -> {
        final String modulePath = getTestAppPath();
        final String srcPath = modulePath + File.separator + "src";
        VirtualFile moduleDir = LocalFileSystem.getInstance().findFileByPath(modulePath.replace(File.separatorChar, '/'));
        VirtualFile srcDir = LocalFileSystem.getInstance().findFileByPath(srcPath.replace(File.separatorChar, '/'));
        final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
        PsiTestUtil.removeAllRoots(myModule, rootManager.getSdk());
        PsiTestUtil.addContentRoot(myModule, moduleDir);
        PsiTestUtil.addSourceRoot(myModule, srcDir);
        IdeaTestUtil.setModuleLanguageLevel(myModule, LanguageLevel.JDK_1_8);
        PsiTestUtil.setCompilerOutputPath(myModule, VfsUtilCore.pathToUrl(FileUtil.toSystemIndependentName(myModuleOutputDir.getAbsolutePath())), false);
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager)

Example 39 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class IdeaTestUtil method setTestVersion.

@TestOnly
public static void setTestVersion(@NotNull final JavaSdkVersion testVersion, @NotNull Module module, @NotNull Disposable parentDisposable) {
    ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    final Sdk sdk = rootManager.getSdk();
    final String oldVersionString = sdk.getVersionString();
    ((ProjectJdkImpl) sdk).setVersionString(testVersion.getDescription());
    assert JavaSdk.getInstance().getVersion(sdk) == testVersion;
    Disposer.register(parentDisposable, () -> ((ProjectJdkImpl) sdk).setVersionString(oldVersionString));
}
Also used : ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) TestOnly(org.jetbrains.annotations.TestOnly)

Example 40 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class InheritedJdkTest method test1.

public void test1() throws Exception {
    final Sdk jdk = IdeaTestUtil.getMockJdk17("java 1.4");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk));
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
    ApplicationManager.getApplication().runWriteAction(() -> {
        final ProjectRootManagerEx rootManagerEx = ProjectRootManagerEx.getInstanceEx(myProject);
        rootManagerEx.setProjectSdkName(jdk.getName());
        ModuleRootModificationUtil.setSdkInherited(myModule);
    });
    assertTrue("JDK is inherited after explicit inheritSdk()", rootManager.isSdkInherited());
    assertEquals("Correct jdk inherited", jdk, rootManager.getSdk());
    ModuleRootModificationUtil.setModuleSdk(myModule, null);
    assertFalse("JDK is not inherited after setJdk(null)", rootManager.isSdkInherited());
    assertNull("No JDK assigned", rootManager.getSdk());
    final Sdk jdk1 = IdeaTestUtil.getMockJdk17("jjj");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk1));
    ModuleRootModificationUtil.setModuleSdk(myModule, jdk1);
    assertFalse("JDK is not inherited after setJdk(jdk1)", rootManager.isSdkInherited());
    assertEquals("jdk1 is assigned", jdk1, rootManager.getSdk());
}
Also used : ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Sdk(com.intellij.openapi.projectRoots.Sdk)

Aggregations

ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)47 VirtualFile (com.intellij.openapi.vfs.VirtualFile)26 Module (com.intellij.openapi.module.Module)19 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)13 ContentEntry (com.intellij.openapi.roots.ContentEntry)11 Sdk (com.intellij.openapi.projectRoots.Sdk)10 File (java.io.File)7 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)6 SourceFolder (com.intellij.openapi.roots.SourceFolder)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 ModuleManager (com.intellij.openapi.module.ModuleManager)2 OrderEntry (com.intellij.openapi.roots.OrderEntry)2 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)2 ProjectRootManagerEx (com.intellij.openapi.roots.ex.ProjectRootManagerEx)2 FilePaths.findParentContentEntry (com.android.tools.idea.gradle.util.FilePaths.findParentContentEntry)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1 CodeInsightTestCase (com.intellij.codeInsight.CodeInsightTestCase)1