KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > Shell


1 package hudson.tasks;
2
3 import hudson.FilePath;
4 import hudson.model.Descriptor;
5 import static hudson.model.Hudson.isWindows;
6 import org.kohsuke.stapler.StaplerRequest;
7
8 import java.util.Map JavaDoc;
9
10 /**
11  * Executes a series of commands by using a shell.
12  *
13  * @author Kohsuke Kawaguchi
14  */

15 public class Shell extends CommandInterpreter {
16     public Shell(String JavaDoc command) {
17         super(fixCrLf(command));
18     }
19
20     /**
21      * Fix CR/LF and always make it Unix style.
22      */

23     private static String JavaDoc fixCrLf(String JavaDoc s) {
24         // eliminate CR
25
int idx;
26         while((idx=s.indexOf("\r\n"))!=-1)
27             s = s.substring(0,idx)+s.substring(idx+1);
28
29         //// add CR back if this is for Windows
30
//if(isWindows()) {
31
// idx=0;
32
// while(true) {
33
// idx = s.indexOf('\n',idx);
34
// if(idx==-1) break;
35
// s = s.substring(0,idx)+'\r'+s.substring(idx);
36
// idx+=2;
37
// }
38
//}
39
return s;
40     }
41
42     protected String JavaDoc[] buildCommandLine(FilePath script) {
43         return new String JavaDoc[] { DESCRIPTOR.getShell(),"-xe",script.getRemote()};
44     }
45
46     protected String JavaDoc getContents() {
47         return fixCrLf(command);
48     }
49
50     protected String JavaDoc getFileExtension() {
51         return ".sh";
52     }
53
54     public Descriptor<Builder> getDescriptor() {
55         return DESCRIPTOR;
56     }
57
58     public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
59
60     public static final class DescriptorImpl extends Descriptor<Builder> {
61         /**
62          * Shell executable, or null to default.
63          */

64         private String JavaDoc shell;
65
66         private DescriptorImpl() {
67             super(Shell.class);
68             load();
69         }
70
71
72         protected void convert(Map JavaDoc<String JavaDoc, Object JavaDoc> oldPropertyBag) {
73             shell = (String JavaDoc)oldPropertyBag.get("shell");
74         }
75
76         public String JavaDoc getShell() {
77             if(shell==null)
78                 return isWindows()?"sh":"/bin/sh";
79             return shell;
80         }
81
82         public void setShell(String JavaDoc shell) {
83             this.shell = shell;
84             save();
85         }
86
87         public String JavaDoc getHelpFile() {
88             return "/help/project-config/shell.html";
89         }
90
91         public String JavaDoc getDisplayName() {
92             return "Execute shell";
93         }
94
95         public Builder newInstance(StaplerRequest req) {
96             return new Shell(req.getParameter("shell"));
97         }
98
99         public boolean configure( StaplerRequest req ) {
100             setShell(req.getParameter("shell"));
101             return true;
102         }
103     }
104 }
105
Popular Tags