|
1 | | -function toLittleHorseSkeleton(model) { |
| 1 | +function toLittleHorseSkeleton(model, options = {}) { |
| 2 | + const realCode = options.realCode === true; |
2 | 3 | const processes = (model.body || []).filter((node) => node.type === "process"); |
3 | 4 |
|
4 | 5 | if (processes.length === 0) { |
5 | 6 | throw new Error("No LittleHorse-exportable blocks found. Supported block types: process."); |
6 | 7 | } |
7 | 8 |
|
8 | | - const lines = [ |
9 | | - "// OrgScript -> LittleHorse workflow skeleton", |
10 | | - "// This is a scaffold. Translate it to your LittleHorse SDK and task definitions.", |
11 | | - "", |
12 | | - ]; |
| 9 | + const lines = []; |
| 10 | + if (!realCode) { |
| 11 | + lines.push( |
| 12 | + "// OrgScript -> LittleHorse workflow skeleton", |
| 13 | + "// This is a scaffold. Translate it to your LittleHorse SDK and task definitions.", |
| 14 | + "" |
| 15 | + ); |
| 16 | + } |
13 | 17 |
|
14 | 18 | processes.forEach((processNode) => { |
15 | 19 | const className = sanitizeClassName(processNode.name); |
16 | 20 | const workflowName = toKebabName(processNode.name); |
17 | 21 | lines.push(`public final class ${className}Workflow {`); |
18 | | - lines.push(" // TODO: adapt this skeleton to your LittleHorse SDK version."); |
| 22 | + if (!realCode) { |
| 23 | + lines.push(" // TODO: adapt this skeleton to your LittleHorse SDK version."); |
| 24 | + } |
19 | 25 | lines.push(" public static Workflow getWorkflow() {"); |
20 | 26 | lines.push(` return new WorkflowImpl("${workflowName}", wf -> {`); |
21 | | - lines.push(` // Process: ${processNode.name}`); |
| 27 | + if (!realCode) { |
| 28 | + lines.push(` // Process: ${processNode.name}`); |
| 29 | + } |
22 | 30 | const declarations = renderDeclarations(processNode.body || [], 6); |
23 | 31 | if (declarations.length > 0) { |
24 | | - lines.push(" // TODO: declare variables and task definitions"); |
| 32 | + if (!realCode) { |
| 33 | + lines.push(" // TODO: declare variables and task definitions"); |
| 34 | + } |
25 | 35 | lines.push(...declarations); |
26 | | - lines.push(""); |
| 36 | + if (!realCode) { |
| 37 | + lines.push(""); |
| 38 | + } |
27 | 39 | } else { |
28 | | - lines.push(" // TODO: declare variables and task definitions"); |
| 40 | + if (!realCode) { |
| 41 | + lines.push(" // TODO: declare variables and task definitions"); |
| 42 | + } |
29 | 43 | } |
30 | 44 |
|
31 | | - const bodyLines = renderStatements(processNode.body || [], 6); |
| 45 | + const bodyLines = renderStatements(processNode.body || [], 6, { realCode }); |
32 | 46 | if (bodyLines.length > 0) { |
33 | 47 | lines.push(...bodyLines); |
34 | 48 | } else { |
35 | | - lines.push(" // TODO: add workflow steps"); |
| 49 | + if (!realCode) { |
| 50 | + lines.push(" // TODO: add workflow steps"); |
| 51 | + } |
36 | 52 | } |
37 | 53 |
|
38 | 54 | lines.push(" });"); |
@@ -172,56 +188,65 @@ function renderDeclaration(entry) { |
172 | 188 | return `var ${variableName} = wf.declare${typeSuffix}("${variableName}");`; |
173 | 189 | } |
174 | 190 |
|
175 | | -function renderStatements(statements, indentSize) { |
| 191 | +function renderStatements(statements, indentSize, options = {}) { |
176 | 192 | const lines = []; |
177 | 193 | const indent = " ".repeat(indentSize); |
| 194 | + const realCode = options.realCode === true; |
178 | 195 |
|
179 | 196 | for (const statement of statements) { |
180 | 197 | if (statement.type === "when") { |
181 | | - lines.push(`${indent}// when ${statement.trigger || "unknown"}`); |
| 198 | + if (!realCode) { |
| 199 | + lines.push(`${indent}// when ${statement.trigger || "unknown"}`); |
| 200 | + } |
182 | 201 | continue; |
183 | 202 | } |
184 | 203 |
|
185 | 204 | if (statement.type === "if") { |
186 | 205 | lines.push(`${indent}wf.doIf(/* ${formatCondition(statement.condition)} */, ifBody -> {`); |
187 | | - lines.push(...renderStatements(statement.then || [], indentSize + 2)); |
| 206 | + lines.push(...renderStatements(statement.then || [], indentSize + 2, options)); |
188 | 207 | lines.push(`${indent}})`); |
189 | 208 |
|
190 | 209 | const elseIfBranches = statement.elseIf || []; |
191 | 210 | for (const branch of elseIfBranches) { |
192 | 211 | lines.push(`${indent}.doElseIf(/* ${formatCondition(branch.condition)} */, elseIfBody -> {`); |
193 | | - lines.push(...renderStatements(branch.then || [], indentSize + 2)); |
| 212 | + lines.push(...renderStatements(branch.then || [], indentSize + 2, options)); |
194 | 213 | lines.push(`${indent}})`); |
195 | 214 | } |
196 | 215 |
|
197 | 216 | if (statement.else && (statement.else.body || []).length > 0) { |
198 | 217 | lines.push(`${indent}.doElse(elseBody -> {`); |
199 | | - lines.push(...renderStatements(statement.else.body || [], indentSize + 2)); |
| 218 | + lines.push(...renderStatements(statement.else.body || [], indentSize + 2, options)); |
200 | 219 | lines.push(`${indent}});`); |
201 | 220 | } else { |
202 | | - lines.push(`${indent};`); |
| 221 | + const lastIndex = lines.length - 1; |
| 222 | + lines[lastIndex] = `${lines[lastIndex]};`; |
203 | 223 | } |
204 | 224 | continue; |
205 | 225 | } |
206 | 226 |
|
207 | 227 | if (statement.type === "stop") { |
208 | | - lines.push(`${indent}// stop`); |
| 228 | + if (!realCode) { |
| 229 | + lines.push(`${indent}// stop`); |
| 230 | + } |
209 | 231 | continue; |
210 | 232 | } |
211 | 233 |
|
212 | | - const action = formatAction(statement); |
213 | | - if (action) { |
| 234 | + const action = formatAction(statement, { realCode }); |
| 235 | + if (action && (!realCode || !action.startsWith("//"))) { |
214 | 236 | lines.push(`${indent}${action}`); |
215 | 237 | continue; |
216 | 238 | } |
217 | 239 |
|
218 | | - lines.push(`${indent}// ${statement.type}`); |
| 240 | + if (!realCode) { |
| 241 | + lines.push(`${indent}// ${statement.type}`); |
| 242 | + } |
219 | 243 | } |
220 | 244 |
|
221 | 245 | return lines; |
222 | 246 | } |
223 | 247 |
|
224 | | -function formatAction(statement) { |
| 248 | +function formatAction(statement, options = {}) { |
| 249 | + const realCode = options.realCode === true; |
225 | 250 | if (statement.type === "assign") { |
226 | 251 | return `// assign ${statement.target || "?"} = ${formatExpression(statement.value)}`; |
227 | 252 | } |
@@ -250,6 +275,9 @@ function formatAction(statement) { |
250 | 275 | return `wf.execute("require", "${requirement}");`; |
251 | 276 | } |
252 | 277 |
|
| 278 | + if (!realCode) { |
| 279 | + return `// ${statement.type}`; |
| 280 | + } |
253 | 281 | return null; |
254 | 282 | } |
255 | 283 |
|
|
0 commit comments