BowlerKernel
BashLoader.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.scripting;
2 
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.List;
9 
10 import com.neuronrobotics.video.OSUtil;
11 
12 public class BashLoader implements IScriptingLanguage {
13 
14  @Override
15  public Object inlineScriptRun(File code, ArrayList<Object> args) throws Exception {
16  // List<String> asList = Arrays.asList("bash",code.getAbsolutePath());
17  ArrayList<String> commands = new ArrayList<>();
18  commands.add("bash");
19  commands.add(code.getAbsolutePath());
20  if (args != null) {
21  for (Object o : args) {
22  commands.add(o.toString());
23  }
24  }
25  ProcessBuilder pb = new ProcessBuilder(commands);
26  // setting the directory
27  pb.directory(code.getParentFile());
28  // startinf the process
29  Process process = pb.start();
30 
31  // for reading the ouput from stream
32  BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
33  BufferedReader errInput = new BufferedReader(new InputStreamReader(process.getErrorStream()));
34 
35  String s = null;
36  String e = null;
37  Thread.sleep(100);
38  ArrayList<String> back = new ArrayList<>();
39  while ((s = stdInput.readLine()) != null || (e = errInput.readLine()) != null) {
40  if (s != null) {
41  back.add(s);
42  System.out.println(s);
43  }
44  if (e != null)
45  System.out.println(e);
46  //
47  }
48  process.waitFor();
49  process.exitValue();
50 
51  while (process.isAlive()) {
52  Thread.sleep(100);
53  }
54  return back;
55  }
56 
57  @Override
58  public Object inlineScriptRun(String code, ArrayList<Object> args) throws Exception {
59  throw new RuntimeException("Bash scripts have to be sent as files");
60  }
61 
62  @Override
63  public String getShellType() {
64  return "Bash";
65  }
66 
67  @Override
68  public ArrayList<String> getFileExtenetion() {
69  if (OSUtil.isWindows())
70  return new ArrayList<>();
71  return new ArrayList<>(Arrays.asList(".sh", ".bash"));
72  }
73 
79  public String getDefaultContents() {
80  return "echo Hello World";
81  }
82 
83  @Override
84  public boolean getIsTextFile() {
85  // TODO Auto-generated method stub
86  return true;
87  }
88 
89 }
Object inlineScriptRun(String code, ArrayList< Object > args)
Definition: BashLoader.java:58
Object inlineScriptRun(File code, ArrayList< Object > args)
Definition: BashLoader.java:15