public static Sdk createMockSdk(String jdkHome, final String versionName) { File jdkHomeFile = new File(jdkHome); // if (!jdkHomeFile.exists()) return null; final Sdk jdk = new ProjectJdkImpl(versionName, LuaSdkType.getInstance()); final SdkModificator sdkModificator = jdk.getSdkModificator(); String path = jdkHome.replace(File.separatorChar, '/'); sdkModificator.setHomePath(path); sdkModificator.setVersionString( versionName); // must be set after home path, otherwise setting home path clears the version string sdkModificator.addRoot(StdLibrary.getStdFileLocation(), OrderRootType.SOURCES); sdkModificator.commitChanges(); return jdk; }
@Override public void reset() { super.reset(); myProjectJdksModel.reset(myProject); myRoot.removeAllChildren(); final Map<Sdk, Sdk> sdks = myProjectJdksModel.getProjectSdks(); for (Sdk sdk : sdks.keySet()) { final JdkConfigurable configurable = new JdkConfigurable((ProjectJdkImpl)sdks.get(sdk), myProjectJdksModel, TREE_UPDATER, myHistory, myProject); addNode(new MyNode(configurable), myRoot); } selectJdk(myProjectJdksModel.getProjectSdk()); //restore selection JBSplitter splitter = extractSplitter(); if (splitter != null) { splitter.setAndLoadSplitterProportionKey("project.jdk.splitter"); } }
@Override @Nullable protected ArrayList<AnAction> createActions(final boolean fromPopup) { if (myProjectJdksModel == null) { return null; } final ArrayList<AnAction> actions = new ArrayList<AnAction>(); DefaultActionGroup group = new DefaultActionGroup(ProjectBundle.message("add.new.jdk.text"), true); group.getTemplatePresentation().setIcon(IconUtil.getAddIcon()); myProjectJdksModel.createAddActions(group, myTree, new Consumer<Sdk>() { @Override public void consume(final Sdk projectJdk) { addNode(new MyNode(new JdkConfigurable(((ProjectJdkImpl)projectJdk), myProjectJdksModel, TREE_UPDATER, myHistory, myProject), false), myRoot); selectNodeInTree(findNodeByObject(myRoot, projectJdk)); } }); actions.add(new MyActionGroupWrapper(group)); actions.add(new MyDeleteAction(Conditions.<Object[]>alwaysTrue())); return actions; }
public void doAdd(JComponent parent, final SdkType type, final Consumer<Sdk> callback) { myModified = true; if (type.supportsCustomCreateUI()) { type.showCustomCreateUI(this, parent, new Consumer<Sdk>() { @Override public void consume(Sdk sdk) { setupSdk(sdk, callback); } }); } else { SdkConfigurationUtil.selectSdkHome(type, new Consumer<String>() { @Override public void consume(final String home) { String newSdkName = SdkConfigurationUtil.createUniqueSdkName(type, home, myProjectSdks.values()); final ProjectJdkImpl newJdk = new ProjectJdkImpl(newSdkName, type); newJdk.setHomePath(home); setupSdk(newJdk, callback); } }); } }
@NotNull private AbstractCreateVirtualEnvDialog.VirtualEnvCallback getVEnvCallback() { return new CreateVirtualEnvDialog.VirtualEnvCallback() { @Override public void virtualEnvCreated(Sdk sdk, boolean associateWithProject) { if (associateWithProject) { SdkAdditionalData additionalData = sdk.getSdkAdditionalData(); if (additionalData == null) { additionalData = new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(sdk.getHomePath())); ((ProjectJdkImpl)sdk).setSdkAdditionalData(additionalData); } if (myNewProject) { ((PythonSdkAdditionalData)additionalData).associateWithNewProject(); } else { ((PythonSdkAdditionalData)additionalData).associateWithProject(myProject); } } mySdkAddedCallback.consume(sdk); } }; }
public void apply() throws ConfigurationException { /*if (mySandboxHome.getText() == null || mySandboxHome.getText().length() == 0) { throw new ConfigurationException(DevKitBundle.message("sandbox.specification")); }*/ mySandboxHome.addCurrentTextToHistory(); final Sandbox additionalData = (Sandbox)myIdeaJdk.getSdkAdditionalData(); if (additionalData != null) { additionalData.cleanupWatchedRoots(); } Sandbox sandbox = new Sandbox(mySandboxHome.getText(), (Sdk)myInternalJres.getSelectedItem(), myIdeaJdk); final SdkModificator modificator = myIdeaJdk.getSdkModificator(); modificator.setSdkAdditionalData(sandbox); ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { modificator.commitChanges(); } }); ((ProjectJdkImpl) myIdeaJdk).resetVersionString(); myModified = false; }
@Override public void reset() { super.reset(); myProjectJdksModel.reset(myProject); myRoot.removeAllChildren(); final Map<Sdk, Sdk> sdks = myProjectJdksModel.getProjectSdks(); for (Sdk sdk : sdks.keySet()) { final JdkConfigurable configurable = new JdkConfigurable((ProjectJdkImpl)sdks.get(sdk), myProjectJdksModel, TREE_UPDATER, myHistory, myProject); addNode(new MyNode(configurable), myRoot); } selectJdk(myProjectJdksModel.getProjectSdk()); //restore selection final String value = PropertiesComponent.getInstance().getValue(SPLITTER_PROPORTION); if (value != null) { try { final Splitter splitter = extractSplitter(); if (splitter != null) { (splitter).setProportion(Float.parseFloat(value)); } } catch (NumberFormatException e) { //do not set proportion } } }
@Override @Nullable protected ArrayList<AnAction> createActions(final boolean fromPopup) { final ArrayList<AnAction> actions = new ArrayList<AnAction>(); DefaultActionGroup group = new DefaultActionGroup(ProjectBundle.message("add.new.jdk.text"), true); group.getTemplatePresentation().setIcon(IconUtil.getAddIcon()); myProjectJdksModel.createAddActions(group, myTree, new Consumer<Sdk>() { @Override public void consume(final Sdk projectJdk) { addNode(new MyNode(new JdkConfigurable(((ProjectJdkImpl)projectJdk), myProjectJdksModel, TREE_UPDATER, myHistory, myProject), false), myRoot); selectNodeInTree(findNodeByObject(myRoot, projectJdk)); } }); actions.add(new MyActionGroupWrapper(group)); actions.add(new MyDeleteAction(Conditions.<Object[]>alwaysTrue())); return actions; }
@Nullable @Override public LabeledComponent<JComponent> getLanguageSettingsComponent(@NotNull Course selectedCourse) { final Project project = ProjectManager.getInstance().getDefaultProject(); PyConfigurableInterpreterList instance = PyConfigurableInterpreterList.getInstance(project); if (instance == null) { return null; } final List<Sdk> sdks = instance.getAllPythonSdks(); VirtualEnvProjectFilter.removeAllAssociated(sdks); // by default we create new virtual env in project, we need to add this non-existing sdk to sdk list ProjectJdkImpl fakeSdk = createFakeSdk(selectedCourse); if (fakeSdk != null) { sdks.add(0, fakeSdk); } PythonSdkChooserCombo combo = new PythonSdkChooserCombo(project, sdks, sdk -> true); if (fakeSdk != null) { patchRenderer(fakeSdk, combo); combo.getComboBox().setSelectedItem(fakeSdk); } if (SystemInfo.isMac && !UIUtil.isUnderDarcula()) { combo.putClientProperty("JButton.buttonType", null); } combo.setButtonIcon(PythonIcons.Python.InterpreterGear); combo.addChangedListener(e -> { Sdk selectedSdk = (Sdk)combo.getComboBox().getSelectedItem(); mySettings.setSdk(selectedSdk == fakeSdk ? null : selectedSdk); }); return LabeledComponent.create(combo, "Interpreter", BorderLayout.WEST); }
@Nullable private static ProjectJdkImpl createFakeSdk(@NotNull Course selectedCourse) { String fakeSdkPath = getBaseSdk(selectedCourse); if (fakeSdkPath == null) { return null; } PythonSdkFlavor flavor = PythonSdkFlavor.getApplicableFlavors(false).get(0); String prefix = flavor.getName() + " "; String versionString = flavor.getVersionString(fakeSdkPath); if (versionString == null || !versionString.contains(prefix)) { return null; } String name = "new virtual env " + versionString.substring(prefix.length()); return new ProjectJdkImpl(name, PythonSdkType.getInstance()); }
private static void patchRenderer(@NotNull ProjectJdkImpl fakeSdk, @NotNull PythonSdkChooserCombo combo) { combo.getComboBox().setRenderer(new PySdkListCellRenderer(true) { @Override public void customize(JList list, Object item, int index, boolean selected, boolean hasFocus) { super.customize(list, item, index, selected, hasFocus); if (item == fakeSdk) { setIcon(IconLoader.getTransparentIcon(PythonIcons.Python.Virtualenv)); } } }); }
public void createAndAddVirtualEnv(Project project, PyNewProjectSettings settings) { final ProjectSdksModel model = PyConfigurableInterpreterList.getInstance(project).getModel(); Course course = StudyTaskManager.getInstance(project).getCourse(); if (course == null) { return; } final String baseSdk = getBaseSdk(course); if (baseSdk != null) { final PyPackageManager packageManager = PyPackageManager.getInstance(new PyDetectedSdk(baseSdk)); try { final String path = packageManager.createVirtualEnv(project.getBasePath() + "/.idea/VirtualEnvironment", false); AbstractCreateVirtualEnvDialog.setupVirtualEnvSdk(path, true, new AbstractCreateVirtualEnvDialog.VirtualEnvCallback() { @Override public void virtualEnvCreated(Sdk createdSdk, boolean associateWithProject) { settings.setSdk(createdSdk); model.addSdk(createdSdk); try { model.apply(); } catch (ConfigurationException exception) { LOG.error("Error adding created virtual env " + exception.getMessage()); } if (associateWithProject) { SdkAdditionalData additionalData = createdSdk.getSdkAdditionalData(); if (additionalData == null) { additionalData = new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(createdSdk.getHomePath())); ((ProjectJdkImpl)createdSdk).setSdkAdditionalData(additionalData); } ((PythonSdkAdditionalData)additionalData).associateWithNewProject(); } } }); } catch (ExecutionException e) { LOG.warn("Failed to create virtual env " + e.getMessage()); } } }
private static Sdk createMockSdk(String jdkHome, final String versionName) { File jdkHomeFile = new File(jdkHome); // if (!jdkHomeFile.exists()) return null; final Sdk jdk = new ProjectJdkImpl(versionName, LuaSdkType.getInstance()); final SdkModificator sdkModificator = jdk.getSdkModificator(); String path = jdkHome.replace(File.separatorChar, '/'); sdkModificator.setHomePath(path); sdkModificator.setVersionString(versionName); // must be set after home path, otherwise setting home path clears the version string sdkModificator.addRoot(StdLibrary.getStdFileLocation(), OrderRootType.SOURCES); sdkModificator.commitChanges(); return jdk; }
public static Sdk getTestJdk() { try { ProjectJdkImpl jdk = (ProjectJdkImpl)JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk().clone(); jdk.setName(TEST_JDK_NAME); return jdk; } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } }
public SdkEditor(SdkModel sdkModel, History history, final ProjectJdkImpl sdk) { mySdkModel = sdkModel; myHistory = history; mySdk = sdk; createMainPanel(); initSdk(sdk); }
private void doSetHomePath(final String homePath, final SdkType sdkType) { if (homePath == null){ return; } setHomePathValue(homePath.replace('/', File.separatorChar)); final String newSdkName = suggestSdkName(homePath); ((ProjectJdkImpl)mySdk).setName(newSdkName); try { final Sdk dummySdk = (Sdk)mySdk.clone(); SdkModificator sdkModificator = dummySdk.getSdkModificator(); sdkModificator.setHomePath(homePath); sdkModificator.removeAllRoots(); sdkModificator.commitChanges(); sdkType.setupSdkPaths(dummySdk, mySdkModel); clearAllPaths(); myVersionString = dummySdk.getVersionString(); if (myVersionString == null) { Messages.showMessageDialog(ProjectBundle.message("sdk.java.corrupt.error", homePath), ProjectBundle.message("sdk.java.corrupt.title"), Messages.getErrorIcon()); } sdkModificator = dummySdk.getSdkModificator(); for (OrderRootType type : myPathEditors.keySet()) { SdkPathEditor pathEditor = myPathEditors.get(type); pathEditor.setAddBaseDir(dummySdk.getHomeDirectory()); pathEditor.addPaths(sdkModificator.getRoots(type)); } mySdkModel.getMulticaster().sdkHomeSelected(dummySdk, homePath); } catch (CloneNotSupportedException e) { LOG.error(e); // should not happen in normal program } }
public JdkConfigurable(final ProjectJdkImpl projectJdk, final ProjectSdksModel sdksModel, final Runnable updateTree, @NotNull History history, Project project) { super(true, updateTree); myProjectJdk = projectJdk; mySdkEditor = createSdkEditor(sdksModel, history, myProjectJdk); final StructureConfigurableContext context = ModuleStructureConfigurable.getInstance(project).getContext(); myProjectStructureElement = new SdkProjectStructureElement(context, myProjectJdk); }
@Override protected void loadTree() { final Map<Sdk,Sdk> sdks = myJdksTreeModel.getProjectSdks(); for (Sdk sdk : sdks.keySet()) { final JdkConfigurable configurable = new JdkConfigurable((ProjectJdkImpl)sdks.get(sdk), myJdksTreeModel, TREE_UPDATER, myHistory, myProject); addNode(new MyNode(configurable), myRoot); } }
public boolean addJdkNode(final Sdk jdk, final boolean selectInTree) { if (!myUiDisposed) { myContext.getDaemonAnalyzer().queueUpdate(new SdkProjectStructureElement(myContext, jdk)); addNode(new MyNode(new JdkConfigurable((ProjectJdkImpl)jdk, myJdksTreeModel, TREE_UPDATER, myHistory, myProject)), myRoot); if (selectInTree) { selectNodeInTree(MasterDetailsComponent.findNodeByObject(myRoot, jdk)); } return true; } return false; }
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); } PyUserSkeletonsUtil.addUserSkeletonsRoot(PySdkUpdater.fromSdkModificator(sdk, sdkModificator)); 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; }
@NotNull private static Sdk createMockSdk(@NotNull PythonSdkFlavor flavor, @NotNull String versionOutput) { final String versionString = flavor.getVersionStringFromOutput(versionOutput); final ProjectJdkImpl sdk = new ProjectJdkImpl("Test", PythonSdkType.getInstance(), "/path/to/sdk", versionString); sdk.setSdkAdditionalData(new PythonSdkAdditionalData(flavor)); return sdk; }
@Override public Sdk getSdk() { try { ProjectJdkImpl jdk = (ProjectJdkImpl) JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk().clone(); jdk.setName("JDK"); return jdk; } catch (CloneNotSupportedException e) { log("cloning not supported: " + e); return null; } }
private Sdk getTestJdk() { try { ProjectJdkImpl jdk = (ProjectJdkImpl)JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk().clone(); jdk.setName("JDK"); return jdk; } catch (CloneNotSupportedException e) { LOG.error(e); return null; } }
private static Sdk getTestJdk() { try { ProjectJdkImpl jdk = (ProjectJdkImpl)JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk().clone(); jdk.setName("JDK"); return jdk; } catch (CloneNotSupportedException e) { //LOG.error(e); return null; } }
private void doSetHomePath(final String homePath, final SdkType sdkType) { if (homePath == null){ return; } setHomePathValue(homePath.replace('/', File.separatorChar)); final String newSdkName = suggestSdkName(homePath); ((ProjectJdkImpl)mySdk).setName(newSdkName); try { final Sdk dummySdk = (Sdk)mySdk.clone(); SdkModificator sdkModificator = dummySdk.getSdkModificator(); sdkModificator.setHomePath(homePath); sdkModificator.removeAllRoots(); sdkModificator.commitChanges(); sdkType.setupSdkPaths(dummySdk, mySdkModel); clearAllPaths(); myVersionString = dummySdk.getVersionString(); if (myVersionString == null) { Messages.showMessageDialog(ProjectBundle.message("sdk.java.corrupt.error", homePath), ProjectBundle.message("sdk.java.corrupt.title"), Messages.getErrorIcon()); } sdkModificator = dummySdk.getSdkModificator(); for (OrderRootType type : myPathEditors.keySet()) { myPathEditors.get(type).addPaths(sdkModificator.getRoots(type)); } mySdkModel.getMulticaster().sdkHomeSelected(dummySdk, homePath); } catch (CloneNotSupportedException e) { LOG.error(e); // should not happen in normal program } }
@Override public void setName(String name) { ((ProjectJdkImpl)mySdk).setName(name); }
protected SdkEditor createSdkEditor(ProjectSdksModel sdksModel, History history, ProjectJdkImpl projectJdk) { return new SdkEditor(sdksModel, history, projectJdk); }
@NotNull public String createVirtualEnv(@NotNull String destinationDir, boolean useGlobalSite) throws ExecutionException { final List<String> args = new ArrayList<String>(); final LanguageLevel languageLevel = PythonSdkType.getLanguageLevelForSdk(mySdk); final boolean usePyVenv = languageLevel.isAtLeast(LanguageLevel.PYTHON33); if (usePyVenv) { args.add("pyvenv"); if (useGlobalSite) { args.add("--system-site-packages"); } args.add(destinationDir); getHelperResult(PACKAGING_TOOL, args, false, true, null); } else { if (useGlobalSite) { args.add("--system-site-packages"); } args.add(destinationDir); final boolean pre26 = languageLevel.isOlderThan(LanguageLevel.PYTHON26); final String name = "virtualenv-" + (pre26 ? VIRTUALENV_PRE_26_VERSION : VIRTUALENV_VERSION); final String dirName = extractHelper(name + ".tar.gz"); try { final String fileName = dirName + name + File.separatorChar + "virtualenv.py"; getPythonProcessResult(fileName, args, false, true, dirName + name); } finally { FileUtil.delete(new File(dirName)); } } final String binary = PythonSdkType.getPythonExecutable(destinationDir); final String binaryFallback = destinationDir + File.separator + "bin" + File.separator + "python"; final String path = (binary != null) ? binary : binaryFallback; if (usePyVenv) { // Still no 'packaging' and 'pysetup3' for Python 3.3rc1, see PEP 405 final VirtualFile binaryFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(path); if (binaryFile != null) { final ProjectJdkImpl tmpSdk = new ProjectJdkImpl("", PythonSdkType.getInstance()); tmpSdk.setHomePath(path); final PyPackageManager manager = PyPackageManager.getInstance(tmpSdk); manager.installManagement(); } } return path; }
public void configureProject(final Project project, @NotNull final VirtualFile baseDir, Ref<Module> moduleRef) { // it it a virtualenv? final PythonSdkType sdkType = PythonSdkType.getInstance(); //find virtualEnv in project directory final List<String> candidates = new ArrayList<String>(); if (project == null) return; final VirtualFile rootDir = project.getBaseDir(); if (rootDir != null) candidates.addAll(VirtualEnvSdkFlavor.findInDirectory(rootDir)); if (!candidates.isEmpty()) { String filePath = candidates.get(0); if (StringUtil.startsWithChar(filePath, '~')) { final String home = SystemProperties.getUserHome(); filePath = home + filePath.substring(1); } final Sdk virtualEnvSdk = SdkConfigurationUtil.createAndAddSDK(filePath, sdkType); if (virtualEnvSdk != null) { SdkConfigurationUtil.setDirectoryProjectSdk(project, virtualEnvSdk); SdkAdditionalData additionalData = virtualEnvSdk.getSdkAdditionalData(); if (additionalData == null) { additionalData = new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(virtualEnvSdk.getHomePath())); ((ProjectJdkImpl)virtualEnvSdk).setSdkAdditionalData(additionalData); } ((PythonSdkAdditionalData) additionalData).associateWithProject(project); return; } return; } final Sdk existingSdk = ProjectRootManager.getInstance(project).getProjectSdk(); if (existingSdk != null && existingSdk.getSdkType() == sdkType) return; // SdkConfigurationUtil does the same final File executableFile = PythonSdkType.findExecutableFile(new File(project.getBasePath(), "bin"), "python"); if (executableFile != null) { final File virtualEnvRoot = PythonSdkType.getVirtualEnvRoot(executableFile.getPath()); if (virtualEnvRoot != null) { // yes, an unknown virtualenv; set it up as SDK final Sdk sdk = SdkConfigurationUtil.createAndAddSDK(executableFile.getPath(), sdkType); if (sdk != null) { SdkConfigurationUtil.setDirectoryProjectSdk(project, sdk); return; } } } // default SdkConfigurationUtil.configureDirectoryProjectSdk(project, new PreferredSdkComparator(), sdkType); }
@Override public void showCustomCreateUI(final SdkModel sdkModel, final JComponent parentComponent, final Consumer<Sdk> sdkCreatedCallback) { customImpl: { final Collection<String> homePaths = suggestHomePaths(); if(homePaths.isEmpty()) { break customImpl; } final String home = homePaths.iterator().next(); final File homePath = new File(home); if(!homePath.isDirectory()) { break customImpl; } if(!checkDependency(sdkModel)) { if(Messages.showOkCancelDialog(parentComponent, getUnsatisfiedDependencyMessage(), "Cannot Create SDK", Messages.getWarningIcon()) != Messages.OK) { return; } if(fixDependency(sdkModel, sdkCreatedCallback) == null) { return; } } final String newSdkName = SdkConfigurationUtil.createUniqueSdkName(this, home, Arrays.asList(sdkModel.getSdks())); final ProjectJdkImpl newJdk = new ProjectJdkImpl(newSdkName, this); newJdk.setHomePath(home); sdkCreatedCallback.consume(newJdk); return; } super.showCustomCreateUI(sdkModel, parentComponent, sdkCreatedCallback); }