Eclipse Monkey is an amazingly useful tool if you want to do some quick hacking in a big workspace. On the project I am currently working on, the workspace consists of 180+ Eclipse projects. Scripting changes to configuration settings and check if settings are conforming is a lifesaver.
Three examples of scripts I wrote to do some project analysis. You only need to install Monkey for these scripts, no additional bells & whistles are needed. The examples are prefixed and suffixed with some wacky lines, these are used by Eclipse Monkey for copy-paste publishing support.
This example finds projects which are in a differently named directory (this happens often because renaming a project does not automatically rename its containing directory).
--- Came wiffling through the eclipsey wood --- /* * Menu: Info Support > Find project name mismatch * Kudos: Peter Hendriks * License: EPL 1.0 * DOM: http://localhost/org.eclipse.eclipsemonkey.lang.javascript */ function main(){ loadBundle("org.eclipse.core.resources"); var ResourcesPlugin = Packages.org.eclipse.core.resources.ResourcesPlugin; var projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); var totalMismatch = 0; for (var i = 0; i < projects.length; i++) { var project = projects[i]; var projectDirName = project.getLocation().makeAbsolute().lastSegment(); var projectName = project.getName(); if (!projectName.equals(projectDirName)) { out.println(projectDirName + " - " + projectName); totalMismatch++; } } out.println("Total projects: " + projects.length + ", total mismatch: " + totalMismatch); } --- And burbled as it ran! ---
This will find Java projects that have no Checkstyle config defined:
--- Came wiffling through the eclipsey wood --- /* * Menu: Info Support > Find invalid Checkstyle config * Kudos: Peter Hendriks * License: EPL 1.0 * DOM: http://localhost/org.eclipse.eclipsemonkey.lang.javascript */ function main() { loadBundle("org.eclipse.core.resources"); var ResourcesPlugin = Packages.org.eclipse.core.resources.ResourcesPlugin; var projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); var totalMismatch = 0; for (var i = 0; i < projects.length; i++) { var project = projects[i]; if (project.isOpen() && project.hasNature("org.eclipse.jdt.core.javanature") && !isProperCheckstyle(project)) { out.println(project.getName()); totalMismatch++; } } out.println("Total projects: " + projects.length + ", total mismatch: " + totalMismatch); } function isProperCheckstyle(project) { var InputStreamReader = Packages.java.io.InputStreamReader; var BufferedReader = Packages.java.io.BufferedReader; var checkstyleFile = project.getFile(".checkstyle"); if (!checkstyleFile.exists()) { return false; } var reader = new BufferedReader(new InputStreamReader(checkstyleFile.getContents())); try { var line = reader.readLine(); var re = /.*check-config-name="Endeavour_CS".*/; while (line) { var result = re.exec(line); if (result) { return true; } line = reader.readLine(); } return false; } finally { reader.close(); } } --- And burbled as it ran! ---
This will find comment patterns that are probably commented code and tag tasks for them (Code is sometimes commented out to try alternative code, but never removed). For extra browny points, this is done using a progress bar, because this operation takes a while on our workspace:
--- Came wiffling through the eclipsey wood --- /* * Menu: Info Support > Find commented code * Kudos: Peter Hendriks * License: EPL 1.0 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript */ function main() { loadBundle("org.eclipse.core.jobs"); var ProgressMonitorDialog = Packages.org.eclipse.jface.dialogs.ProgressMonitorDialog; var IRunnableWithProgress = Packages.org.eclipse.jface.operation.IRunnableWithProgress; var runnableWithProgress = new IRunnableWithProgress({ run: runCommentSearch }); new ProgressMonitorDialog(window.getShell()).run(true, true, runnableWithProgress); window.getActivePage().showView("org.eclipse.ui.views.TaskList"); } function runCommentSearch(monitor) { var files = resources.filesMatching(".*\.java"); monitor.beginTask("Searching for commented code...", files.length); try { var match; for each( file in files ) { monitor.subTask(file.getEclipseObject().getName()); file.removeMyTasks(); var previousLineCodeComment = false; for each( line in file.lines ) { if (monitor.isCanceled()) { return; } if (match = line.string.match(/^.*//.*[;{}]s*$/)) { if (!previousLineCodeComment) { line.addMyTask("Commented code: " + match[0]); } previousLineCodeComment = true; } else { previousLineCodeComment = false; } } monitor.worked(1); } } finally { monitor.done(); } } --- And burbled as it ran! ---
Hacking Monkey scripts is pretty easy if you know the Eclipse API and where to look for documentation. However, the docs are limited and if you are not really into Eclipse development, documentation and help is pretty rough. You can always google around and try to find some useful scripts on someone's blog though. ๐
4 comments
These examples made me giggle. ๐
Nice work! ๐
Jeroen Leenarts
Nice… I have been trying to come up with examples to highlight features for the new Eclipse Monkey and I think I will borrow yours. Btw, when you have a chance, look into Groovy Monkey, it is my port of Eclipse Monkey, with a bunch more features and will be the basis of the new Eclipse Monkey project.
James E. Ervin
For fun I rewrote your script to run in Groovy Monkey using the Groovy Language. I think it is straightforward to figure out. Notice you did not have to spawn the ProgressMonitorDialog to get out of the UI Thread.
James E. Ervin
Thank you James! I am glad the Eclipse Monkey project gets reincarnated as a new separate project. I think it’s a nice power tool and geek toy and it deserves an active project. I will look into GroovyMonkey some more, your rewritten code is a lot simpler. It’s also a good excuse to finally learn some Groovy. ๐
peterhe