Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active July 10, 2023 04:06
Show Gist options
  • Save sandipchitale/6f53c646ec00752d41cddcca243d76cf to your computer and use it in GitHub Desktop.
Save sandipchitale/6f53c646ec00752d41cddcca243d76cf to your computer and use it in GitHub Desktop.
Run groovysh in Gradle build context #gradle #groovysh

Usage

Use the following command to execute groovysh task in the context of the project. The project, settings and gradle bindings are set.

> .\gradlew -q --no-daemon --console=plain --init-script groovysh-task.gradle groovysh
...
...
groovy:000> :inspect project
groovy:000> :inspect settings
groovy:000> :inspect gradle

image

BTW this is implemented by VSCode extension:

Extend Gradle for Java extention

Once the PR refered to by the issue:

https://issues.apache.org/jira/browse/GROOVY-10661

lands in the groovy inside gradle, you will be able to launch ObjectBrowser and drill down in any objects in the build to any depth you wish. The PR is already merged in the master branch of Groovy and will be available in next 4.x and 5.x release.

image

Someday I will submit a PR to use a TreeTable UI instead of a Table UI for ObjectBrowser. This will avoid proliferation of windows. Instead we will be able to expand Tree nodes of TreeTable to drill down.

image

Actually this has been implemented in:

Extend Gradle for Java extention

gradle.projectsLoaded {
rootProject {
afterEvaluate { project ->
if (!project.repositories.any{it.name == 'MavenRepo'}) {
project.repositories {
// To be able to load org.codehaus.groovy:groovy-groovysh
mavenCentral()
}
}
project.configurations {
groovyshdependencies
}
project.dependencies {
groovyshdependencies("org.codehaus.groovy:groovy-groovysh:${GroovySystem.version}") {
exclude group: 'org.codehaus.groovy'
}
}
project.tasks.register('groovysh') {
group 'debug'
description 'Runs an interactive shell in the context of the project.'
doLast {
URLClassLoader groovyObjectClassLoader = GroovyObject.class.classLoader
def groovyshClass
def groovyShell
// Add dependency jars to classloader
configurations.groovyshdependencies.each {File file ->
groovyObjectClassLoader.addURL(file.toURL())
}
Class.forName('jline.console.history.FileHistory', true, groovyObjectClassLoader)
groovyshClass = Class.forName('org.codehaus.groovy.tools.shell.Groovysh', true, groovyObjectClassLoader)
if (groovyshClass) {
groovyShell = groovyshClass.newInstance()
}
if (groovyShell) {
groovyShell.interp.context.variables.put("gradle", gradle)
groovyShell.interp.context.variables.put("settings", gradle.settings)
groovyShell.interp.context.variables.put("project", project)
groovyShell.run('')
}
}
}
}
}
}
gradle.projectsLoaded {
rootProject {
afterEvaluate { project ->
if (!project.repositories.any{it.name == 'MavenRepo'}) {
project.repositories {
// To be able to load org.apache.groovy:groovy-groovysh and dependencies
mavenCentral {
content {
includeGroup 'org.apache.groovy'
includeGroup 'jline'
includeGroup 'com.github.javaparser'
includeGroup 'org.ow2.asm'
includeGroup 'org.abego.treelayout'
includeGroup 'org.apache.ivy'
}
}
}
}
project.configurations {
groovyshdependencies
}
project.dependencies {
groovyshdependencies "org.apache.groovy:groovy-groovysh:4.0.13"
}
project.tasks.register('groovysh') {
group 'debug'
description 'Runs an interactive shell in the context of the project. Use :inspect command to inspect project, gradle, settings or other objects.'
doLast {
URLClassLoader groovyshClassLoader = new URLClassLoader();
configurations.groovyshdependencies.each {File file ->
groovyshClassLoader.addURL(file.toURI().toURL())
}
def fileHistoryClass
def groovyshClass
def groovyShell
fileHistoryClass = Class.forName('jline.console.history.FileHistory', true, groovyshClassLoader)
groovyshClass = Class.forName('org.apache.groovy.groovysh.Groovysh', true, groovyshClassLoader)
if (groovyshClass) {
groovyShell = groovyshClass.newInstance()
if (groovyShell) {
groovyShell.interp.context.variables.put("gradle", gradle)
groovyShell.interp.context.variables.put("settings", gradle.settings)
groovyShell.interp.context.variables.put("project", project)
groovyShell.run('# Available objects: gradle, settings, project\n# Try :inspect project')
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment