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.util.List;
20
21 import org.codehaus.plexus.util.FileUtils;
22
23
24
25
26
27
28
29 public class ScalaContinuousCompileMojo extends ScalaCompilerSupport {
30
31
32
33
34 protected File mainOutputDir;
35
36
37
38
39 protected File mainSourceDir;
40
41
42
43
44 protected File testOutputDir;
45
46
47
48
49 protected File testSourceDir;
50
51
52
53
54
55
56 protected boolean useFsc = true;
57
58
59
60
61
62 protected boolean once = false;
63
64
65
66
67 protected boolean verbose = false;
68
69 @Override
70 protected List<String> getClasspathElements() throws Exception {
71 throw new UnsupportedOperationException("USELESS");
72 }
73
74 @Override
75 protected File getOutputDir() throws Exception {
76 throw new UnsupportedOperationException("USELESS");
77 }
78
79 @Override
80 protected List<String> getSourceDirectories() throws Exception {
81 throw new UnsupportedOperationException("USELESS");
82 }
83 @Override
84 protected JavaCommand getScalaCommand() throws Exception {
85 JavaCommand jcmd = super.getScalaCommand();
86 if (useFsc) {
87 jcmd.addOption("verbose", verbose);
88 }
89 return jcmd;
90 }
91
92 @SuppressWarnings("unchecked")
93 @Override
94 protected final void doExecute() throws Exception {
95
96 mainOutputDir = normalize(mainOutputDir);
97 if (!mainOutputDir.exists()) {
98 mainOutputDir.mkdirs();
99 }
100 mainSourceDir = normalize(mainSourceDir);
101
102 testOutputDir = normalize(testOutputDir);
103 if (!testOutputDir.exists()) {
104 testOutputDir.mkdirs();
105 }
106 testSourceDir = normalize(testSourceDir);
107
108 if (useFsc) {
109 getLog().info("use fsc for compilation");
110 scalaClassName = "scala.tools.nsc.CompileClient";
111 if (!once) {
112 StopServer stopServer = new StopServer();
113 stopServer.run();
114 startNewCompileServer();
115 Runtime.getRuntime().addShutdownHook(stopServer);
116 } else {
117 startNewCompileServer();
118 }
119 }
120
121 getLog().info("wait for files to compile...");
122 do {
123 int nbFile = 0;
124 if (mainSourceDir.exists()) {
125 nbFile = compile(mainSourceDir, mainOutputDir, project.getCompileClasspathElements(), true);
126 }
127 if (testSourceDir.exists()) {
128 nbFile += compile(testSourceDir, testOutputDir, project.getTestClasspathElements(), true);
129 }
130 if (!once) {
131 if (nbFile > 0) {
132 getLog().info("wait for files to compile...");
133 Thread.sleep(5000);
134 } else {
135 Thread.sleep(3000);
136 }
137 }
138 } while (!once);
139 }
140
141 private void startNewCompileServer() throws Exception {
142 File serverTagFile = new File(mainOutputDir + ".server");
143 if (serverTagFile.exists()) {
144 return;
145 }
146 getLog().info("start server...");
147 JavaCommand jcmd = getEmptyScalaCommand("scala.tools.nsc.MainGenericRunner");
148 jcmd.addArgs("scala.tools.nsc.CompileServer");
149 jcmd.addJvmArgs(jvmArgs);
150 jcmd.addArgs(args);
151 jcmd.spawn(displayCmd);
152 FileUtils.fileWrite(serverTagFile.getAbsolutePath(), ".");
153 }
154
155 private class StopServer extends Thread {
156 @Override
157 public void run() {
158 try {
159 getLog().info("stop server(s)...");
160 JavaCommand jcmd = getScalaCommand();
161 jcmd.addArgs("-shutdown");
162 jcmd.run(displayCmd, false);
163 File serverTagFile = new File(mainOutputDir + ".server");
164 if (serverTagFile.exists()) {
165 serverTagFile.delete();
166 }
167 } catch (Exception exc) {
168
169 }
170 }
171 }
172
173 }