-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKatalonUtils.java
More file actions
207 lines (170 loc) · 7.52 KB
/
KatalonUtils.java
File metadata and controls
207 lines (170 loc) · 7.52 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.katalon.jenkins.plugin;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import hudson.FilePath;
import hudson.model.BuildListener;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
class KatalonUtils {
private static final String RELEASES_LIST =
"https://raw.githubusercontent.com/katalon-studio/katalon-studio/master/releases.json";
private static KatalonVersion getVersionInfo(BuildListener buildListener, String versionNumber) throws IOException {
URL url = new URL(RELEASES_LIST);
String os = OsUtils.getOSVersion(buildListener);
LogUtils.log(buildListener, "Retrieve Katalon Studio version: " + versionNumber + ", OS: " + os);
ObjectMapper objectMapper = new ObjectMapper();
List<KatalonVersion> versions = objectMapper.readValue(url, new TypeReference<List<KatalonVersion>>() {
});
LogUtils.log(buildListener, "Number of releases: " + versions.size());
for (KatalonVersion version : versions) {
if ((version.getVersion().equals(versionNumber)) && (version.getOs().equalsIgnoreCase(os))) {
String containingFolder = version.getContainingFolder();
if (containingFolder == null) {
String fileName = version.getFilename();
String fileExtension = "";
if (fileName.endsWith(".zip")) {
fileExtension = ".zip";
} else if (fileName.endsWith(".tar.gz")) {
fileExtension = ".tar.gz";
}
containingFolder = fileName.replace(fileExtension, "");
version.setContainingFolder(containingFolder);
}
LogUtils.log(buildListener, "Katalon Studio is hosted at " + version.getUrl() + ".");
return version;
}
}
return null;
}
private static void downloadAndExtract(
BuildListener buildListener, String versionNumber, File targetDir)
throws IOException, InterruptedException {
KatalonVersion version = KatalonUtils.getVersionInfo(buildListener, versionNumber);
String versionUrl = version.getUrl();
LogUtils.log(
buildListener,
"Downloading Katalon Studio from " + versionUrl + ". It may take a few minutes.");
URL url = new URL(versionUrl);
try (InputStream inputStream = url.openStream()) {
Path temporaryFile = Files.createTempFile("Katalon-" + versionNumber, "");
Files.copy(
inputStream,
temporaryFile,
StandardCopyOption.REPLACE_EXISTING);
FilePath temporaryFilePath = new FilePath(temporaryFile.toFile());
FilePath targetDirPath = new FilePath(targetDir);
if (versionUrl.contains(".zip")) {
temporaryFilePath.unzip(targetDirPath);
} else if (versionUrl.contains(".tar.gz")) {
temporaryFilePath.untar(targetDirPath, FilePath.TarCompression.GZIP);
} else {
throw new IllegalStateException();
}
}
}
private static File getKatalonFolder(String version) {
String path = System.getProperty("user.home");
Path p = Paths.get(path, ".katalon", version);
return p.toFile();
}
static File getKatalonPackage(
BuildListener buildListener, String versionNumber)
throws IOException, InterruptedException {
File katalonDir = getKatalonFolder(versionNumber);
Path fileLog = Paths.get(katalonDir.toString(), ".katalon.done");
if (fileLog.toFile().exists()) {
LogUtils.log(buildListener, "Katalon Studio package has been downloaded already.");
} else {
FileUtils.deleteDirectory(katalonDir);
boolean katalonDirCreated = katalonDir.mkdirs();
if (!katalonDirCreated) {
throw new IllegalStateException("Cannot create directory to store Katalon Studio package.");
}
KatalonUtils.downloadAndExtract(buildListener, versionNumber, katalonDir);
boolean fileLogCreated = fileLog.toFile().createNewFile();
if (fileLogCreated) {
LogUtils.log(buildListener, "Katalon Studio has been installed successfully.");
}
}
String[] childrenNames = katalonDir.list((dir, name) -> {
File file = new File(dir, name);
return file.isDirectory() && name.contains("Katalon");
});
String katalonContainingDirName = Arrays.stream(childrenNames).findFirst().get();
File katalonContainingDir = new File(katalonDir, katalonContainingDirName);
return katalonContainingDir;
}
private static boolean executeKatalon(
BuildListener buildListener,
String katalonExecutableFile,
String projectPath,
String executeArgs,
String x11Display,
String xvfbConfiguration)
throws IOException, InterruptedException {
File file = new File(katalonExecutableFile);
if (!file.exists()) {
file = new File(katalonExecutableFile + ".exe");
}
if (file.exists()) {
file.setExecutable(true);
}
if (katalonExecutableFile.contains(" ")) {
katalonExecutableFile = "\"" + katalonExecutableFile + "\"";
}
String command = katalonExecutableFile +
" -noSplash " +
" -runMode=console ";
if (!executeArgs.contains("-projectPath")) {
command += " -projectPath=\"" + projectPath + "\" ";
}
command += " " + executeArgs + " ";
return OsUtils.runCommand(buildListener, command, x11Display, xvfbConfiguration);
}
public static boolean executeKatalon(
BuildListener buildListener,
String version,
String location,
String projectPath,
String executeArgs,
String x11Display,
String xvfbConfiguration)
throws IOException, InterruptedException {
String katalonDirPath;
if (StringUtils.isBlank(location)) {
File katalonDir = KatalonUtils.getKatalonPackage(buildListener, version);
katalonDirPath = katalonDir.getAbsolutePath();
} else {
katalonDirPath = location;
}
LogUtils.log(buildListener, "Using Katalon Studio at " + katalonDirPath);
String katalonExecutableFile;
String os = OsUtils.getOSVersion(buildListener);
if (os.contains("macos")) {
katalonExecutableFile = Paths.get(katalonDirPath, "Contents", "MacOS", "katalon")
.toAbsolutePath()
.toString();
} else {
katalonExecutableFile = Paths.get(katalonDirPath, "katalon")
.toAbsolutePath()
.toString();
}
return executeKatalon(
buildListener,
katalonExecutableFile,
projectPath,
executeArgs,
x11Display,
xvfbConfiguration);
}
}