Skip to content

Commit

Permalink
Merge branch 'openhab:main' into openhab#13456-hue-clip2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfg authored Nov 30, 2022
2 parents 7a34cac + ae677dc commit 3ef1de5
Show file tree
Hide file tree
Showing 34 changed files with 1,744 additions and 1,367 deletions.
23 changes: 15 additions & 8 deletions bundles/org.openhab.automation.jrubyscripting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ After installing this add-on, you will find configuration options in the openHAB

Alternatively, JRuby configuration parameters may be set by creating a `jruby.cfg` file in `conf/services/`

| Parameter | Default | Description |
| ----------------------------------------------------- | --------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| org.openhab.automation.jrubyscripting:gem_home | $OPENHAB_CONF/scripts/lib/ruby/gem_home | Location ruby gems will be installed and loaded, directory will be created if missing and gem installs are specified |
| org.openhab.automation.jrubyscripting:rubylib | $OPENHAB_CONF/automation/lib/ruby/ | Search path for user libraries. Separate each path with a colon (semicolon in Windows). |
| org.openhab.automation.jrubyscripting:local_context | singlethread | The local context holds Ruby runtime, name-value pairs for sharing variables between Java and Ruby. See [this](https://github.com/jruby/jruby/wiki/RedBridge#Context_Instance_Type) for options and details |
| org.openhab.automation.jrubyscripting:local_variables | transient | Defines how variables are shared between Ruby and Java. See [this](https://github.com/jruby/jruby/wiki/RedBridge#local-variable-behavior-options) for options and details |
| org.openhab.automation.jrubyscripting:gems | | A comma separated list of [Ruby Gems](https://rubygems.org/) to install. |
| org.openhab.automation.jrubyscripting:require | | A comma separated list of script names to be required by the JRuby Scripting Engine at the beginning of user scripts. |
| Parameter | Default | Description |
| ----------------------------------------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| org.openhab.automation.jrubyscripting:gem_home | $OPENHAB_CONF/scripts/lib/ruby/gem_home | Location ruby gems will be installed and loaded, directory will be created if missing and gem installs are specified |
| org.openhab.automation.jrubyscripting:rubylib | $OPENHAB_CONF/automation/lib/ruby/ | Search path for user libraries. Separate each path with a colon (semicolon in Windows). |
| org.openhab.automation.jrubyscripting:local_context | singlethread | The local context holds Ruby runtime, name-value pairs for sharing variables between Java and Ruby. See [this](https://github.com/jruby/jruby/wiki/RedBridge#Context_Instance_Type) for options and details |
| org.openhab.automation.jrubyscripting:local_variables | transient | Defines how variables are shared between Ruby and Java. See [this](https://github.com/jruby/jruby/wiki/RedBridge#local-variable-behavior-options) for options and details |
| org.openhab.automation.jrubyscripting:gems | | A comma separated list of [Ruby Gems](https://rubygems.org/) to install. |
| org.openhab.automation.jrubyscripting:require | | A comma separated list of script names to be required by the JRuby Scripting Engine at the beginning of user scripts. |
| org.openhab.automation.jrubyscripting:check_update | true | Check RubyGems for updates to the above gems when OpenHAB starts or JRuby settings are changed. Otherwise it will try to fulfil the requirements with locally installed gems, and you can manage them yourself with an external Ruby by setting the same GEM_HOME. |

## Ruby Gems

This automation add-on will install user specified gems and make them available on the library search path.
Gem versions may be specified using the standard ruby gem_name=version format.
The version number follows the [pessimistic version constraint](https://guides.rubygems.org/patterns/#pessimistic-version-constraint) syntax.
Multiple version specifiers can be added by separating them with a semicolon.

For example this configuration will install the latest version of the [openHAB JRuby Scripting Library](https://boc-tothefuture.github.io/openhab-jruby/), and instruct the scripting engine to automatically insert `require 'openhab'` at the start of the script.

Expand All @@ -31,6 +32,12 @@ org.openhab.automation.jrubyscripting:gems=openhab-scripting
org.openhab.automation.jrubyscripting:require=openhab
```

Example with multiple version specifiers:

```text
org.openhab.automation.jrubyscripting:gems=library= >= 2.2.0; < 3.0, another-gem= > 4.0.0.a; < 5
```

## Creating JRuby Scripts

When this add-on is installed, you can select JRuby as a scripting language when creating a script action within the rule editor of the UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,23 @@ private synchronized void configureGems(ScriptEngine engine) {
int validGems = 0;
for (String gem : gems) {
gem = gem.trim();
String version = "";
String[] versions = {};
if (gem.contains("=")) {
String[] gemParts = gem.split("=");
String[] gemParts = gem.split("=", 2);
gem = gemParts[0].trim();
version = gemParts[1].trim();
versions = gemParts[1].split(";");
}

if (gem.isEmpty()) {
continue;
}

gemCommand += " gem '" + gem + "'";
if (!version.isEmpty()) {
gemCommand += ", '" + version + "'";
for (String version : versions) {
version = version.trim();
if (!version.isEmpty()) {
gemCommand += ", '" + version + "'";
}
}
gemCommand += ", require: false\n";
validGems += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
*/
package org.openhab.automation.jsscripting.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.script.ScriptEngine;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.automation.jsscripting.internal.fs.watch.JSDependencyTracker;
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.openhab.core.config.core.ConfigurableService;
import org.osgi.framework.Constants;
Expand All @@ -39,21 +40,22 @@
public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
private static final String CFG_INJECTION_ENABLED = "injectionEnabled";
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
private final JSDependencyTracker jsDependencyTracker;
private boolean injectionEnabled = true;

public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021";
private final JSScriptServiceUtil jsScriptServiceUtil;

@Activate
public GraalJSScriptEngineFactory(final @Reference JSScriptServiceUtil jsScriptServiceUtil,
Map<String, Object> config) {
final @Reference JSDependencyTracker jsDependencyTracker, Map<String, Object> config) {
this.jsDependencyTracker = jsDependencyTracker;
this.jsScriptServiceUtil = jsScriptServiceUtil;
modified(config);
}

@Override
public List<String> getScriptTypes() {
List<String> scriptTypes = new ArrayList<>();

/*
* Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
Expand All @@ -66,9 +68,7 @@ public List<String> getScriptTypes() {
// scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
// scriptTypes.addAll(graalJSEngineFactory.getExtensions());

scriptTypes.add(MIME_TYPE);

return Collections.unmodifiableList(scriptTypes);
return List.of(MIME_TYPE);
}

@Override
Expand All @@ -82,6 +82,11 @@ public ScriptEngine createScriptEngine(String scriptType) {
new OpenhabGraalJSScriptEngine(injectionEnabled ? INJECTION_CODE : null, jsScriptServiceUtil));
}

@Override
public @Nullable ScriptDependencyTracker getDependencyTracker() {
return jsDependencyTracker;
}

@Modified
protected void modified(Map<String, ?> config) {
Object injectionEnabled = config.get(CFG_INJECTION_ENABLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@

import java.io.File;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.OpenHAB;
import org.openhab.core.automation.module.script.rulesupport.loader.DependencyTracker;
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptDependencyTracker;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -24,7 +32,9 @@
*
* @author Jonathan Gilbert - Initial contribution
*/
public class JSDependencyTracker extends DependencyTracker {
@Component(service = JSDependencyTracker.class)
@NonNullByDefault
public class JSDependencyTracker extends AbstractScriptDependencyTracker {

private final Logger logger = LoggerFactory.getLogger(JSDependencyTracker.class);

Expand All @@ -35,6 +45,7 @@ public JSDependencyTracker() {
super(LIB_PATH);
}

@Activate
public void activate() {
File directory = new File(LIB_PATH);
if (!directory.exists()) {
Expand All @@ -47,4 +58,18 @@ public void activate() {

super.activate();
}

@Deactivate
public void deactivate() {
super.deactivate();
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, unbind = "removeChangeTracker")
public void addChangeTracker(ScriptDependencyTracker.Listener listener) {
super.addChangeTracker(listener);
}

public void removeChangeTracker(ScriptDependencyTracker.Listener listener) {
super.removeChangeTracker(listener);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,30 @@

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
import org.openhab.core.automation.module.script.ScriptEngineManager;
import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
import org.openhab.core.service.ReadyService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
*
* @author Jonathan Gilbert - Initial contribution
*/
public class JSScriptFileWatcher extends ScriptFileWatcher {
@Component(immediate = true, service = ScriptDependencyTracker.Listener.class)
public class JSScriptFileWatcher extends AbstractScriptFileWatcher {
private static final String FILE_DIRECTORY = "automation" + File.separator + "js";

private final String ignorePath;

public JSScriptFileWatcher(final ScriptEngineManager manager, final ReadyService readyService,
JSDependencyTracker dependencyTracker) {
super(manager, dependencyTracker, readyService, FILE_DIRECTORY);
@Activate
public JSScriptFileWatcher(final @Reference ScriptEngineManager manager,
final @Reference ReadyService readyService) {
super(manager, readyService, FILE_DIRECTORY);

ignorePath = pathToWatch + File.separator + "node_modules";
}
Expand Down
Loading

0 comments on commit 3ef1de5

Please sign in to comment.