View Javadoc

1   /*
2    * Copyright 2007 scala-tools.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing,
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions
14   * and limitations under the License.
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   * Compile the main and test scala source directory in continuous (infinite loop). !! This is an util goal for commandline usage only (Do not use or call it in a pom) !!!
25   *
26   * @goal cc
27   * @requiresDependencyResolution test
28   */
29  public class ScalaContinuousCompileMojo extends ScalaCompilerSupport {
30  
31      /**
32       * @parameter expression="${project.build.outputDirectory}"
33       */
34      protected File mainOutputDir;
35  
36      /**
37       * @parameter expression="${project.build.sourceDirectory}/../scala"
38       */
39      protected File mainSourceDir;
40  
41      /**
42       * @parameter expression="${project.build.testOutputDirectory}
43       */
44      protected File testOutputDir;
45  
46      /**
47       * @parameter expression="${project.build.testSourceDirectory}/../scala"
48       */
49      protected File testSourceDir;
50  
51      /**
52       * Define if fsc should be used, else scalac is used.
53       * fsc => scala.tools.nsc.CompileClient, scalac => scala.tools.nsc.Main.
54       * @parameter expression="${fsc}" default="true"
55       */
56      protected boolean useFsc = true;
57  
58      /**
59       * Define if cc should run once or in infinite loop. (useful for test or working with editor)
60       * @parameter expression="${once}" default="false"
61       */
62      protected boolean once = false;
63  
64      /**
65       * @parameter expression="${verbose}" default="false"
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                 //getLog().warn(exc);
169             }
170         }
171     }
172 
173 }