private Collection<File> getClasspathFiles(
      ModuleChunk chunk,
      JpsJavaClasspathKind kind,
      final boolean excludeMainModuleOutput,
      ClasspathPart classpathPart,
      final boolean exportedOnly) {
    final Set<File> files = new LinkedHashSet<File>();
    for (JpsModule module : chunk.getModules()) {
      JpsJavaDependenciesEnumerator enumerator =
          JpsJavaExtensionService.dependencies(module).includedIn(kind).recursively();
      if (exportedOnly) {
        enumerator = enumerator.exportedOnly();
      }
      if (classpathPart == ClasspathPart.BEFORE_JDK) {
        enumerator = enumerator.satisfying(new BeforeJavaSdkItemFilter(module));
      } else if (classpathPart == ClasspathPart.AFTER_JDK) {
        enumerator = enumerator.satisfying(new AfterJavaSdkItemFilter(module));
      }
      JpsJavaDependenciesRootsEnumerator rootsEnumerator = enumerator.classes();
      if (excludeMainModuleOutput) {
        rootsEnumerator = rootsEnumerator.withoutSelfModuleOutput();
      }
      files.addAll(rootsEnumerator.getRoots());
    }

    if (classpathPart == ClasspathPart.BEFORE_JDK) {
      for (JpsModule module : chunk.getModules()) {
        JpsSdk<JpsDummyElement> sdk = module.getSdk(JpsJavaSdkType.INSTANCE);
        if (sdk != null) {
          files.addAll(sdk.getParent().getFiles(JpsOrderRootType.COMPILED));
        }
      }
    }
    return files;
  }
 @Override
 protected void addJUnitDefaultLib(
     JpsModule rootModel, String junitName, ExpandMacroToPathMap macroMap) {
   final String ideaHome =
       macroMap.substitute("$APPLICATION_HOME_DIR$", SystemInfo.isFileSystemCaseSensitive);
   final FilenameFilter junitFilter =
       new FilenameFilter() {
         @Override
         public boolean accept(File dir, String name) {
           return name.startsWith("junit");
         }
       };
   File[] junitJars = new File(ideaHome, "lib").listFiles(junitFilter);
   if (junitJars == null || junitJars.length == 0) {
     junitJars = new File(new File(ideaHome, "community"), "lib").listFiles(junitFilter);
   }
   if (junitJars != null && junitJars.length > 0) {
     final boolean isJUnit4 = junitName.contains("4");
     File junitJar = null;
     for (File jar : junitJars) {
       final boolean isCurrentJarV4 = jar.getName().contains("4");
       if (isCurrentJarV4 && isJUnit4 || !isCurrentJarV4 && !isJUnit4) {
         junitJar = jar;
         break;
       }
     }
     if (junitJar != null) {
       final JpsLibrary jpsLibrary =
           rootModel.addModuleLibrary(junitName, JpsJavaLibraryType.INSTANCE);
       jpsLibrary.addRoot(pathToUrl(junitJar.getPath()), JpsOrderRootType.COMPILED);
       final JpsDependenciesList dependenciesList = rootModel.getDependenciesList();
       dependenciesList.addLibraryDependency(jpsLibrary);
     }
   }
 }
 @Override
 protected void addNamedLibrary(
     JpsModule rootModel,
     Collection<String> unknownLibraries,
     boolean exported,
     String name,
     boolean applicationLevel) {
   if (LOG.isDebugEnabled()) {
     LOG.debug(
         "loading "
             + rootModel.getName()
             + ": adding "
             + (applicationLevel ? "application" : "project")
             + " library '"
             + name
             + "'");
   }
   JpsElementFactory factory = JpsElementFactory.getInstance();
   JpsLibraryReference libraryReference;
   final String level = myLibLevels.get(name);
   libraryReference =
       level != null
           ? factory.createLibraryReference(
               name, JpsLibraryTableSerializer.createLibraryTableReference(level))
           : factory.createLibraryReference(name, factory.createGlobalReference());
   final JpsLibraryDependency dependency =
       rootModel.getDependenciesList().addLibraryDependency(libraryReference);
   setLibraryEntryExported(dependency, exported);
 }
Esempio n. 4
0
  @Override
  public void build(
      @NotNull ErlangTarget target,
      @NotNull DirtyFilesHolder<ErlangSourceRootDescriptor, ErlangTarget> holder,
      @NotNull BuildOutputConsumer outputConsumer,
      @NotNull CompileContext context)
      throws ProjectBuildException, IOException {
    if (!holder.hasDirtyFiles() && !holder.hasRemovedFiles()) return;

    JpsModule module = target.getModule();
    JpsProject project = module.getProject();
    ErlangCompilerOptions compilerOptions =
        JpsErlangCompilerOptionsExtension.getOrCreateExtension(project).getOptions();
    if (!compilerOptions.myUseRebarCompiler) return;

    String rebarExecutablePath = getRebarExecutablePath(project);
    if (rebarExecutablePath == null) {
      String errorMessage = "Rebar path is not set";
      context.processMessage(new CompilerMessage(NAME, BuildMessage.Kind.ERROR, errorMessage));
      throw new ProjectBuildException(errorMessage);
    }

    for (String contentRootUrl : module.getContentRootsList().getUrls()) {
      String contentRootPath = new URL(contentRootUrl).getPath();
      File contentRootDir = new File(contentRootPath);
      File rebarConfigFile = new File(contentRootDir, REBAR_CONFIG_FILE_NAME);
      if (!rebarConfigFile.exists()) continue;
      runRebar(
          contentRootPath, rebarExecutablePath, compilerOptions.myAddDebugInfoEnabled, context);
    }
  }
 @NotNull
 private JpsModule findModule(@NotNull String name) {
   for (JpsModule module : myProject.getModules()) {
     if (module.getName().equals(name)) {
       return module;
     }
   }
   throw new IllegalStateException("Couldn't find module " + name);
 }
 private void doTest(boolean rebuildBeforeMake) {
   initProject();
   rebuildAll();
   for (JpsModule module : myProject.getModules()) {
     for (JpsModuleSourceRoot sourceRoot : module.getSourceRoots()) {
       processFile(sourceRoot.getFile(), rebuildBeforeMake);
     }
   }
 }
 public void exportModuleOutputProperties() {
   for (JpsModule module : myModel.getProject().getModules()) {
     for (boolean test : new boolean[] {true, false}) {
       myProject.setProperty(
           "module." + module.getName() + ".output." + (test ? "test" : "main"),
           getModuleOutput(module, test));
     }
   }
 }
 public void testExtractDirectoryFromExcludedJar() throws IOException {
   String jarPath = createFile("dir/lib/j.jar");
   FileUtil.copy(new File(getJUnitJarPath()), new File(jarPath));
   JpsModule module = addModule("m");
   String libDir = PathUtil.getParentPath(jarPath);
   module.getContentRootsList().addUrl(JpsPathUtil.pathToUrl(PathUtil.getParentPath(libDir)));
   module.getExcludeRootsList().addUrl(JpsPathUtil.pathToUrl(libDir));
   final JpsArtifact a = addArtifact("a", root().extractedDir(jarPath, "/junit/textui/"));
   buildAll();
   assertOutput(a, fs().file("ResultPrinter.class").file("TestRunner.class"));
 }
 @Override
 protected void setUpModuleJdk(
     JpsModule rootModel,
     Collection<String> unknownJdks,
     EclipseModuleManager eclipseModuleManager,
     String jdkName) {
   if (LOG.isDebugEnabled()) {
     LOG.debug("loading " + rootModel.getName() + ": set module jdk " + jdkName);
   }
   rootModel.getDependenciesList().addSdkDependency(JpsJavaSdkType.INSTANCE);
 }
 private static Set<String> getModuleDependencies(JpsModule module, boolean includeTests) {
   Set<JpsModule> modules =
       JpsJavaExtensionService.dependencies(module)
           .recursively()
           .includedIn(JpsJavaClasspathKind.compile(includeTests))
           .getModules();
   Set<String> names = new HashSet<String>();
   for (JpsModule depModule : modules) {
     names.add(depModule.getName());
   }
   return names;
 }
 public void readClasspath(
     JpsModule model,
     final String testPattern,
     Element classpathElement,
     JpsMacroExpander expander)
     throws IOException {
   LOG.debug("start loading classpath for " + model.getName());
   final HashSet<String> libs = new HashSet<String>();
   for (Object o : classpathElement.getChildren(EclipseXml.CLASSPATHENTRY_TAG)) {
     try {
       readClasspathEntry(
           model,
           new ArrayList<String>(),
           new ArrayList<String>(),
           new HashSet<String>(),
           testPattern,
           (Element) o,
           0,
           null,
           expander.getExpandMacroMap(),
           libs);
     } catch (ConversionException e) {
       throw new IOException(e);
     }
   }
   boolean foundSdkDependency = false;
   JpsDependenciesList dependenciesList = model.getDependenciesList();
   for (JpsDependencyElement element : dependenciesList.getDependencies()) {
     if (element instanceof JpsSdkDependency) {
       foundSdkDependency = true;
       break;
     }
   }
   if (!foundSdkDependency) {
     dependenciesList.addSdkDependency(JpsJavaSdkType.INSTANCE);
   }
   if (LOG.isDebugEnabled()) {
     String name = model.getName();
     LOG.debug(
         "finished loading classpath for "
             + name
             + " ("
             + dependenciesList.getDependencies().size()
             + " items):");
     for (JpsDependencyElement element : dependenciesList.getDependencies()) {
       LOG.debug(" [" + name + "]:" + element.toString());
     }
   }
 }
  private void addLibClasses(final Element rootElement) throws IOException {
    final JpsCompilerExcludes excludes =
        JpsJavaExtensionService.getInstance()
            .getOrCreateCompilerConfiguration(myModule.getProject())
            .getCompilerExcludes();
    final Ref<Boolean> noClasses = new Ref<Boolean>(true);

    for (JpsTypedModuleSourceRoot srcRoot : myModule.getSourceRoots(JavaSourceRootType.SOURCE)) {
      final File srcFolder = JpsPathUtil.urlToFile(srcRoot.getUrl());
      if (srcFolder.isDirectory()) {
        processFilesRecursively(
            srcFolder,
            new Processor<File>() {
              public boolean process(final File file) {
                if (myProjectDescriptor.getIgnoredFileIndex().isIgnored(file.getName()))
                  return false;
                if (file.isDirectory()) return true;
                if (!FlexCommonUtils.isSourceFile(file.getName())) return true;
                if (excludes.isExcluded(file)) return true;

                String packageRelativePath =
                    FileUtil.getRelativePath(srcFolder, file.getParentFile());
                assert packageRelativePath != null : srcFolder.getPath() + ": " + file.getPath();
                if (packageRelativePath.equals(".")) packageRelativePath = "";

                final String packageName = packageRelativePath.replace(File.separatorChar, '.');
                final String qName =
                    StringUtil.getQualifiedName(
                        packageName, FileUtil.getNameWithoutExtension(file));

                if (isSourceFileWithPublicDeclaration(file)) {
                  addOption(rootElement, CompilerOptionInfo.INCLUDE_CLASSES_INFO, qName);
                  noClasses.set(false);
                }

                return true;
              }
            });
      }
    }

    if (noClasses.get()
        && myBC.getCompilerOptions().getFilesToIncludeInSWC().isEmpty()
        && !Utils.IS_TEST_MODE) {
      throw new IOException(
          FlexCommonBundle.message(
              "nothing.to.compile.in.library", myModule.getName(), myBC.getName()));
    }
  }
  public void testCopyExcludedFolder() {
    // explicitly added excluded files should be copied (e.g. compile output)
    final String file = createFile("xxx/excluded/a.txt");
    createFile("xxx/excluded/CVS");
    final String excluded = PathUtil.getParentPath(file);
    final String dir = PathUtil.getParentPath(excluded);

    final JpsModule module = addModule("myModule");
    module.getContentRootsList().addUrl(JpsPathUtil.pathToUrl(dir));
    module.getExcludeRootsList().addUrl(JpsPathUtil.pathToUrl(excluded));

    final JpsArtifact a = addArtifact(root().dirCopy(excluded));
    buildAll();
    assertOutput(a, fs().file("a.txt"));
  }
  public void testCopyExcludedFile() {
    // excluded files under non-excluded directory should not be copied
    final String file = createFile("xxx/excluded/a.txt");
    createFile("xxx/b.txt");
    createFile("xxx/CVS");
    final String dir = PathUtil.getParentPath(PathUtil.getParentPath(file));

    JpsModule module = addModule("myModule");
    module.getContentRootsList().addUrl(JpsPathUtil.pathToUrl(dir));
    module.getExcludeRootsList().addUrl(JpsPathUtil.pathToUrl(PathUtil.getParentPath(file)));

    final JpsArtifact a = addArtifact(root().dirCopy(dir));
    buildAll();
    assertOutput(a, fs().file("b.txt"));
  }
 private static boolean isInTestSourceRoot(final JpsModule module, final String path) {
   for (JpsModuleSourceRoot testSrcRoot : module.getSourceRoots(JavaSourceRootType.TEST_SOURCE)) {
     final String testSrcRootPath = JpsPathUtil.urlToPath(testSrcRoot.getUrl());
     if (path.startsWith(testSrcRootPath + "/")) return true;
   }
   return false;
 }
  @Nullable
  public File getAnnotationProcessorGeneratedSourcesOutputDir(
      JpsModule module, final boolean forTests, String sourceDirName) {
    if (!StringUtil.isEmpty(sourceDirName)) {
      List<String> roots = module.getContentRootsList().getUrls();
      if (roots.isEmpty()) {
        return null;
      }
      if (roots.size() > 1) {
        roots = new ArrayList<String>(roots); // sort roots to get deterministic result
        Collections.sort(
            roots,
            new Comparator<String>() {
              @Override
              public int compare(String o1, String o2) {
                return o1.compareTo(o2);
              }
            });
      }
      return new File(JpsPathUtil.urlToFile(roots.get(0)), sourceDirName);
    }

    final File outputDir = getModuleOutputDir(module, forTests);
    if (outputDir == null) {
      return null;
    }
    final File parentFile = outputDir.getParentFile();
    if (parentFile == null) {
      return null;
    }
    return new File(parentFile, outputDir.getName() + "_" + DEFAULT_GENERATED_DIR_NAME);
  }
  public void setupOutput(JpsModule rootModel, final String path) {
    final JpsJavaModuleExtension extension = getService().getOrCreateModuleExtension(rootModel);
    String outputUrl = pathToUrl(path);
    extension.setOutputUrl(outputUrl);
    extension.setTestOutputUrl(outputUrl);
    // extension.setInheritOutput(false);

    rootModel.getDependenciesList().addModuleSourceDependency();
  }
 @Override
 public boolean value(JpsDependencyElement dependency) {
   if (myModule.equals(dependency.getContainingModule())
       && dependency instanceof JpsSdkDependency
       && ((JpsSdkDependency) dependency).getSdkType().equals(JpsJavaSdkType.INSTANCE)) {
     mySdkFound = true;
     return false;
   }
   return mySdkFound;
 }
 @Override
 public boolean value(JpsDependencyElement dependency) {
   boolean isJavaSdk =
       dependency instanceof JpsSdkDependency
           && ((JpsSdkDependency) dependency).getSdkType().equals(JpsJavaSdkType.INSTANCE);
   if (myModule.equals(dependency.getContainingModule()) && isJavaSdk) {
     mySdkFound = true;
   }
   return !mySdkFound && !isJavaSdk;
 }
 @Override
 protected void addInvalidModuleEntry(JpsModule rootModel, boolean exported, String moduleName) {
   final JpsElementFactory elementFactory = JpsElementFactory.getInstance();
   final JpsDependenciesList dependenciesList = rootModel.getDependenciesList();
   final JpsModuleDependency dependency =
       dependenciesList.addModuleDependency(elementFactory.createModuleReference(moduleName));
   final JpsJavaDependencyExtension extension =
       getService().getOrCreateDependencyExtension(dependency);
   extension.setExported(exported);
 }
  private void addFilesIncludedInSwc(final Element rootElement) {
    final JpsCompilerExcludes excludes =
        JpsJavaExtensionService.getInstance()
            .getOrCreateCompilerConfiguration(myModule.getProject())
            .getCompilerExcludes();

    final Map<String, String> filePathToPathInSwc = new THashMap<String, String>();

    for (String path : myBC.getCompilerOptions().getFilesToIncludeInSWC()) {
      final File fileOrDir = new File(path);
      if (excludes.isExcluded(fileOrDir)) continue;
      if (myProjectDescriptor.getIgnoredFileIndex().isIgnored(fileOrDir.getName())) continue;

      final String baseRelativePath =
          StringUtil.notNullize(
              FlexCommonUtils.getPathRelativeToSourceRoot(myModule, fileOrDir.getPath()),
              fileOrDir.getName());

      if (fileOrDir.isDirectory()) {
        processFilesRecursively(
            fileOrDir,
            new Processor<File>() {
              public boolean process(final File file) {
                if (myProjectDescriptor.getIgnoredFileIndex().isIgnored(file.getName()))
                  return false;

                if (!file.isDirectory()
                    && !FlexCommonUtils.isSourceFile(file.getName())
                    && !excludes.isExcluded(file)) {
                  final String relativePath = FileUtil.getRelativePath(fileOrDir, file);
                  assert relativePath != null;
                  final String pathInSwc =
                      baseRelativePath.isEmpty()
                          ? relativePath
                          : baseRelativePath + "/" + relativePath;
                  filePathToPathInSwc.put(file.getPath(), pathInSwc);
                }
                return true;
              }
            });
      } else if (fileOrDir.isFile()) {
        filePathToPathInSwc.put(fileOrDir.getPath(), baseRelativePath);
      }
    }

    for (Map.Entry<String, String> entry : filePathToPathInSwc.entrySet()) {
      final String value =
          FileUtil.toSystemIndependentName(entry.getValue())
              + CompilerOptionInfo.LIST_ENTRY_PARTS_SEPARATOR
              + FileUtil.toSystemIndependentName(entry.getKey());
      addOption(rootElement, CompilerOptionInfo.INCLUDE_FILE_INFO, value);
    }
  }
  protected <T extends JpsElement> JpsModule addModule(
      String moduleName,
      String[] srcPaths,
      @Nullable String outputPath,
      @Nullable String testOutputPath,
      JpsSdk<T> sdk) {
    JpsModule module = myProject.addModule(moduleName, JpsJavaModuleType.INSTANCE);
    final JpsSdkType<T> sdkType = sdk.getSdkType();
    final JpsSdkReferencesTable sdkTable = module.getSdkReferencesTable();
    sdkTable.setSdkReference(sdkType, sdk.createReference());

    if (sdkType instanceof JpsJavaSdkTypeWrapper) {
      final JpsSdkReference<T> wrapperRef = sdk.createReference();
      sdkTable.setSdkReference(
          JpsJavaSdkType.INSTANCE,
          JpsJavaExtensionService.getInstance()
              .createWrappedJavaSdkReference((JpsJavaSdkTypeWrapper) sdkType, wrapperRef));
    }
    module.getDependenciesList().addSdkDependency(sdkType);
    if (srcPaths.length > 0 || outputPath != null) {
      for (String srcPath : srcPaths) {
        module.getContentRootsList().addUrl(JpsPathUtil.pathToUrl(srcPath));
        module.addSourceRoot(JpsPathUtil.pathToUrl(srcPath), JavaSourceRootType.SOURCE);
      }
      JpsJavaModuleExtension extension =
          JpsJavaExtensionService.getInstance().getOrCreateModuleExtension(module);
      if (outputPath != null) {
        extension.setOutputUrl(JpsPathUtil.pathToUrl(outputPath));
        if (!StringUtil.isEmpty(testOutputPath)) {
          extension.setTestOutputUrl(JpsPathUtil.pathToUrl(testOutputPath));
        } else {
          extension.setTestOutputUrl(extension.getOutputUrl());
        }
      } else {
        extension.setInheritOutput(true);
      }
    }
    return module;
  }
  @Override
  protected void addModuleLibrary(
      JpsModule rootModel,
      Element element,
      boolean exported,
      String libName,
      String url,
      String srcUrl,
      ExpandMacroToPathMap macroMap) {
    final JpsLibrary jpsLibrary = rootModel.addModuleLibrary(libName, JpsJavaLibraryType.INSTANCE);
    final JpsDependenciesList dependenciesList = rootModel.getDependenciesList();
    final JpsLibraryDependency dependency = dependenciesList.addLibraryDependency(jpsLibrary);
    url = StringUtil.trimStart(url, "file://");
    final String linked = expandLinkedResourcesPath(url, macroMap);
    if (linked != null) {
      url = pathToUrl(linked);
    } else {
      url = expandEclipsePath2Url(rootModel, url);
    }
    LOG.debug("loading " + rootModel.getName() + ": adding module library " + libName + ": " + url);
    jpsLibrary.addRoot(url, JpsOrderRootType.COMPILED);

    setLibraryEntryExported(dependency, exported);
  }
  private void addInputOutputPaths(final Element rootElement) throws IOException {
    if (myBC.getOutputType() == OutputType.Library) {
      addFilesIncludedInSwc(rootElement);

      if (!myFlexmojos) {
        addLibClasses(rootElement);
      }
    } else {
      final InfoFromConfigFile info =
          InfoFromConfigFile.getInfoFromConfigFile(
              myBC.getCompilerOptions().getAdditionalConfigFilePath());

      final String pathToMainClassFile =
          myCSS
              ? myBC.getMainClass()
              : myFlexUnit
                  ? getPathToFlexUnitMainClass(
                      myProjectDescriptor, myBC.getNature(), myBC.getMainClass())
                  : FlexCommonUtils.getPathToMainClassFile(myBC.getMainClass(), myModule);

      if (pathToMainClassFile.isEmpty()
          && info.getMainClass(myModule) == null
          && !Utils.IS_TEST_MODE) {
        throw new IOException(
            FlexCommonBundle.message(
                "bc.incorrect.main.class",
                myBC.getMainClass(),
                myBC.getName(),
                myModule.getName()));
      }

      if (!pathToMainClassFile.isEmpty()) {
        addOption(
            rootElement,
            CompilerOptionInfo.MAIN_CLASS_INFO,
            FileUtil.toSystemIndependentName(pathToMainClassFile));
      }
    }

    addOption(rootElement, CompilerOptionInfo.OUTPUT_PATH_INFO, myBC.getActualOutputFilePath());
  }
  public static ThriftCompilerOptions getSettings(JpsModule module) {
    final ThriftCompilerOptions config = module.getContainer().getChild(ROLE);

    return config == null ? new ThriftCompilerOptions() : config;
  }
  @Override
  public ExitCode build(
      CompileContext context,
      ModuleChunk chunk,
      DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget> dirtyFilesHolder,
      OutputConsumer outputConsumer)
      throws ProjectBuildException, IOException {
    final IceConfig config =
        context.getProjectDescriptor().getProject().getContainer().getChild(IceConfig.ROLE);
    IceConfig iceConfig = config == null ? new IceConfig("") : config;

    File frameworkHome = JpsPathUtil.urlToFile(iceConfig.getFrameworkHomeUrl());
    List<String> includes = new ArrayList<String>();
    for (String include : iceConfig.getIncludeUrls()) {
      includes.add(JpsPathUtil.urlToFile(include).getAbsolutePath());
    }

    final Map<ModuleBuildTarget, List<File>> toCompile =
        collectChangedFiles(context, dirtyFilesHolder);
    if (toCompile.isEmpty()) {
      return ExitCode.NOTHING_DONE;
    }

    for (Map.Entry<ModuleBuildTarget, List<File>> e : toCompile.entrySet()) {
      final ModuleBuildTarget target = e.getKey();
      final JpsModule module = target.getModule();
      SliceCompilerSettings settings = module.getContainer().getChild(SliceCompilerSettings.ROLE);
      final SliceCompilerSettings facetConfig =
          settings == null ? new SliceCompilerSettings() : settings;
      final List<File> sourceFiles = e.getValue();

      List<Target> translators = facetConfig.getComponents();

      if (translators.isEmpty()) {
        context.processMessage(
            new CompilerMessage(
                getPresentableName(),
                BuildMessage.Kind.WARNING,
                "No valid translators found for module "
                    + module.getName()
                    + ". Check facet configuration."));

        continue;
      }

      // Translate files
      for (Target c : translators) {
        final File outputDir = c.getOutputFile();

        if (facetConfig.isCleanOutput()) {
          try {
            FileUtils.cleanDirectory(outputDir);
          } catch (IOException ex) {
            context.processMessage(
                new CompilerMessage(
                    getPresentableName(),
                    BuildMessage.Kind.ERROR,
                    "Failed to empty target directory: "
                        + outputDir.getPath()
                        + " . Error: "
                        + ex.getMessage()));
            return ExitCode.ABORT;
          }
        }

        compileFiles(
            context, frameworkHome, includes, target, sourceFiles, c.getComponent(), outputDir);
      }
    }

    return ExitCode.OK;
  }
  private void compileFiles(
      final CompileContext context,
      File frameworkHome,
      List<String> includes,
      ModuleBuildTarget buildTarget,
      List<File> sourceFiles,
      IceComponent target,
      File outputDir)
      throws StopBuildException {
    final JpsModule module = buildTarget.getModule();

    final String translatorName = target.getTranslatorName();
    if (outputDir == null) {
      context.processMessage(
          new CompilerMessage(
              getPresentableName(),
              BuildMessage.Kind.WARNING,
              "Output folder is not specified for "
                  + translatorName
                  + " in module "
                  + module.getName()
                  + ". Check facet configuration."));

      return;
    }

    List<String> command = new ArrayList<String>();
    command.add(target.getTranslatorPath(frameworkHome).getAbsolutePath());
    command.add("--list-generated");
    command.add("--output-dir");
    final String outputDirPath = outputDir.getAbsolutePath();
    command.add(outputDirPath);
    for (String include : includes) {
      command.add("-I" + include);
    }
    for (JpsModuleSourceRoot contentRoot : module.getSourceRoots()) {
      command.add("-I" + contentRoot.getFile().getAbsolutePath());
    }
    for (File source : sourceFiles) {
      command.add(source.getAbsolutePath());
    }

    try {
      Process process = new ProcessBuilder().command(command).start();

      BaseOSProcessHandler handler =
          new BaseOSProcessHandler(
              process, StringUtil.join(command, " "), CharsetToolkit.UTF8_CHARSET);
      final AtomicBoolean hasErrors = new AtomicBoolean();
      handler.addProcessListener(
          new ProcessAdapter() {
            final StringBuilder errorOutput = new StringBuilder();
            final StringBuilder stdOutput = new StringBuilder();

            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
              if (outputType == ProcessOutputTypes.STDERR) {
                errorOutput.append(event.getText());
              } else if (outputType == ProcessOutputTypes.STDOUT) {
                stdOutput.append(event.getText());
              }
            }

            @Override
            public void processTerminated(ProcessEvent event) {
              Document res;
              final String stdout = stdOutput.toString();
              try {
                res = JDOMUtil.loadDocument(stdout);
              } catch (Exception e) {
                context.processMessage(
                    new CompilerMessage(
                        BUILDER_NAME,
                        BuildMessage.Kind.ERROR,
                        "Can't process compiler output: " + stdout));
                hasErrors.set(true);
                return;
              }

              int exitCode = event.getExitCode();
              if (exitCode != 0) {
                for (Element source : res.getRootElement().getChildren("source")) {
                  final Element output = source.getChild("output");
                  if (output != null) {
                    String message = output.getTextTrim();

                    for (String line : message.split("\n")) {
                      int separatorIndex = line.indexOf(": ");
                      final String path;
                      final long lineNumber;
                      if (separatorIndex <= 0) {
                        path = null;
                        lineNumber = -1L;
                      } else {
                        int lineSep = line.lastIndexOf(':', separatorIndex - 1);
                        if (lineSep == -1) {
                          path = null;
                          lineNumber = -1L;
                        } else {
                          path = line.substring(0, lineSep);
                          long l;
                          try {
                            l = Long.parseLong(line.substring(lineSep + 1, separatorIndex));
                          } catch (NumberFormatException e) {
                            l = -1L;
                          }
                          lineNumber = l;
                        }
                      }

                      context.processMessage(
                          new CompilerMessage(
                              BUILDER_NAME,
                              BuildMessage.Kind.ERROR,
                              line,
                              path,
                              -1L,
                              -1L,
                              -1L,
                              lineNumber,
                              -1L));
                    }
                  }
                }

                final String stdErr = errorOutput.toString();
                if (stdErr.length() > 0) {
                  context.processMessage(
                      new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, stdErr));
                }
                context.processMessage(
                    new CompilerMessage(
                        BUILDER_NAME,
                        BuildMessage.Kind.ERROR,
                        "translator '"
                            + translatorName
                            + "' for '"
                            + module.getName()
                            + "' finished with exit code "
                            + exitCode));
                hasErrors.set(true);
              } else {
                final FileGeneratedEvent msg = new FileGeneratedEvent();

                for (Element source : res.getRootElement().getChildren("source")) {
                  for (Element file : source.getChildren("file")) {
                    final String fileName = file.getAttributeValue("name");

                    if (fileName.startsWith(outputDirPath)) {
                      msg.add(outputDirPath, fileName.substring(outputDirPath.length() + 1));
                    }
                  }
                }

                context.processMessage(msg);
              }
            }
          });
      handler.startNotify();
      handler.waitFor();
      if (hasErrors.get()) {
        throw new StopBuildException();
      }

    } catch (IOException e) {
      context.processMessage(
          new CompilerMessage(
              getPresentableName(),
              BuildMessage.Kind.ERROR,
              "Failed to translate files with " + translatorName + ". Error: " + e.getMessage()));
    }
  }
Esempio n. 28
0
  @Override
  public void build(
      @NotNull ErlangTarget target,
      @NotNull DirtyFilesHolder<ErlangSourceRootDescriptor, ErlangTarget> holder,
      @NotNull BuildOutputConsumer outputConsumer,
      @NotNull final CompileContext context)
      throws ProjectBuildException, IOException {
    LOG.debug(target.getPresentableName());
    final Ref<Boolean> hasDirtyFiles = Ref.create(false);
    holder.processDirtyFiles(
        new FileProcessor<ErlangSourceRootDescriptor, ErlangTarget>() {
          @Override
          public boolean apply(ErlangTarget target, File file, ErlangSourceRootDescriptor root)
              throws IOException {
            hasDirtyFiles.set(true);
            return true;
          }
        });
    if (!hasDirtyFiles.get() && !holder.hasRemovedFiles()) {
      return;
    }

    JpsModule module = target.getModule();
    JpsJavaExtensionService instance = JpsJavaExtensionService.getInstance();

    File outputDirectory = instance.getOutputDirectory(module, target.isTests());
    if (outputDirectory == null) {
      context.processMessage(
          new CompilerMessage(
              NAME, BuildMessage.Kind.ERROR, "No output dir for module " + module.getName()));
      throw new ProjectBuildException();
    }

    if (!outputDirectory.exists()) FileUtil.createDirectory(outputDirectory);

    JpsSdk<JpsDummyElement> sdk = module.getSdk(JpsErlangSdkType.INSTANCE);
    if (sdk == null) {
      context.processMessage(
          new CompilerMessage(
              NAME, BuildMessage.Kind.ERROR, "No SDK for module " + module.getName()));
      throw new ProjectBuildException();
    }

    File executable = JpsErlangSdkType.getByteCodeCompilerExecutable(sdk.getHomePath());

    List<String> commandList = new ArrayList<String>();
    commandList.add(executable.getAbsolutePath());

    CommonProcessors.CollectProcessor<File> processor =
        new CommonProcessors.CollectProcessor<File>() {
          @Override
          protected boolean accept(File file) {
            return !file.isDirectory() && FileUtilRt.extensionEquals(file.getName(), "erl");
          }
        };
    for (JpsModuleSourceRoot root : module.getSourceRoots()) {
      commandList.add("-I");
      commandList.add(root.getFile().getAbsolutePath());
      FileUtil.processFilesRecursively(root.getFile(), processor);
    }

    for (File f : processor.getResults()) {
      commandList.add(f.getAbsolutePath());
    }

    LOG.debug(StringUtil.join(commandList, " "));
    Process process = new ProcessBuilder(commandList).directory(outputDirectory).start();
    BaseOSProcessHandler handler =
        new BaseOSProcessHandler(process, null, Charset.defaultCharset());
    ProcessAdapter adapter =
        new ProcessAdapter() {
          @Override
          public void onTextAvailable(ProcessEvent event, Key outputType) {
            ErlangCompilerError error = ErlangCompilerError.create("", event.getText());
            if (error != null) {

              boolean isError = error.getCategory() == CompilerMessageCategory.ERROR;
              BuildMessage.Kind kind =
                  isError ? BuildMessage.Kind.ERROR : BuildMessage.Kind.WARNING;
              CompilerMessage msg =
                  new CompilerMessage(
                      NAME,
                      kind,
                      error.getErrorMessage(),
                      VirtualFileManager.extractPath(error.getUrl()),
                      -1,
                      -1,
                      -1,
                      error.getLine(),
                      -1);
              context.processMessage(msg);
            }
          }
        };
    handler.addProcessListener(adapter);
    handler.startNotify();
    handler.waitFor();
  }
 @Override
 protected void addSourceFolderToCurrentContentRoot(
     JpsModule rootModel, String srcUrl, boolean testFolder) {
   rootModel.addSourceRoot(
       srcUrl, testFolder ? JavaSourceRootType.TEST_SOURCE : JavaSourceRootType.SOURCE);
 }
  private void addSourcePaths(final Element rootElement) {
    final String localeValue =
        getValueAndSource(CompilerOptionInfo.getOptionInfo("compiler.locale")).first;
    final List<String> locales =
        StringUtil.split(localeValue, CompilerOptionInfo.LIST_ENTRIES_SEPARATOR);
    // when adding source paths we respect locales set both in UI and in Additional compiler options
    locales.addAll(
        FlexCommonUtils.getOptionValues(
            myProjectLevelCompilerOptions.getAdditionalOptions(), "locale", "compiler.locale"));
    locales.addAll(
        FlexCommonUtils.getOptionValues(
            myModuleLevelCompilerOptions.getAdditionalOptions(), "locale", "compiler.locale"));
    locales.addAll(
        FlexCommonUtils.getOptionValues(
            myBC.getCompilerOptions().getAdditionalOptions(), "locale", "compiler.locale"));

    final Set<String> sourcePathsWithLocaleToken =
        new THashSet<String>(); // Set - to avoid duplication of paths like "locale/{locale}"
    final List<String> sourcePathsWithoutLocaleToken = new LinkedList<String>();

    for (JpsModuleSourceRoot srcRoot : myModule.getSourceRoots(JavaSourceRootType.SOURCE)) {
      final String srcRootPath = JpsPathUtil.urlToPath(srcRoot.getUrl());
      if (locales.contains(PathUtilRt.getFileName(srcRootPath))) {
        sourcePathsWithLocaleToken.add(
            PathUtilRt.getParentPath(srcRootPath) + "/" + FlexCommonUtils.LOCALE_TOKEN);
      } else {
        sourcePathsWithoutLocaleToken.add(srcRootPath);
      }
    }

    if (includeTestRoots()) {
      for (JpsModuleSourceRoot srcRoot : myModule.getSourceRoots(JavaSourceRootType.TEST_SOURCE)) {
        final String srcRootPath = JpsPathUtil.urlToPath(srcRoot.getUrl());
        if (locales.contains(PathUtilRt.getFileName(srcRootPath))) {
          sourcePathsWithLocaleToken.add(
              PathUtilRt.getParentPath(srcRootPath) + "/" + FlexCommonUtils.LOCALE_TOKEN);
        } else {
          sourcePathsWithoutLocaleToken.add(srcRootPath);
        }
      }
    }

    final StringBuilder sourcePathBuilder = new StringBuilder();

    if (myCSS) {
      final String cssFolderPath = PathUtilRt.getParentPath(myBC.getMainClass());
      if (!sourcePathsWithoutLocaleToken.contains(cssFolderPath)) {
        sourcePathBuilder.append(cssFolderPath);
      }
    }

    for (final String sourcePath : sourcePathsWithLocaleToken) {
      if (sourcePathBuilder.length() > 0) {
        sourcePathBuilder.append(CompilerOptionInfo.LIST_ENTRIES_SEPARATOR);
      }
      sourcePathBuilder.append(sourcePath);
    }

    for (final String sourcePath : sourcePathsWithoutLocaleToken) {
      if (sourcePathBuilder.length() > 0) {
        sourcePathBuilder.append(CompilerOptionInfo.LIST_ENTRIES_SEPARATOR);
      }
      sourcePathBuilder.append(sourcePath);
    }

    addOption(rootElement, CompilerOptionInfo.SOURCE_PATH_INFO, sourcePathBuilder.toString());
  }