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.io.IOException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.codehaus.plexus.util.FileUtils;
24  
25  /**
26   * Abstract parent of all Scala Mojo
27   */
28  public abstract class ScalaCompilerSupport extends ScalaMojoSupport {
29      /**
30       * Pause duration between to scan to detect changed file to compile.
31       * Used only if compileInLoop or testCompileInLoop is true.
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          // filter uptodate
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 }