Skip to content

Commit 828d941

Browse files
committed
Allow tag prefix and filters to be set after creation
Might delete this later, so don't count on it. This is for GradleUtils 2.x backwards compatibility in 2.4.
1 parent 35844bb commit 828d941

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/main/java/net/minecraftforge/gitver/api/GitVersion.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
2525
import java.util.Collection;
26+
import java.util.List;
2627
import java.util.stream.Collectors;
2728

2829
/**
@@ -329,9 +330,38 @@ default String getBranch(boolean versionFriendly) {
329330
/** @return The tag prefix used when filtering tags */
330331
String getTagPrefix();
331332

333+
/**
334+
* Sets the tag prefix to use when filtering tags.
335+
*
336+
* @param tagPrefix The tag prefix
337+
* @deprecated This method only exists for backwards compatibility in GradleUtils 2.4. Using this is discouraged
338+
* since it breaks the contract that the {@linkplain GitVersionConfig config} has the declarations of all the
339+
* project's values, including the {@linkplain GitVersionConfig.Project#getTagPrefix() tag prefix} and any
340+
* additional {@linkplain GitVersionConfig.Project#getFilters() filters}.
341+
*/
342+
@Deprecated
343+
default void setTagPrefix(String tagPrefix) { }
344+
332345
/** @return The filters used when filtering tags (excluding the {@linkplain #getTagPrefix() tag prefix}) */
333346
@UnmodifiableView Collection<String> getFilters();
334347

348+
/** @see #setFilters(String...) */
349+
default void setFilters(Collection<String> filters) {
350+
this.setFilters(filters.toArray(new String[0]));
351+
}
352+
353+
/**
354+
* Sets the filters to use when filtering tags (excluding the {@linkplain #getTagPrefix() tag prefix}).
355+
*
356+
* @param filters The filters
357+
* @deprecated This method only exists for backwards compatibility in GradleUtils 2.4. Using this is discouraged
358+
* since it breaks the contract that the {@linkplain GitVersionConfig config} has the declarations of all the
359+
* project's values, including the {@linkplain GitVersionConfig.Project#getTagPrefix() tag prefix} and any
360+
* additional {@linkplain GitVersionConfig.Project#getFilters() filters}.
361+
*/
362+
@Deprecated
363+
default void setFilters(String... filters) { }
364+
335365

336366
/* FILE SYSTEM */
337367

src/main/java/net/minecraftforge/gitver/internal/GitVersionImpl.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.util.ArrayList;
24-
import java.util.Arrays;
2524
import java.util.Collection;
2625
import java.util.Collections;
2726
import java.util.List;
@@ -40,12 +39,12 @@ public sealed class GitVersionImpl implements GitVersion permits GitVersionImpl.
4039
public final String localPath;
4140

4241
// Config
43-
private final String tagPrefix;
44-
private final String[] filters;
42+
private String tagPrefix;
43+
private String[] filters;
4544
private final List<File> subprojects;
4645

4746
// Unmodifiable views
48-
private final List<String> filtersView;
47+
private final Lazy<List<String>> filtersView = Lazy.of(() -> this.filters.length == 0 ? Collections.emptyList() : List.of(this.filters));
4948

5049
public GitVersionImpl(File gitDir, File root, File project, GitVersionConfig config, boolean strict) {
5150
this.strict = strict;
@@ -73,8 +72,6 @@ public GitVersionImpl(File gitDir, File root, File project, GitVersionConfig con
7372
this.tagPrefix = projectConfig.getTagPrefix();
7473
this.filters = projectConfig.getFilters();
7574
this.subprojects = this.calculateSubprojects(config.getAllProjects());
76-
77-
this.filtersView = this.filters.length == 0 ? Collections.emptyList() : Collections.unmodifiableList(Arrays.asList(this.filters));
7875
}
7976

8077

@@ -211,9 +208,22 @@ public String getTagPrefix() {
211208
return this.tagPrefix;
212209
}
213210

211+
@Override
212+
public void setTagPrefix(String tagPrefix) {
213+
this.tagPrefix = tagPrefix;
214+
this.info.reset();
215+
}
216+
214217
@Override
215218
public @UnmodifiableView Collection<String> getFilters() {
216-
return this.filtersView;
219+
return this.filtersView.get();
220+
}
221+
222+
@Override
223+
public void setFilters(String... filters) {
224+
this.filters = filters;
225+
this.filtersView.reset();
226+
this.info.reset();
217227
}
218228

219229

@@ -326,7 +336,6 @@ private GitVersionImpl() {
326336
this.tagPrefix = "";
327337
this.filters = new String[0];
328338
this.subprojects = Collections.emptyList();
329-
this.filtersView = Collections.emptyList();
330339
}
331340

332341
static non-sealed class Empty extends GitVersionImpl {

src/main/java/net/minecraftforge/gitver/internal/Lazy.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
import org.jetbrains.annotations.Nullable;
88

9+
import java.util.function.Consumer;
910
import java.util.function.Supplier;
1011

1112
final class Lazy<T> implements Supplier<T> {
12-
private final Supplier<T> supplier;
13+
private Supplier<T> supplier;
1314
private @Nullable T value;
1415

1516
static <T> Lazy<T> of(Supplier<T> supplier) {
@@ -24,4 +25,18 @@ private Lazy(Supplier<T> supplier) {
2425
public T get() {
2526
return this.value != null ? this.value : (this.value = this.supplier.get());
2627
}
28+
29+
void reset() {
30+
this.value = null;
31+
}
32+
33+
void modify(Consumer<T> action) {
34+
Supplier<T> supplier = this.value != null ? () -> this.value : this.supplier;
35+
this.supplier = () -> {
36+
T value = supplier.get();
37+
action.accept(value);
38+
return value;
39+
};
40+
this.value = null;
41+
}
2742
}

0 commit comments

Comments
 (0)