Skip to content

Commit f013721

Browse files
authored
Merge pull request #62 from denise-khuu/feature/katacodachangefile
feature/katacodachangefile
2 parents 209d04f + 039e0cb commit f013721

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

documentation/Functions.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ installCobiGen()
3232
2. The numbers that represent the templates that CobiGen uses to generate code: int array
3333
#### example
3434
cobiGenJava("path/to/java/file/MyEntity.java",[1,3,5,6,8])
35+
### details
36+
| Number | Description |
37+
| --- | -- |
38+
| 1 | CRUD logic: Generates the logic layer and implementations for some use cases.|
39+
| 3 | CRUD REST services: Generates the service layer with CRUD operations for using in REST services.|
40+
| 5 | TO's: Generates the related Transfer Objects.|
41+
| 6 | Entity infrastructure: Creates the entity main interface and edits (by a merge) the current entity to extend the newly generated classes.|
42+
| 8 | CRUD SpringData Repository: Generates the entity repository (that contains the CRUD operations) in the data access layer.
3543

3644
***
3745

@@ -60,3 +68,31 @@ buildJava("cobigenexample", true)
6068
createFile("cobigenexample/core/src/main/java/com/example/application/cobigenexample/customermanagement/dataaccess/api/CustomerEntity.java", "files/CustomerEntity.java")
6169

6270
***
71+
72+
### changeFile
73+
#### parameter
74+
1. Path of the file to be changed (relative path to the workspace directory)
75+
2.
76+
* path of the file to get the content from or a string, that should be inserted.
77+
* (Optional) Name of a placeholder
78+
#### example
79+
changeFile("cobigenexample/core/src/main/java/com/example/application/cobigenexample/customermanagement/dataaccess/api/CustomerEntity.java", { "file": "files/Placeholder.java", "placeholder": "private static final long serialVersionUID = 1L;" })
80+
#### details
81+
##### Path of the file to get the content from or a string, that should be inserted.
82+
If you want to add content from a file:
83+
{"file": "[path]"}
84+
If you want to add a string to a file:
85+
{"content": "[string]"}
86+
##### Name of the placeholder
87+
If you want to insert content into your code between two existing lines, take the previous line as your placeholder. Add your placeholder into the new file or string, otherwise it will be replaced entirely.
88+
89+
example:{...,"placeholder": "private int age;"}
90+
| Before | Content or File | After |
91+
| --- | --- | --- |
92+
|<p>private int age;<br><br>public String getFirstname() {<br>return firstname;<br>}<br></p>|<p>private int age;<br><br>private String company;<br>public String getCompany() {<br>return firstname;<br>}<br>public void setCompany(String company) {<br>this.company = company;<br>}</p>|<p>private int age;<br><br>private String company;<br>public String getCompany() {<br>return firstname;<br>}<br>public void setCompany(String company) {<br>this.company = company;<br><br>public String getFirstname() {<br>return firstname;<br>}<br></p>|
93+
94+
A placeholder is optional. If you do not define a placeholder, the content in the existing file will be simply replaced by the new content.
95+
96+
Please try not to use custom placeholders. Keep in mind that you might want to build the project before changing them. Custom placeholders with a comment-syntax (e.g. "//PLACEHOLDER") will be removed by the console-environment and others might cause errors.
97+
98+
***

runners/katacoda/index.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class Katacoda extends Runner {
144144
let workspaceDir = path.join("devonfw", "workspaces", "main");
145145
let filePath = path.join(command.parameters[0].substring(0,path.join(command.parameters[0]).lastIndexOf(path.sep))).replace(/\\/g, "/");
146146
let fileDir = path.join(workspaceDir, command.parameters[0]).replace(/\\/g, "/");
147-
let fileName = path.join(command.parameters[0].substring(path.join( command.parameters[0]).lastIndexOf(path.sep) + 1 , command.parameters[0].length )).replace(/\\/g, "/");
147+
let fileName = path.basename(path.join(command.parameters[0]));
148148
let content = "";
149149
if(command.parameters.length == 2) {
150150
content = fs.readFileSync(path.join(this.playbookPath, command.parameters[1]), { encoding: "utf-8" });
@@ -159,6 +159,34 @@ export class Katacoda extends Runner {
159159
return null;
160160
}
161161

162+
runChangeFile(step: Step, command: Command): RunResult{
163+
let workspaceDir = path.join("devonfw", "workspaces", "main");
164+
let fileName = path.basename(path.join(command.parameters[0]));
165+
let fileDir = path.join(workspaceDir, command.parameters[0]).replace(/\\/g, "/");
166+
let content = "";
167+
let placeholder = "";
168+
let dataTarget = "replace";
169+
let changeDescr = "Replace the content of "+ fileName +" with the following code.";
170+
if(command.parameters[1].placeholder){
171+
dataTarget = "insert";
172+
placeholder = command.parameters[1].placeholder;
173+
changeDescr = "Insert after ' " + command.parameters[1].placeholder + " ' the following segment of code.";
174+
}
175+
if(command.parameters[1].content){
176+
content = command.parameters[1].content;
177+
}else if(command.parameters[1].file){
178+
content = fs.readFileSync(path.join(this.playbookPath, command.parameters[1].file), { encoding: "utf-8" });
179+
}
180+
181+
this.steps.push({
182+
"title": "Change " + fileName,
183+
"text": "step" + this.stepsCount + ".md"
184+
});
185+
186+
this.renderTemplate("changeFile.md", this.outputPathTutorial + "step" + (this.stepsCount++) + ".md", { text: step.text, textAfter: step.textAfter, fileDir: fileDir, fileName:fileName, content: content, placeholder: placeholder, dataTarget: dataTarget, changeDescr: changeDescr});
187+
return null;
188+
}
189+
162190
runBuildJava(step: Step, command: Command): RunResult{
163191

164192
let cdCommand = this.changeCurrentDir(path.join("/root", "devonfw", "workspaces", "main", command.parameters[0]));
@@ -181,8 +209,6 @@ export class Katacoda extends Runner {
181209

182210
}
183211

184-
185-
186212
private renderTemplate(name: string, targetPath: string, variables) {
187213
let template = fs.readFileSync(path.join(this.getRunnerDirectory(),"templates", name), 'utf8');
188214
let result = ejs.render(template, variables);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%= text; %>
2+
3+
Go back to the IDE and find the file <%= fileName; %>.
4+
5+
<%= changeDescr; %>
6+
7+
Click on 'Copy to Editor' to change it automatically.
8+
9+
<pre class="file" data-filename="<%= fileDir; %>" data-target="<%= dataTarget; %>" data-marker="<%= placeholder; %>">
10+
<%= content; %>
11+
</pre>
12+
13+
14+
<%= textAfter; %>

0 commit comments

Comments
 (0)