Skip to content

Commit f34adfd

Browse files
Merge pull request devonfw-tutorials#151 from GuentherJulian/fix/vscoderunner
fix for vscode runner
2 parents b2310e2 + 5dec6f8 commit f34adfd

File tree

8 files changed

+104
-48
lines changed

8 files changed

+104
-48
lines changed

engine/engine.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export class Engine {
1313
constructor(private environmentName: string, private environment: Environment, private playbook: Playbook) { }
1414

1515
async run() {
16+
for (let runnerIndex in this.environment.runners) {
17+
(await this.getRunner(this.environment.runners[runnerIndex])).init(this.playbook);
18+
}
19+
1620
console.log("Environment: " + this.environmentName);
1721
if (! await this.isEnvironmentComplete()) {
1822
if (this.environment.failOnIncomplete) {
@@ -21,17 +25,14 @@ export class Engine {
2125
console.log("Environment incomplete: " + this.environmentName);
2226
return;
2327
}
24-
for (let runnerIndex in this.environment.runners) {
25-
(await this.getRunner(this.environment.runners[runnerIndex])).init(this.playbook);
26-
}
2728

2829
mainloop: for (let stepIndex = 0; stepIndex < this.playbook.steps.length; stepIndex++) {
2930
for (let lineIndex = 0; lineIndex < this.playbook.steps[stepIndex].lines.length; lineIndex++) {
3031
let runCommand = this.initRunCommand(stepIndex, lineIndex);
3132
let foundRunnerToExecuteCommand = false;
3233
for (let runnerIndex in this.environment.runners) {
3334
let runner = await this.getRunner(this.environment.runners[runnerIndex]);
34-
if (runner.supports(this.playbook.steps[stepIndex].lines[lineIndex].name)) {
35+
if (runner.supports(this.playbook.steps[stepIndex].lines[lineIndex].name, this.playbook.steps[stepIndex].lines[lineIndex].parameters)) {
3536
var result = new RunResult();
3637
if(runner.commandIsSkippable(runCommand.command.name)) {
3738
console.log("Command " + runCommand.command.name + " will be skipped.");
@@ -70,7 +71,7 @@ export class Engine {
7071
for (let lineIndex in this.playbook.steps[stepIndex].lines) {
7172
let isSupported = false;
7273
for (let runnerIndex in this.environment.runners) {
73-
if ((await this.getRunner(this.environment.runners[runnerIndex])).supports(this.playbook.steps[stepIndex].lines[lineIndex].name)) {
74+
if ((await this.getRunner(this.environment.runners[runnerIndex])).supports(this.playbook.steps[stepIndex].lines[lineIndex].name, this.playbook.steps[stepIndex].lines[lineIndex].parameters)) {
7475
isSupported = true;
7576
break;
7677
}

engine/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export abstract class Runner {
7373
return dir;
7474
}
7575

76-
supports(name: string): boolean {
76+
supports(name: string, parameters: any[]): boolean {
7777
return !!this[this.getMethodName("run", name)];
7878
}
7979

runners/console/consoleUtils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { RunResult } from "../../engine/run_result";
22
import * as child_process from "child_process";
3+
import * as fs from 'fs';
34
import * as path from 'path';
5+
const os = require("os");
46

57
export class ConsoleUtils {
68
static executeCommandSync(command: string, directory: string, result: RunResult, env: any, input?: string) {
@@ -33,4 +35,22 @@ export class ConsoleUtils {
3335
let scriptsDir = path.join(devonInstallDirectory, "scripts");
3436
return ConsoleUtils.executeCommandAsync(path.join(scriptsDir, "devon") + " " + devonCommand, directory, result, env);
3537
}
38+
39+
static createBackupDevonDirectory() {
40+
let homedir = os.homedir();
41+
if(fs.existsSync(path.join(homedir, ".devon")) && !fs.existsSync(path.join(homedir, ".devon_backup")) ) {
42+
fs.renameSync(path.join(homedir, ".devon"), path.join(homedir, ".devon_backup"));
43+
}
44+
}
45+
46+
static restoreDevonDirectory() {
47+
let homedir = os.homedir();
48+
if(fs.existsSync(path.join(homedir, ".devon")) && fs.existsSync(path.join(homedir, ".devon_backup"))) {
49+
fs.rmdirSync(path.join(homedir, ".devon"), { recursive: true });
50+
fs.renameSync(path.join(homedir, ".devon_backup"), path.join(homedir, ".devon"));
51+
}
52+
if(!fs.existsSync(path.join(homedir, ".devon")) && fs.existsSync(path.join(homedir, ".devon_backup"))) {
53+
fs.renameSync(path.join(homedir, ".devon_backup"), path.join(homedir, ".devon"));
54+
}
55+
}
3656
}

runners/console/index.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,17 @@ export class Console extends Runner {
2929
.set("npm", "node")
3030
.set("ng", "node");
3131

32-
let homedir = os.homedir();
33-
if(fs.existsSync(path.join(homedir, ".devon"))) {
34-
fs.renameSync(path.join(homedir, ".devon"), path.join(homedir, ".devon_backup"))
35-
}
32+
ConsoleUtils.createBackupDevonDirectory();
33+
34+
this.createFolder(path.normalize(this.getWorkingDirectory()), true);
3635
this.setVariable(this.workspaceDirectory, path.join(this.getWorkingDirectory()));
3736
this.env = process.env;
38-
3937
}
4038

4139
destroy(playbook: Playbook): void {
4240
this.cleanUp();
4341
}
4442

45-
cleanUp(): void {
46-
this.killAsyncProcesses();
47-
48-
let homedir = os.homedir();
49-
if(fs.existsSync(path.join(homedir, ".devon"))) {
50-
fs.rmdirSync(path.join(homedir, ".devon"), { recursive: true })
51-
}
52-
if(fs.existsSync(path.join(homedir, ".devon_backup"))) {
53-
fs.renameSync(path.join(homedir, ".devon_backup"), path.join(homedir, ".devon"))
54-
}
55-
}
56-
5743
runInstallDevonfwIde(runCommand: RunCommand): RunResult {
5844
let result = new RunResult();
5945
result.returnCode = 0;
@@ -742,4 +728,8 @@ export class Console extends Runner {
742728
}
743729
}
744730

731+
private cleanUp(): void {
732+
this.killAsyncProcesses();
733+
ConsoleUtils.restoreDevonDirectory();
734+
}
745735
}

runners/katacoda/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class Katacoda extends Runner {
2222
private setupDir: string;
2323
private currentDir: string = path.join("/root");
2424
private terminalCounter: number = 1;
25+
private showVsCodeIde: boolean = false;
2526
private terminals: KatacodaTerminals[] = [{function: "default", terminalId: 1}];
2627

2728
init(playbook: Playbook): void {
@@ -68,13 +69,14 @@ export class Katacoda extends Runner {
6869
this.assetManager.copyAssets();
6970

7071
// write index file, required for katacoda to load the tutorial
71-
let indexJsonObject = KatacodaTools.generateIndexJson(playbook.title, ((this.stepsCount) * 5), this.steps, this.assetManager.getKatacodaAssets());
72+
let indexJsonObject = KatacodaTools.generateIndexJson(playbook.title, ((this.stepsCount) * 5), this.steps, this.assetManager.getKatacodaAssets(), this.showVsCodeIde);
7273
fs.writeFileSync(this.outputPathTutorial + 'index.json', JSON.stringify(indexJsonObject, null, 2));
7374
}
7475

7576
runInstallDevonfwIde(runCommand: RunCommand): RunResult {
7677
let cdCommand = this.changeCurrentDir(path.join("/root"));
7778
let tools = runCommand.command.parameters[0].join(" ").replace(/vscode/,"").replace(/eclipse/, "").trim();
79+
if(runCommand.command.parameters[0].indexOf("vscode") > -1) this.showVsCodeIde = true;
7880

7981
// create script to download devonfw ide settings
8082
this.renderTemplate(path.join("scripts", "cloneDevonfwIdeSettings.sh"), path.join(this.setupDir, "cloneDevonfwIdeSettings.sh"), { tools: tools, cloneDir: "/root/devonfw-settings/"});
@@ -102,6 +104,7 @@ export class Katacoda extends Runner {
102104

103105
runRestoreDevonfwIde(runCommand: RunCommand): RunResult {
104106
let tools = runCommand.command.parameters[0].join(" ").replace(/vscode/,"").replace(/eclipse/, "").trim();
107+
if(runCommand.command.parameters[0].indexOf("vscode") > -1) this.showVsCodeIde = true;
105108

106109
// create script to download devonfw ide settings.
107110
this.renderTemplate(path.join("scripts", "cloneDevonfwIdeSettings.sh"), path.join(this.setupDir, "cloneDevonfwIdeSettings.sh"), { tools: tools, cloneDir: "/root/devonfw-settings/"});
@@ -137,14 +140,16 @@ export class Katacoda extends Runner {
137140
let params = runCommand.command.parameters;
138141
let cobiGenTemplates = params[1].join(",");
139142

140-
this.renderTemplate(path.join("scripts", "installCobiGenPlugin.sh"), path.join(this.setupDir, "installCobiGenPlugin.sh"), { });
141-
this.setupScripts.push({
142-
"name": "Install CobiGen plugin",
143-
"script": "installCobiGenPlugin.sh"
144-
});
143+
if(this.showVsCodeIde) {
144+
this.renderTemplate(path.join("scripts", "installCobiGenPlugin.sh"), path.join(this.setupDir, "installCobiGenPlugin.sh"), { });
145+
this.setupScripts.push({
146+
"name": "Install CobiGen plugin",
147+
"script": "installCobiGenPlugin.sh"
148+
});
149+
}
150+
145151
this.pushStep(runCommand, "CobiGen Java", "step" + this.getStepsCount(runCommand) + ".md");
146-
147-
this.renderTemplate("cobiGenJava.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, javaFile: params[0], cobiGenTemplates: cobiGenTemplates });
152+
this.renderTemplate("cobiGenJava.md", this.outputPathTutorial + "step" + this.stepsCount + ".md", { text: runCommand.text, textAfter: runCommand.textAfter, javaFile: params[0], cobiGenTemplates: cobiGenTemplates, useVsCode: this.showVsCodeIde });
148153
return null;
149154

150155
}

runners/katacoda/katacodaTools.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { KatacodaStep, KatacodaAsset } from "./katacodaInterfaces";
22

33
export class KatacodaTools {
4-
public static generateIndexJson(title: string, minutes: number, steps: KatacodaStep[], assets: KatacodaAsset[]) {
4+
public static generateIndexJson(title: string, minutes: number, steps: KatacodaStep[], assets: KatacodaAsset[], showVsCodeIde: boolean) {
5+
let environment = showVsCodeIde ? { "uilayout": "terminal", "showide": true } : { "uilayout": "editor-terminal", "showide": false };
56
let indexJsonObject = {
67
"title": title,
78
"difficulty": "Beginner",
@@ -20,10 +21,7 @@ export class KatacodaTools {
2021
"client": assets
2122
}
2223
},
23-
"environment": {
24-
"uilayout": "terminal",
25-
"showide": true
26-
},
24+
"environment": environment,
2725
"backend": {
2826
"imageid": "ubuntu:2004"
2927
}

runners/katacoda/templates/cobiGenJava.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## CobiGen Java
44

5+
<% if(useVsCode) { %>
56
Open the following java file in the IDE.
67
`devonfw/workspaces/main/<%= javaFile; %>`{{open}}
78

@@ -12,5 +13,14 @@ A terminal will open on the bottom of the IDE and CobiGen CLI will start.
1213
You can choose the templates CobiGen should use by entering the numbers in the terminal of the IDE.
1314

1415
`<%= cobiGenTemplates; %>`
16+
<% } else { %>
17+
Start CobiGen CLI and pass the file as parameter by executing the following command.
18+
`devon cobigen generate <%= javaFile; %>`{{execute T1}}
19+
20+
CobiGen will ask you which files to generate. You can enter the numbers separated by commas.
21+
`<%= cobiGenTemplates; %>`{{execute T1}}
22+
23+
CobiGen will now generate code based on the source file and the templates you have passed.
24+
<% } %>
1525

1626
<%= textAfter; %>

runners/vscode/index.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@ export class VsCode extends Runner {
1515
private installedExtensions: string[] = [];
1616
private env: any;
1717
private vsCodeSetup: boolean = false;
18+
private installVsCodeFlag: boolean = false;
1819

1920
init(playbook: Playbook): void {
20-
this.createFolder(path.join(this.getWorkingDirectory(), "vscode_tests"), true);
21+
ConsoleUtils.createBackupDevonDirectory();
22+
2123
this.createFolder(path.join(__dirname, "resources"), false);
24+
this.createFolder(path.normalize(this.getWorkingDirectory()), true);
2225
this.env = process.env;
26+
27+
playbook.steps.forEach(step => {
28+
step.lines.forEach(stepLine => {
29+
if((stepLine.name == "installDevonfwIde" || stepLine.name == "restoreDevonfwIde") && stepLine.parameters[0].indexOf("vscode") > -1) {
30+
this.installVsCodeFlag = true;
31+
}
32+
});
33+
});
2334
}
2435

2536
destroy(playbook: Playbook): void {
26-
this.uninstallExtensions(VsCodeUtils.getVsCodeExecutable());
37+
this.cleanUp();
2738
}
2839

2940
runInstallCobiGen(runCommand: RunCommand): RunResult {
@@ -67,23 +78,35 @@ export class VsCode extends Runner {
6778
}
6879

6980
async assertInstallCobiGen(runCommand: RunCommand, result: RunResult) {
70-
new Assertions()
71-
.noErrorCode(result)
72-
.noException(result)
73-
.directoryExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli"))
74-
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen.jar"))
75-
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen"));
81+
try {
82+
new Assertions()
83+
.noErrorCode(result)
84+
.noException(result)
85+
.directoryExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli"))
86+
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen.jar"))
87+
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen"));
88+
} catch(error) {
89+
this.cleanUp();
90+
throw error;
91+
}
7692
}
7793

7894
async assertCobiGenJava(runCommand: RunCommand, result: RunResult) {
79-
new Assertions()
80-
.noErrorCode(result)
81-
.noException(result)
82-
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main", runCommand.command.parameters[0]));
95+
try {
96+
new Assertions()
97+
.noErrorCode(result)
98+
.noException(result)
99+
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main", runCommand.command.parameters[0]));
100+
} catch(error) {
101+
this.cleanUp();
102+
throw error;
103+
}
83104
}
84105

85106
setupVsCode() {
86107
this.vsCodeSetup = true;
108+
this.createFolder(path.join(this.getWorkingDirectory(), "vscode_tests"), true);
109+
87110
let vsCodeExecutable = VsCodeUtils.getVsCodeExecutable();
88111
if(!vsCodeExecutable || vsCodeExecutable == "") {
89112
console.error("Visual Studio Code seems not to be installed!");
@@ -147,4 +170,13 @@ export class VsCode extends Runner {
147170
child_process.spawnSync(vsCodeBin + " --uninstall-extension " + extension, { shell: true });
148171
});
149172
}
173+
174+
private cleanUp(): void {
175+
this.uninstallExtensions(VsCodeUtils.getVsCodeExecutable());
176+
ConsoleUtils.restoreDevonDirectory();
177+
}
178+
179+
supports(name: string, parameters: any[]): boolean {
180+
return super.supports(name, parameters) && this.installVsCodeFlag;
181+
}
150182
}

0 commit comments

Comments
 (0)