/
VersionedItem.java
284 lines (255 loc) · 9.35 KB
/
VersionedItem.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
package com.kantenkugel.discordbot.versioncheck.items;
import com.almightyalpaca.discord.jdabutler.Bot;
import com.kantenkugel.discordbot.versioncheck.DependencyType;
import com.kantenkugel.discordbot.versioncheck.RepoType;
import com.kantenkugel.discordbot.versioncheck.UpdateHandler;
import com.kantenkugel.discordbot.versioncheck.VersionUtils;
import com.kantenkugel.discordbot.versioncheck.changelog.ChangelogProvider;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import java.util.*;
import java.util.function.Supplier;
public abstract class VersionedItem
{
/**
* The name of this item. Used to display this item in various command callbacks.
* <br/> Should be human readable and capitalized as needed. Will be cast to lowercase for the registry
*
* @return The name of the item
*/
public abstract String getName();
/**
* The description of this version item, used for listing purposes.
*
* @return The description
*/
public String getDescription() { throw new UnsupportedOperationException(); }
/**
* If available, this is used in the registry lookup methods to get this item via alternative names.
* Should be collision free with other items.
*
* @return Null-able list of aliases of this item.
*/
public List<String> getAliases()
{
return null;
}
/**
* The maven repository type of this item (maven artifact).
* <br/>Used for automated version retrieval from maven repository.
*
* <p><b>Note:</b> This may only be {@code null}, if {@link #getCustomVersionSupplier()} is used.
*
* @return The repository type of this maven artifact
*/
public abstract RepoType getRepoType();
/**
* All additional repositories needed to fully resolve this dependency, including transitive dependencies.
* <br/>Used for the maven/gradle commands.
*
* <p><b>Note:</b> This may never be {@code null}.
*
* @return The extra repositories needed for this maven artifact
*/
public Set<RepoType> getAdditionalRepositories() {
return EnumSet.noneOf(RepoType.class);
}
/**
* Helper method that combines all Elements off {@link #getAdditionalRepositories()} with {@link #getRepoType()}
*
* @return Set of all relevant Repositories
*/
public final Set<RepoType> getAllRepositories() {
Set<RepoType> repoTypes = new HashSet<>(getAdditionalRepositories());
if(getRepoType() != null) {
repoTypes.add(getRepoType());
}
return repoTypes;
}
/**
* The group id of this item (maven artifact).
* <br/>Used for automated version retrieval from maven repository.
*
* <p><b>Note:</b> This may only be {@code null}, if {@link #getCustomVersionSupplier()} is used.
*
* @return The group id of this maven artifact
*/
public abstract String getGroupId();
/**
* The artifact id of this item (maven artifact).
* <br/>Used for automated version retrieval from maven repository.
*
* <p><b>Note:</b> This may only be {@code null}, if {@link #getCustomVersionSupplier()} is used.
*
* @return The artifact id of this maven artifact
*/
public abstract String getArtifactId();
/**
* The (maven) dependency type of this artifact.
* This is used in the maven commands to set the {@literal <type>} tag (if not DEFAULT)
* as maven can't properly use the type given via pom.
*
* @return The artifact dependency type
*/
public DependencyType getDependencyType()
{
return DependencyType.DEFAULT;
}
/**
* This url, if provided is used in embeds to link to this items main website.
* <br>Best values for this are personal websites, github repo or build servers for example.
*
* @return Null-able main url of this item
*/
public String getUrl()
{
return null;
}
/**
* If this method returns a non-zero Role ID,
* this item integrates into the !notify command to toggle an announcement role.
*
* @return Role ID of the announcement role, or {@code 0} (zero) if unused
*/
public long getAnnouncementRoleId()
{
return 0;
}
/**
* If this method returns a non-zero TextChannel ID,
* this item integrates into the !announce command for custom announcements.
*
* <p>If {@link #getAnnouncementRoleId()} returns {@code 0}, then this is ignored,
* as announcements require a role.
*
* @return TextChannel ID of the announcement channel, or {@code 0} (zero) if unused
*/
public long getAnnouncementChannelId()
{
return 0;
}
/**
* The channel used for the !notify command.
* <br>If this channel is a library channel such as #lavalink or #lavaplayer the notify command will default
* to the notify roles of that channel instead.
*
* <p>If {@link #getAnnouncementRoleId()} returns {@code 0}, then this is ignored,
* as announcements require a role.
*
* @return TextChannel ID of the notify channel, or {@code 0} (zero) if unused
*/
public long getNotifyChannelId()
{
return getAnnouncementChannelId();
}
/**
* This method is used to determine if some specific User can use announcements for this item (!announce command).
* Only used, when {@link #getAnnouncementRoleId()} and {@link #getAnnouncementChannelId()} are returning non-zero.
*
* <p>Note: JDA Staff can always use the announcement command and do therefore not require whitelisting through this method.
*
* @param u
* The User which wants to make an announcement for this item
* @return True, if the given User is allowed to make an announcement for this item
*/
public boolean canAnnounce(User u)
{
return false;
}
/**
* Hook to run custom code once a new version of this item is detected by JDA-Butler.
*
* @return Null-able custom update handler
*/
public UpdateHandler getUpdateHandler()
{
return null;
}
/**
* Hook to run custom code to retrieve the latest version. This is periodically called by the update-checker code.
* <br/>If this is provided, all maven related methods are ignored
* and this item will not be available for maven/gradle commands.
*
* <p><b>Return type of Supplier:</b>
* <br/>Null-able version String indicating current version or failure if {@code null}
*
* @return Null-able custom version supplier
*/
public Supplier<String> getCustomVersionSupplier()
{
return null;
}
/**
* Hook to enable changelogs for this item. This is used in the !changelog command
*
* @return Null-able ChangelogProvider to use
*/
public ChangelogProvider getChangelogProvider()
{
return null;
}
/**
* Parses this item's version to {@link com.kantenkugel.discordbot.versioncheck.VersionUtils.VersionSplits}.
* <br/>The default implementation uses {@link VersionUtils#parseVersion(String)} to parse this version.
* This does not work for versions that are not in the extended SemVer format (as described in {@link VersionUtils#parseVersion(String)})
* <br/>This is currently not used anywhere but should still be correctly implemented if not extended SemVer is available.
*
* @return VersionSplits object reflecting this items version
*/
public VersionUtils.VersionSplits parseVersion()
{
String version = getVersion();
if(version == null)
throw new IllegalStateException("No version fetched so far");
return VersionUtils.parseVersion(version);
}
/**
* The url to this item's {@code maven-metadata.xml} document. This is used in the version-check routine.
* <br/>The default implementation should already work for every standard maven repository and should not be changed.
*
* @return The url to this items maven-metadata.xml file
*/
public String getRepoUrl()
{
return String.format("%s/%s/%s/maven-metadata.xml", getRepoType().getRepoBase(),
getGroupId().replace('.', '/'), getArtifactId());
}
private String version = null;
public final String getVersion()
{
return version;
}
public final void setVersion(String version)
{
this.version = version;
}
public final Role getAnnouncementRole()
{
long rid = getAnnouncementRoleId();
return rid == 0 ? null : Bot.getGuildJda().getRoleById(rid);
}
public final TextChannel getAnnouncementChannel()
{
long cid = getAnnouncementChannelId();
return cid == 0 ? null : Bot.getGuildJda().getTextChannelById(cid);
}
@Override
public int hashCode()
{
return Objects.hash(getRepoType(), getGroupId(), getArtifactId());
}
@Override
public boolean equals(Object obj)
{
if(!(obj instanceof VersionedItem))
return false;
VersionedItem other = (VersionedItem) obj;
return other.getName().equals(this.getName());
}
@Override
public String toString()
{
return String.format("%s (%s:%s:%s)", getName(), getGroupId(), getArtifactId(), version == null ? "Unversioned" : version);
}
}