From 78e1eebcd94171dfe517bcfd17a821fed5fbd616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Die=C3=9Fner?= Date: Wed, 24 Feb 2021 15:31:09 +0100 Subject: [PATCH 1/5] Added possibility to add multiple functions per step --- engine/engine.ts | 23 +++- engine/parser.def | 4 +- engine/parser.ts | 7 +- engine/run_command.ts | 9 ++ engine/runner.ts | 13 +- runners/console/index.ts | 271 +++++++++++++++++++------------------- runners/katacoda/index.ts | 142 ++++++++++---------- 7 files changed, 246 insertions(+), 223 deletions(-) create mode 100644 engine/run_command.ts diff --git a/engine/engine.ts b/engine/engine.ts index 3aafebd7..6437afdd 100644 --- a/engine/engine.ts +++ b/engine/engine.ts @@ -1,6 +1,7 @@ import { Environment } from "./environment"; import { Playbook } from "./playbook"; import { Runner } from "./runner"; +import { RunCommand } from "./run_command"; import { RunResult } from "./run_result"; @@ -24,25 +25,35 @@ export class Engine { (await this.getRunner(this.environment.runners[runnerIndex])).init(this.playbook); } - mainloop: for (let stepIndex in this.playbook.steps) { - for (let lineIndex in this.playbook.steps[stepIndex].lines) { + mainloop: for (let stepIndex = 0; stepIndex < this.playbook.steps.length; stepIndex++) { + for (let lineIndex = 0; lineIndex < this.playbook.steps[stepIndex].lines.length; lineIndex++) { + let runCommand = new RunCommand(); + if(lineIndex == 0) { + runCommand.text = this.playbook.steps[stepIndex].text; + } + if(lineIndex == (this.playbook.steps[stepIndex].lines.length - 1)){ + runCommand.textAfter = this.playbook.steps[stepIndex].textAfter; + } + runCommand.command = this.playbook.steps[stepIndex].lines[lineIndex]; + runCommand.stepIndex = stepIndex; + runCommand.lineIndex = lineIndex; let foundRunnerToExecuteCommand = false; for (let runnerIndex in this.environment.runners) { let runner = await this.getRunner(this.environment.runners[runnerIndex]); if (runner.supports(this.playbook.steps[stepIndex].lines[lineIndex].name)) { var result = new RunResult(); - if(runner.commandIsSkippable(this.playbook.steps[stepIndex].lines[lineIndex].name)) { - console.log("Command " + this.playbook.steps[stepIndex].lines[lineIndex].name + " will be skipped."); + if(runner.commandIsSkippable(runCommand.command.name)) { + console.log("Command " + runCommand.command.name + " will be skipped."); continue; } try { - result = runner.run(this.playbook.steps[stepIndex], this.playbook.steps[stepIndex].lines[lineIndex]); + result = runner.run(runCommand); } catch (e) { result.exceptions.push(e); } - await runner.assert(this.playbook.steps[stepIndex], this.playbook.steps[stepIndex].lines[lineIndex], result); + await runner.assert(runCommand, result); foundRunnerToExecuteCommand = true; break; diff --git a/engine/parser.def b/engine/parser.def index b08174ca..3ea58558 100644 --- a/engine/parser.def +++ b/engine/parser.def @@ -46,10 +46,10 @@ steptextafterline = !"====" string __ steplines - = stepline+ + = stepline+ { return { "steplines": text()}; } stepline - = !"--" string __ { return { "stepline": text()}; } + = !"--" string __ string "string" = [^\r\n]+ { return text(); } diff --git a/engine/parser.ts b/engine/parser.ts index 2208d658..7b04e0fb 100644 --- a/engine/parser.ts +++ b/engine/parser.ts @@ -12,7 +12,6 @@ export class Parser { constructor() { let def = fs.readFileSync(__dirname + "/parser.def", 'utf8'); this.parser = pegjs.generate(def); - } parse(inputFile: string): Playbook { @@ -23,7 +22,7 @@ export class Parser { result.description = parseResult[1][2].descriptionlines; for(let index in parseResult[2]){ let step = new Step(); - step.text = this.getText(parseResult,index);; + step.text = this.getText(parseResult,index); step.lines = this.getLines(parseResult,index); step.textAfter = this.getTextAfter(parseResult, index); @@ -43,9 +42,9 @@ export class Parser { getLines(parseResult, index):Command[]{ try { - return (parseResult[2][index][5][0].stepline || parseResult[2][index][2][5][0].stepline).split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); + return (parseResult[2][index][5].steplines || parseResult[2][index][2][5].steplines).split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); } catch (error) { - return parseResult[2][index][2][5][0].stepline.split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); + return parseResult[2][index][2][5].steplines.split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); } } diff --git a/engine/run_command.ts b/engine/run_command.ts new file mode 100644 index 00000000..78797c11 --- /dev/null +++ b/engine/run_command.ts @@ -0,0 +1,9 @@ +import { Command } from "./command"; + +export class RunCommand { + public text: string; + public command: Command; + public lineIndex: number; + public stepIndex: number; + public textAfter: string; +} \ No newline at end of file diff --git a/engine/runner.ts b/engine/runner.ts index c6e029cb..7fa590bc 100644 --- a/engine/runner.ts +++ b/engine/runner.ts @@ -4,6 +4,7 @@ import { Playbook } from "./playbook"; import { Step } from "./step"; import * as fs from 'fs'; import * as rimraf from 'rimraf'; +import { RunCommand } from "./run_command"; export abstract class Runner { public path: string; @@ -86,14 +87,14 @@ export abstract class Runner { this.setVariable(this.useDevonCommand, false); } - run(step: Step, command: Command): RunResult { - console.log("Run " + command.name, command.parameters); - return this[this.getMethodName("run", command.name)](step, command); + run(runCommand: RunCommand): RunResult { + console.log("Run " + runCommand.command.name, runCommand.command.parameters); + return this[this.getMethodName("run", runCommand.command.name)](runCommand); } - async assert(step: Step, command: Command, runResult: RunResult): Promise { - if (this[this.getMethodName("assert", command.name)]) { - await this[this.getMethodName("assert", command.name)](step, command, runResult); + async assert(runCommand: RunCommand, runResult: RunResult): Promise { + if (this[this.getMethodName("assert", runCommand.command.name)]) { + await this[this.getMethodName("assert", runCommand.command.name)](runCommand, runResult); } } diff --git a/runners/console/index.ts b/runners/console/index.ts index 29a9c88b..100e9933 100644 --- a/runners/console/index.ts +++ b/runners/console/index.ts @@ -1,7 +1,6 @@ import { Runner } from "../../engine/runner" import { RunResult } from "../../engine/run_result"; -import { Step } from "../../engine/step"; -import { Command } from "../../engine/command"; +import { RunCommand } from "../../engine/run_command"; import { Assertions } from "../../assertions"; import { Playbook } from "../../engine/playbook"; import { ConsolePlatform, AsyncProcess } from "./consoleInterfaces"; @@ -55,11 +54,11 @@ export class Console extends Runner { } } - runInstallDevonfwIde(step: Step, command: Command): RunResult { + runInstallDevonfwIde(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - if(command.parameters[0].indexOf("npm") > -1 || command.parameters[0].indexOf("ng")) { + if(runCommand.command.parameters[0].indexOf("npm") > -1 || runCommand.command.parameters[0].indexOf("ng")) { let nodeInstallDir = path.join(this.getWorkingDirectory(), "devonfw", "software", "node"); this.env["npm_config_prefix"] = nodeInstallDir; this.env["npm_config_cache"] = ""; @@ -68,7 +67,7 @@ export class Console extends Runner { let settingsDir = this.createFolder(path.join(this.getWorkingDirectory(), "devonfw-settings"), true); this.executeCommandSync("git clone https://github.com/devonfw/ide-settings.git settings", settingsDir, result); - let tools = "DEVON_IDE_TOOLS=(" + command.parameters[0].join(" ") + ")"; + let tools = "DEVON_IDE_TOOLS=(" + runCommand.command.parameters[0].join(" ") + ")"; fs.writeFileSync(path.join(settingsDir, "settings", "devon.properties"), tools); fs.appendFileSync(path.join(settingsDir, "settings", "devon", "conf", "npm", ".npmrc"), "\nunsafe-perm=true"); fs.renameSync(path.join(settingsDir, "settings"), path.join(settingsDir, "settings.git")); @@ -78,8 +77,8 @@ export class Console extends Runner { this.createFolder(installDir, true); let downloadUrl = "https://bit.ly/2BCkFa9"; - if(command.parameters.length > 1 && command.parameters[1] != "") { - downloadUrl = "https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.devonfw.tools.ide&a=devonfw-ide-scripts&p=tar.gz&v=" + command.parameters[1]; + if(runCommand.command.parameters.length > 1 && runCommand.command.parameters[1] != "") { + downloadUrl = "https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.devonfw.tools.ide&a=devonfw-ide-scripts&p=tar.gz&v=" + runCommand.command.parameters[1]; } if(this.platform == ConsolePlatform.WINDOWS) { this.executeCommandSync("powershell.exe \"Invoke-WebRequest -OutFile devonfw.tar.gz '" + downloadUrl + "'\"", installDir, result); @@ -97,11 +96,11 @@ export class Console extends Runner { } - runRestoreDevonfwIde(step: Step, command: Command): RunResult { - return this.runInstallDevonfwIde(step, command); + runRestoreDevonfwIde(runCommand: RunCommand): RunResult { + return this.runInstallDevonfwIde(runCommand); } - runInstallCobiGen(step: Step, command: Command): RunResult { + runInstallCobiGen(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; @@ -113,7 +112,7 @@ export class Console extends Runner { return result; } - runCreateDevon4jProject(step: Step, command: Command): RunResult { + runCreateDevon4jProject(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; @@ -122,23 +121,23 @@ export class Console extends Runner { } let workspaceDir = path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main"); - let projectName = command.parameters[0]; + let projectName = runCommand.command.parameters[0]; this.executeDevonCommandSync("java create com.example.application." + projectName, workspaceDir, result); return result; } - runCreateFile(step: Step, command: Command): RunResult { + runCreateFile(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let filepath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let filepath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); if(!fs.existsSync(filepath.substr(0, filepath.lastIndexOf(path.sep)))) { fs.mkdirSync(filepath.substr(0, filepath.lastIndexOf(path.sep)), { recursive: true }); } let content = ""; - if(command.parameters.length == 2) { - content = fs.readFileSync(path.join(this.playbookPath, command.parameters[1]), { encoding: "utf-8" }); + if(runCommand.command.parameters.length == 2) { + content = fs.readFileSync(path.join(this.playbookPath, runCommand.command.parameters[1]), { encoding: "utf-8" }); } fs.writeFileSync(filepath, content); @@ -146,12 +145,12 @@ export class Console extends Runner { } - runBuildJava(step: Step, command: Command): RunResult { + runBuildJava(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let projectDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); - let buildCommand = (command.parameters.length == 2 && command.parameters[1] == true) + let projectDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); + let buildCommand = (runCommand.command.parameters.length == 2 && runCommand.command.parameters[1] == true) ? "mvn clean install" : "mvn clean install -Dmaven.test.skip=true"; @@ -162,7 +161,7 @@ export class Console extends Runner { return result; } - runCobiGenJava(step: Step, command: Command): RunResult { + runCobiGenJava(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; @@ -171,32 +170,32 @@ export class Console extends Runner { } let workspaceDir = path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main"); - this.executeDevonCommandSync("cobigen generate " + command.parameters[0], workspaceDir, result, command.parameters[1].toString()); + this.executeDevonCommandSync("cobigen generate " + runCommand.command.parameters[0], workspaceDir, result, runCommand.command.parameters[1].toString()); return result; } - runChangeFile(step: Step, command: Command): RunResult { + runChangeFile(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let filepath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let filepath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let content = fs.readFileSync(filepath, { encoding: "utf-8" }); - if(command.parameters[1].placeholder) { - let placeholder = command.parameters[1].placeholder; - if(command.parameters[1].content || command.parameters[1].contentConsole) { - let contentReplace = command.parameters[1].contentConsole ? command.parameters[1].contentConsole : command.parameters[1].content; + if(runCommand.command.parameters[1].placeholder) { + let placeholder = runCommand.command.parameters[1].placeholder; + if(runCommand.command.parameters[1].content || runCommand.command.parameters[1].contentConsole) { + let contentReplace = runCommand.command.parameters[1].contentConsole ? runCommand.command.parameters[1].contentConsole : runCommand.command.parameters[1].content; content = content.replace(placeholder, contentReplace); - } else if (command.parameters[1].file || command.parameters[1].fileConsole) { - let file = command.parameters[1].fileConsole ? command.parameters[1].fileConsole : command.parameters[1].file; + } else if (runCommand.command.parameters[1].file || runCommand.command.parameters[1].fileConsole) { + let file = runCommand.command.parameters[1].fileConsole ? runCommand.command.parameters[1].fileConsole : runCommand.command.parameters[1].file; let contentFile = fs.readFileSync(path.join(this.playbookPath, file), { encoding: "utf-8" }); content = content.replace(placeholder, contentFile); } } else { - if(command.parameters[1].content || command.parameters[1].contentConsole) { - content = command.parameters[1].contentConsole ? command.parameters[1].contentConsole : command.parameters[1].content; + if(runCommand.command.parameters[1].content || runCommand.command.parameters[1].contentConsole) { + content = runCommand.command.parameters[1].contentConsole ? runCommand.command.parameters[1].contentConsole : runCommand.command.parameters[1].content; } else { - let file = command.parameters[1].fileConsole ? command.parameters[1].fileConsole : command.parameters[1].file; + let file = runCommand.command.parameters[1].fileConsole ? runCommand.command.parameters[1].fileConsole : runCommand.command.parameters[1].file; content = fs.readFileSync(path.join(this.playbookPath, file), { encoding: "utf-8" }); } } @@ -206,11 +205,11 @@ export class Console extends Runner { } - runDockerCompose(step: Step, command: Command): RunResult { + runDockerCompose(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let filepath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let filepath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let process = this.executeCommandAsync("docker-compose up", filepath, result); process.on('close', (code) => { @@ -218,49 +217,49 @@ export class Console extends Runner { result.returnCode = code; } }); - if(process.pid && command.parameters.length == 2) { - this.asyncProcesses.push({ pid: process.pid, name: "dockerCompose", port: command.parameters[1].port }); + if(process.pid && runCommand.command.parameters.length == 2) { + this.asyncProcesses.push({ pid: process.pid, name: "dockerCompose", port: runCommand.command.parameters[1].port }); } return result; } - runRunServerJava(step: Step, command: Command): RunResult { + runRunServerJava(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let serverDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let serverDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let process = (this.getVariable(this.useDevonCommand)) ? this.executeDevonCommandAsync("mvn spring-boot:run", serverDir, result) : this.executeCommandAsync("mvn spring-boot:run", serverDir, result); if(process.pid) { - this.asyncProcesses.push({ pid: process.pid, name: "java", port: command.parameters[1].port }); + this.asyncProcesses.push({ pid: process.pid, name: "java", port: runCommand.command.parameters[1].port }); } return result; } - runCloneRepository(step: Step, command: Command): RunResult { + runCloneRepository(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let directorypath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); - if(command.parameters[0] != "") { + let directorypath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); + if(runCommand.command.parameters[0] != "") { this.createFolder(directorypath, true); } - this.executeCommandSync("git clone " + command.parameters[1], directorypath, result); + this.executeCommandSync("git clone " + runCommand.command.parameters[1], directorypath, result); return result; } - runNpmInstall(step: Step, command: Command): RunResult { + runNpmInstall(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let projectPath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let projectPath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); this.getVariable(this.useDevonCommand) ? this.executeDevonCommandSync("npm install", projectPath, result) : this.executeCommandSync("npm install", projectPath, result); @@ -268,45 +267,45 @@ export class Console extends Runner { return result; } - runDownloadFile(step: Step, command: Command): RunResult { + runDownloadFile(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; let downloadlDir = this.getVariable(this.workspaceDirectory); - if (command.parameters.length == 3) { - downloadlDir = path.join(downloadlDir, command.parameters[2]); + if (runCommand.command.parameters.length == 3) { + downloadlDir = path.join(downloadlDir, runCommand.command.parameters[2]); this.createFolder(downloadlDir, false); } let command1 = (this.platform == ConsolePlatform.WINDOWS) - ? "powershell.exe \"Invoke-WebRequest -OutFile " + command.parameters[1] + " '" + command.parameters[0] + "'\"" - : "wget -c " + command.parameters[0] + " -O " + command.parameters[1]; + ? "powershell.exe \"Invoke-WebRequest -OutFile " + runCommand.command.parameters[1] + " '" + runCommand.command.parameters[0] + "'\"" + : "wget -c " + runCommand.command.parameters[0] + " -O " + runCommand.command.parameters[1]; this.executeCommandSync(command1, downloadlDir, result); return result; } - runRunClientNg(step: Step, command: Command): RunResult { + runRunClientNg(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let projectDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let projectDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let process = this.getVariable(this.useDevonCommand) ? this.executeDevonCommandAsync("ng serve", projectDir, result) : this.executeCommandAsync("ng serve", projectDir, result); if(process.pid) { - this.asyncProcesses.push({ pid: process.pid, name: "node", port: command.parameters[1].port }); + this.asyncProcesses.push({ pid: process.pid, name: "node", port: runCommand.command.parameters[1].port }); } return result; } - runBuildNg(step: Step, command: Command): RunResult { + runBuildNg(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let projectDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let projectDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let command1 = "ng build"; - if(command.parameters.length == 2) { - command1 = command1 + " --output-path " + command.parameters[1]; + if(runCommand.command.parameters.length == 2) { + command1 = command1 + " --output-path " + runCommand.command.parameters[1]; } this.getVariable(this.useDevonCommand) ? this.executeDevonCommandSync(command1, projectDir, result) @@ -315,11 +314,11 @@ export class Console extends Runner { return result; } - runCreateFolder(step: Step, command: Command): RunResult { + runCreateFolder(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; - let folderPath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let folderPath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); if(folderPath && !fs.existsSync(folderPath)) { this.createFolder(folderPath, true); } @@ -327,12 +326,12 @@ export class Console extends Runner { return result; } - runNextKatacodaStep(step: Step, command: Command): RunResult { + runNextKatacodaStep(runCommand: RunCommand): RunResult { //Only needed for katacoda runner return null; } - runAdaptTemplatesCobiGen(step: Step, command: Command): RunResult { + runAdaptTemplatesCobiGen(runCommand: RunCommand): RunResult { let result = new RunResult(); result.returnCode = 0; @@ -343,9 +342,9 @@ export class Console extends Runner { return result; } - async assertInstallDevonfwIde(step: Step, command: Command, result: RunResult) { + async assertInstallDevonfwIde(runCommand: RunCommand, result: RunResult) { try { - let installedTools = command.parameters[0]; + let installedTools = runCommand.command.parameters[0]; let assert = new Assertions() .noErrorCode(result) @@ -363,11 +362,11 @@ export class Console extends Runner { } } - async assertRestoreDevonfwIde(step: Step, command: Command, result: RunResult) { - this.assertInstallDevonfwIde(step, command, result); + async assertRestoreDevonfwIde(runCommand: RunCommand, result: RunResult) { + this.assertInstallDevonfwIde(runCommand, result); } - async assertInstallCobiGen(step: Step, command: Command, result: RunResult) { + async assertInstallCobiGen(runCommand: RunCommand, result: RunResult) { try { new Assertions() .noErrorCode(result) @@ -381,72 +380,72 @@ export class Console extends Runner { } } - async assertBuildJava(step: Step, command: Command, result: RunResult) { + async assertBuildJava(runCommand: RunCommand, result: RunResult) { try { new Assertions() .noErrorCode(result) .noException(result) - .directoryExits(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0], "api", "target")) - .directoryExits(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0], "core", "target")) - .directoryExits(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0], "server", "target")); + .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "api", "target")) + .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "core", "target")) + .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "server", "target")); } catch(error) { this.cleanUp(); throw error; } } - async assertCobiGenJava(step: Step, command: Command, result: RunResult) { + async assertCobiGenJava(runCommand: RunCommand, result: RunResult) { try { new Assertions() .noErrorCode(result) .noException(result) - .fileExits(path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main", command.parameters[0])); + .fileExits(path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main", runCommand.command.parameters[0])); } catch(error) { this.cleanUp(); throw error; } } - async assertCreateDevon4jProject(step: Step, command: Command, result: RunResult) { + async assertCreateDevon4jProject(runCommand: RunCommand, result: RunResult) { try { let workspaceDir = path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main"); new Assertions() .noErrorCode(result) .noException(result) - .directoryExits(path.join(workspaceDir, command.parameters[0])) - .directoryExits(path.join(workspaceDir, command.parameters[0], "api", "src", "main", "java")) - .directoryExits(path.join(workspaceDir, command.parameters[0], "core", "src", "main", "java")) - .directoryExits(path.join(workspaceDir, command.parameters[0], "server", "src", "main", "java")) - .fileExits(path.join(workspaceDir, command.parameters[0], "core", "src", "main", "java", "com", "example", "application", command.parameters[0], "SpringBootApp.java")); + .directoryExits(path.join(workspaceDir, runCommand.command.parameters[0])) + .directoryExits(path.join(workspaceDir, runCommand.command.parameters[0], "api", "src", "main", "java")) + .directoryExits(path.join(workspaceDir, runCommand.command.parameters[0], "core", "src", "main", "java")) + .directoryExits(path.join(workspaceDir, runCommand.command.parameters[0], "server", "src", "main", "java")) + .fileExits(path.join(workspaceDir, runCommand.command.parameters[0], "core", "src", "main", "java", "com", "example", "application", runCommand.command.parameters[0], "SpringBootApp.java")); } catch(error) { this.cleanUp(); throw error; } } - async assertCreateFile(step: Step, command: Command, result: RunResult) { + async assertCreateFile(runCommand: RunCommand, result: RunResult) { try { new Assertions() .noErrorCode(result) .noException(result) - .fileExits(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0])); + .fileExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); } catch(error) { this.cleanUp(); throw error; } } - async assertChangeFile(step: Step, command: Command, result: RunResult) { + async assertChangeFile(runCommand: RunCommand, result: RunResult) { try{ let content = ""; - if(command.parameters[1].content) { - content = command.parameters[1].content; - } else if (command.parameters[1].file) { - content = fs.readFileSync(path.join(this.playbookPath, command.parameters[1].file), { encoding: "utf-8" }); + if(runCommand.command.parameters[1].content) { + content = runCommand.command.parameters[1].content; + } else if (runCommand.command.parameters[1].file) { + content = fs.readFileSync(path.join(this.playbookPath, runCommand.command.parameters[1].file), { encoding: "utf-8" }); } - let filepath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let filepath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); new Assertions() .noErrorCode(result) .noException(result) @@ -458,27 +457,27 @@ export class Console extends Runner { } } - async assertDockerCompose(step: Step, command: Command, result: RunResult) { + async assertDockerCompose(runCommand: RunCommand, result: RunResult) { try { let assert = new Assertions() .noErrorCode(result) .noException(result); - if(command.parameters.length > 1) { - if(!command.parameters[1].startupTime) { - console.warn("No startup time for command dockerCompose has been set") + if(runCommand.command.parameters.length > 1) { + if(!runCommand.command.parameters[1].startupTime) { + console.warn("No startup time for runCommand.command dockerCompose has been set") } - let startupTimeInSeconds = command.parameters[1].startupTime ? command.parameters[1].startupTime : 0; - await this.sleep(command.parameters[1].startupTime); + let startupTimeInSeconds = runCommand.command.parameters[1].startupTime ? runCommand.command.parameters[1].startupTime : 0; + await this.sleep(runCommand.command.parameters[1].startupTime); - if(!command.parameters[1].port) { + if(!runCommand.command.parameters[1].port) { this.killAsyncProcesses(); - throw new Error("Missing arguments for command dockerCompose. You have to specify a port and a path for the server. For further information read the function documentation."); + throw new Error("Missing arguments for runCommand.command dockerCompose. You have to specify a port and a path for the server. For further information read the function documentation."); } else { - let isReachable = await assert.serverIsReachable(command.parameters[1].port, command.parameters[1].path); + let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { this.killAsyncProcesses(); - throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + command.parameters[1].port + "/" + command.parameters[1].path); + throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path); } } } @@ -488,27 +487,27 @@ export class Console extends Runner { } } - async assertRunServerJava(step: Step, command: Command, result: RunResult) { + async assertRunServerJava(runCommand: RunCommand, result: RunResult) { try { let assert = new Assertions() .noErrorCode(result) .noException(result); - if(command.parameters.length > 1) { - if(!command.parameters[1].startupTime) { - console.warn("No startup time for command runServerJava has been set") + if(runCommand.command.parameters.length > 1) { + if(!runCommand.command.parameters[1].startupTime) { + console.warn("No startup time for runCommand.command runServerJava has been set") } - let startupTimeInSeconds = command.parameters[1].startupTime ? command.parameters[1].startupTime : 0; - await this.sleep(command.parameters[1].startupTime); + let startupTimeInSeconds = runCommand.command.parameters[1].startupTime ? runCommand.command.parameters[1].startupTime : 0; + await this.sleep(runCommand.command.parameters[1].startupTime); - if(!command.parameters[1].port || !command.parameters[1].path) { + if(!runCommand.command.parameters[1].port || !runCommand.command.parameters[1].path) { this.killAsyncProcesses(); - throw new Error("Missing arguments for command runServerJava. You have to specify a port and a path for the server. For further information read the function documentation."); + throw new Error("Missing arguments for runCommand.command runServerJava. You have to specify a port and a path for the server. For further information read the function documentation."); } else { - let isReachable = await assert.serverIsReachable(command.parameters[1].port, command.parameters[1].path); + let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { this.killAsyncProcesses(); - throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + command.parameters[1].port + "/" + command.parameters[1].path) + throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path) } } } @@ -518,17 +517,17 @@ export class Console extends Runner { } } - async assertCloneRepository(step: Step, command: Command, result: RunResult) { + async assertCloneRepository(runCommand: RunCommand, result: RunResult) { try { - let repository = command.parameters[1]; + let repository = runCommand.command.parameters[1]; let repoName = repository.slice(repository.lastIndexOf("/"), -4); - let directorypath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0], repoName); + let directorypath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], repoName); new Assertions() .noErrorCode(result) .noException(result) - .directoryExits(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0], repoName)) - .directoryNotEmpty(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0], repoName)) + .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], repoName)) + .directoryNotEmpty(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], repoName)) .repositoryIsClean(directorypath); } catch(error) { this.cleanUp(); @@ -536,58 +535,58 @@ export class Console extends Runner { } } - async assertNpmInstall(step: Step, command: Command, result: RunResult) { + async assertNpmInstall(runCommand: RunCommand, result: RunResult) { try { new Assertions() .noErrorCode(result) .noException(result) - .directoryExits(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0])) - .directoryExits(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0], "node_modules")); + .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])) + .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "node_modules")); } catch(error) { this.cleanUp(); throw error; } } - async assertDownloadFile(step: Step, command: Command, result: RunResult){ + async assertDownloadFile(runCommand: RunCommand, result: RunResult){ try { let directory = this.getVariable(this.workspaceDirectory); - if(command.parameters.length == 3) { - directory = path.join(directory, command.parameters[2]); + if(runCommand.command.parameters.length == 3) { + directory = path.join(directory, runCommand.command.parameters[2]); } new Assertions() .noErrorCode(result) .noException(result) .directoryExits(directory) .directoryNotEmpty(directory) - .fileExits(path.join(directory, command.parameters[1])); + .fileExits(path.join(directory, runCommand.command.parameters[1])); } catch(error) { this.cleanUp(); throw error; } } - async assertRunClientNg(step: Step, command: Command, result: RunResult) { + async assertRunClientNg(runCommand: RunCommand, result: RunResult) { try { let assert = new Assertions() .noErrorCode(result) .noException(result); - if(command.parameters.length > 1) { - if(!command.parameters[1].startupTime) { - console.warn("No startup time for command runClientNg has been set") + if(runCommand.command.parameters.length > 1) { + if(!runCommand.command.parameters[1].startupTime) { + console.warn("No startup time for runCommand.command runClientNg has been set") } - let startupTimeInSeconds = command.parameters[1].startupTime ? command.parameters[1].startupTime : 0; - await this.sleep(command.parameters[1].startupTime); + let startupTimeInSeconds = runCommand.command.parameters[1].startupTime ? runCommand.command.parameters[1].startupTime : 0; + await this.sleep(runCommand.command.parameters[1].startupTime); - if(!command.parameters[1].port) { + if(!runCommand.command.parameters[1].port) { this.killAsyncProcesses(); - throw new Error("Missing arguments for command runClientNg. You have to specify a port for the server. For further information read the function documentation."); + throw new Error("Missing arguments for runCommand.command runClientNg. You have to specify a port for the server. For further information read the function documentation."); } else { - let isReachable = await assert.serverIsReachable(command.parameters[1].port, command.parameters[1].path); + let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { this.killAsyncProcesses(); - throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + command.parameters[1].port + "/" + command.parameters[1].path) + throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path) } } } @@ -597,12 +596,12 @@ export class Console extends Runner { } } - async assertBuildNg(step: Step, command: Command, result: RunResult) { + async assertBuildNg(runCommand: RunCommand, result: RunResult) { try { - let projectPath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let projectPath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); var outputpath; - if(command.parameters.length == 2) { - outputpath = command.parameters[1].trim(); + if(runCommand.command.parameters.length == 2) { + outputpath = runCommand.command.parameters[1].trim(); } else { let content = fs.readFileSync(path.join(projectPath, "angular.json"), { encoding: "utf-8" }); outputpath = this.lookup(JSON.parse(content), "outputPath")[1]; @@ -622,9 +621,9 @@ export class Console extends Runner { } } - async assertCreateFolder(step: Step, command: Command, result: RunResult){ + async assertCreateFolder(runCommand: RunCommand, result: RunResult){ try { - let folderPath = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let folderPath = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); new Assertions() .noErrorCode(result) .noException(result) @@ -635,7 +634,7 @@ export class Console extends Runner { } } - async assertAdaptTemplatesCobiGen(step: Step, command: Command, result: RunResult) { + async assertAdaptTemplatesCobiGen(runCommand: RunCommand, result: RunResult) { try { let templatesDir = path.join(os.homedir(), ".cobigen", "templates"); new Assertions() @@ -655,7 +654,7 @@ export class Console extends Runner { let process = child_process.spawnSync(command, { shell: true, cwd: directory, input: input, maxBuffer: Infinity, env: this.env }); if(process.status != 0) { - console.log("Error executing command: " + command + " (exit code: " + process.status + ")"); + console.log("Error executing runCommand.command: " + command + " (exit code: " + process.status + ")"); console.log(process.stderr.toString(), process.stdout.toString()); result.returnCode = process.status; } diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index 7dd0298d..f34b33a5 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -1,8 +1,7 @@ import { Runner } from "../../engine/runner" import { RunResult } from "../../engine/run_result"; import { Playbook } from "../../engine/playbook"; -import { Step } from "../../engine/step"; -import { Command } from "../../engine/command"; +import { RunCommand } from "../../engine/run_command"; import { KatacodaTools } from "./katacodaTools"; import { KatacodaStep, KatacodaSetupScript, KatacodaTerminals } from "./katacodaInterfaces"; import { KatacodaAssetManager } from "./katacodaAssetManager"; @@ -72,9 +71,9 @@ export class Katacoda extends Runner { fs.writeFileSync(this.outputPathTutorial + 'index.json', JSON.stringify(indexJsonObject, null, 2)); } - runInstallDevonfwIde(step: Step, command: Command): RunResult { + runInstallDevonfwIde(runCommand: RunCommand): RunResult { let cdCommand = this.changeCurrentDir(path.join("/root")); - let tools = command.parameters[0].join(" ").replace(/vscode/,"").replace(/eclipse/, "").trim(); + let tools = runCommand.command.parameters[0].join(" ").replace(/vscode/,"").replace(/eclipse/, "").trim(); // create script to download devonfw ide settings this.renderTemplate(path.join("scripts", "cloneDevonfwIdeSettings.sh"), path.join(this.setupDir, "cloneDevonfwIdeSettings.sh"), { tools: tools, cloneDir: "/root/devonfw-settings/"}); @@ -89,7 +88,7 @@ export class Katacoda extends Runner { "title": "Install devonfw IDE", "text": "step" + this.stepsCount + ".md", }); - this.renderTemplate("installDevonfwIde.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand}); + this.renderTemplate("installDevonfwIde.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand}); //update current and working directory this.currentDir = path.join(this.currentDir, "devonfw"); @@ -102,8 +101,8 @@ export class Katacoda extends Runner { return null; } - runRestoreDevonfwIde(step: Step, command: Command): RunResult { - let tools = command.parameters[0].join(" ").replace(/vscode/,"").replace(/eclipse/, "").trim(); + runRestoreDevonfwIde(runCommand: RunCommand): RunResult { + let tools = runCommand.command.parameters[0].join(" ").replace(/vscode/,"").replace(/eclipse/, "").trim(); // create script to download devonfw ide settings. this.renderTemplate(path.join("scripts", "cloneDevonfwIdeSettings.sh"), path.join(this.setupDir, "cloneDevonfwIdeSettings.sh"), { tools: tools, cloneDir: "/root/devonfw-settings/"}); @@ -129,17 +128,17 @@ export class Katacoda extends Runner { return null; } - runInstallCobiGen(step: Step, command: Command): RunResult { + runInstallCobiGen(runCommand: RunCommand): RunResult { this.steps.push({ "title": "Install CobiGen", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("installCobiGen.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter}); + this.renderTemplate("installCobiGen.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter}); return null; } - runCobiGenJava(step: Step, command: Command): RunResult { - let params = command.parameters; + runCobiGenJava(runCommand: RunCommand): RunResult { + let params = runCommand.command.parameters; let cobiGenTemplates = params[1].join(","); this.renderTemplate(path.join("scripts", "installCobiGenPlugin.sh"), path.join(this.setupDir, "installCobiGenPlugin.sh"), { }); @@ -152,12 +151,12 @@ export class Katacoda extends Runner { "title": "CobiGen Java", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("cobiGenJava.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, javaFile: params[0], cobiGenTemplates: cobiGenTemplates }); + this.renderTemplate("cobiGenJava.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, javaFile: params[0], cobiGenTemplates: cobiGenTemplates }); return null; } - runCreateDevon4jProject(step: Step, command:Command): RunResult { + runCreateDevon4jProject(runCommand: RunCommand): RunResult { // generate template to change directory, if the current directory is not equal to the required start directory let cdCommand = this.changeCurrentDir(path.join("/root", "devonfw", "workspaces", "main")); @@ -167,18 +166,18 @@ export class Katacoda extends Runner { "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("createDevon4jProject.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, name : command.parameters[0]}); + this.renderTemplate("createDevon4jProject.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, name : runCommand.command.parameters[0]}); return null; } - runCreateFile(step: Step, command: Command): RunResult{ + runCreateFile(runCommand: RunCommand): RunResult{ let workspaceDir = path.join(this.getVariable(this.workspaceDirectory).concat(path.sep).replace(path.sep + "root" + path.sep, "")); - let filePath = path.join(this.getVariable(this.workspaceDirectory), path.dirname(command.parameters[0])).replace(/\\/g, "/"); - let fileDir = path.join(workspaceDir, command.parameters[0]).replace(/\\/g, "/"); - let fileName = path.basename(path.join(command.parameters[0])); + let filePath = path.join(this.getVariable(this.workspaceDirectory), path.dirname(runCommand.command.parameters[0])).replace(/\\/g, "/"); + let fileDir = path.join(workspaceDir, runCommand.command.parameters[0]).replace(/\\/g, "/"); + let fileName = path.basename(path.join(runCommand.command.parameters[0])); let content = ""; - if(command.parameters.length == 2) { - content = fs.readFileSync(path.join(this.playbookPath, command.parameters[1]), { encoding: "utf-8" }); + if(runCommand.command.parameters.length == 2) { + content = fs.readFileSync(path.join(this.playbookPath, runCommand.command.parameters[1]), { encoding: "utf-8" }); } this.steps.push({ @@ -186,22 +185,22 @@ export class Katacoda extends Runner { "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("createFile.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, filePath: filePath, fileDir: fileDir , fileName:fileName , content: content}); + this.renderTemplate("createFile.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, filePath: filePath, fileDir: fileDir , fileName:fileName , content: content}); return null; } - runChangeFile(step: Step, command: Command): RunResult{ + runChangeFile(runCommand: RunCommand): RunResult{ let workspaceDir = path.join(this.getVariable(this.workspaceDirectory).concat(path.sep).replace(path.sep + "root" + path.sep, "")); - let fileName = path.basename(path.join(command.parameters[0])); - let fileDir = path.join(workspaceDir, command.parameters[0]).replace(/\\/g, "/"); - let placeholder = command.parameters[1].placeholder ? command.parameters[1].placeholder : ""; - let dataTarget = command.parameters[1].placeholder ? "insert" : "replace"; + let fileName = path.basename(path.join(runCommand.command.parameters[0])); + let fileDir = path.join(workspaceDir, runCommand.command.parameters[0]).replace(/\\/g, "/"); + let placeholder = runCommand.command.parameters[1].placeholder ? runCommand.command.parameters[1].placeholder : ""; + let dataTarget = runCommand.command.parameters[1].placeholder ? "insert" : "replace"; let content = ""; - if(command.parameters[1].content || command.parameters[1].contentKatacoda){ - content = (command.parameters[1].contentKatacoda) ? command.parameters[1].contentKatacoda : command.parameters[1].content; - }else if(command.parameters[1].file || command.parameters[1].fileKatacoda){ - let file = (command.parameters[1].fileKatacoda) ? command.parameters[1].fileKatacoda : command.parameters[1].file; + if(runCommand.command.parameters[1].content || runCommand.command.parameters[1].contentKatacoda){ + content = (runCommand.command.parameters[1].contentKatacoda) ? runCommand.command.parameters[1].contentKatacoda : runCommand.command.parameters[1].content; + }else if(runCommand.command.parameters[1].file || runCommand.command.parameters[1].fileKatacoda){ + let file = (runCommand.command.parameters[1].fileKatacoda) ? runCommand.command.parameters[1].fileKatacoda : runCommand.command.parameters[1].file; content = fs.readFileSync(path.join(this.playbookPath, file), { encoding: "utf-8" }); } @@ -210,83 +209,83 @@ export class Katacoda extends Runner { "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("changeFile.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, fileDir: fileDir, content: content, placeholder: placeholder, dataTarget: dataTarget }); + this.renderTemplate("changeFile.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, fileDir: fileDir, content: content, placeholder: placeholder, dataTarget: dataTarget }); return null; } - runBuildJava(step: Step, command: Command): RunResult{ + runBuildJava(runCommand: RunCommand): RunResult{ - let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0])); - let skipTest = (command.parameters.length == 2 && command.parameters[1] == true) ? false : true; + let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); + let skipTest = (runCommand.command.parameters.length == 2 && runCommand.command.parameters[1] == true) ? false : true; this.steps.push({ "title": "Build the java project", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("buildJava.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, skipTest: skipTest, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.renderTemplate("buildJava.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, skipTest: skipTest, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } - runBuildNg(step: Step, command: Command): RunResult { - let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0])); + runBuildNg(runCommand: RunCommand): RunResult { + let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); this.steps.push({ "title": "Build the Angular Project", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("buildNg.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, outputDir: command.parameters[1], useDevonCommand: this.getVariable(this.useDevonCommand) }); + this.renderTemplate("buildNg.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, outputDir: runCommand.command.parameters[1], useDevonCommand: this.getVariable(this.useDevonCommand) }); return null; } - runCloneRepository(step: Step, command: Command): RunResult { + runCloneRepository(runCommand: RunCommand): RunResult { let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory))); let directoryPath = ""; - if(command.parameters[0].trim()) { - directoryPath = path.join(command.parameters[0]).replace(/\\/g, "/"); + if(runCommand.command.parameters[0].trim()) { + directoryPath = path.join(runCommand.command.parameters[0]).replace(/\\/g, "/"); this.currentDir = path.join(this.currentDir, directoryPath); } this.steps.push({ - "title": "Clones Repository " + command.parameters[1], + "title": "Clones Repository " + runCommand.command.parameters[1], "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("cloneRepository.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, directoryPath: directoryPath, repository: command.parameters[1] }); + this.renderTemplate("cloneRepository.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, directoryPath: directoryPath, repository: runCommand.command.parameters[1] }); return null; } - runRunServerJava(step: Step, command: Command): RunResult{ - let serverDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + runRunServerJava(runCommand: RunCommand): RunResult{ + let serverDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let terminal = this.getTerminal('runServerJava'); let cdCommand = this.changeCurrentDir(serverDir, terminal.terminalId, terminal.isRunning); this.steps.push({ "title": "Start the java server", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("runServerJava.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.renderTemplate("runServerJava.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } - runNpmInstall(step: Step, command: Command): RunResult { - let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0])); + runNpmInstall(runCommand: RunCommand): RunResult { + let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); this.steps.push({ "title": "Install the dependencies", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("npmInstall.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.renderTemplate("npmInstall.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } - runRunClientNg(step: Step, command: Command): RunResult { - let serverDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + runRunClientNg(runCommand: RunCommand): RunResult { + let serverDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let terminal = this.getTerminal('runClientNg'); let cdCommand = this.changeCurrentDir(serverDir, terminal.terminalId, terminal.isRunning); @@ -297,26 +296,26 @@ export class Katacoda extends Runner { fs.appendFileSync(path.join(this.getRunnerDirectory(),"templates","scripts", "intro_background.sh"), "\necho \'export NODE_OPTIONS=\"--max-old-space-size=16384\"\' >> /root/.profile\n"); - this.renderTemplate("runClientNg.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: command.parameters[1].port, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.renderTemplate("runClientNg.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: runCommand.command.parameters[1].port, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } - runCreateFolder(step: Step, command: Command): RunResult { - let folderPath = new DirUtils().getCdParam(this.currentDir, path.join(this.getVariable(this.workspaceDirectory), command.parameters[0])); + runCreateFolder(runCommand: RunCommand): RunResult { + let folderPath = new DirUtils().getCdParam(this.currentDir, path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); this.steps.push({ "title": "Create a new folder", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("createFolder.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, folderPath: folderPath }); + this.renderTemplate("createFolder.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, folderPath: folderPath }); return null; } - runNextKatacodaStep(step: Step, command: Command): RunResult { - let tempFile = path.join(this.getTempDirectory(), command.name + ".md"); + runNextKatacodaStep(runCommand: RunCommand): RunResult { + let tempFile = path.join(this.getTempDirectory(), runCommand.command.name + ".md"); fs.writeFileSync(tempFile, ""); - for(let i = 0; i < command.parameters[1].length; i++) { - let param = command.parameters[1][i]; + for(let i = 0; i < runCommand.command.parameters[1].length; i++) { + let param = runCommand.command.parameters[1][i]; if(param.content) { fs.appendFileSync(tempFile, param.content); } else if(param.file) { @@ -331,38 +330,38 @@ export class Katacoda extends Runner { let content = fs.readFileSync(tempFile, "utf-8"); this.steps.push({ - "title": command.parameters[0], + "title": runCommand.command.parameters[0], "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("nextKatacodaStep.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, content: content }); + this.renderTemplate("nextKatacodaStep.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, content: content }); - if(command.parameters[2]) { - this.currentDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[2]); + if(runCommand.command.parameters[2]) { + this.currentDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[2]); } return null; } - runAdaptTemplatesCobiGen(step: Step, command: Command): RunResult { + runAdaptTemplatesCobiGen(runCommand: RunCommand): RunResult { this.steps.push({ "title": "Adapt cobiGen templates", "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("adaptTemplatesCobiGen.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter}); + this.renderTemplate("adaptTemplatesCobiGen.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter}); return null; } - runDockerCompose(step: Step, command: Command) : RunResult { + runDockerCompose(runCommand: RunCommand) : RunResult { let terminal = this.getTerminal('runDockerCompose'); - let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]), terminal.terminalId, terminal.isRunning); + let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]), terminal.terminalId, terminal.isRunning); this.steps.push({ "title": "Execute Docker Compose", "text": "step" +this.stepsCount + ".md" }); - this.renderTemplate("dockerCompose.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: command.parameters[1].port}); + this.renderTemplate("dockerCompose.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: runCommand.command.parameters[1].port}); return null; } @@ -370,7 +369,7 @@ export class Katacoda extends Runner { private renderTemplate(name: string, targetPath: string, variables) { let template = fs.readFileSync(path.join(this.getRunnerDirectory(),"templates", name), 'utf8'); let result = ejs.render(template, variables); - fs.writeFileSync(targetPath, result); + fs.writeFileSync(targetPath, result, {flag: "a"}); } private writeSetupFile(setupFile: string) { @@ -411,4 +410,9 @@ export class Katacoda extends Runner { return {terminalId: this.terminalCounter, isRunning: false}; } + private getStepsCount(runCommand: RunCommand): number { + let returnCount = runCommand.stepIndex == this.stepsCount - 1 ? this.stepsCount : ++this.stepsCount; + return returnCount; + } + } \ No newline at end of file From 2b3de03a5f2028e2f7360409588ab0877e43b997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Die=C3=9Fner?= Date: Tue, 2 Mar 2021 10:52:58 +0100 Subject: [PATCH 2/5] Added title function for multiple steps --- engine/engine.ts | 1 + engine/parser.def | 16 +++-- engine/parser.ts | 17 +++-- engine/run_command.ts | 1 + engine/step.ts | 1 + runners/console/index.ts | 14 ++-- runners/katacoda/index.ts | 139 +++++++++++++++----------------------- 7 files changed, 89 insertions(+), 100 deletions(-) diff --git a/engine/engine.ts b/engine/engine.ts index 6437afdd..195117ac 100644 --- a/engine/engine.ts +++ b/engine/engine.ts @@ -37,6 +37,7 @@ export class Engine { runCommand.command = this.playbook.steps[stepIndex].lines[lineIndex]; runCommand.stepIndex = stepIndex; runCommand.lineIndex = lineIndex; + runCommand.stepTitle = this.playbook.steps[stepIndex].title; let foundRunnerToExecuteCommand = false; for (let runnerIndex in this.environment.runners) { let runner = await this.getRunner(this.environment.runners[runnerIndex]); diff --git a/engine/parser.def b/engine/parser.def index 3ea58558..befcbf1d 100644 --- a/engine/parser.def +++ b/engine/parser.def @@ -28,10 +28,14 @@ step stepinner = steptextlines? - "[step]" ___ - "--" ___ - steplines - "--" __ + "[step]" ___ + stepstitle? + "--" ___ + steplines + "--" __ + +stepstitle + = "==" _ string __ steptextlines = steptextline* { return { "steptextlines": text()}; } @@ -47,10 +51,10 @@ steptextafterline steplines = stepline+ { return { "steplines": text()}; } - + stepline = !"--" string __ - + string "string" = [^\r\n]+ { return text(); } diff --git a/engine/parser.ts b/engine/parser.ts index 7b04e0fb..50885a8e 100644 --- a/engine/parser.ts +++ b/engine/parser.ts @@ -22,9 +22,10 @@ export class Parser { result.description = parseResult[1][2].descriptionlines; for(let index in parseResult[2]){ let step = new Step(); - step.text = this.getText(parseResult,index); - step.lines = this.getLines(parseResult,index); + step.text = this.getText(parseResult, index); + step.lines = this.getLines(parseResult, index); step.textAfter = this.getTextAfter(parseResult, index); + step.title = this.getTitle(parseResult, index); result.steps.push(step); } @@ -42,9 +43,17 @@ export class Parser { getLines(parseResult, index):Command[]{ try { - return (parseResult[2][index][5].steplines || parseResult[2][index][2][5].steplines).split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); + return (parseResult[2][index][6].steplines || parseResult[2][index][2][6].steplines).split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); } catch (error) { - return parseResult[2][index][2][5].steplines.split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); + return parseResult[2][index][2][6].steplines.split("\r\n").filter(e => e != '').map(e => this.createCommand(e)); + } + } + + getTitle(parseResult, index) { + try { + return (parseResult[2][index][3][2]|| parseResult[2][index][2][3][2]); + } catch(error) { + return parseResult[2][index][2][3][2]; } } diff --git a/engine/run_command.ts b/engine/run_command.ts index 78797c11..15910cb3 100644 --- a/engine/run_command.ts +++ b/engine/run_command.ts @@ -6,4 +6,5 @@ export class RunCommand { public lineIndex: number; public stepIndex: number; public textAfter: string; + public stepTitle: string; } \ No newline at end of file diff --git a/engine/step.ts b/engine/step.ts index 96312fa4..2fa04549 100644 --- a/engine/step.ts +++ b/engine/step.ts @@ -3,5 +3,6 @@ import { Command } from "./command"; export class Step { public text: string; public lines: Command[] = []; + public title: string; public textAfter: string; } \ No newline at end of file diff --git a/runners/console/index.ts b/runners/console/index.ts index 100e9933..308e6847 100644 --- a/runners/console/index.ts +++ b/runners/console/index.ts @@ -465,14 +465,14 @@ export class Console extends Runner { if(runCommand.command.parameters.length > 1) { if(!runCommand.command.parameters[1].startupTime) { - console.warn("No startup time for runCommand.command dockerCompose has been set") + console.warn("No startup time for command dockerCompose has been set") } let startupTimeInSeconds = runCommand.command.parameters[1].startupTime ? runCommand.command.parameters[1].startupTime : 0; await this.sleep(runCommand.command.parameters[1].startupTime); if(!runCommand.command.parameters[1].port) { this.killAsyncProcesses(); - throw new Error("Missing arguments for runCommand.command dockerCompose. You have to specify a port and a path for the server. For further information read the function documentation."); + throw new Error("Missing arguments for command dockerCompose. You have to specify a port and a path for the server. For further information read the function documentation."); } else { let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { @@ -495,14 +495,14 @@ export class Console extends Runner { if(runCommand.command.parameters.length > 1) { if(!runCommand.command.parameters[1].startupTime) { - console.warn("No startup time for runCommand.command runServerJava has been set") + console.warn("No startup time for command runServerJava has been set") } let startupTimeInSeconds = runCommand.command.parameters[1].startupTime ? runCommand.command.parameters[1].startupTime : 0; await this.sleep(runCommand.command.parameters[1].startupTime); if(!runCommand.command.parameters[1].port || !runCommand.command.parameters[1].path) { this.killAsyncProcesses(); - throw new Error("Missing arguments for runCommand.command runServerJava. You have to specify a port and a path for the server. For further information read the function documentation."); + throw new Error("Missing arguments for command runServerJava. You have to specify a port and a path for the server. For further information read the function documentation."); } else { let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { @@ -574,14 +574,14 @@ export class Console extends Runner { if(runCommand.command.parameters.length > 1) { if(!runCommand.command.parameters[1].startupTime) { - console.warn("No startup time for runCommand.command runClientNg has been set") + console.warn("No startup time for command runClientNg has been set") } let startupTimeInSeconds = runCommand.command.parameters[1].startupTime ? runCommand.command.parameters[1].startupTime : 0; await this.sleep(runCommand.command.parameters[1].startupTime); if(!runCommand.command.parameters[1].port) { this.killAsyncProcesses(); - throw new Error("Missing arguments for runCommand.command runClientNg. You have to specify a port for the server. For further information read the function documentation."); + throw new Error("Missing arguments for command runClientNg. You have to specify a port for the server. For further information read the function documentation."); } else { let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { @@ -654,7 +654,7 @@ export class Console extends Runner { let process = child_process.spawnSync(command, { shell: true, cwd: directory, input: input, maxBuffer: Infinity, env: this.env }); if(process.status != 0) { - console.log("Error executing runCommand.command: " + command + " (exit code: " + process.status + ")"); + console.log("Error executing command: " + command + " (exit code: " + process.status + ")"); console.log(process.stderr.toString(), process.stdout.toString()); result.returnCode = process.status; } diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index f34b33a5..4bf3cd0e 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -84,11 +84,9 @@ export class Katacoda extends Runner { "script": "cloneDevonfwIdeSettings.sh" }); - this.steps.push({ - "title": "Install devonfw IDE", - "text": "step" + this.stepsCount + ".md", - }); - this.renderTemplate("installDevonfwIde.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand}); + this.pushStep(runCommand, "Install devonfw IDE", "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("installDevonfwIde.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand}); //update current and working directory this.currentDir = path.join(this.currentDir, "devonfw"); @@ -129,11 +127,9 @@ export class Katacoda extends Runner { } runInstallCobiGen(runCommand: RunCommand): RunResult { - this.steps.push({ - "title": "Install CobiGen", - "text": "step" + this.stepsCount + ".md" - }); - this.renderTemplate("installCobiGen.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter}); + this.pushStep(runCommand, "Install CobiGen", "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("installCobiGen.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter}); return null; } @@ -146,12 +142,9 @@ export class Katacoda extends Runner { "name": "Install CobiGen plugin", "script": "installCobiGenPlugin.sh" }); - - this.steps.push({ - "title": "CobiGen Java", - "text": "step" + this.stepsCount + ".md" - }); - this.renderTemplate("cobiGenJava.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, javaFile: params[0], cobiGenTemplates: cobiGenTemplates }); + this.pushStep(runCommand, "CobiGen Java", "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("cobiGenJava.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, javaFile: params[0], cobiGenTemplates: cobiGenTemplates }); return null; } @@ -161,12 +154,9 @@ export class Katacoda extends Runner { // generate template to change directory, if the current directory is not equal to the required start directory let cdCommand = this.changeCurrentDir(path.join("/root", "devonfw", "workspaces", "main")); - this.steps.push({ - "title": "Create a new project", - "text": "step" + this.stepsCount + ".md" - }); + this.pushStep(runCommand, "Create a new project", "step" + this.getStepsCount(runCommand) + ".md"); - this.renderTemplate("createDevon4jProject.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, name : runCommand.command.parameters[0]}); + this.renderTemplate("createDevon4jProject.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, name : runCommand.command.parameters[0]}); return null; } @@ -179,13 +169,9 @@ export class Katacoda extends Runner { if(runCommand.command.parameters.length == 2) { content = fs.readFileSync(path.join(this.playbookPath, runCommand.command.parameters[1]), { encoding: "utf-8" }); } - - this.steps.push({ - "title": "Create a new file", - "text": "step" + this.stepsCount + ".md" - }); + this.pushStep(runCommand, "Create a new file", "step" + this.getStepsCount(runCommand) + ".md"); - this.renderTemplate("createFile.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, filePath: filePath, fileDir: fileDir , fileName:fileName , content: content}); + this.renderTemplate("createFile.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, filePath: filePath, fileDir: fileDir , fileName:fileName , content: content}); return null; } @@ -204,12 +190,9 @@ export class Katacoda extends Runner { content = fs.readFileSync(path.join(this.playbookPath, file), { encoding: "utf-8" }); } - this.steps.push({ - "title": "Change " + fileName, - "text": "step" + this.stepsCount + ".md" - }); + this.pushStep(runCommand, "Change " + fileName, "step" + this.getStepsCount(runCommand) + ".md"); - this.renderTemplate("changeFile.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, fileDir: fileDir, content: content, placeholder: placeholder, dataTarget: dataTarget }); + this.renderTemplate("changeFile.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, fileDir: fileDir, content: content, placeholder: placeholder, dataTarget: dataTarget }); return null; } @@ -218,12 +201,9 @@ export class Katacoda extends Runner { let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); let skipTest = (runCommand.command.parameters.length == 2 && runCommand.command.parameters[1] == true) ? false : true; - this.steps.push({ - "title": "Build the java project", - "text": "step" + this.stepsCount + ".md" - }); + this.pushStep(runCommand, "Build the java project", "step" + this.getStepsCount(runCommand) + ".md"); - this.renderTemplate("buildJava.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, skipTest: skipTest, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.renderTemplate("buildJava.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, skipTest: skipTest, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } @@ -232,12 +212,9 @@ export class Katacoda extends Runner { runBuildNg(runCommand: RunCommand): RunResult { let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); - this.steps.push({ - "title": "Build the Angular Project", - "text": "step" + this.stepsCount + ".md" - }); + this.pushStep(runCommand, "Build the Angular Project", "step" + this.getStepsCount(runCommand) + ".md"); - this.renderTemplate("buildNg.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, outputDir: runCommand.command.parameters[1], useDevonCommand: this.getVariable(this.useDevonCommand) }); + this.renderTemplate("buildNg.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, outputDir: runCommand.command.parameters[1], useDevonCommand: this.getVariable(this.useDevonCommand) }); return null; } @@ -251,13 +228,9 @@ export class Katacoda extends Runner { this.currentDir = path.join(this.currentDir, directoryPath); } + this.pushStep(runCommand, "Clones Repository " + runCommand.command.parameters[1], "step" + this.getStepsCount(runCommand) + ".md"); - this.steps.push({ - "title": "Clones Repository " + runCommand.command.parameters[1], - "text": "step" + this.stepsCount + ".md" - }); - - this.renderTemplate("cloneRepository.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, directoryPath: directoryPath, repository: runCommand.command.parameters[1] }); + this.renderTemplate("cloneRepository.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, directoryPath: directoryPath, repository: runCommand.command.parameters[1] }); return null; } @@ -265,22 +238,18 @@ export class Katacoda extends Runner { let serverDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); let terminal = this.getTerminal('runServerJava'); let cdCommand = this.changeCurrentDir(serverDir, terminal.terminalId, terminal.isRunning); - this.steps.push({ - "title": "Start the java server", - "text": "step" + this.stepsCount + ".md" - }); - this.renderTemplate("runServerJava.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.pushStep(runCommand, "Start the java server", "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("runServerJava.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } runNpmInstall(runCommand: RunCommand): RunResult { let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); - this.steps.push({ - "title": "Install the dependencies", - "text": "step" + this.stepsCount + ".md" - }); - this.renderTemplate("npmInstall.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.pushStep(runCommand, "Install the dependencies", "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("npmInstall.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } @@ -289,25 +258,20 @@ export class Katacoda extends Runner { let terminal = this.getTerminal('runClientNg'); let cdCommand = this.changeCurrentDir(serverDir, terminal.terminalId, terminal.isRunning); - this.steps.push({ - "title": "Start the Angular Project", - "text": "step" + this.stepsCount + ".md" - }); + this.pushStep(runCommand, "Start the Angular Project", "step" + this.getStepsCount(runCommand) + ".md"); fs.appendFileSync(path.join(this.getRunnerDirectory(),"templates","scripts", "intro_background.sh"), "\necho \'export NODE_OPTIONS=\"--max-old-space-size=16384\"\' >> /root/.profile\n"); - this.renderTemplate("runClientNg.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: runCommand.command.parameters[1].port, useDevonCommand: this.getVariable(this.useDevonCommand)}); + this.renderTemplate("runClientNg.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: runCommand.command.parameters[1].port, useDevonCommand: this.getVariable(this.useDevonCommand)}); return null; } runCreateFolder(runCommand: RunCommand): RunResult { let folderPath = new DirUtils().getCdParam(this.currentDir, path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); - this.steps.push({ - "title": "Create a new folder", - "text": "step" + this.stepsCount + ".md" - }); - this.renderTemplate("createFolder.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, folderPath: folderPath }); + this.pushStep(runCommand, "Create a new folder", "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("createFolder.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, folderPath: folderPath }); return null; } @@ -329,11 +293,9 @@ export class Katacoda extends Runner { } let content = fs.readFileSync(tempFile, "utf-8"); - this.steps.push({ - "title": runCommand.command.parameters[0], - "text": "step" + this.stepsCount + ".md" - }); - this.renderTemplate("nextKatacodaStep.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, content: content }); + this.pushStep(runCommand, runCommand.command.parameters[0], "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("nextKatacodaStep.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, content: content }); if(runCommand.command.parameters[2]) { this.currentDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[2]); @@ -344,11 +306,9 @@ export class Katacoda extends Runner { runAdaptTemplatesCobiGen(runCommand: RunCommand): RunResult { - this.steps.push({ - "title": "Adapt cobiGen templates", - "text": "step" + this.stepsCount + ".md" - }); - this.renderTemplate("adaptTemplatesCobiGen.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter}); + this.pushStep(runCommand, "Adapt cobiGen templates", "step" + this.getStepsCount(runCommand) + ".md"); + + this.renderTemplate("adaptTemplatesCobiGen.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter}); return null; } @@ -356,12 +316,9 @@ export class Katacoda extends Runner { let terminal = this.getTerminal('runDockerCompose'); let cdCommand = this.changeCurrentDir(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]), terminal.terminalId, terminal.isRunning); - this.steps.push({ - "title": "Execute Docker Compose", - "text": "step" +this.stepsCount + ".md" - }); + this.pushStep(runCommand, "Execute Docker Compose", "step" + this.getStepsCount(runCommand) + ".md"); - this.renderTemplate("dockerCompose.md", this.outputPathTutorial + "step" + (this.getStepsCount(runCommand)) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: runCommand.command.parameters[1].port}); + this.renderTemplate("dockerCompose.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, terminalId: terminal.terminalId, interrupt: terminal.isRunning, port: runCommand.command.parameters[1].port}); return null; } @@ -415,4 +372,20 @@ export class Katacoda extends Runner { return returnCount; } + private pushStep(runCommand: RunCommand, title: string, text: string) { + if (this.steps.length == this.stepsCount - 1) { + if (runCommand.stepTitle) { + this.steps.push({ + "title": runCommand.stepTitle, + "text": text + }); + } else { + this.steps.push({ + "title": title, + "text": text + }); + } + } + } + } \ No newline at end of file From 0a8d1a0c6352b497734857cb523dfc14e85bb137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Die=C3=9Fner?= Date: Tue, 2 Mar 2021 11:03:46 +0100 Subject: [PATCH 3/5] Fixed error if no steptitle is given --- engine/parser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/parser.ts b/engine/parser.ts index 50885a8e..b021d1f5 100644 --- a/engine/parser.ts +++ b/engine/parser.ts @@ -53,7 +53,7 @@ export class Parser { try { return (parseResult[2][index][3][2]|| parseResult[2][index][2][3][2]); } catch(error) { - return parseResult[2][index][2][3][2]; + return null; } } From 393d79e808defd382787d5b2268366c3668063a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Die=C3=9Fner?= Date: Wed, 3 Mar 2021 11:29:49 +0100 Subject: [PATCH 4/5] Missing Refactoring --- runners/console/index.ts | 2 +- runners/katacoda/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runners/console/index.ts b/runners/console/index.ts index 7223fdc0..30b9656b 100644 --- a/runners/console/index.ts +++ b/runners/console/index.ts @@ -542,7 +542,7 @@ export class Console extends Runner { async assertNpmInstall(runCommand: RunCommand, result: RunResult) { try { - let projectDir = path.join(this.getVariable(this.workspaceDirectory), command.parameters[0]); + let projectDir = path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]); new Assertions() .noErrorCode(result) .noException(result) diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index d14ba409..a199d2fb 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -257,7 +257,7 @@ export class Katacoda extends Runner { "title": "Install " + packageTitle, "text": "step" + this.stepsCount + ".md" }); - this.renderTemplate("npmInstall.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, useDevonCommand: this.getVariable(this.useDevonCommand), npmCommand: npmCommand}); + this.renderTemplate("npmInstall.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, cdCommand: cdCommand, useDevonCommand: this.getVariable(this.useDevonCommand), npmCommand: npmCommand}); return null; } From 752ac31d4d03d88b1605844c8fd6a9f0de73683e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Die=C3=9Fner?= Date: Thu, 4 Mar 2021 10:10:08 +0100 Subject: [PATCH 5/5] Small changes --- engine/engine.ts | 28 +++++++++++++++++----------- runners/katacoda/index.ts | 18 ++++++------------ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/engine/engine.ts b/engine/engine.ts index 195117ac..7019e443 100644 --- a/engine/engine.ts +++ b/engine/engine.ts @@ -27,17 +27,7 @@ export class Engine { mainloop: for (let stepIndex = 0; stepIndex < this.playbook.steps.length; stepIndex++) { for (let lineIndex = 0; lineIndex < this.playbook.steps[stepIndex].lines.length; lineIndex++) { - let runCommand = new RunCommand(); - if(lineIndex == 0) { - runCommand.text = this.playbook.steps[stepIndex].text; - } - if(lineIndex == (this.playbook.steps[stepIndex].lines.length - 1)){ - runCommand.textAfter = this.playbook.steps[stepIndex].textAfter; - } - runCommand.command = this.playbook.steps[stepIndex].lines[lineIndex]; - runCommand.stepIndex = stepIndex; - runCommand.lineIndex = lineIndex; - runCommand.stepTitle = this.playbook.steps[stepIndex].title; + let runCommand = this.initRunCommand(stepIndex, lineIndex); let foundRunnerToExecuteCommand = false; for (let runnerIndex in this.environment.runners) { let runner = await this.getRunner(this.environment.runners[runnerIndex]); @@ -117,4 +107,20 @@ export class Engine { runner.playbookTitle = this.playbook.title; this.runners.set(name, runner); } + + private initRunCommand(stepIndex: number, lineIndex: number): RunCommand { + let runCommand = new RunCommand(); + if(lineIndex == 0) { + runCommand.text = this.playbook.steps[stepIndex].text; + } + if(lineIndex == (this.playbook.steps[stepIndex].lines.length - 1)){ + runCommand.textAfter = this.playbook.steps[stepIndex].textAfter; + } + runCommand.command = this.playbook.steps[stepIndex].lines[lineIndex]; + runCommand.stepIndex = stepIndex; + runCommand.lineIndex = lineIndex; + runCommand.stepTitle = this.playbook.steps[stepIndex].title; + + return runCommand; + } } \ No newline at end of file diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index a199d2fb..6ecc5c5a 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -67,7 +67,7 @@ export class Katacoda extends Runner { this.assetManager.copyAssets(); // write index file, required for katacoda to load the tutorial - let indexJsonObject = KatacodaTools.generateIndexJson(playbook.title, ((this.stepsCount - 1) * 5), this.steps, this.assetManager.getKatacodaAssets()); + let indexJsonObject = KatacodaTools.generateIndexJson(playbook.title, ((this.stepsCount) * 5), this.steps, this.assetManager.getKatacodaAssets()); fs.writeFileSync(this.outputPathTutorial + 'index.json', JSON.stringify(indexJsonObject, null, 2)); } @@ -382,17 +382,11 @@ export class Katacoda extends Runner { private pushStep(runCommand: RunCommand, title: string, text: string) { if (this.steps.length == this.stepsCount - 1) { - if (runCommand.stepTitle) { - this.steps.push({ - "title": runCommand.stepTitle, - "text": text - }); - } else { - this.steps.push({ - "title": title, - "text": text - }); - } + let stepTitle = runCommand.stepTitle ? runCommand.stepTitle : title; + this.steps.push({ + "title": stepTitle, + "text": text + }); } }