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 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                 //getLog().warn(exc);
170             }
171         }
172     }
173 
174 }