|
8 | 8 | import groovy.lang.DelegatesTo; |
9 | 9 | import groovy.transform.stc.ClosureParams; |
10 | 10 | import groovy.transform.stc.FirstParam; |
| 11 | +import kotlin.jvm.functions.Function0; |
11 | 12 | import org.gradle.TaskExecutionRequest; |
12 | 13 | import org.gradle.api.Action; |
13 | 14 | import org.gradle.api.DomainObjectCollection; |
|
24 | 25 | import org.gradle.api.plugins.JavaPluginExtension; |
25 | 26 | import org.gradle.api.provider.Property; |
26 | 27 | import org.gradle.api.provider.Provider; |
| 28 | +import org.gradle.api.provider.ProviderConvertible; |
27 | 29 | import org.gradle.api.provider.ProviderFactory; |
28 | 30 | import org.gradle.api.specs.Spec; |
29 | 31 | import org.gradle.api.tasks.SourceSet; |
|
41 | 43 | import java.io.InputStreamReader; |
42 | 44 | import java.io.PipedInputStream; |
43 | 45 | import java.io.PipedOutputStream; |
| 46 | +import java.text.MessageFormat; |
44 | 47 | import java.util.ArrayList; |
45 | 48 | import java.util.Collection; |
46 | 49 | import java.util.List; |
47 | 50 | import java.util.Objects; |
48 | 51 | import java.util.Set; |
| 52 | +import java.util.concurrent.Callable; |
49 | 53 | import java.util.function.Consumer; |
50 | 54 | import java.util.function.Predicate; |
| 55 | +import java.util.function.Supplier; |
51 | 56 | import java.util.stream.Collectors; |
52 | 57 | import java.util.stream.Stream; |
53 | 58 |
|
@@ -445,6 +450,118 @@ public static String toString(Dependency dependency) { |
445 | 450 | dependency instanceof FileCollectionDependency ? String.format(" [%s]", String.join(", ", ((FileCollectionDependency) dependency).getFiles().getFiles().stream().map(File::getAbsolutePath).map(CharSequence.class::cast)::iterator)) : "" |
446 | 451 | ); |
447 | 452 | } |
| 453 | + |
| 454 | + /// Converts a given dependency to a relative path, accounting for the possible nullable |
| 455 | + /// {@linkplain Dependency#getGroup() group} and {@linkplain Dependency#getVersion() version}. |
| 456 | + /// |
| 457 | + /// @param dependency The dependency to pathify |
| 458 | + /// @return The pathified dependency |
| 459 | + public static String pathify(Dependency dependency) { |
| 460 | + var group = dependency.getGroup(); |
| 461 | + var name = dependency.getName(); |
| 462 | + var version = dependency.getVersion(); |
| 463 | + return MessageFormat.format("{0}{1}/{2}", |
| 464 | + group == null ? "" : group.replace('.', '/') + '/', |
| 465 | + name, |
| 466 | + version == null ? "/" : '/' + version + '/' |
| 467 | + ); |
| 468 | + } |
| 469 | + |
| 470 | + //endregion |
| 471 | + |
| 472 | + //region Deferred Objects |
| 473 | + |
| 474 | + /// Unpacks a deferred value. |
| 475 | + /// |
| 476 | + /// @param value The value to unpack |
| 477 | + /// @param <T> The type of value held by the provider |
| 478 | + /// @return The unpacked value |
| 479 | + /// @see #unpack(Object) |
| 480 | + public static <T> T unpack(Provider<T> value) { |
| 481 | + return value.get(); |
| 482 | + } |
| 483 | + |
| 484 | + /// Unpacks a deferred value. |
| 485 | + /// |
| 486 | + /// @param value The value to unpack |
| 487 | + /// @param <T> The type of value held by the provider |
| 488 | + /// @return The unpacked value |
| 489 | + /// @see #unpack(Object) |
| 490 | + public static <T> T unpack(ProviderConvertible<T> value) { |
| 491 | + return value.asProvider().get(); |
| 492 | + } |
| 493 | + |
| 494 | + /// Unpacks a deferred value. |
| 495 | + /// |
| 496 | + /// @param value The value to unpack |
| 497 | + /// @param <T> The type of value held by the provider |
| 498 | + /// @return The unpacked value |
| 499 | + /// @see #unpack(Object) |
| 500 | + public static <T> T unpack(Closure<T> value) { |
| 501 | + return Closures.invoke(value); |
| 502 | + } |
| 503 | + |
| 504 | + /// Unpacks a deferred value. |
| 505 | + /// |
| 506 | + /// @param value The value to unpack |
| 507 | + /// @param <T> The type of value held by the provider |
| 508 | + /// @return The unpacked value |
| 509 | + /// @see #unpack(Object) |
| 510 | + public static <T> T unpack(Callable<T> value) { |
| 511 | + try { |
| 512 | + return value.call(); |
| 513 | + } catch (Exception e) { |
| 514 | + throw new RuntimeException(e); |
| 515 | + } |
| 516 | + } |
| 517 | + |
| 518 | + /// Unpacks a deferred value. |
| 519 | + /// |
| 520 | + /// @param value The value to unpack |
| 521 | + /// @param <T> The type of value held by the provider |
| 522 | + /// @return The unpacked value |
| 523 | + /// @see #unpack(Object) |
| 524 | + public static <T> T unpack(Function0<T> value) { |
| 525 | + return value.invoke(); |
| 526 | + } |
| 527 | + |
| 528 | + /// Unpacks a deferred value. |
| 529 | + /// |
| 530 | + /// @param value The value to unpack |
| 531 | + /// @param <T> The type of value held by the provider |
| 532 | + /// @return The unpacked value |
| 533 | + /// @see #unpack(Object) |
| 534 | + public static <T> T unpack(Supplier<T> value) { |
| 535 | + return value.get(); |
| 536 | + } |
| 537 | + |
| 538 | + /// Unpacks a deferred value. |
| 539 | + /// |
| 540 | + /// Since buildscripts are dynamically compiled, this allows buildscript authors to use this method with version |
| 541 | + /// catalog entries, other provider-like objects. This prevents the need to arbitrarily call [Provider#get()] (or |
| 542 | + /// similar) on values which may or may not be deferred based on circumstance. |
| 543 | + /// |
| 544 | + /// @param value The value to unpack |
| 545 | + /// @param <T> The type of value held by the provider |
| 546 | + /// @return The unpacked value |
| 547 | + @SuppressWarnings("unchecked") |
| 548 | + public static <T> T unpack(Object value) { |
| 549 | + if (value instanceof ProviderConvertible<?> deferred) { |
| 550 | + return (T) unpack(deferred); |
| 551 | + } else if (value instanceof Provider<?> deferred) { |
| 552 | + return (T) unpack(deferred); |
| 553 | + } else if (value instanceof Closure<?> deferred) { |
| 554 | + return (T) unpack(deferred); |
| 555 | + } else if (value instanceof Callable<?> deferred) { |
| 556 | + return (T) unpack(deferred); |
| 557 | + } else if (value instanceof Function0<?> deferred) { |
| 558 | + return (T) unpack(deferred); |
| 559 | + } else if (value instanceof Supplier<?> deferred) { |
| 560 | + return (T) unpack(deferred); |
| 561 | + } else { |
| 562 | + return (T) value; |
| 563 | + } |
| 564 | + } |
448 | 565 | //endregion |
449 | 566 |
|
450 | 567 | //region Properties |
|
0 commit comments