76
76
import com .google .devtools .common .options .OptionsBase ;
77
77
import com .google .devtools .common .options .OptionsParser ;
78
78
import com .google .devtools .common .options .OptionsParsingException ;
79
+ import com .google .errorprone .annotations .CanIgnoreReturnValue ;
79
80
import com .google .protobuf .Any ;
80
81
import com .google .protobuf .Message ;
81
82
import java .util .ArrayList ;
@@ -172,19 +173,25 @@ public final CommandEnvironment newCommand(Class<? extends BlazeCommand> command
172
173
173
174
/**
174
175
* Creates a new command environment with additional proto extensions as if they were passed to
175
- * the blaze server.
176
+ * the Blaze server.
177
+ *
178
+ * @param command the command instance for which to create a new environment.
179
+ * @param extensions additional proto extensions to pass to the command.
180
+ * @return the new command environment.
176
181
*/
177
- public final CommandEnvironment newCommandWithExtensions (
178
- Class <? extends BlazeCommand > command , List <Message > extensions ) throws Exception {
182
+ @ CanIgnoreReturnValue
183
+ public final CommandEnvironment newCustomCommandWithExtensions (
184
+ BlazeCommand command , List <Message > extensions ) throws Exception {
179
185
Command commandAnnotation =
180
186
checkNotNull (
181
- command .getAnnotation (Command .class ),
187
+ command .getClass (). getAnnotation (Command .class ),
182
188
"BlazeCommand %s missing command annotation" ,
183
- command );
184
- this .command = command .getDeclaredConstructor ().newInstance ();
189
+ command .getClass ());
190
+ this .command = command ;
191
+
185
192
additionalOptionsClasses .addAll (
186
193
BlazeCommandUtils .getOptions (
187
- command , runtime .getBlazeModules (), runtime .getRuleClassProvider ()));
194
+ command . getClass () , runtime .getBlazeModules (), runtime .getRuleClassProvider ()));
188
195
initializeOptionsParser (commandAnnotation );
189
196
190
197
checkNotNull (
@@ -211,6 +218,22 @@ public final CommandEnvironment newCommandWithExtensions(
211
218
return env ;
212
219
}
213
220
221
+ /**
222
+ * Creates a new command environment with additional proto extensions as if they were passed to
223
+ * the Blaze server. This method creates a new instance of the provided command class via its
224
+ * default constructor. For command classes with constructor parameters, use {@link
225
+ * #newCustomCommandWithExtensions} and pass in a pre-existing {@link BlazeCommand} instance.
226
+ *
227
+ * @param command the command class for which to create a new environment. This class must have a
228
+ * default constructor or this method will throw an exception.
229
+ * @param extensions additional proto extensions to pass to the command.
230
+ */
231
+ public final CommandEnvironment newCommandWithExtensions (
232
+ Class <? extends BlazeCommand > command , List <Message > extensions ) throws Exception {
233
+ return newCustomCommandWithExtensions (
234
+ command .getDeclaredConstructor ().newInstance (), extensions );
235
+ }
236
+
214
237
/**
215
238
* Returns the command environment. You must call {@link #newCommand()} before calling this
216
239
* method.
@@ -236,6 +259,14 @@ public void addOptions(List<String> args) {
236
259
optionsToParse .addAll (args );
237
260
}
238
261
262
+ public void setOptionsParserResidue (List <String > residue , List <String > postDoubleDashResidue ) {
263
+ optionsParser .setResidue (residue , postDoubleDashResidue );
264
+ }
265
+
266
+ public void setConfiguration (BuildConfigurationValue configuration ) {
267
+ this .configuration = configuration ;
268
+ }
269
+
239
270
public void addStarlarkOption (String label , Object value ) {
240
271
starlarkOptions .put (Label .parseCanonicalUnchecked (label ).getCanonicalForm (), value );
241
272
}
@@ -319,10 +350,10 @@ private OptionsParser createOptionsParser(Command commandAnnotation) {
319
350
return OptionsParser .builder ().optionsClasses (options ).ignoreUserOptions ().build ();
320
351
}
321
352
322
- void executeNonBuildCommand () throws Exception {
353
+ public void executeCustomCommand () throws Exception {
323
354
checkNotNull (command , "No command created, try calling newCommand()" );
324
355
checkState (
325
- env .getCommand ().buildPhase () == NONE ,
356
+ env .getCommand ().buildPhase () == NONE || env . getCommandName (). equals ( "run" ) ,
326
357
"%s is a build command, did you mean to call executeBuild()?" ,
327
358
env .getCommandName ());
328
359
@@ -337,6 +368,11 @@ void executeNonBuildCommand() throws Exception {
337
368
try {
338
369
Crash crash = null ;
339
370
try {
371
+ if (env .getCommandName ().equals ("run" )) {
372
+ try (SilentCloseable c = Profiler .instance ().profile ("syncPackageLoading" )) {
373
+ env .syncPackageLoading (optionsParser );
374
+ }
375
+ }
340
376
result = command .exec (env , optionsParser );
341
377
} catch (RuntimeException | Error e ) {
342
378
crash = Crash .from (e );
0 commit comments