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 File getSourceDir() throws Exception {
81 throw new UnsupportedOperationException("USELESS");
82 }
83
84 @Override
85 protected JavaCommand getScalaCommand() throws Exception {
86 JavaCommand jcmd = super.getScalaCommand();
87 if (useFsc) {
88 jcmd.addOption("verbose", verbose);
89 }
90 return jcmd;
91 }
92
93 @SuppressWarnings("unchecked")
94 @Override
95 protected final void doExecute() throws Exception {
96
97 mainOutputDir = normalize(mainOutputDir);
98 if (!mainOutputDir.exists()) {
99 mainOutputDir.mkdirs();
100 }
101 mainSourceDir = normalize(mainSourceDir);
102
103 testOutputDir = normalize(testOutputDir);
104 if (!testOutputDir.exists()) {
105 testOutputDir.mkdirs();
106 }
107 testSourceDir = normalize(testSourceDir);
108
109 if (useFsc) {
110 getLog().info("use fsc for compilation");
111 scalaClassName = "scala.tools.nsc.CompileClient";
112 if (!once) {
113 StopServer stopServer = new StopServer();
114 stopServer.run();
115 startNewCompileServer();
116 Runtime.getRuntime().addShutdownHook(stopServer);
117 } else {
118 startNewCompileServer();
119 }
120 }
121
122 getLog().info("wait for files to compile...");
123 do {
124 int nbFile = 0;
125 if (mainSourceDir.exists()) {
126 nbFile = compile(mainSourceDir, mainOutputDir, project.getCompileClasspathElements(), true);
127 }
128 if (testSourceDir.exists()) {
129 nbFile += compile(testSourceDir, testOutputDir, project.getTestClasspathElements(), true);
130 }
131 if (!once) {
132 if (nbFile > 0) {
133 getLog().info("wait for files to compile...");
134 Thread.sleep(5000);
135 } else {
136 Thread.sleep(3000);
137 }
138 }
139 } while (!once);
140 }
141
142 private void startNewCompileServer() throws Exception {
143 File serverTagFile = new File(mainOutputDir + ".server");
144 if (serverTagFile.exists()) {
145 return;
146 }
147 getLog().info("start server...");
148 JavaCommand jcmd = getEmptyScalaCommand("scala.tools.nsc.MainGenericRunner");
149 jcmd.addArgs("scala.tools.nsc.CompileServer");
150 jcmd.addJvmArgs(jvmArgs);
151 jcmd.addArgs(args);
152 jcmd.spawn(displayCmd);
153 FileUtils.fileWrite(serverTagFile.getAbsolutePath(), ".");
154 }
155
156 private class StopServer extends Thread {
157 @Override
158 public void run() {
159 try {
160 getLog().info("stop server(s)...");
161 JavaCommand jcmd = getScalaCommand();
162 jcmd.addArgs("-shutdown");
163 jcmd.run(displayCmd, false);
164 File serverTagFile = new File(mainOutputDir + ".server");
165 if (serverTagFile.exists()) {
166 serverTagFile.delete();
167 }
168 } catch (Exception exc) {
169
170 }
171 }
172 }
173
174 }