@@ -274,20 +274,25 @@ public Integer call() throws IOException {
274274 logEnteringPhase (Logs .ExecutionPhase .STARTING );
275275 log .info ("codemodder: java/{}" , CLI .class .getPackage ().getImplementationVersion ());
276276
277- if (output == null ) {
278- log .error ("The output file is required" );
279- return ERROR_CANT_WRITE_OUTPUT_FILE ;
280- }
281-
282277 if (projectDirectory == null ) {
283278 log .error ("No project directory specified" );
284279 return ERROR_CANT_READ_PROJECT_DIRECTORY ;
285280 }
286281
287- Path outputPath = output .toPath ();
288- if (!Files .isWritable (outputPath ) && !Files .isWritable (outputPath .getParent ())) {
289- log .error ("The output file (or its parent directory) is not writable" );
290- return ERROR_CANT_WRITE_OUTPUT_FILE ;
282+ Path outputPath = null ;
283+ if (output != null ) {
284+ outputPath = output .toPath ();
285+
286+ // check if the output file parent directory doesn't exist
287+ if (!Files .exists (outputPath .getParent ())) {
288+ // create it
289+ Files .createDirectories (outputPath .getParent ());
290+ }
291+
292+ if (!Files .isWritable (outputPath ) && !Files .isWritable (outputPath .getParent ())) {
293+ log .error ("The output file (or its parent directory) is not writable" );
294+ return ERROR_CANT_WRITE_OUTPUT_FILE ;
295+ }
291296 }
292297
293298 Path projectPath = projectDirectory .toPath ();
@@ -454,23 +459,25 @@ public Integer call() throws IOException {
454459 logEnteringPhase (Logs .ExecutionPhase .REPORT );
455460 logMetrics (results );
456461
457- // write out the output
458- if (OutputFormat .CODETF .equals (outputFormat )) {
459- CodeTFReport report =
460- reportGenerator .createReport (
461- projectDirectory .toPath (),
462- String .join (" " , args ),
463- sarifs == null
464- ? List .of ()
465- : sarifs .stream ().map (Path ::of ).collect (Collectors .toList ()),
466- results ,
467- elapsed );
468- ObjectMapper mapper = new ObjectMapper ();
469- mapper .setSerializationInclusion (JsonInclude .Include .NON_NULL );
470- Files .writeString (outputPath , mapper .writeValueAsString (report ));
471- log .debug ("report file: {}" , outputPath );
472- } else if (OutputFormat .DIFF .equals (outputFormat )) {
473- throw new UnsupportedOperationException ("not supported yet" );
462+ // write out the output if they want it
463+ if (outputPath != null ) {
464+ if (OutputFormat .CODETF .equals (outputFormat )) {
465+ CodeTFReport report =
466+ reportGenerator .createReport (
467+ projectDirectory .toPath (),
468+ String .join (" " , args ),
469+ sarifs == null
470+ ? List .of ()
471+ : sarifs .stream ().map (Path ::of ).collect (Collectors .toList ()),
472+ results ,
473+ elapsed );
474+ ObjectMapper mapper = new ObjectMapper ();
475+ mapper .setSerializationInclusion (JsonInclude .Include .NON_NULL );
476+ Files .writeString (outputPath , mapper .writeValueAsString (report ));
477+ log .debug ("report file: {}" , outputPath );
478+ } else if (OutputFormat .DIFF .equals (outputFormat )) {
479+ throw new UnsupportedOperationException ("not supported yet" );
480+ }
474481 }
475482
476483 log .debug ("elapsed: {}ms" , elapsed );
0 commit comments