11package org .antipathy .mvn_scalafmt ;
22
33import org .antipathy .mvn_scalafmt .model .Summary ;
4+ import org .apache .maven .model .Build ;
45import org .apache .maven .plugin .AbstractMojo ;
56import org .apache .maven .plugin .MojoExecutionException ;
67import org .apache .maven .plugins .annotations .LifecyclePhase ;
1011import org .apache .maven .model .Repository ;
1112
1213import java .io .File ;
14+ import java .io .IOException ;
1315import java .util .ArrayList ;
16+ import java .util .HashSet ;
1417import java .util .List ;
1518
1619/**
@@ -25,9 +28,9 @@ public class FormatMojo extends AbstractMojo {
2528 private boolean skipTestSources ;
2629 @ Parameter (property = "format.skipSources" , defaultValue = "false" )
2730 private boolean skipSources ;
28- @ Parameter (defaultValue = "${project.build.sourceDirectory}/../scala" , required = true )
31+ @ Parameter ()
2932 private List <File > sourceDirectories ;
30- @ Parameter (defaultValue = "${project.build.testSourceDirectory}/../scala" , required = true )
33+ @ Parameter ()
3134 private List <File > testSourceDirectories ;
3235 @ Parameter (property = "format.respectVersion" , defaultValue = "false" , required = true )
3336 private boolean respectVersion ;
@@ -62,19 +65,13 @@ private List<String> getRepositoriesUrls(List<Repository> repositories) {
6265
6366 public void execute () throws MojoExecutionException {
6467
65- List <File > sources = new ArrayList <>();
66-
67- if (!skipSources ) {
68- sources .addAll (sourceDirectories );
69- } else {
70- getLog ().warn ("format.skipSources set, ignoring main directories" );
68+ List <File > sources ;
69+ try {
70+ sources = getSources ();
71+ } catch (IOException exception ) {
72+ throw new MojoExecutionException ("Couldn't determine canonical sources" , exception );
7173 }
7274
73- if (!skipTestSources ) {
74- sources .addAll (testSourceDirectories );
75- } else {
76- getLog ().warn ("format.skipTestSources set, ignoring validateOnly directories" );
77- }
7875 if (!sources .isEmpty ()) {
7976 try {
8077
@@ -87,7 +84,9 @@ public void execute() throws MojoExecutionException {
8784 showReformattedOnly ,
8885 branch ,
8986 project .getBasedir (),
90- useSpecifiedRepositories ? getRepositoriesUrls (mavenRepositories ) : new ArrayList <String >()
87+ useSpecifiedRepositories ?
88+ getRepositoriesUrls (project .getRepositories ()) :
89+ new ArrayList <String >()
9190 ).format (sources );
9291 getLog ().info (result .toString ());
9392 if (validateOnly && result .unformattedFiles () != 0 ) {
@@ -101,4 +100,51 @@ public void execute() throws MojoExecutionException {
101100 getLog ().warn ("No sources specified, skipping formatting" );
102101 }
103102 }
103+
104+ private List <File > getSources () throws IOException {
105+ HashSet <File > sources = new HashSet <>();
106+ Build build = project .getBuild ();
107+
108+ if (skipSources ) {
109+ getLog ().warn ("format.skipSources set, ignoring main directories" );
110+ } else if (sourceDirectories == null || sourceDirectories .isEmpty ()) {
111+ appendCanonicalSources (
112+ sources ,
113+ project .getCompileSourceRoots (),
114+ build .getSourceDirectory ()
115+ );
116+ } else {
117+ sources .addAll (sourceDirectories );
118+ }
119+
120+ if (skipTestSources ) {
121+ getLog ().warn ("format.skipTestSources set, ignoring validateOnly directories" );
122+ } else if (testSourceDirectories == null || testSourceDirectories .isEmpty ()) {
123+ appendCanonicalSources (
124+ sources ,
125+ project .getTestCompileSourceRoots (),
126+ build .getTestSourceDirectory ()
127+ );
128+ } else {
129+ sources .addAll (testSourceDirectories );
130+ }
131+
132+ return new ArrayList <>(sources );
133+ }
134+
135+ private void appendCanonicalSources (
136+ HashSet <File > sources ,
137+ List <String > sourceRoots ,
138+ String defaultSource
139+ ) throws IOException {
140+ for (String source : sourceRoots ) {
141+ sources .add (getCanonicalFile (source ));
142+ }
143+ sources .add (getCanonicalFile (defaultSource + "/../scala" ));
144+ }
145+
146+ private File getCanonicalFile (String relative ) throws IOException {
147+ return new File (project .getBasedir (), relative ).getCanonicalFile ();
148+ }
149+
104150}
0 commit comments