diff --git a/README.md b/README.md index dfbdc4c..d4ab5c0 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ The builder offers various methods to configure the runner: - `#registerFile(String, Function)`, `#registerFiles(Map>)`: Register file creators that allow scripts to aggregate information into output files. - `#addDefaultUtils(boolean)`: if set to `true`, The script will know about the `arrays`, `stream`, `class`, and `collections` utility namespaces. - `#runHandler(com.swisscom.aem.tools.jcrhopper.config.RunHandler)`: Set a listener that is informed about script output and events. The default run handler does nothing (but log messages are logged using Slf4j in any case). +- `#scriptEngineManager(javax.script.ScriptEngineManager)`: Set the script engine manager to use when searching for non-JEXL scripting engines. Once the builder is configured, it can be turned into a runner with either `#build(Script)` or `#build(String)` (for JSON scripts). diff --git a/mock/script-builder.html b/mock/script-builder.html index 88d704a..d2f45b5 100644 --- a/mock/script-builder.html +++ b/mock/script-builder.html @@ -42,6 +42,7 @@
diff --git a/src/main/content/jcr_root/apps/jcr-hopper/script-builder/build-area/build-area.html b/src/main/content/jcr_root/apps/jcr-hopper/script-builder/build-area/build-area.html index 1bba759..7dc5731 100644 --- a/src/main/content/jcr_root/apps/jcr-hopper/script-builder/build-area/build-area.html +++ b/src/main/content/jcr_root/apps/jcr-hopper/script-builder/build-area/build-area.html @@ -1 +1,6 @@ -
+
diff --git a/src/main/frontend/App.tsx b/src/main/frontend/App.tsx index 8ec40fc..a0991b9 100644 --- a/src/main/frontend/App.tsx +++ b/src/main/frontend/App.tsx @@ -9,7 +9,10 @@ import { HistoryUpdater, useHistoryImmutable } from './hooks/useHistoryImmutable import { useOnce } from './hooks/useOnce'; import { ScriptEditor } from './sections/ScriptEditor'; -export const RunEndpointContext = createContext(''); +export const EnvironmentContext = createContext({ + runEndpoint: '/', + validScriptingLanguages: {} as Record, +}); export const ScriptContext = createContext>(null!); const RootElement = styled('div')` @@ -78,7 +81,7 @@ const RootElement = styled('div')` } `; -export const App: FC<{ runEndpoint: string }> = props => { +export const App: FC<{ runEndpoint: string; validScriptingLanguages: Record }> = props => { const initialScript = useOnce(getInitialScript); const scriptContext = useHistoryImmutable(initialScript, current => { @@ -86,7 +89,7 @@ export const App: FC<{ runEndpoint: string }> = props => { }); return ( - + @@ -94,6 +97,6 @@ export const App: FC<{ runEndpoint: string }> = props => { - + ); }; diff --git a/src/main/frontend/editor.tsx b/src/main/frontend/editor.tsx index 2fb82ae..862ec5b 100644 --- a/src/main/frontend/editor.tsx +++ b/src/main/frontend/editor.tsx @@ -11,11 +11,11 @@ patchCoralUiCreateElement(); function init() { const target = document.querySelector('.jcr-hopper-builder')!; - const { runEndpoint } = target.dataset; + const { runEndpoint, validScriptingLanguages } = target.dataset; const root = createRoot(target.parentElement!.parentElement!); root.render( - + , ); } diff --git a/src/main/frontend/model/hops/runScript.ts b/src/main/frontend/model/hops/runScript.ts index d97e51c..ef2297f 100644 --- a/src/main/frontend/model/hops/runScript.ts +++ b/src/main/frontend/model/hops/runScript.ts @@ -1,6 +1,6 @@ import { AnyHop } from '.'; -export const SCRIPT_LANGUAGES = { +export const SCRIPT_LANGUAGES: Record = { jexl: 'JEXL', js: 'JavaScript', }; @@ -8,7 +8,7 @@ export const SCRIPT_LANGUAGES = { export interface Type extends AnyHop { type: 'runScript'; code: string; - extension: keyof typeof SCRIPT_LANGUAGES; + extension: string; putLocalsBackIntoScope?: boolean; } @@ -22,7 +22,7 @@ export const title = 'Run a Script'; export function shortDescription(config: Type) { const lang = config.extension ?? 'js'; - const name = SCRIPT_LANGUAGES[lang] ?? lang.toUpperCase(); + const name = lang in SCRIPT_LANGUAGES ? SCRIPT_LANGUAGES[lang] : lang.toUpperCase(); const lines = config.code?.split(/\n/).filter(Boolean).length; if (!lines) { diff --git a/src/main/frontend/sections/RunControls.tsx b/src/main/frontend/sections/RunControls.tsx index 8c920c3..5065836 100644 --- a/src/main/frontend/sections/RunControls.tsx +++ b/src/main/frontend/sections/RunControls.tsx @@ -1,7 +1,7 @@ import React, { FC, FormEvent, useContext, useRef } from 'react'; import { styled } from 'goober'; -import { RunEndpointContext, ScriptContext } from '../App'; +import { EnvironmentContext, ScriptContext } from '../App'; const Elm = styled('form', React.forwardRef)` grid-auto-flow: row; @@ -13,7 +13,7 @@ const Elm = styled('form', React.forwardRef)` export const RunControls: FC<{ runWith: (data: FormData) => Promise }> = ({ runWith }) => { const { current: script } = useContext(ScriptContext); - const endpoint = useContext(RunEndpointContext); + const environmentContext = useContext(EnvironmentContext); const formRef = useRef(null); @@ -31,7 +31,14 @@ export const RunControls: FC<{ runWith: (data: FormData) => Promise }> = ( } return ( - + {script.parameters.length ? (
Arguments diff --git a/src/main/frontend/sections/Runner.tsx b/src/main/frontend/sections/Runner.tsx index e0c9501..3d7fa81 100644 --- a/src/main/frontend/sections/Runner.tsx +++ b/src/main/frontend/sections/Runner.tsx @@ -1,17 +1,17 @@ import React, { FC, useContext, useState } from 'react'; -import { RunEndpointContext } from '../App'; +import { EnvironmentContext } from '../App'; import { Output } from './Output'; import { Run } from '../model/Run'; import { RunControls } from './RunControls'; export const Runner: FC = () => { - const endpoint = useContext(RunEndpointContext); + const environmentContext = useContext(EnvironmentContext); const [runs, setRuns] = useState([]); async function runWith(data: FormData) { - const response = fetch(endpoint, { + const response = fetch(environmentContext.runEndpoint, { method: 'POST', body: data, }); diff --git a/src/main/frontend/sections/editor/types/RunScriptStep.tsx b/src/main/frontend/sections/editor/types/RunScriptStep.tsx index ccb55e5..04eea38 100644 --- a/src/main/frontend/sections/editor/types/RunScriptStep.tsx +++ b/src/main/frontend/sections/editor/types/RunScriptStep.tsx @@ -3,26 +3,28 @@ import React, { forwardRef, useContext } from 'react'; import { Hop } from '../../../model/hops'; import { StepEditor } from '../../../widgets/StepEditor'; -import { SCRIPT_LANGUAGES, shortDescription, title, Type, iconFor } from '../../../model/hops/runScript'; +import { shortDescription, title, Type, iconFor } from '../../../model/hops/runScript'; import { Help } from '../../../widgets/Help'; import { Select } from '../../../widgets/Select'; -import { CodeEditor } from '../../../widgets/CodeEditor'; -import { ScriptContext } from '../../../App'; +import { CodeEditor, EditorLanguage } from '../../../widgets/CodeEditor'; +import { EnvironmentContext, ScriptContext } from '../../../App'; import { Switch } from '../../../widgets/Switch'; export const RunScriptStep = forwardRef(function RunScriptStep({ parentHops, hop }, ref) { const scriptContext = useContext(ScriptContext); + const { validScriptingLanguages } = useContext(EnvironmentContext); + const languageName = validScriptingLanguages[hop.extension]; return (