22
33import org .slf4j .Logger ;
44
5+ import java .io .File ;
56import java .io .IOException ;
67import java .nio .file .FileAlreadyExistsException ;
78import java .nio .file .Files ;
@@ -31,10 +32,14 @@ public ChangesetWriter(Path baseDir) {
3132 }
3233
3334 public void writeChangeset (Changeset changeset ) throws FileAlreadyExistsException {
34- writeChangeset (changeset .packageName (), changeset .level (), changeset .message ());
35+ writeChangeset (changeset .packageName (), changeset .level (), changeset .message (), changeset . file () );
3536 }
3637
3738 Path writeChangeset (String packageName , Level changeLevel , String message ) throws FileAlreadyExistsException {
39+ return writeChangeset (packageName , changeLevel , message , null );
40+ }
41+
42+ Path writeChangeset (String packageName , Level changeLevel , String message , File file ) throws FileAlreadyExistsException {
3843 final String fileContent ;
3944 if (message == null ) {
4045 fileContent = """
@@ -62,6 +67,26 @@ Path writeChangeset(String packageName, Level changeLevel, String message) throw
6267 }
6368 }
6469
70+
71+ Path changesetFile ;
72+ if (file != null ) {
73+ // TODO Add tests
74+ changesetFile = file .toPath ();
75+ } else {
76+ changesetFile = generateChangesetFilename (changesetsDir );
77+ }
78+
79+ try {
80+ LOG .info ("Writing changeset to {}" , changesetFile );
81+ Files .writeString (changesetFile , fileContent , StandardOpenOption .CREATE_NEW );
82+ } catch (IOException e ) {
83+ LOG .error ("Failed to create new changeset" , e );
84+ }
85+
86+ return changesetFile ;
87+ }
88+
89+ private Path generateChangesetFilename (Path changesetsDir ) throws FileAlreadyExistsException {
6590 String newFileName = this .nameGenerator .humanId () + ".md" ;
6691 Path changesetFile = changesetsDir .resolve (newFileName );
6792
@@ -80,14 +105,6 @@ Path writeChangeset(String packageName, Level changeLevel, String message) throw
80105 throw new FileAlreadyExistsException (string , null , "Failed to generate a unique name after %s attempts" .formatted (attempt ));
81106 }
82107 }
83-
84- try {
85- LOG .info ("Writing changeset to {}" , changesetFile );
86- Files .writeString (changesetFile , fileContent , StandardOpenOption .CREATE_NEW );
87- } catch (IOException e ) {
88- LOG .error ("Failed to create new changeset" , e );
89- }
90-
91108 return changesetFile ;
92109 }
93110}
0 commit comments