Skip to content

Commit a6495c0

Browse files
committed
Merge pull request jenkinsci#1078 from synopsys-arc-oss/run-shell-command_renaming
Renamed "Run Command" to "Run Shell Command"
2 parents 93db0d1 + a7643e2 commit a6495c0

29 files changed

+194
-71
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2009-2014 Sun Microsystems and contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package hudson.tools;
25+
26+
import hudson.FilePath;
27+
import hudson.model.Node;
28+
import hudson.model.TaskListener;
29+
import hudson.util.FormValidation;
30+
import java.io.IOException;
31+
import org.kohsuke.stapler.QueryParameter;
32+
33+
/**
34+
* A generic script-based installer.
35+
* @since TODO: define a version
36+
* @see BatchCommandInstaller
37+
* @see CommandInstaller
38+
* @author Oleg Nenashev
39+
*
40+
*/
41+
public abstract class AbstractCommandInstaller extends ToolInstaller {
42+
43+
/**
44+
* Command to execute, similar to {@link CommandInterpreter#command}.
45+
*/
46+
private final String command;
47+
private final String toolHome;
48+
49+
public AbstractCommandInstaller(String label, String command, String toolHome) {
50+
super(label);
51+
this.command = fixCrLf(command);
52+
this.toolHome = toolHome;
53+
}
54+
55+
public String getCommand() {
56+
return command;
57+
}
58+
59+
public String getToolHome() {
60+
return toolHome;
61+
}
62+
63+
public abstract String getCommandFileExtension();
64+
65+
/**
66+
* Retrieves a call for remote script caller.
67+
*/
68+
public abstract String[] getCommandCall(FilePath script);
69+
70+
@Override
71+
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
72+
FilePath dir = preferredLocation(tool, node);
73+
// XXX support Windows batch scripts, Unix scripts with interpreter line, etc. (see CommandInterpreter subclasses)
74+
FilePath script = dir.createTextTempFile("hudson", getCommandFileExtension(), command);
75+
try {
76+
String cmd[] = getCommandCall(script);
77+
int r = node.createLauncher(log).launch().cmds(cmd).stdout(log).pwd(dir).join();
78+
if (r != 0) {
79+
throw new IOException("Command returned status " + r);
80+
}
81+
} finally {
82+
script.delete();
83+
}
84+
return dir.child(getToolHome());
85+
}
86+
87+
/**
88+
* Fix CR/LF and always make it Unix style.
89+
*/
90+
//TODO: replace by a Windows style
91+
private static String fixCrLf(String s) {
92+
// eliminate CR
93+
int idx;
94+
while ((idx = s.indexOf("\r\n")) != -1) {
95+
s = s.substring(0, idx) + s.substring(idx + 1);
96+
}
97+
return s;
98+
}
99+
100+
public static abstract class Descriptor<TInstallerClass extends AbstractCommandInstaller>
101+
extends ToolInstallerDescriptor<TInstallerClass> {
102+
103+
public FormValidation doCheckCommand(@QueryParameter String value) {
104+
if (value.length() > 0) {
105+
return FormValidation.ok();
106+
} else {
107+
return FormValidation.error(Messages.CommandInstaller_no_command());
108+
}
109+
}
110+
111+
public FormValidation doCheckToolHome(@QueryParameter String value) {
112+
if (value.length() > 0) {
113+
return FormValidation.ok();
114+
} else {
115+
return FormValidation.error(Messages.CommandInstaller_no_command());
116+
}
117+
}
118+
}
119+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2013, Oleg Nenashev
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package hudson.tools;
26+
27+
import hudson.Extension;
28+
import hudson.FilePath;
29+
import org.kohsuke.stapler.DataBoundConstructor;
30+
31+
/**
32+
* Installs tool via script execution of Batch script.
33+
* Inspired by "Command installer" from the Jenkins core.
34+
* @since 0.1
35+
*/
36+
public class BatchCommandInstaller extends AbstractCommandInstaller {
37+
38+
@DataBoundConstructor
39+
public BatchCommandInstaller(String label, String command, String toolHome) {
40+
super(label, command, toolHome);
41+
}
42+
43+
@Override
44+
public String getCommandFileExtension() {
45+
return ".bat";
46+
}
47+
48+
@Override
49+
public String[] getCommandCall(FilePath script) {
50+
String[] cmd = {"cmd", "/c", "call", script.getRemote()};
51+
return cmd;
52+
}
53+
54+
@Extension
55+
public static class DescriptorImpl extends Descriptor<BatchCommandInstaller> {
56+
57+
@Override
58+
public String getDisplayName() {
59+
return Messages.BatchCommandInstaller_DescriptorImpl_displayName();
60+
}
61+
}
62+
}

core/src/main/java/hudson/tools/CommandInstaller.java

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,95 +26,36 @@
2626

2727
import hudson.Extension;
2828
import hudson.FilePath;
29-
import hudson.model.Node;
30-
import hudson.model.TaskListener;
31-
import hudson.tasks.CommandInterpreter;
32-
import hudson.util.FormValidation;
33-
import java.io.IOException;
3429
import org.kohsuke.stapler.DataBoundConstructor;
35-
import org.kohsuke.stapler.QueryParameter;
3630

3731
/**
3832
* Installs a tool by running an arbitrary shell command.
3933
* @since 1.305
4034
*/
41-
public class CommandInstaller extends ToolInstaller {
42-
43-
/**
44-
* Command to execute, similar to {@link CommandInterpreter#command}.
45-
*/
46-
private final String command;
47-
48-
/**
49-
* Resulting tool home directory.
50-
*/
51-
private final String toolHome;
35+
public class CommandInstaller extends AbstractCommandInstaller {
5236

5337
@DataBoundConstructor
5438
public CommandInstaller(String label, String command, String toolHome) {
55-
super(label);
56-
this.command = fixCrLf(command);
57-
this.toolHome = toolHome;
58-
}
59-
60-
/**
61-
* Fix CR/LF and always make it Unix style.
62-
*/
63-
private static String fixCrLf(String s) {
64-
// eliminate CR
65-
int idx;
66-
while((idx=s.indexOf("\r\n"))!=-1)
67-
s = s.substring(0,idx)+s.substring(idx+1);
68-
return s;
69-
}
70-
71-
public String getCommand() {
72-
return command;
39+
super(label, command, toolHome);
7340
}
7441

75-
public String getToolHome() {
76-
return toolHome;
42+
@Override
43+
public String getCommandFileExtension() {
44+
return ".sh";
7745
}
7846

79-
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
80-
FilePath dir = preferredLocation(tool, node);
81-
// TODO support Windows batch scripts, Unix scripts with interpreter line, etc. (see CommandInterpreter subclasses)
82-
FilePath script = dir.createTextTempFile("hudson", ".sh", command);
83-
try {
84-
String[] cmd = {"sh", "-e", script.getRemote()};
85-
int r = node.createLauncher(log).launch().cmds(cmd).stdout(log).pwd(dir).join();
86-
if (r != 0) {
87-
throw new IOException("Command returned status " + r);
88-
}
89-
} finally {
90-
script.delete();
91-
}
92-
return dir.child(toolHome);
47+
@Override
48+
public String[] getCommandCall(FilePath script) {
49+
String[] cmd = {"sh", "-e", script.getRemote()};
50+
return cmd;
9351
}
9452

9553
@Extension
96-
public static class DescriptorImpl extends ToolInstallerDescriptor<CommandInstaller> {
54+
public static class DescriptorImpl extends Descriptor<CommandInstaller> {
9755

56+
@Override
9857
public String getDisplayName() {
9958
return Messages.CommandInstaller_DescriptorImpl_displayName();
10059
}
101-
102-
public FormValidation doCheckCommand(@QueryParameter String value) {
103-
if (value.length() > 0) {
104-
return FormValidation.ok();
105-
} else {
106-
return FormValidation.error(Messages.CommandInstaller_no_command());
107-
}
108-
}
109-
110-
public FormValidation doCheckToolHome(@QueryParameter String value) {
111-
if (value.length() > 0) {
112-
return FormValidation.ok();
113-
} else {
114-
return FormValidation.error(Messages.CommandInstaller_no_toolHome());
115-
}
116-
}
117-
11860
}
119-
12061
}

0 commit comments

Comments
 (0)