<commit_before>/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.android.compiler;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.compiler.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ModuleFileIndex;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.HashMap;
import org.jetbrains.android.facet.AndroidFacet;
import org.jetbrains.android.util.AndroidBundle;
import org.jetbrains.android.util.AndroidUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;

/**
 * @author Eugene.Kudelevsky
 */
public class AndroidIncludingCompiler implements SourceGeneratingCompiler {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.android.compiler.AndroidIncludingCompiler");

  @Override
  public GenerationItem[] getGenerationItems(final CompileContext context) {
    return ApplicationManager.getApplication().runReadAction(new Computable<GenerationItem[]>() {
      @Override
      public GenerationItem[] compute() {
        Map<String, MyItem> result = new HashMap<String, MyItem>();
        for (Module module : context.getProjectCompileScope().getAffectedModules()) {
          AndroidFacet facet = AndroidFacet.getInstance(module);
          if (facet != null && !facet.getConfiguration().LIBRARY_PROJECT) {
            for (AndroidFacet depFacet : AndroidUtils.getAllAndroidDependencies(module, true)) {
              String genSrcRootPath = depFacet.getAptGenSourceRootPath();
              VirtualFile genSrcRoot = genSrcRootPath != null ? LocalFileSystem.getInstance().findFileByPath(genSrcRootPath) : null;
              VirtualFile[] srcRoots = ModuleRootManager.getInstance(depFacet.getModule()).getSourceRoots();

              for (VirtualFile depSourceRoot : srcRoots) {
                if (depSourceRoot != genSrcRoot) {
                  collectCompilableFiles(module, context, depSourceRoot, result);
                }
              }
            }
          }
        }
        Collection<MyItem> items = result.values();
        return items.toArray(new MyItem[items.size()]);
      }
    });
  }

  private static void collectCompilableFiles(final Module module,
                                             final CompileContext context,
                                             @NotNull final VirtualFile sourceRoot,
                                             final Map<String, MyItem> relativePath2GenItem) {

    ModuleFileIndex fileIndex = ModuleRootManager.getInstance(module).getFileIndex();

    fileIndex.iterateContentUnderDirectory(sourceRoot, new ContentIterator() {
      @Override
      public boolean processFile(VirtualFile fileOrDir) {
        if (fileOrDir.isDirectory()) {
          return true;
        }
        if (fileOrDir.getFileType() == StdFileTypes.JAVA) {
          String relativePath = VfsUtil.getRelativePath(fileOrDir, sourceRoot, '/');
          LOG.assertTrue(relativePath != null);
          MyItem existingItem = relativePath2GenItem.get(relativePath);
          if (existingItem != null) {
            String path1 = FileUtil.toSystemDependentName(existingItem.mySourceFile.getPath());
            String path2 = FileUtil.toSystemDependentName(fileOrDir.getPath());
            context.addMessage(CompilerMessageCategory.ERROR, "Duplicate file for " + relativePath +
                                                              "\nOrigin 1: " + path1 +
                                                              "\nOrigin 2: " + path2, null, -1, -1);
            return false;
          }
          relativePath2GenItem.put(relativePath, new MyItem(module, fileOrDir, relativePath));
        }
        return true;
      }
    });
  }

  @Override<commit_msg>clean up: iterate content under dependent module<commit_after>/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.android.compiler;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.compiler.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ModuleFileIndex;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.HashMap;
import org.jetbrains.android.facet.AndroidFacet;
import org.jetbrains.android.util.AndroidBundle;
import org.jetbrains.android.util.AndroidUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;

/**
 * @author Eugene.Kudelevsky
 */
public class AndroidIncludingCompiler implements SourceGeneratingCompiler {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.android.compiler.AndroidIncludingCompiler");

  @Override
  public GenerationItem[] getGenerationItems(final CompileContext context) {
    return ApplicationManager.getApplication().runReadAction(new Computable<GenerationItem[]>() {
      @Override
      public GenerationItem[] compute() {
        Map<String, MyItem> result = new HashMap<String, MyItem>();
        for (Module module : context.getProjectCompileScope().getAffectedModules()) {
          AndroidFacet facet = AndroidFacet.getInstance(module);
          if (facet != null && !facet.getConfiguration().LIBRARY_PROJECT) {
            for (AndroidFacet depFacet : AndroidUtils.getAllAndroidDependencies(module, true)) {
              String genSrcRootPath = depFacet.getAptGenSourceRootPath();
              VirtualFile genSrcRoot = genSrcRootPath != null ? LocalFileSystem.getInstance().findFileByPath(genSrcRootPath) : null;
              VirtualFile[] srcRoots = ModuleRootManager.getInstance(depFacet.getModule()).getSourceRoots();

              for (VirtualFile depSourceRoot : srcRoots) {
                if (depSourceRoot != genSrcRoot) {
                  collectCompilableFiles(module, depFacet.getModule(), context, depSourceRoot, result);
                }
              }
            }
          }
        }
        Collection<MyItem> items = result.values();
        return items.toArray(new MyItem[items.size()]);
      }
    });
  }

  private static void collectCompilableFiles(final Module module,
                                             final Module depModule,
                                             final CompileContext context,
                                             @NotNull final VirtualFile sourceRoot,
                                             final Map<String, MyItem> relativePath2GenItem) {

    ModuleFileIndex fileIndex = ModuleRootManager.getInstance(depModule).getFileIndex();

    fileIndex.iterateContentUnderDirectory(sourceRoot, new ContentIterator() {
      @Override
      public boolean processFile(VirtualFile fileOrDir) {
        if (fileOrDir.isDirectory()) {
          return true;
        }
        if (fileOrDir.getFileType() == StdFileTypes.JAVA) {
          String relativePath = VfsUtil.getRelativePath(fileOrDir, sourceRoot, '/');
          LOG.assertTrue(relativePath != null);
          MyItem existingItem = relativePath2GenItem.get(relativePath);
          if (existingItem != null) {
            String path1 = FileUtil.toSystemDependentName(existingItem.mySourceFile.getPath());
            String path2 = FileUtil.toSystemDependentName(fileOrDir.getPath());
            context.addMessage(CompilerMessageCategory.ERROR, "Duplicate file for " + relativePath +
                                                              "\nOrigin 1: " + path1 +
                                                              "\nOrigin 2: " + path2, null, -1, -1);
            return false;
          }
          relativePath2GenItem.put(relativePath, new MyItem(module, fileOrDir, relativePath));
        }
        return true;
      }
    });
  }

  @Override
{'fraction_non_alphanumeric': 0.09305625950329448, 'fraction_numerical': 0.00466294982260517, 'mean_word_length': 2.8766208251473477, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5328742', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2545, 'n_words': 583}