Skip to content

Commit f9ea402

Browse files
committed
Template will only be generated, if targetDir and currentDir have different strings
1 parent 95a0b26 commit f9ea402

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

localBuildTest.ps1

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
tsc
2-
Copy-Item -Force -Recurse -Path $PSScriptRoot\engine\parser.def -Destination $PSScriptRoot\build\engine\parser.def
3-
Copy-Item -Force -Recurse -Path $PSScriptRoot\..\tutorials -Destination $PSScriptRoot\build\playbooks
4-
Copy-Item -Force -Recurse -Path $PSScriptRoot\environments\ -Destination $PSScriptRoot\build
5-
Copy-Item -Force -Recurse -Path $PSScriptRoot\runners\ -Destination $PSScriptRoot\build
62
npm test
7-
if(-not $?) { throw 'tests failed' }
3+
if(-not $?){ throw 'tests failed' }

localBuildTest.sh

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
tsc
2-
cp engine/parser.def build/engine/parser.def
3-
cp -r ../tutorials build/playbooks
4-
cp -r environments build
5-
cp -r runners build
62
npm test

runners/katacoda/dirUtils.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
import { captureRejectionSymbol } from "events";
21
import * as path from 'path';
32

43
export class DirUtils{
54

6-
getCdParam(currentDir:string, dir:string):string{
7-
5+
getCdParam(currentDir:string, targetDir:string):string{
6+
7+
//returns an empty string, if both variables have the same path
8+
if(currentDir == targetDir){
9+
return "";
10+
}
11+
812
let dirPath = "";
913

1014
let currentPaths = currentDir.split(path.sep);
11-
let dirPaths = dir.split(path.sep);
15+
let targetPaths = targetDir.split(path.sep);
1216

1317
let index;
1418
let isEqual = true;
15-
16-
//returns an empty string, if both variables have the same path
17-
if(currentDir == dir){
18-
return "";
19-
}
2019

21-
//saves the remaining path, if currentdir is the prefix of dir
22-
if(dir.substring(0,currentDir.length) == currentDir){
23-
return path.join(dir.replace(currentDir + path.sep, ''));
20+
//saves the remaining path, if currentdir is the prefix of targetDir
21+
if(targetDir.substring(0,currentDir.length) == currentDir){
22+
return path.join(targetDir.replace(currentDir + path.sep, ''));
2423
}
2524

25+
2626
else{
2727
//returns the absolut directory, if the first parent folder is different
28-
if(currentPaths[1] != dirPaths[1]){
29-
return dir;
28+
if(currentPaths[1] != targetPaths[1]){
29+
return targetDir;
3030
}
3131

3232
//iterates throught currentPath array to compare parent directories
3333
currentPaths.forEach((currentPath, i) => {
34-
if(currentPath == dirPaths[i] && isEqual == true){
34+
if(currentPath == targetPaths[i] && isEqual == true){
3535
index = i;
3636
}else{
3737
isEqual = false;
3838
dirPath = path.join(dirPath,'..');
3939
}
4040
})
4141

42-
//slice dirPaths to get the relative path
43-
dirPaths = dirPaths.slice(index + 1, dirPaths.length);
42+
//slice targetPaths to get the relative path
43+
targetPaths = targetPaths.slice(index + 1, targetPaths.length);
4444

45-
return path.join(dirPath, dirPaths.join(path.sep));
45+
return path.join(dirPath, targetPaths.join(path.sep));
4646

4747
}
4848

runners/katacoda/index.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ export class Katacoda extends Runner {
123123

124124
runCreateProject(step: Step, command:Command): RunResult {
125125

126-
let params = command.parameters;
127-
let language = params[0];
128-
let name = params[1];
129126

130127
// generate template to change directory, if the current directory is not equal to the required start directory
131128
let cdCommand = this.changeCurrentDir(path.join("/root", "devonfw"));
@@ -136,9 +133,9 @@ export class Katacoda extends Runner {
136133
});
137134

138135
//update current directory
139-
this.currentDir = path.join(this.currentDir, "workspace", "main", name);
136+
this.currentDir = path.join(this.currentDir, "workspace", "main", command.parameters[1]);
140137

141-
this.renderTemplate("createProject.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, language: language, name : name});
138+
this.renderTemplate("createProject.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, cdCommand: cdCommand, language: command.parameters[0], name : command.parameters[1]});
142139
return null;
143140
}
144141

@@ -159,14 +156,18 @@ export class Katacoda extends Runner {
159156
this.assetManager.registerFile(setupFile, "setup/setup.txt", "/root/setup", false);
160157
}
161158

162-
private changeCurrentDir(dir:string):string{
159+
private changeCurrentDir(targetDir:string):string{
160+
if(this.currentDir == targetDir){
161+
return "";
162+
}
163163
let dirUtils = new DirUtils();
164-
let dirPath = dirUtils.getCdParam(this.currentDir, dir);
165-
this.currentDir = dir;
164+
let dir = dirUtils.getCdParam(this.currentDir, targetDir);
165+
166+
this.currentDir = targetDir;
166167

167168
//create template to change directory
168169
let template = fs.readFileSync(path.join(this.getRunnerDirectory(),"templates", 'cd.md'), 'utf8');
169-
return ejs.render(template, {dirPath: dirPath});
170+
return ejs.render(template, {dir: dir});
170171
}
171172

172173

runners/katacoda/templates/cd.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
Please change the folder to <%= dirPath; %>
2+
Please change the folder to <%= dir; %>
33

4-
`cd <%= dirPath; %>`{{execute}}
4+
`cd <%= dir; %>`{{execute}}

0 commit comments

Comments
 (0)