-
Notifications
You must be signed in to change notification settings - Fork 266
/
MsBuildSQRunnerInstallation.java
153 lines (132 loc) · 5 KB
/
MsBuildSQRunnerInstallation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* SonarQube Scanner for Jenkins
* Copyright (C) 2007-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package hudson.plugins.sonar;
import com.google.common.annotations.VisibleForTesting;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.Util;
import hudson.model.EnvironmentSpecific;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolDescriptor;
import hudson.tools.ToolInstallation;
import hudson.tools.ToolInstaller;
import hudson.tools.ToolProperty;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
public class MsBuildSQRunnerInstallation extends ToolInstallation implements EnvironmentSpecific<MsBuildSQRunnerInstallation>, NodeSpecific<MsBuildSQRunnerInstallation> {
private static final long serialVersionUID = 1L;
static final String SCANNER_EXE_NAME = "SonarScanner.MSBuild.exe";
static final String SCANNER_DLL_NAME = "SonarScanner.MSBuild.dll";
static final String OLD_SCANNER_EXE_NAME = "MSBuild.SonarQube.Runner.exe";
private static String testExeName = null;
@DataBoundConstructor
public MsBuildSQRunnerInstallation(String name, @CheckForNull String home, List<? extends ToolProperty<?>> properties) {
super(Util.fixEmptyAndTrim(name), Util.fixEmptyAndTrim(home), properties);
}
@VisibleForTesting
static void setTestExeName(String exe) {
testExeName = exe;
}
@VisibleForTesting
static String getScannerToolPath(String home) {
if (testExeName != null) {
File testScanner = new File(home, testExeName);
return testScanner.getPath();
}
File scannerNet46 = new File(home, SCANNER_EXE_NAME);
if (scannerNet46.exists()) {
return scannerNet46.getPath();
}
File scannerNetCore = new File(home, SCANNER_DLL_NAME);
if (scannerNetCore.exists()) {
return scannerNetCore.getPath();
}
File oldScanner = new File(home, OLD_SCANNER_EXE_NAME);
if (oldScanner.exists()) {
return oldScanner.getPath();
}
return null;
}
@Override
public MsBuildSQRunnerInstallation forEnvironment(EnvVars environment) {
return new MsBuildSQRunnerInstallation(getName(), environment.expand(getHome()), getProperties().toList());
}
@Override
public MsBuildSQRunnerInstallation forNode(Node node, TaskListener log) throws IOException, InterruptedException {
return new MsBuildSQRunnerInstallation(getName(), translateFor(node, log), getProperties().toList());
}
@Extension
public static class DescriptorImpl extends ToolDescriptor<MsBuildSQRunnerInstallation> {
// super already manages: persistence of installations and form json deserialization
public DescriptorImpl() {
super();
load();
}
@Override
public void setInstallations(MsBuildSQRunnerInstallation... installations) {
super.setInstallations(installations);
save();
}
@Override
public MsBuildSQRunnerInstallation newInstance(StaplerRequest req, JSONObject formData) {
return (MsBuildSQRunnerInstallation) req.bindJSON(clazz, formData);
}
@Override
public String getDisplayName() {
return "SonarScanner for MSBuild";
}
@Override
public List<? extends ToolInstaller> getDefaultInstallers() {
return Collections.singletonList(new MsBuildSonarQubeRunnerInstaller(null));
}
}
public static String getScannerName() {
if (testExeName != null) {
return testExeName;
} else {
return SCANNER_EXE_NAME;
}
}
public String getToolPath(Launcher launcher) throws IOException, InterruptedException {
return launcher.getChannel().call(new GetToolPath(getHome()));
}
private static class GetToolPath extends MasterToSlaveCallable<String, IOException> {
private static final long serialVersionUID = 1L;
private final String rawHome;
GetToolPath(String rawHome) {
this.rawHome = rawHome;
}
@Override
public String call() {
String home = Util.replaceMacro(rawHome, EnvVars.masterEnvVars);
return getScannerToolPath(home);
}
}
}