Skip to content

Commit f899b88

Browse files
authored
[jsscripting] Extend auto-injection configuration with transformations & Update default (openhab#19130)
* [jsscripting] Extend auto-injection configuration with transformations Signed-off-by: Florian Hotze <dev@florianhotze.com>
1 parent c777ccf commit f899b88

4 files changed

Lines changed: 41 additions & 16 deletions

File tree

bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/GraalJSScriptEngineConfiguration.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public class GraalJSScriptEngineConfiguration {
3333
private static final String CFG_WRAPPER_ENABLED = "wrapperEnabled";
3434
private static final String CFG_DEPENDENCY_TRACKING_ENABLED = "dependencyTrackingEnabled";
3535

36-
public static final int INJECTION_DISABLED = 0;
37-
public static final int INJECTION_ENABLED_FOR_UI_BASED_SCRIPTS_ONLY = 2;
38-
public static final int INJECTION_ENABLED_FOR_ALL_SCRIPTS = 1;
36+
private static final int INJECTION_ENABLED_FOR_UI_BASED_SCRIPTS_ONLY = 1;
37+
private static final int INJECTION_ENABLED_FOR_UI_BASED_SCRIPTS_AND_TRANSFORMATIONS = 2;
38+
private static final int INJECTION_ENABLED_FOR_ALL_SCRIPTS = 3;
3939

40-
private int injectionEnabled = 2;
40+
private int injectionEnabled = INJECTION_ENABLED_FOR_ALL_SCRIPTS;
4141
private boolean injectionCachingEnabled = true;
4242
private boolean wrapperEnabled = true;
4343
private boolean dependencyTrackingEnabled = true;
@@ -91,8 +91,16 @@ private void update(Map<String, ?> config) {
9191
Boolean.class, true);
9292
}
9393

94-
public boolean isInjection(int type) {
95-
return type == injectionEnabled;
94+
public boolean isInjectionEnabledForUiBasedScript() {
95+
return injectionEnabled >= INJECTION_ENABLED_FOR_UI_BASED_SCRIPTS_ONLY;
96+
}
97+
98+
public boolean isInjectionEnabledForTransformations() {
99+
return injectionEnabled >= INJECTION_ENABLED_FOR_UI_BASED_SCRIPTS_AND_TRANSFORMATIONS;
100+
}
101+
102+
public boolean isInjectionEnabledForAllScripts() {
103+
return injectionEnabled == INJECTION_ENABLED_FOR_ALL_SCRIPTS;
96104
}
97105

98106
public boolean isInjectionCachingEnabled() {

bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ protected void beforeInvocation() {
307307
logger.debug("Evaluating cached global script for engine '{}' ...", engineIdentifier);
308308
delegate.getPolyglotContext().eval(GLOBAL_SOURCE);
309309

310-
if (configuration.isInjection(GraalJSScriptEngineConfiguration.INJECTION_ENABLED_FOR_ALL_SCRIPTS)
311-
|| (isUiBasedScript() && configuration.isInjection(
312-
GraalJSScriptEngineConfiguration.INJECTION_ENABLED_FOR_UI_BASED_SCRIPTS_ONLY))) {
310+
if (configuration.isInjectionEnabledForAllScripts()
311+
|| (isUiBasedScript() && configuration.isInjectionEnabledForUiBasedScript())
312+
|| (isTransformationScript() && configuration.isInjectionEnabledForTransformations())) {
313313
if (configuration.isInjectionCachingEnabled()) {
314314
logger.debug("Evaluating cached openhab-js injection for engine '{}' ...", engineIdentifier);
315315
delegate.getPolyglotContext().eval(OPENHAB_JS_SOURCE);
@@ -385,6 +385,21 @@ private boolean isUiBasedScript() {
385385
&& !engineIdentifier.startsWith(OPENHAB_TRANSFORMATION_SCRIPT);
386386
}
387387

388+
/**
389+
* Tests if the current script is a transformation script, i.e. it is created from the script transformation
390+
* service.
391+
*
392+
* @return true if the script is a transformation script, false otherwise
393+
*/
394+
private boolean isTransformationScript() {
395+
ScriptContext ctx = delegate.getContext();
396+
if (ctx == null) {
397+
logger.warn("Failed to retrieve script context from engine '{}'.", engineIdentifier);
398+
return false;
399+
}
400+
return engineIdentifier.startsWith(OPENHAB_TRANSFORMATION_SCRIPT);
401+
}
402+
388403
/**
389404
* Tests if this is a root node directory, `/node_modules`, `C:\node_modules`, etc...
390405
*

bundles/org.openhab.automation.jsscripting/src/main/resources/OH-INF/config/config.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
<label>Inject Global Variables from Helper Library</label>
2020
<description><![CDATA[
2121
Import all variables from the openHAB JavaScript library for common services like items, things, actions, log, etc... <br>
22-
If disabled, the openHAB JavaScript library can be imported manually using"<code>require('openhab')</code>.
22+
If disabled, the openHAB JavaScript library can be imported manually using <code>require('openhab')</code>.
2323
]]></description>
2424
<options>
25-
<option value="2">Auto injection enabled only for UI-based scripts (recommended)</option>
26-
<option value="1">Auto injection enabled for all scripts, i.e. including file-based scripts</option>
25+
<option value="3">Auto injection for all scripts, including file-based scripts and transformations</option>
26+
<option value="2">Auto injection for UI-based scripts and transformations</option>
27+
<option value="1">Auto injection only for UI-based scripts (recommended)</option>
2728
<option value="0">Disable auto-injection and import manually instead</option>
2829
</options>
29-
<default>2</default>
30+
<default>3</default>
3031
</parameter>
3132
<parameter name="wrapperEnabled" type="boolean" required="true" groupName="environment">
3233
<label>Wrap UI-based scripts in Self-Executing Function</label>

bundles/org.openhab.automation.jsscripting/src/main/resources/OH-INF/i18n/jsscripting.properties

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ automation.config.jsscripting.group.system.description = This group defines Java
1414
automation.config.jsscripting.injectionCachingEnabled.label = Cache openHAB JavaScript Library Injection
1515
automation.config.jsscripting.injectionCachingEnabled.description = Cache the openHAB JavaScript library injection for optimal performance.<br> Disable this option to allow loading the library from the local user configuration directory "automation/js/node_modules". Disabling caching may increase script loading times, especially on less powerful systems.
1616
automation.config.jsscripting.injectionEnabledV2.label = Inject Global Variables from Helper Library
17-
automation.config.jsscripting.injectionEnabledV2.description = Import all variables from the openHAB JavaScript library for common services like items, things, actions, log, etc... <br> If disabled, the openHAB JavaScript library can be imported manually using"<code>require('openhab')</code>.
18-
automation.config.jsscripting.injectionEnabledV2.option.2 = Auto injection enabled only for UI-based scripts (recommended)
19-
automation.config.jsscripting.injectionEnabledV2.option.1 = Auto injection enabled for all scripts, i.e. including file-based scripts
17+
automation.config.jsscripting.injectionEnabledV2.description = Import all variables from the openHAB JavaScript library for common services like items, things, actions, log, etc... <br> If disabled, the openHAB JavaScript library can be imported manually using <code>require('openhab')</code>.
18+
automation.config.jsscripting.injectionEnabledV2.option.3 = Auto injection for all scripts, including file-based scripts and transformations
19+
automation.config.jsscripting.injectionEnabledV2.option.2 = Auto injection for UI-based scripts and transformations
20+
automation.config.jsscripting.injectionEnabledV2.option.1 = Auto injection only for UI-based scripts (recommended)
2021
automation.config.jsscripting.injectionEnabledV2.option.0 = Disable auto-injection and import manually instead
2122
automation.config.jsscripting.wrapperEnabled.label = Wrap UI-based scripts in Self-Executing Function
2223
automation.config.jsscripting.wrapperEnabled.description = Wrapping UI-based scripts in a self-executing function allows the use of the <code>let</code> and <code>const</code> variable declarations, as well as the use of <code>function</code> and <code>class</code> declarations.<br> With this option enabled, you can also use <code>return</code> statements in your scripts to abort execution at any point.

0 commit comments

Comments
 (0)