1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.scala_tools.maven;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.codehaus.plexus.util.FileUtils;
24
25
26
27
28 public abstract class ScalaCompilerSupport extends ScalaMojoSupport {
29
30
31
32
33 protected long loopSleep = 2500;
34
35
36 abstract protected File getOutputDir() throws Exception;
37
38 abstract protected File getSourceDir() throws Exception;
39
40 abstract protected List<String> getClasspathElements() throws Exception;
41
42 @Override
43 protected void doExecute() throws Exception {
44 File outputDir = normalize(getOutputDir());
45 if (!outputDir.exists()) {
46 outputDir.mkdirs();
47 }
48
49 File sourceDir = normalize(getSourceDir());
50 if (!sourceDir.exists()) {
51 return;
52 }
53 int nbFiles = compile(sourceDir, outputDir, getClasspathElements(), false);
54 switch (nbFiles) {
55 case -1:
56 getLog().warn("No source files found.");
57 break;
58 case 0:
59 getLog().info("Nothing to compile - all classes are up to date");;
60 break;
61 default:
62 break;
63 }
64 }
65
66 protected File normalize(File f) {
67 try {
68 f = f.getCanonicalFile();
69 } catch (IOException exc) {
70 f = f.getAbsoluteFile();
71 }
72 return f;
73 }
74
75 protected int compile(File sourceDir, File outputDir, List<String> classpathElements, boolean compileInLoop) throws Exception, InterruptedException {
76 String[] sourceFiles = JavaCommand.findFiles(sourceDir, "**/*.scala");
77 if (sourceFiles.length == 0) {
78 return -1;
79 }
80 int sourceDirPathLength = sourceDir.getAbsolutePath().length();
81
82
83 File lastCompileAtFile = new File(outputDir + ".timestamp");
84 long lastCompileAt = lastCompileAtFile.lastModified();
85 ArrayList<File> files = new ArrayList<File>(sourceFiles.length);
86 for (String x : sourceFiles) {
87 File f = new File(sourceDir, x);
88 if (f.lastModified() >= lastCompileAt) {
89 files.add(f);
90 }
91 }
92 if (files.size() == 0) {
93 return 0;
94 }
95 if (!compileInLoop) {
96 getLog().info(String.format("Compiling %d source files to %s", files.size(), outputDir.getAbsolutePath()));
97 }
98 long now = System.currentTimeMillis();
99 JavaCommand jcmd = getScalaCommand();
100 jcmd.addArgs("-classpath", JavaCommand.toMultiPath(classpathElements));
101 jcmd.addArgs("-d", outputDir.getAbsolutePath());
102 jcmd.addArgs("-sourcepath", sourceDir.getAbsolutePath());
103 for (File f : files) {
104 jcmd.addArgs(f.getAbsolutePath());
105 if (compileInLoop) {
106 getLog().info(String.format("%tR compiling %s", now, f.getAbsolutePath().substring(sourceDirPathLength+1)));
107 }
108 }
109 jcmd.run(displayCmd, !compileInLoop);
110 if (lastCompileAtFile.exists()) {
111 lastCompileAtFile.setLastModified(now);
112 } else {
113 FileUtils.fileWrite(lastCompileAtFile.getAbsolutePath(), ".");
114 }
115 return files.size();
116 }
117 }