/** Fetch R package info including description and version. */ public static Set<RPackage> getInstalledPackages() { RHelperUtil.runHelperWithArgs(RHelperUtil.R_HELPER_INSTALL_TIDYVERSE); String helperOutput = RHelperUtil.getHelperOutput(RHELPER_PACKAGE_SUMMARIES); if (helperOutput != null) { return Sets.newHashSet( Iterables.transform( Lists.newArrayList(helperOutput.split("\n")), new ParseDescriptorIntoPackage())); } else { return Sets.newHashSet(); } }
private void reindexPackage(RPackage rPackage, RPackage indexPackage) { // replace package RHelperUtil.LOG.info("detecting methods in " + rPackage.getName()); String allFunsConcat = RHelperUtil.runCommand("cat(getNamespaceExports('" + rPackage.getName() + "'))").trim(); List<String> allFuns = Splitter.on(" ").trimResults().splitToList(allFunsConcat); List<Function> functions = Lists.transform( allFuns, new com.google.common.base.Function<String, Function>() { @Override public Function apply(String s) { return new Function(s, "NA"); } }); rPackage.setFunctions(functions); if (indexPackage != null) { allPackages.remove(indexPackage); } allPackages.add(rPackage); }
static String getPackageVersion(String packageName) { String s = RHelperUtil.runCommand("cat(packageDescription('" + packageName + "')$Version)"); Preconditions.checkNotBlank(s, "version is empty"); return s; }
/** * @param packageNames Packages to be reindexd if version has changed or package has been * installed since last indexing run.If non are provided all packages will be refreshed. */ public boolean refreshIndex(String... packageNames) { RHelperUtil.runHelperWithArgs(RHelperUtil.R_HELPER_INSTALL_TIDYVERSE); Set<RPackage> installedPackages = LocalRUtil.getInstalledPackages(); // remove packages from index that are no longer present if (packageNames.length == 0) { Sets.SetView<RPackage> noLongerInstalled = Sets.difference(allPackages, installedPackages); allPackages.removeAll(noLongerInstalled); } // cut down packges to be refreshed to speed up calculations // if(packageNames.length>0){ // installedPackages = installedPackages.stream(). // filter(p -> Arrays.asList(packageNames).contains(p.getName())). // collect(Collectors.toSet()); // } ExecutorService executorService = Executors.newFixedThreadPool(8); final boolean[] hasChanged = {false}; for (final RPackage rPackage : installedPackages) { final RPackage indexPackage = getByName(rPackage.getName()); if (indexPackage != null && ObjectUtils.equals(indexPackage.getVersion(), rPackage.getVersion())) { continue; } executorService.submit( new Runnable() { @Override public void run() { reindexPackage(rPackage, indexPackage); hasChanged[0] = true; } }); } executorService.shutdown(); try { executorService.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) { e.printStackTrace(); } // allPackages.clear(); // allPackages.addAll(installedPackages); if (hasChanged[0]) { if (ApplicationManager.getApplication() != null) { Project[] projects = ProjectManager.getInstance().getOpenProjects(); for (Project project : projects) { if (project.isInitialized() && project.isOpen() && !project.isDefault()) { SpellCheckerManager spellCheckerManager = SpellCheckerManager.getInstance(project); EditableDictionary dictionary = spellCheckerManager.getUserDictionary(); for (RPackage rPackage : RPackageService.getInstance().allPackages) { dictionary.addToDictionary(rPackage.getName()); dictionary.addToDictionary(rPackage.getFunctionNames()); } DaemonCodeAnalyzer.getInstance(project).restart(); } } } } return hasChanged[0]; }