15
15
* Abstract parent of all Scala Mojo who run compilation
16
16
*/
17
17
public abstract class ScalaCompilerSupport extends ScalaSourceMojoSupport {
18
-
18
+
19
19
public static final String ALL = "all" ;
20
20
public static final String INCREMENTAL = "incremental" ;
21
-
21
+
22
22
/**
23
23
* Keeps track of if we get compile errors in incremental mode
24
24
*/
25
25
private boolean compileErrors ;
26
-
26
+
27
27
/**
28
28
* Recompile mode to use when sources were previously compiled and there is at
29
29
* least one change:
@@ -34,7 +34,7 @@ public abstract class ScalaCompilerSupport extends ScalaSourceMojoSupport {
34
34
*/
35
35
@ Parameter (property = "recompileMode" , defaultValue = "all" )
36
36
protected String recompileMode = ALL ;
37
-
37
+
38
38
/**
39
39
* notifyCompilation if true then print a message "path: compiling"
40
40
* for each root directory or files that will be compiled.
@@ -44,20 +44,20 @@ public abstract class ScalaCompilerSupport extends ScalaSourceMojoSupport {
44
44
*/
45
45
@ Parameter (property = "notifyCompilation" , defaultValue = "true" )
46
46
private boolean notifyCompilation = true ;
47
-
47
+
48
48
abstract protected File getOutputDir () throws Exception ;
49
-
49
+
50
50
abstract protected List <String > getClasspathElements () throws Exception ;
51
-
51
+
52
52
private long _lastCompileAt = -1 ;
53
-
53
+
54
54
private SbtIncrementalCompiler incremental ;
55
-
55
+
56
56
/**
57
57
* Analysis cache file for incremental recompilation.
58
58
*/
59
59
abstract protected File getAnalysisCacheFile () throws Exception ;
60
-
60
+
61
61
/**
62
62
* Compile order for Scala and Java sources for sbt incremental compile.
63
63
*
@@ -66,7 +66,7 @@ public abstract class ScalaCompilerSupport extends ScalaSourceMojoSupport {
66
66
*/
67
67
@ Parameter (property = "compileOrder" , defaultValue = "Mixed" )
68
68
private CompileOrder compileOrder ;
69
-
69
+
70
70
@ Override
71
71
protected void doExecute () throws Exception {
72
72
if (getLog ().isDebugEnabled ()) {
@@ -88,7 +88,7 @@ protected void doExecute() throws Exception {
88
88
break ;
89
89
}
90
90
}
91
-
91
+
92
92
protected int compile (List <File > sourceRootDirs , File outputDir , File analysisCacheFile , List <String > classpathElements , boolean compileInLoop ) throws Exception , InterruptedException {
93
93
if (!compileInLoop && INCREMENTAL .equals (recompileMode )) {
94
94
// TODO - Do we really need this duplicated here?
@@ -98,27 +98,27 @@ protected int compile(List<File> sourceRootDirs, File outputDir, File analysisCa
98
98
// if not compileInLoop, invoke incrementalCompile immediately
99
99
return incrementalCompile (classpathElements , sourceRootDirs , outputDir , analysisCacheFile , compileInLoop );
100
100
}
101
-
101
+
102
102
long t0 = System .currentTimeMillis ();
103
103
LastCompilationInfo lastCompilationInfo = LastCompilationInfo .find (sourceRootDirs , outputDir );
104
104
if (_lastCompileAt < 0 ) {
105
105
_lastCompileAt = lastCompilationInfo .getLastSuccessfullTS ();
106
106
}
107
-
107
+
108
108
List <File > files = getFilesToCompile (sourceRootDirs , _lastCompileAt );
109
-
109
+
110
110
if (files == null ) {
111
111
return -1 ;
112
112
}
113
-
113
+
114
114
if (files .size () < 1 ) {
115
115
return 0 ;
116
116
}
117
117
if (!outputDir .exists ()) {
118
118
outputDir .mkdirs ();
119
119
}
120
120
long t1 = System .currentTimeMillis ();
121
-
121
+
122
122
if (compileInLoop && INCREMENTAL .equals (recompileMode )) {
123
123
// if compileInLoop, do not invoke incrementalCompile when there's no change
124
124
int retCode = incrementalCompile (classpathElements , sourceRootDirs , outputDir , analysisCacheFile , compileInLoop );
@@ -128,7 +128,7 @@ protected int compile(List<File> sourceRootDirs, File outputDir, File analysisCa
128
128
}
129
129
return retCode ;
130
130
}
131
-
131
+
132
132
getLog ().info (String .format ("Compiling %d source files to %s at %d" , files .size (), outputDir .getAbsolutePath (), t1 ));
133
133
JavaMainCaller jcmd = getScalaCommand ();
134
134
jcmd .redirectToLog ();
@@ -149,24 +149,24 @@ protected int compile(List<File> sourceRootDirs, File outputDir, File analysisCa
149
149
_lastCompileAt = t1 ;
150
150
return files .size ();
151
151
}
152
-
152
+
153
153
/**
154
154
* Returns true if the previous compile failed
155
155
*/
156
156
protected boolean hasCompileErrors () {
157
157
return compileErrors ;
158
158
}
159
-
159
+
160
160
protected void clearCompileErrors () {
161
161
compileErrors = false ;
162
162
}
163
-
163
+
164
164
protected List <File > getFilesToCompile (List <File > sourceRootDirs , long lastSuccessfullCompileTime ) throws Exception {
165
165
List <File > sourceFiles = findSourceWithFilters (sourceRootDirs );
166
166
if (sourceFiles .size () == 0 ) {
167
167
return null ;
168
168
}
169
-
169
+
170
170
// filter uptodate
171
171
// filter is not applied to .java, because scalac failed to used existing .class for unmodified .java
172
172
// failed with "error while loading Xxx, class file '.../target/classes/.../Xxxx.class' is broken"
@@ -199,15 +199,15 @@ protected List<File> getFilesToCompile(List<File> sourceRootDirs, long lastSucce
199
199
}
200
200
return files ;
201
201
}
202
-
202
+
203
203
private void notifyCompilation (List <File > files ) throws Exception {
204
204
if (notifyCompilation ) {
205
205
for (File f : files ) {
206
206
getLog ().info (String .format ("%s:-1: info: compiling" , FileUtils .pathOf (f , useCanonicalPath )));
207
207
}
208
208
}
209
209
}
210
-
210
+
211
211
private static class LastCompilationInfo {
212
212
static LastCompilationInfo find (List <File > sourceRootDirs , File outputDir ) throws Exception {
213
213
StringBuilder hash = new StringBuilder ();
@@ -216,42 +216,42 @@ static LastCompilationInfo find(List<File> sourceRootDirs, File outputDir) throw
216
216
}
217
217
return new LastCompilationInfo (new File (outputDir .getAbsolutePath () + "." + hash .toString ().hashCode () + ".timestamp" ), outputDir );
218
218
}
219
-
219
+
220
220
private final File _lastCompileAtFile ;
221
221
private final File _outputDir ;
222
-
222
+
223
223
private LastCompilationInfo (File f , File outputDir ) {
224
224
_lastCompileAtFile = f ;
225
225
_outputDir = outputDir ;
226
226
}
227
-
227
+
228
228
long getLastSuccessfullTS () throws Exception {
229
229
long back = -1 ;
230
230
if (_lastCompileAtFile .exists () && _outputDir .exists () && (_outputDir .list ().length > 0 )) {
231
231
back = _lastCompileAtFile .lastModified ();
232
232
}
233
233
return back ;
234
234
}
235
-
235
+
236
236
void setLastSuccessfullTS (long v ) throws Exception {
237
237
if (!_lastCompileAtFile .exists ()) {
238
238
FileUtils .fileWrite (_lastCompileAtFile .getAbsolutePath (), "." );
239
239
}
240
240
_lastCompileAtFile .setLastModified (v );
241
241
}
242
242
}
243
-
243
+
244
244
//
245
245
// Incremental compilation
246
246
//
247
-
247
+
248
248
@ SuppressWarnings ("unchecked" )
249
249
protected int incrementalCompile (List <String > classpathElements , List <File > sourceRootDirs , File outputDir , File cacheFile , boolean compileInLoop ) throws Exception {
250
250
List <File > sources = findSourceWithFilters (sourceRootDirs );
251
251
if (sources .isEmpty ()) {
252
252
return -1 ;
253
253
}
254
-
254
+
255
255
if (incremental == null ) {
256
256
File libraryJar = getLibraryJar ();
257
257
File reflectJar = getReflectJar ();
@@ -261,11 +261,11 @@ protected int incrementalCompile(List<String> classpathElements, List<File> sour
261
261
File compilerBridgeJar = getCompilerBridgeJar ();
262
262
incremental = new SbtIncrementalCompiler (libraryJar , reflectJar , compilerJar , findScalaVersion (), extraJars , compilerBridgeJar , getLog (), cacheFile , compileOrder );
263
263
}
264
-
264
+
265
265
classpathElements .remove (outputDir .getAbsolutePath ());
266
266
List <String > scalacOptions = getScalaOptions ();
267
267
List <String > javacOptions = getJavacOptions ();
268
-
268
+
269
269
try {
270
270
incremental .compile (classpathElements , sources , outputDir , scalacOptions , javacOptions );
271
271
} catch (xsbti .CompileFailed e ) {
@@ -275,7 +275,7 @@ protected int incrementalCompile(List<String> classpathElements, List<File> sour
275
275
throw e ;
276
276
}
277
277
}
278
-
278
+
279
279
return 1 ;
280
280
}
281
281
}
0 commit comments