1616import org .gradle .api .tasks .Internal ;
1717import org .gradle .api .tasks .JavaExec ;
1818import org .gradle .api .tasks .Optional ;
19+ import org .jetbrains .annotations .ApiStatus ;
1920import org .jetbrains .annotations .MustBeInvokedByOverriders ;
20- import org .jetbrains .annotations .Nullable ;
21+ import org .jetbrains .annotations .UnknownNullability ;
2122
2223import javax .inject .Inject ;
2324import java .io .File ;
24- import java .util .Collections ;
25- import java .util .List ;
2625import java .util .Locale ;
27- import java .util .Objects ;
26+ import java .util .Map ;
2827
2928/// This tool execution task is a template on top of [JavaExec] to make executing [tools][Tool] much easier and more
3029/// consistent between plugins.
@@ -143,23 +142,52 @@ protected final void args(String arg, Iterable<? extends File> files) {
143142 /// @param arg The flag to use
144143 /// @param fileProvider The file to add
145144 protected final void args (String arg , FileSystemLocationProperty <? extends FileSystemLocation > fileProvider ) {
146- this .args (arg , fileProvider .getLocationOnly ());
145+ this .args (arg , fileProvider , false );
146+ }
147+
148+ /// Adds the given argument followed by the given file location to the arguments.
149+ ///
150+ /// @param arg The flag to use
151+ /// @param fileProvider The file to add
152+ protected final void args (String arg , FileSystemLocationProperty <? extends FileSystemLocation > fileProvider , boolean locationOnly ) {
153+ this .args (arg , locationOnly ? fileProvider .getLocationOnly () : fileProvider );
147154 }
148155
149156 /// Adds the given argument followed by the given object (may be a file location) to the arguments.
150157 ///
151158 /// @param arg The flag to use
152159 /// @param provider The object (or file) to add
153- protected final void args (String arg , Provider <?> provider ) {
154- Object value = provider . map ( it -> it instanceof FileSystemLocation ? (( FileSystemLocation ) it ). getAsFile () : it ). get () ;
160+ protected final void args (String arg , @ UnknownNullability Provider <?> provider ) {
161+ if ( provider == null || ! provider . isPresent ()) return ;
155162
156- this .args (arg , String .valueOf (value ));
163+ // NOTE: We don't use File#getAbsoluteFile because path sensitivity should be handled by tasks.
164+ Object value = provider .map (it -> it instanceof FileSystemLocation ? ((FileSystemLocation ) it ).getAsFile () : it ).getOrNull ();
165+ if (value == null ) return ;
166+
167+ if (value instanceof Boolean && ((boolean ) value ))
168+ this .args (arg );
169+ else
170+ this .args (arg , String .valueOf (value ));
171+ }
172+
173+ protected final void args (Map <?, ?> args ) {
174+ for (Map .Entry <?, ?> entry : args .entrySet ()) {
175+ Object key = entry .getKey ();
176+ Object value = entry .getValue ();
177+ this .args (
178+ key instanceof Provider ? ((Provider <?>) key ).map (Object ::toString ).get () : this .getProviderFactory ().provider (() -> key ).map (Object ::toString ).get (),
179+ value instanceof Provider ? (Provider <?>) value : this .getProviderFactory ().provider (() -> value )
180+ );
181+ }
157182 }
158183
159184 /// Adds the given argument if and only if the given boolean property is [present][Provider#isPresent()] and true.
160185 ///
161186 /// @param arg The argument to add
162187 /// @param onlyIf The provider to test
188+ /// @deprecated Use [#args(String, Provider)].
189+ @ Deprecated
190+ @ ApiStatus .ScheduledForRemoval
163191 protected final void argOnlyIf (String arg , Provider <Boolean > onlyIf ) {
164192 this .argOnlyIf (arg , task -> onlyIf .isPresent () && onlyIf .getOrElse (false ));
165193 }
@@ -168,6 +196,9 @@ protected final void argOnlyIf(String arg, Provider<Boolean> onlyIf) {
168196 ///
169197 /// @param arg The argument to add
170198 /// @param onlyIf The spec to test
199+ /// @deprecated Use [#args(String, Provider)].
200+ @ Deprecated
201+ @ ApiStatus .ScheduledForRemoval
171202 protected final void argOnlyIf (String arg , Spec <? super ToolExecBase <?>> onlyIf ) {
172203 if (onlyIf .isSatisfiedBy (this ))
173204 this .args (arg );
0 commit comments