Ejemplo n.º 1
0
 private File findResourceDir(CompilerConfiguration config) {
   if (config.isDebug()) {
     System.out.println("Looking for resourcesDir");
   }
   Map compilerArguments = config.getCustomCompilerArguments();
   String tempResourcesDirAsString = (String) compilerArguments.get("-resourceDir");
   File filteredResourceDir = null;
   if (tempResourcesDirAsString != null) {
     filteredResourceDir = new File(tempResourcesDirAsString);
     if (config.isDebug()) {
       System.out.println("Found resourceDir at: " + filteredResourceDir.toString());
     }
   } else {
     if (config.isDebug()) {
       System.out.println("No resourceDir was available.");
     }
   }
   return filteredResourceDir;
 }
Ejemplo n.º 2
0
  // added for debug purposes....
  protected static String[] getSourceFiles(CompilerConfiguration config) {
    Set sources = new HashSet();

    // Set sourceFiles = null;
    // was:
    Set sourceFiles = config.getSourceFiles();

    if (sourceFiles != null && !sourceFiles.isEmpty()) {
      for (Iterator it = sourceFiles.iterator(); it.hasNext(); ) {
        Object o = it.next();

        File sourceFile = null;

        if (o instanceof String) {
          sourceFile = new File((String) o);
          System.out.println((String) o);
        } else if (o instanceof File) {
          sourceFile = (File) o;
        } else {
          break; // ignore it
        }

        sources.add(sourceFile.getAbsolutePath());
      }
    } else {
      for (Iterator it = config.getSourceLocations().iterator(); it.hasNext(); ) {
        String sourceLocation = (String) it.next();

        sources.addAll(getSourceFilesForSourceRoot(config, sourceLocation));
      }
    }

    String[] result;

    if (sources.isEmpty()) {
      result = new String[0];
    } else {
      result = (String[]) sources.toArray(new String[sources.size()]);
    }

    return result;
  }
Ejemplo n.º 3
0
  public List compile(CompilerConfiguration config) throws CompilerException {
    File destinationDir = new File(config.getOutputLocation());

    if (!destinationDir.exists()) {
      destinationDir.mkdirs();
    }

    config.setSourceFiles(null);

    String[] sourceFiles = CSharpCompiler.getSourceFiles(config);

    if (sourceFiles.length == 0) {
      return Collections.EMPTY_LIST;
    }

    System.out.println(
        "Compiling "
            + sourceFiles.length
            + " "
            + "source file"
            + (sourceFiles.length == 1 ? "" : "s")
            + " to "
            + destinationDir.getAbsolutePath());

    String[] args = buildCompilerArguments(config, sourceFiles);

    List messages;

    if (config.isFork()) {
      messages =
          compileOutOfProcess(
              config.getWorkingDirectory(),
              config.getBuildDirectory(),
              findExecutable(config),
              args);
    } else {
      throw new CompilerException("This compiler doesn't support in-process compilation.");
    }

    return messages;
  }
Ejemplo n.º 4
0
  public List compile(CompilerConfiguration config) throws CompilerException {
    File destinationDir = new File(config.getOutputLocation());

    if (!destinationDir.exists()) {
      destinationDir.mkdirs();
    }

    String[] sourceFiles = getSourceFiles(config);

    if (sourceFiles.length == 0) {
      return Collections.EMPTY_LIST;
    }

    System.out.println(
        "Compiling "
            + sourceFiles.length
            + " "
            + "source file"
            + (sourceFiles.length == 1 ? "" : "s")
            + " to "
            + destinationDir.getAbsolutePath());

    String[] args = buildCompilerArguments(config, sourceFiles);

    List messages;

    if (config.isFork()) {
      String executable = config.getExecutable();

      if (StringUtils.isEmpty(executable)) {
        executable = "javac";
      }

      messages = compileOutOfProcess(config.getWorkingDirectory(), executable, args);
    } else {
      messages = compileInProcess(args);
    }

    return messages;
  }
Ejemplo n.º 5
0
  private String getTypeExtension(CompilerConfiguration configuration) throws CompilerException {
    String type = getType(configuration.getCustomCompilerArguments());

    if (type.equals("exe") || type.equals("winexe")) {
      return "exe";
    }

    if (type.equals("library") || type.equals("module")) {
      return "dll";
    }

    throw new CompilerException("Unrecognized type '" + type + "'.");
  }
Ejemplo n.º 6
0
  private String findExecutable(CompilerConfiguration config) {
    String executable = config.getExecutable();

    if (!StringUtils.isEmpty(executable)) {
      return executable;
    }

    if (Os.isFamily("windows")) {
      return "csc";
    }

    return "mcs";
  }
Ejemplo n.º 7
0
  protected static Set getSourceFilesForSourceRoot(
      CompilerConfiguration config, String sourceLocation) {
    DirectoryScanner scanner = new DirectoryScanner();

    scanner.setBasedir(sourceLocation);

    Set includes = config.getIncludes();

    if (includes != null && !includes.isEmpty()) {
      String[] inclStrs = (String[]) includes.toArray(new String[includes.size()]);
      scanner.setIncludes(inclStrs);
    } else {
      scanner.setIncludes(new String[] {"**/*.cs"});
    }

    Set excludes = config.getExcludes();

    if (excludes != null && !excludes.isEmpty()) {
      String[] exclStrs = (String[]) excludes.toArray(new String[excludes.size()]);
      scanner.setIncludes(exclStrs);
    }

    scanner.scan();

    String[] sourceDirectorySources = scanner.getIncludedFiles();

    Set sources = new HashSet();

    for (int j = 0; j < sourceDirectorySources.length; j++) {
      File f = new File(sourceLocation, sourceDirectorySources[j]);

      sources.add(f.getPath());
    }

    return sources;
  }
Ejemplo n.º 8
0
  private void addResourceArgs(CompilerConfiguration config, List args) {
    File filteredResourceDir = this.findResourceDir(config);
    if ((filteredResourceDir != null) && filteredResourceDir.exists()) {
      DirectoryScanner scanner = new DirectoryScanner();
      scanner.setBasedir(filteredResourceDir);
      scanner.setIncludes(DEFAULT_INCLUDES);
      scanner.addDefaultExcludes();
      scanner.scan();

      List includedFiles = Arrays.asList(scanner.getIncludedFiles());
      for (Iterator iter = includedFiles.iterator(); iter.hasNext(); ) {
        String name = (String) iter.next();
        File filteredResource = new File(filteredResourceDir, name);
        String assemblyResourceName = this.convertNameToAssemblyResourceName(name);
        String argLine = "/resource:\"" + filteredResource + "\",\"" + assemblyResourceName + "\"";
        if (config.isDebug()) {
          System.out.println("adding resource arg line:" + argLine);
        }
        args.add(argLine);
      }
    }
  }
Ejemplo n.º 9
0
 public String getOutputFile(CompilerConfiguration configuration) throws CompilerException {
   return configuration.getOutputFileName() + "." + getTypeExtension(configuration);
 }
Ejemplo n.º 10
0
  private String[] buildCompilerArguments(CompilerConfiguration config, String[] sourceFiles)
      throws CompilerException {
    List args = new ArrayList();

    if (config.isDebug()) {
      args.add("/debug+");
    } else {
      args.add("/debug-");
    }

    // config.isShowWarnings()
    // config.getSourceVersion()
    // config.getTargetVersion()
    // config.getSourceEncoding()

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    for (Iterator it = config.getClasspathEntries().iterator(); it.hasNext(); ) {
      String element = (String) it.next();

      File f = new File(element);

      if (!f.isFile()) {
        continue;
      }

      args.add("/reference:\"" + element + "\"");
    }

    // ----------------------------------------------------------------------
    // Main class
    // ----------------------------------------------------------------------

    Map compilerArguments = config.getCustomCompilerArguments();

    String mainClass = (String) compilerArguments.get("-main");

    if (!StringUtils.isEmpty(mainClass)) {
      args.add("/main:" + mainClass);
    }

    // ----------------------------------------------------------------------
    // Xml Doc output
    // ----------------------------------------------------------------------

    String doc = (String) compilerArguments.get("-doc");

    if (!StringUtils.isEmpty(doc)) {
      args.add(
          "/doc:"
              + new File(config.getOutputLocation(), config.getOutputFileName() + ".xml")
                  .getAbsolutePath());
    }

    // ----------------------------------------------------------------------
    // Xml Doc output
    // ----------------------------------------------------------------------

    String nowarn = (String) compilerArguments.get("-nowarn");

    if (!StringUtils.isEmpty(nowarn)) {
      args.add("/nowarn:" + nowarn);
    }

    // ----------------------------------------------------------------------
    // Out - Override output name, this is required for generating the unit test dll
    // ----------------------------------------------------------------------

    String out = (String) compilerArguments.get("-out");

    if (!StringUtils.isEmpty(out)) {
      args.add("/out:" + new File(config.getOutputLocation(), out).getAbsolutePath());
    } else {
      args.add(
          "/out:" + new File(config.getOutputLocation(), getOutputFile(config)).getAbsolutePath());
    }

    // ----------------------------------------------------------------------
    // Resource File - compile in a resource file into the assembly being created
    // ----------------------------------------------------------------------
    String resourcefile = (String) compilerArguments.get("-resourcefile");

    if (!StringUtils.isEmpty(resourcefile)) {
      String resourceTarget = (String) compilerArguments.get("-resourcetarget");
      args.add("/res:" + new File(resourcefile).getAbsolutePath() + "," + resourceTarget);
    }

    // ----------------------------------------------------------------------
    // Target - type of assembly to produce, lib,exe,winexe etc...
    // ----------------------------------------------------------------------

    String target = (String) compilerArguments.get("-target");

    if (StringUtils.isEmpty(target)) {
      args.add("/target:library");
    } else {
      args.add("/target:" + target);
    }

    // ----------------------------------------------------------------------
    // remove MS logo from output (not applicable for mono)
    // ----------------------------------------------------------------------
    String nologo = (String) compilerArguments.get("-nologo");

    if (!StringUtils.isEmpty(nologo)) {
      args.add("/nologo");
    }

    // ----------------------------------------------------------------------
    // add any resource files
    // ----------------------------------------------------------------------
    this.addResourceArgs(config, args);

    // ----------------------------------------------------------------------
    // add source files
    // ----------------------------------------------------------------------
    for (int i = 0; i < sourceFiles.length; i++) {
      String sourceFile = sourceFiles[i];

      args.add(sourceFile);
    }

    return (String[]) args.toArray(new String[args.size()]);
  }
Ejemplo n.º 11
0
 private static boolean suppressEncoding(CompilerConfiguration config) {
   return "1.3".equals(config.getCompilerVersion());
 }
Ejemplo n.º 12
0
  public static String[] buildCompilerArguments(
      CompilerConfiguration config, String[] sourceFiles) {
    List args = new ArrayList();

    // ----------------------------------------------------------------------
    // Set output
    // ----------------------------------------------------------------------

    File destinationDir = new File(config.getOutputLocation());

    args.add("-d");

    args.add(destinationDir.getAbsolutePath());

    // ----------------------------------------------------------------------
    // Set the class and source paths
    // ----------------------------------------------------------------------

    List classpathEntries = config.getClasspathEntries();
    if (classpathEntries != null && !classpathEntries.isEmpty()) {
      args.add("-classpath");

      args.add(getPathString(classpathEntries));
    }

    List sourceLocations = config.getSourceLocations();
    if (sourceLocations != null && !sourceLocations.isEmpty()) {
      args.add("-sourcepath");

      args.add(getPathString(sourceLocations));
    }

    for (int i = 0; i < sourceFiles.length; i++) {
      args.add(sourceFiles[i]);
    }

    if (config.isOptimize()) {
      args.add("-O");
    }

    if (config.isDebug()) {
      args.add("-g");
    }

    if (config.isVerbose()) {
      args.add("-verbose");
    }

    if (config.isShowDeprecation()) {
      args.add("-deprecation");

      // This is required to actually display the deprecation messages
      config.setShowWarnings(true);
    }

    if (!config.isShowWarnings()) {
      args.add("-nowarn");
    }

    // TODO: this could be much improved
    if (StringUtils.isEmpty(config.getTargetVersion())) {
      // Required, or it defaults to the target of your JDK (eg 1.5)
      args.add("-target");
      args.add("1.1");
    } else {
      args.add("-target");
      args.add(config.getTargetVersion());
    }

    if (!suppressSource(config) && StringUtils.isEmpty(config.getSourceVersion())) {
      // If omitted, later JDKs complain about a 1.1 target
      args.add("-source");
      args.add("1.3");
    } else if (!suppressSource(config)) {
      args.add("-source");
      args.add(config.getSourceVersion());
    }

    if (!suppressEncoding(config) && !StringUtils.isEmpty(config.getSourceEncoding())) {
      args.add("-encoding");
      args.add(config.getSourceEncoding());
    }

    for (Iterator it = config.getCustomCompilerArguments().entrySet().iterator(); it.hasNext(); ) {
      Map.Entry entry = (Map.Entry) it.next();

      String key = (String) entry.getKey();

      if (StringUtils.isEmpty(key)) {
        continue;
      }

      args.add(key);

      String value = (String) entry.getValue();

      if (StringUtils.isEmpty(value)) {
        continue;
      }

      args.add(value);
    }

    return (String[]) args.toArray(new String[args.size()]);
  }