Skip to content

Commit 119063d

Browse files
committed
support bash and pwsh in plugin
1 parent 6a5cee5 commit 119063d

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

core/src/main/java/com/flowci/core/agent/domain/ShellIn.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
@Accessors(chain = true)
1919
public final class ShellIn extends CmdIn {
2020

21+
public enum ShellType {
22+
Bash,
23+
24+
PowerShell;
25+
}
26+
2127
// from ExecutedCmd id
2228
private String id;
2329

@@ -48,11 +54,18 @@ public ShellIn() {
4854
super(Type.SHELL);
4955
}
5056

51-
public void addBash(String script) {
57+
public void addScript(String script, ShellType type) {
5258
if (Strings.isNullOrEmpty(script)) {
5359
return;
5460
}
55-
bash.add(script);
61+
62+
if (type == ShellType.Bash) {
63+
bash.add(script);
64+
}
65+
66+
if (type == ShellType.PowerShell) {
67+
pwsh.add(script);
68+
}
5669
}
5770

5871
public void addEnvFilters(Set<String> exports) {

core/src/main/java/com/flowci/core/job/manager/CmdManagerImpl.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@
4646
@Component
4747
public class CmdManagerImpl implements CmdManager {
4848

49-
private enum ShellType {
50-
Bash,
51-
52-
PowerShell;
53-
}
54-
5549
@Autowired
5650
private SpringEventManager eventManager;
5751

@@ -66,8 +60,8 @@ public CmdIn createShellCmd(Job job, Step step, NodeTree tree) {
6660
.setCondition(node.getCondition())
6761
.setAllowFailure(node.isAllowFailure())
6862
.setDockers(findDockerOptions(node))
69-
.setBash(linkScript(node, ShellType.Bash))
70-
.setPwsh(linkScript(node, ShellType.PowerShell))
63+
.setBash(linkScript(node, ShellIn.ShellType.Bash))
64+
.setPwsh(linkScript(node, ShellIn.ShellType.PowerShell))
7165
.setEnvFilters(linkFilters(node))
7266
.setInputs(linkInputs(node).merge(job.getContext(), false))
7367
.setTimeout(job.getTimeout());
@@ -132,7 +126,7 @@ private Set<String> linkFilters(StepNode current) {
132126
return output;
133127
}
134128

135-
private List<String> linkScript(StepNode current, ShellType shellType) {
129+
private List<String> linkScript(StepNode current, ShellIn.ShellType shellType) {
136130
List<String> output = new LinkedList<>();
137131

138132
if (current.hasParent()) {
@@ -142,11 +136,11 @@ private List<String> linkScript(StepNode current, ShellType shellType) {
142136
}
143137
}
144138

145-
if (shellType == ShellType.Bash) {
139+
if (shellType == ShellIn.ShellType.Bash) {
146140
output.add(current.getBash());
147141
}
148142

149-
if (shellType == ShellType.PowerShell) {
143+
if (shellType == ShellIn.ShellType.PowerShell) {
150144
output.add(current.getPwsh());
151145
}
152146

@@ -175,7 +169,8 @@ private void setPlugin(String name, ShellIn cmd) {
175169
cmd.setPlugin(name);
176170
cmd.setAllowFailure(plugin.isAllowFailure());
177171
cmd.addEnvFilters(plugin.getExports());
178-
cmd.addBash(plugin.getScript());
172+
cmd.addScript(plugin.getBash(), ShellIn.ShellType.Bash);
173+
cmd.addScript(plugin.getPwsh(), ShellIn.ShellType.PowerShell);
179174

180175
// apply docker from plugin as run time if it's specified
181176
ObjectsHelper.ifNotNull(plugin.getDocker(), (docker) -> {

core/src/main/java/com/flowci/core/job/service/LocalTaskServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public ExecutedLocalTask execute(Job job, LocalTask task) {
140140
}
141141

142142
Plugin plugin = event.getFetched();
143-
option.addEntryPoint(plugin.getScript());
143+
option.addEntryPoint(plugin.getBash());
144144
option.addBind(event.getDir().toString(), "/ws/.plugins/" + plugin.getName());
145145
ObjectsHelper.ifNotNull(plugin.getDocker(), (docker) -> option.setImage(docker.getImage()));
146146
}

core/src/main/java/com/flowci/core/plugin/domain/Plugin.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public class Plugin extends PluginRepoInfo {
5757

5858
private DockerOption docker;
5959

60-
private String script;
60+
private String bash;
61+
62+
private String pwsh;
6163

6264
private String icon;
6365

@@ -72,7 +74,8 @@ public void update(Plugin src) {
7274
this.setInputs(src.inputs);
7375
this.setStatsTypes(src.statsTypes);
7476
this.setAllowFailure(src.allowFailure);
75-
this.setScript(src.script);
77+
this.setBash(src.bash);
78+
this.setPwsh(src.pwsh);
7679
this.setDocker(src.docker);
7780
}
7881
}

core/src/main/java/com/flowci/core/plugin/domain/PluginParser.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import com.flowci.domain.Version;
2323
import com.flowci.tree.yml.DockerYml;
2424
import com.flowci.util.ObjectsHelper;
25+
import com.flowci.util.StringHelper;
2526
import com.flowci.util.YamlHelper;
2627
import lombok.NoArgsConstructor;
2728
import lombok.NonNull;
2829
import org.yaml.snakeyaml.Yaml;
30+
import sun.jvm.hotspot.oops.ObjectHeap;
2931

3032
import java.io.InputStream;
3133
import java.util.LinkedList;
@@ -62,14 +64,19 @@ private static class PluginWrapper {
6264

6365
public Boolean allow_failure;
6466

67+
public String bash;
68+
69+
public String pwsh;
70+
6571
public String script;
6672

6773
public DockerYml docker;
6874

6975
public Plugin toPlugin() {
7076
Plugin plugin = new Plugin(name, Version.parse(version));
7177
plugin.setIcon(icon);
72-
plugin.setScript(script);
78+
plugin.setBash(bash);
79+
plugin.setPwsh(pwsh);
7380

7481
ObjectsHelper.ifNotNull(docker, val -> plugin.setDocker(val.toDockerOption()));
7582
ObjectsHelper.ifNotNull(exports, plugin::setExports);
@@ -85,6 +92,13 @@ public Plugin toPlugin() {
8592
}
8693
});
8794

95+
// backward compatible, set script to bash
96+
if (StringHelper.hasValue(script)) {
97+
if (!StringHelper.hasValue(bash)) {
98+
plugin.setBash(script);
99+
}
100+
}
101+
88102
return plugin;
89103
}
90104
}

core/src/test/java/com/flowci/core/test/job/CmdManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private Plugin createDummyPlugin() {
207207
plugin.setName("gittest");
208208
plugin.setInputs(Lists.newArrayList(intInput, strInput));
209209
plugin.setDocker(option);
210-
plugin.setScript("echo ${GIT_DEFAULT_VAL} ${GIT_STR_VAL}");
210+
plugin.setBash("echo ${GIT_DEFAULT_VAL} ${GIT_STR_VAL}");
211211

212212
return plugin;
213213
}

core/src/test/java/com/flowci/core/test/job/LocalTaskManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void should_execute_local_task() {
3737
Plugin p = new Plugin();
3838
p.setName("test-plugin");
3939
p.setVersion(Version.parse("0.1.0"));
40-
p.setScript("echo aaa \n echo bbb");
40+
p.setBash("echo aaa \n echo bbb");
4141
p.setDocker(new DockerOption().setImage("sonarqube:latest"));
4242
pluginDao.save(p);
4343

0 commit comments

Comments
 (0)