Search in sources :

Example 56 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class IpnbConnectionManager method startIpythonServer.

public boolean startIpythonServer(@NotNull final String initUrl, @NotNull final IpnbFileEditor fileEditor) {
    final Module module = ProjectFileIndex.SERVICE.getInstance(myProject).getModuleForFile(fileEditor.getVirtualFile());
    if (module == null)
        return false;
    final Sdk sdk = PythonSdkType.findPythonSdk(module);
    if (sdk == null) {
        showWarning(fileEditor, "Please check Python Interpreter in Settings->Python Interpreter", null);
        return false;
    }
    final List<PyPackage> packages = PyPackageManager.getInstance(sdk).getPackages();
    final PyPackage ipythonPackage = packages != null ? PyPackageUtil.findPackage(packages, "ipython") : null;
    final PyPackage jupyterPackage = packages != null ? PyPackageUtil.findPackage(packages, "jupyter") : null;
    if (ipythonPackage == null && jupyterPackage == null) {
        showWarning(fileEditor, "Add Jupyter to the interpreter of the current project.", null);
        return false;
    }
    String url = showDialogUrl(initUrl);
    if (url == null)
        return false;
    final IpnbSettings ipnbSettings = IpnbSettings.getInstance(myProject);
    ipnbSettings.setURL(url);
    final Pair<String, String> hostPort = getHostPortFromUrl(url);
    if (hostPort == null) {
        showWarning(fileEditor, "Please, check Jupyter Notebook URL in <a href=\"\">Settings->Tools->Jupyter Notebook</a>", new IpnbSettingsAdapter());
        return false;
    }
    final String homePath = sdk.getHomePath();
    if (homePath == null) {
        showWarning(fileEditor, "Python Sdk is invalid, please check Python Interpreter in Settings->Python Interpreter", null);
        return false;
    }
    Map<String, String> env = null;
    final ArrayList<String> parameters = Lists.newArrayList(homePath);
    String ipython = findJupyterRunner(homePath);
    if (ipython == null) {
        ipython = findIPythonRunner(homePath);
        if (ipython == null) {
            ipython = PythonHelper.LOAD_ENTRY_POINT.asParamString();
            env = ImmutableMap.of("PYCHARM_EP_DIST", "ipython", "PYCHARM_EP_NAME", "ipython");
        }
        parameters.add(ipython);
        parameters.add("notebook");
    } else {
        parameters.add(ipython);
    }
    parameters.add("--no-browser");
    if (hostPort.getFirst() != null) {
        parameters.add("--ip");
        parameters.add(hostPort.getFirst());
    }
    if (hostPort.getSecond() != null) {
        parameters.add("--port");
        parameters.add(hostPort.getSecond());
    }
    final String arguments = ipnbSettings.getArguments();
    if (!StringUtil.isEmptyOrSpaces(arguments)) {
        parameters.addAll(StringUtil.split(arguments, " "));
    }
    final String directory = ipnbSettings.getWorkingDirectory();
    final String baseDir = !StringUtil.isEmptyOrSpaces(directory) ? directory : ModuleRootManager.getInstance(module).getContentRoots()[0].getCanonicalPath();
    final GeneralCommandLine commandLine = new GeneralCommandLine(parameters).withWorkDirectory(baseDir);
    if (env != null) {
        commandLine.withEnvironment(env);
    }
    try {
        final boolean[] serverStarted = { false };
        final KillableColoredProcessHandler processHandler = new KillableColoredProcessHandler(commandLine) {

            @Override
            protected void doDestroyProcess() {
                super.doDestroyProcess();
                myKernels.clear();
                myToken = null;
                UnixProcessManager.sendSigIntToProcessTree(getProcess());
            }

            @Override
            public void coloredTextAvailable(@NotNull @NonNls String text, @NotNull Key attributes) {
                super.coloredTextAvailable(text, attributes);
                if (text.toLowerCase().contains("active kernels")) {
                    serverStarted[0] = true;
                }
                final String token = "?token=";
                if (text.toLowerCase().contains(token)) {
                    myToken = text.substring(text.indexOf(token) + token.length()).trim();
                }
            }

            @Override
            public boolean isSilentlyDestroyOnClose() {
                return true;
            }
        };
        processHandler.setShouldDestroyProcessRecursively(true);
        GuiUtils.invokeLaterIfNeeded(() -> new RunContentExecutor(myProject, processHandler).withTitle("Jupyter Notebook").withStop(() -> {
            myKernels.clear();
            processHandler.destroyProcess();
            UnixProcessManager.sendSigIntToProcessTree(processHandler.getProcess());
        }, () -> !processHandler.isProcessTerminated()).withRerun(() -> startIpythonServer(url, fileEditor)).withHelpId("reference.manage.py").withFilter(new UrlFilter()).run(), ModalityState.defaultModalityState());
        int countAttempt = 0;
        while (!serverStarted[0] && countAttempt < MAX_ATTEMPTS) {
            countAttempt += 1;
            TimeoutUtil.sleep(1000);
        }
        return true;
    } catch (ExecutionException e) {
        return false;
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) UrlFilter(com.intellij.execution.filters.UrlFilter) NotNull(org.jetbrains.annotations.NotNull) PyPackage(com.jetbrains.python.packaging.PyPackage) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) RunContentExecutor(com.intellij.execution.RunContentExecutor) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key) KillableColoredProcessHandler(com.intellij.execution.process.KillableColoredProcessHandler)

Example 57 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class BuildoutFacetConfigurator method configureProject.

@Override
public void configureProject(Project project, @NotNull VirtualFile baseDir, Ref<Module> moduleRef) {
    final Module[] modules = ModuleManager.getInstance(project).getModules();
    if (modules.length > 0) {
        final Module module = modules[0];
        if (BuildoutFacet.getInstance(module) == null) {
            baseDir.refresh(false, false);
            final VirtualFile runner = BuildoutFacet.getRunner(baseDir);
            if (runner != null) {
                // TODO parse buildout.cfg and find out the part to use for the default script
                final File script = BuildoutFacet.findScript(null, "buildout", project.getBaseDir());
                if (script != null) {
                    final ProjectFacetManager facetManager = ProjectFacetManager.getInstance(module.getProject());
                    final BuildoutFacetConfiguration config = facetManager.createDefaultConfiguration(BuildoutFacetType.getInstance());
                    config.setScriptName(script.getPath());
                    setupFacet(module, config);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ProjectFacetManager(com.intellij.facet.ProjectFacetManager)

Example 58 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class PythonLanguageLevelPusher method afterRootsChanged.

public void afterRootsChanged(@NotNull final Project project) {
    Set<Sdk> updatedSdks = new HashSet<>();
    final Module[] modules = ModuleManager.getInstance(project).getModules();
    boolean needReparseOpenFiles = false;
    for (Module module : modules) {
        if (isPythonModule(module)) {
            Sdk newSdk = PythonSdkType.findPythonSdk(module);
            if (myModuleSdks.containsKey(module)) {
                Sdk oldSdk = myModuleSdks.get(module);
                if ((newSdk != null || oldSdk != null) && newSdk != oldSdk) {
                    needReparseOpenFiles = true;
                }
            }
            myModuleSdks.put(module, newSdk);
            if (newSdk != null && !updatedSdks.contains(newSdk)) {
                updatedSdks.add(newSdk);
                updateSdkLanguageLevel(project, newSdk);
            }
        }
    }
    if (needReparseOpenFiles) {
        ApplicationManager.getApplication().invokeLater(() -> {
            if (project.isDisposed()) {
                return;
            }
            FileContentUtil.reparseFiles(project, Collections.<VirtualFile>emptyList(), true);
        });
    }
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module)

Example 59 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class PythonLanguageLevelPusher method initExtra.

public void initExtra(@NotNull Project project, @NotNull MessageBus bus, @NotNull Engine languageLevelUpdater) {
    final Module[] modules = ModuleManager.getInstance(project).getModules();
    Set<Sdk> usedSdks = new HashSet<>();
    for (Module module : modules) {
        if (isPythonModule(module)) {
            final Sdk sdk = PythonSdkType.findPythonSdk(module);
            myModuleSdks.put(module, sdk);
            if (sdk != null && !usedSdks.contains(sdk)) {
                usedSdks.add(sdk);
                updateSdkLanguageLevel(project, sdk);
            }
        }
    }
    project.putUserData(PYTHON_LANGUAGE_LEVEL, PyUtil.guessLanguageLevel(project));
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module)

Example 60 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class XsltCommandLineState method createJavaParameters.

protected SimpleJavaParameters createJavaParameters() throws ExecutionException {
    final Sdk jdk = myXsltRunConfiguration.getEffectiveJDK();
    if (jdk == null) {
        throw CantRunException.noJdkConfigured();
    }
    final SimpleJavaParameters parameters = new SimpleJavaParameters();
    parameters.setJdk(jdk);
    if (myXsltRunConfiguration.getJdkChoice() == XsltRunConfiguration.JdkChoice.FROM_MODULE) {
        final Module module = myXsltRunConfiguration.getEffectiveModule();
        // OK to run as if just a JDK has been selected (a missing JDK would already have been complained about above)
        if (module != null) {
            OrderEnumerator.orderEntries(module).productionOnly().recursively().classes().collectPaths(parameters.getClassPath());
        }
    }
    final ParametersList vmParameters = parameters.getVMParametersList();
    vmParameters.addParametersString(myXsltRunConfiguration.myVmArguments);
    if (isEmpty(myXsltRunConfiguration.getXsltFile())) {
        throw new CantRunException("No XSLT file selected");
    }
    vmParameters.defineProperty("xslt.file", myXsltRunConfiguration.getXsltFile());
    if (isEmpty(myXsltRunConfiguration.getXmlInputFile())) {
        throw new CantRunException("No XML input file selected");
    }
    vmParameters.defineProperty("xslt.input", myXsltRunConfiguration.getXmlInputFile());
    final XsltRunConfiguration.OutputType outputType = myXsltRunConfiguration.getOutputType();
    if (outputType == XsltRunConfiguration.OutputType.CONSOLE) {
        //noinspection deprecation
        myPort = NetUtils.tryToFindAvailableSocketPort(myXsltRunConfiguration.myRunnerPort);
        vmParameters.defineProperty("xslt.listen-port", String.valueOf(myPort));
    }
    if (myXsltRunConfiguration.isSaveToFile()) {
        vmParameters.defineProperty("xslt.output", myXsltRunConfiguration.myOutputFile);
    }
    for (Pair<String, String> pair : myXsltRunConfiguration.getParameters()) {
        final String name = pair.getFirst();
        final String value = pair.getSecond();
        if (isEmpty(name) || value == null)
            continue;
        vmParameters.defineProperty("xslt.param." + name, value);
    }
    vmParameters.defineProperty("xslt.smart-error-handling", String.valueOf(myXsltRunConfiguration.mySmartErrorHandling));
    final PluginId pluginId = PluginManagerCore.getPluginByClassName(getClass().getName());
    assert pluginId != null || System.getProperty("xslt.plugin.path") != null : "PluginId not found - development builds need to specify -Dxslt.plugin.path=../out/classes/production/xslt-rt";
    final File pluginPath;
    if (pluginId != null) {
        final IdeaPluginDescriptor descriptor = PluginManager.getPlugin(pluginId);
        assert descriptor != null;
        pluginPath = descriptor.getPath();
    } else {
        // -Dxslt.plugin.path=C:\work\java\intellij/ultimate\out\classes\production\xslt-rt
        pluginPath = new File(System.getProperty("xslt.plugin.path"));
    }
    LOG.debug("Plugin Path = " + pluginPath.getAbsolutePath());
    final char c = File.separatorChar;
    File rtClasspath = new File(pluginPath, "lib" + c + "rt" + c + "xslt-rt.jar");
    //        File rtClasspath = new File("C:/Demetra/plugins/xpath/lib/rt/xslt-rt.jar");
    if (!rtClasspath.exists()) {
        LOG.warn("Plugin's Runtime classes not found in " + rtClasspath.getAbsolutePath());
        if (!(rtClasspath = new File(pluginPath, "classes")).exists()) {
            if (ApplicationManagerEx.getApplicationEx().isInternal() && new File(pluginPath, "org").exists()) {
                rtClasspath = pluginPath;
            } else {
                throw new CantRunException("Runtime classes not found");
            }
        }
        parameters.getVMParametersList().prepend("-ea");
    }
    parameters.getClassPath().addTail(rtClasspath.getAbsolutePath());
    parameters.setMainClass("org.intellij.plugins.xslt.run.rt.XSLTRunner");
    if (isEmpty(myXsltRunConfiguration.myWorkingDirectory)) {
        parameters.setWorkingDirectory(new File(myXsltRunConfiguration.getXsltFile()).getParentFile().getAbsolutePath());
    } else {
        parameters.setWorkingDirectory(expandPath(myXsltRunConfiguration.myWorkingDirectory, myXsltRunConfiguration.getEffectiveModule(), myXsltRunConfiguration.getProject()));
    }
    myExtensionData = new UserDataHolderBase();
    final List<XsltRunnerExtension> extensions = XsltRunnerExtension.getExtensions(myXsltRunConfiguration, myIsDebugger);
    for (XsltRunnerExtension extension : extensions) {
        extension.patchParameters(parameters, myXsltRunConfiguration, myExtensionData);
    }
    parameters.setUseDynamicClasspath(JdkUtil.useDynamicClasspath(myXsltRunConfiguration.getProject()));
    return parameters;
}
Also used : UserDataHolderBase(com.intellij.openapi.util.UserDataHolderBase) SimpleJavaParameters(com.intellij.execution.configurations.SimpleJavaParameters) IdeaPluginDescriptor(com.intellij.ide.plugins.IdeaPluginDescriptor) PluginId(com.intellij.openapi.extensions.PluginId) CantRunException(com.intellij.execution.CantRunException) ParametersList(com.intellij.execution.configurations.ParametersList) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

Module (com.intellij.openapi.module.Module)1893 VirtualFile (com.intellij.openapi.vfs.VirtualFile)585 Project (com.intellij.openapi.project.Project)379 NotNull (org.jetbrains.annotations.NotNull)330 Nullable (org.jetbrains.annotations.Nullable)267 File (java.io.File)185 PsiFile (com.intellij.psi.PsiFile)147 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)134 ArrayList (java.util.ArrayList)118 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)112 Sdk (com.intellij.openapi.projectRoots.Sdk)95 PsiElement (com.intellij.psi.PsiElement)89 PsiDirectory (com.intellij.psi.PsiDirectory)77 ModuleManager (com.intellij.openapi.module.ModuleManager)65 PsiClass (com.intellij.psi.PsiClass)65 IOException (java.io.IOException)61 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)57 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)57 List (java.util.List)57 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)51