Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jrubyscripting] Add JRuby REPL and other utilities inside karaf console #18374

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

jimtng
Copy link
Contributor

@jimtng jimtng commented Mar 10, 2025

Add Karaf console utilities for JRuby, including a REPL environment that runs inside the same script engine as normal rules.

openhab> jrubyscripting 
Usage: openhab:jrubyscripting info - displays information about JRuby Scripting add-on
Usage: openhab:jrubyscripting shell - starts an interactive JRuby shell
Usage: openhab:jrubyscripting gem - manages JRuby Scripting add-on's RubyGems
Usage: openhab:jrubyscripting update - updates the configured gems
Usage: openhab:jrubyscripting prune [-f|--force] - cleans up older versions in the .gem directory
Usage: openhab:jrubyscripting unload - unloads the console's script engine
openhab> jrubyscripting info                                                                                                   
JRuby 9.4.12.0
JRuby Scripting Library 5.35.1
GEM_HOME: /home/openhab/openhab-jruby/tmp/openhab/conf/automation/ruby/.gem/9.4.12.0
RUBYLIB: /home/openhab/openhab-jruby/tmp/openhab/conf/automation/lib/ruby/personal
Script path: /home/openhab/openhab-jruby/tmp/openhab/conf/automation/ruby

JRuby Scripting Add-on Configuration:
=====================================
rubylib: /home/openhab/openhab-jruby/tmp/openhab/conf/automation/lib/ruby/personal
local_variable: transient
local_context: singlethread
gems: openhab-scripting=~>5.0
gem_home: /home/openhab/openhab-jruby/tmp/openhab/conf/automation/ruby/.gem/{RUBY_ENGINE_VERSION}
dependency_tracking: true
require: openhab/dsl
check_update: true
openhab> jrubyscripting prune                                                                                                  
Some files and directories exist in '/openhab/conf/automation/ruby/.gem' outside of your configured gem home '/openhab/conf/automation/ruby/.gem/9.4.12.0'.
They may have been left over from previous installations or updates.

  /openhab/conf/automation/ruby/.gem/9.4.9.0/
  /openhab/conf/automation/ruby/.gem/test1/
  /openhab/conf/automation/ruby/.gem/test2/
  /openhab/conf/automation/ruby/.gem/test3/

Press Enter to delete them or Ctrl+C to cancel.

Deleting: /openhab/conf/automation/ruby/.gem/test3/4/5/6
Deleting: /openhab/conf/automation/ruby/.gem/test3/4/5
Deleting: /openhab/conf/automation/ruby/.gem/test3/4
Deleting: /openhab/conf/automation/ruby/.gem/test3
Deleting: /openhab/conf/automation/ruby/.gem/test2
Deleting: /openhab/conf/automation/ruby/.gem/test1
Deleting: /openhab/conf/automation/ruby/.gem/9.4.9.0
openhab> jrubyscripting prune                                                                                                  
No files or directories to delete from /openhab/conf/automation/ruby/.gem
image

The REPL shell supports:

  • A rudimentary tab completion on item names, constants, DSL methods, and local variables.
  • History recall (up/down arrow) within the same session

This next screenshot shows it running in my production instance:
image

An alternative implementation can be done should openhab/openhab-core#4635 be merged. For the time being, this PR stands alone and will work without the linked core PR. When the core PR is merged and this is refactored, we can remove the dependency to karaf in pom.xml.

If the core PR gets merged, I can refactor this later if necessary. I don't want the core PR be a blocker for this PR though.

@jimtng jimtng added the enhancement An enhancement or new feature for an existing add-on label Mar 10, 2025
@jimtng jimtng requested a review from ccutrer as a code owner March 10, 2025 11:16
@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from 6b2209c to 2eb74b6 Compare March 10, 2025 11:20
@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from 2eb74b6 to 547ac87 Compare March 10, 2025 11:49
@jimtng jimtng changed the title [jrubyscripting] Add JRuby shell and other utilities inside karaf console [jrubyscripting] Add JRuby REPL and other utilities inside karaf console Mar 10, 2025
@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from 547ac87 to 81d7750 Compare March 10, 2025 14:53
@jimtng
Copy link
Contributor Author

jimtng commented Mar 11, 2025

Updated to use repl code from the helper library openhab/openhab-jruby#405

@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from 6d8eca4 to 9f01a5d Compare March 11, 2025 14:15
Copy link
Contributor

@ccutrer ccutrer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to run mvn i18n-generate-default-translations, no?

<description><![CDATA[The script file to be required by the <tt>jrubyscripting console</tt>
console command.]]></description>
<default>openhab/console/irb</default>
<advanced>true</advanced>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the whole group is already advanced, does the individual item need other be advanced?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried removing the advanced tag from the individual items, whilst keeping it in the group. This would make them show up even when the "advanced" checkbox isn't selected. It seems like a "bug" somewhere, but for the time being we'll have to stick with how it goes.

}

executeWithFullJRuby(console, engine -> {
engine.getContext().setAttribute("$console", console, ScriptContext.ENGINE_SCOPE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need console? or would session be enough? (really, all I'm using right now is terminal - which you get from session). then you don't event need the JRubyConsole. in the one or two places that need session, just pass it through from execute().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to just $terminal. However, we still need JRubyConsole because the OSGiConsole class from core is in an internal package. We still want to have a console object to keep a resemblance to the standard ConsoleCommandExtension in case we need to refactor this back if openhab/openhab-core#4635 is accepted.

@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from e8d4c09 to 04d2f9f Compare March 12, 2025 00:10
@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from 04d2f9f to 0618843 Compare March 12, 2025 00:14
@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from a4ecdce to c94593c Compare March 12, 2025 10:50
@jimtng jimtng force-pushed the jruby-console-without-core-changes branch from c94593c to 2411ea4 Compare March 12, 2025 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature for an existing add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants