forked from JabRef/jabref
/
Version.java
296 lines (255 loc) · 10.6 KB
/
Version.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package org.jabref.logic.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents the Application Version with the major and minor number, the full Version String and if it's a developer
* version
*/
public class Version {
public static final String JABREF_DOWNLOAD_URL = "https://downloads.jabref.org";
private static final Logger LOGGER = LoggerFactory.getLogger(Version.class);
private static final Version UNKNOWN_VERSION = new Version();
private final static Pattern VERSION_PATTERN = Pattern.compile("(?<major>\\d+)(\\.(?<minor>\\d+))?(\\.(?<patch>\\d+))?(?<stage>-alpha|-beta)?(?<dev>-?dev)?.*");
private final static Pattern CI_SUFFIX_PATTERN = Pattern.compile("-ci\\.\\d+$");
private static final String JABREF_GITHUB_RELEASES = "https://api.github.com/repos/JabRef/JabRef/releases";
private String fullVersion = BuildInfo.UNKNOWN_VERSION;
private int major = -1;
private int minor = -1;
private int patch = -1;
private DevelopmentStage developmentStage = DevelopmentStage.UNKNOWN;
private boolean isDevelopmentVersion;
/**
* Dummy constructor to create a local object (and {@link Version#UNKNOWN_VERSION})
*/
private Version() {
}
/**
* @param version must be in form of following pattern: {@code (\d+)(\.(\d+))?(\.(\d+))?(-alpha|-beta)?(-?dev)?}
* (e.g., 3.3; 3.4-dev)
* @return the parsed version or {@link Version#UNKNOWN_VERSION} if an error occurred
*/
public static Version parse(String version) {
if ((version == null) || "".equals(version) || version.equals(BuildInfo.UNKNOWN_VERSION)
|| "${version}".equals(version)) {
return UNKNOWN_VERSION;
}
Version parsedVersion = new Version();
// remove "-ci.1" suffix
Matcher ciSuffixMatcher = CI_SUFFIX_PATTERN.matcher(version);
version = ciSuffixMatcher.replaceAll("");
parsedVersion.fullVersion = version;
Matcher matcher = VERSION_PATTERN.matcher(version);
if (matcher.find()) {
try {
parsedVersion.major = Integer.parseInt(matcher.group("major"));
String minorString = matcher.group("minor");
parsedVersion.minor = minorString == null ? 0 : Integer.parseInt(minorString);
String patchString = matcher.group("patch");
parsedVersion.patch = patchString == null ? 0 : Integer.parseInt(patchString);
String versionStageString = matcher.group("stage");
parsedVersion.developmentStage = versionStageString == null ? DevelopmentStage.STABLE : DevelopmentStage.parse(versionStageString);
parsedVersion.isDevelopmentVersion = matcher.group("dev") != null;
} catch (NumberFormatException e) {
LOGGER.warn("Invalid version string used: " + version, e);
return UNKNOWN_VERSION;
} catch (IllegalArgumentException e) {
LOGGER.warn("Invalid version pattern is used", e);
return UNKNOWN_VERSION;
}
} else {
LOGGER.warn("Version could not be recognized by the pattern");
return UNKNOWN_VERSION;
}
return parsedVersion;
}
/**
* Grabs all the available releases from the GitHub repository
*/
public static List<Version> getAllAvailableVersions() throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(JABREF_GITHUB_RELEASES).openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
try (BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
List<Version> versions = new ArrayList<>();
JSONArray objects = new JSONArray(rd.readLine());
for (int i = 0; i < objects.length(); i++) {
JSONObject jsonObject = objects.getJSONObject(i);
Version version = Version.parse(jsonObject.getString("tag_name").replaceFirst("v", ""));
versions.add(version);
}
connection.disconnect();
return versions;
}
}
/**
* @return true if this version is newer than the passed one
*/
public boolean isNewerThan(Version otherVersion) {
Objects.requireNonNull(otherVersion);
if (Objects.equals(this, otherVersion)) {
return false;
} else if (this.getFullVersion().equals(BuildInfo.UNKNOWN_VERSION)) {
return false;
} else if (otherVersion.getFullVersion().equals(BuildInfo.UNKNOWN_VERSION)) {
return false;
}
// compare the majors
if (this.getMajor() > otherVersion.getMajor()) {
return true;
} else if (this.getMajor() == otherVersion.getMajor()) {
// if the majors are equal compare the minors
if (this.getMinor() > otherVersion.getMinor()) {
return true;
} else if (this.getMinor() == otherVersion.getMinor()) {
// if the minors are equal compare the patch numbers
if (this.getPatch() > otherVersion.getPatch()) {
return true;
} else if (this.getPatch() == otherVersion.getPatch()) {
// if the patch numbers are equal compare the development stages
if (this.developmentStage.isMoreStableThan(otherVersion.developmentStage)) {
return true;
} else if (this.developmentStage == otherVersion.developmentStage) {
// if the stage is equal check if this version is in development and the other is not
return !this.isDevelopmentVersion && otherVersion.isDevelopmentVersion;
}
}
}
}
return false;
}
/**
* Checks if this version should be updated to one of the given ones.
* Ignoring the other Version if this one is Stable and the other one is not.
*
* @return The version this one should be updated to, or an empty Optional
*/
public Optional<Version> shouldBeUpdatedTo(List<Version> availableVersions) {
Optional<Version> newerVersion = Optional.empty();
for (Version version : availableVersions) {
if (this.shouldBeUpdatedTo(version)
&& (!newerVersion.isPresent() || version.isNewerThan(newerVersion.get()))) {
newerVersion = Optional.of(version);
}
}
return newerVersion;
}
/**
* Checks if this version should be updated to the given one.
* Ignoring the other Version if this one is Stable and the other one is not.
*
* @return True if this version should be updated to the given one
*/
public boolean shouldBeUpdatedTo(Version otherVersion) {
// ignoring the other version if it is not stable, except if this version itself is not stable
if (developmentStage == Version.DevelopmentStage.STABLE
&& otherVersion.developmentStage != Version.DevelopmentStage.STABLE) {
return false;
}
// check if the other version is newer than given one
return otherVersion.isNewerThan(this);
}
public String getFullVersion() {
return fullVersion;
}
public int getMajor() {
return major;
}
public int getMinor() {
return minor;
}
public int getPatch() {
return patch;
}
public boolean isDevelopmentVersion() {
return isDevelopmentVersion;
}
/**
* @return The link to the changelog on GitHub to this specific version (https://github.com/JabRef/jabref/blob/vX.X/CHANGELOG.md)
*/
public String getChangelogUrl() {
if (isDevelopmentVersion) {
return "https://github.com/JabRef/jabref/blob/master/CHANGELOG.md#unreleased";
} else {
StringBuilder changelogLink = new StringBuilder()
.append("https://github.com/JabRef/jabref/blob/v")
.append(this.getMajor())
.append(".")
.append(this.getMinor());
if (this.getPatch() != 0) {
changelogLink
.append(".")
.append(this.getPatch());
}
changelogLink
.append(this.developmentStage.stage)
.append("/CHANGELOG.md");
return changelogLink.toString();
}
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Version)) {
return false;
}
// till all the information are stripped from the fullversion this should suffice
return this.getFullVersion().equals(((Version) other).getFullVersion());
}
@Override
public int hashCode() {
return getFullVersion().hashCode();
}
@Override
public String toString() {
return this.getFullVersion();
}
public enum DevelopmentStage {
UNKNOWN("", 0),
ALPHA("-alpha", 1),
BETA("-beta", 2),
STABLE("", 3);
/**
* describes how stable this stage is, the higher the better
*/
private final int stability;
private final String stage;
DevelopmentStage(String stage, int stability) {
this.stage = stage;
this.stability = stability;
}
public static DevelopmentStage parse(String stage) {
if (stage == null) {
LOGGER.warn("The stage cannot be null");
return UNKNOWN;
} else if (stage.equals(STABLE.stage)) {
return STABLE;
} else if (stage.equals(ALPHA.stage)) {
return ALPHA;
} else if (stage.equals(BETA.stage)) {
return BETA;
}
LOGGER.warn("Unknown development stage: {}", stage);
return UNKNOWN;
}
/**
* @return true if this stage is more stable than the {@code otherStage}
*/
public boolean isMoreStableThan(DevelopmentStage otherStage) {
return this.stability > otherStage.stability;
}
}
}