<commit_before>package xdean.jex.internal.codecov;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import xdean.jex.util.file.FileUtil;

import com.google.common.base.Charsets;

/**
 * TODO add test
 *
 * @author XDean
 *
 */
@Slf4j
@CodecovIgnore
public class CodecovIgnoreHandler {
  private static final String END = "#end";
  private static final String IGNORE = "ignore:";
  private static final String GENERATED = "#generated";

  public static void main(String[] args) {
    Path codecov = Paths.get("codecov.yml");
    if (!Files.exists(codecov)) {
      log.error("Can't find codecov.yml");
      return;
    }
    Path path = Paths.get("src", "main", "java");
    updateCodecovIgnore(codecov, path);
  }

  public static void updateCodecovIgnore(Path codecov, Path sourcePath) {
    FileUtil.deepTraversal(sourcePath)
        .filter(p -> !Files.isDirectory(p))
        .filter(p -> {
          System.out.println(p.toAbsolutePath());
          String name = StreamSupport.stream(p.spliterator(), false)
              .skip(3)
              .map(Path::toString)
              .collect(Collectors.joining("."));
          String clzName = name.substring(0, name.length() - 5);
          Class<?> clz = Class.forName(clzName);
          System.out.println(clz);
          return clz.getAnnotation(CodecovIgnore.class) != null;
        })
        .doOnNext(p -> log.debug("Find file to ignore: " + p))
        .toList()
        .subscribe(ignores -> writeIgnore(codecov, ignores));
  }

  @SneakyThrows
  private static void writeIgnore(Path codecov, List<Path> ignores) {
    List<String> lines = Files.readAllLines(codecov, Charsets.UTF_8);
    List<String> ignoreLines = ignores.stream()
        .map(p -> p.toString().replace('\\', '/'))
        .map(s -> "  - \"" + s + "\"")
        .collect(Collectors.toList());
    ignoreLines.add(0, GENERATED);
    ignoreLines.add(END);
    int ignoreLine = lines.indexOf(IGNORE);
    int generateLine = lines.indexOf(GENERATED);
    int endLine = lines.indexOf(END);
    if (ignoreLine == -1) {<commit_msg>update CodecovIgnoreHandler<commit_after>package xdean.jex.internal.codecov;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import xdean.jex.util.file.FileUtil;

import com.google.common.base.Charsets;

/**
 * TODO add test
 *
 * @author XDean
 *
 */
@Slf4j
@CodecovIgnore
public class CodecovIgnoreHandler {
  private static final String END = "#end";
  private static final String IGNORE = "ignore:";
  private static final String GENERATED = "#generated";

  public static void main(String[] args) {
    updateCodecovIgnore();
  }

  public static void updateCodecovIgnore() {
    Path codecov = Paths.get("codecov.yml");
    if (!Files.exists(codecov)) {
      log.error("Can't find codecov.yml");
      return;
    }
    Path path = Paths.get("src", "main", "java");
    updateCodecovIgnore(codecov, path);
  }

  public static void updateCodecovIgnore(Path codecov, Path sourcePath) {
    FileUtil.deepTraversal(sourcePath)
        .filter(p -> !Files.isDirectory(p))
        .filter(p -> {
          String name = StreamSupport.stream(p.spliterator(), false)
              .skip(3)
              .map(Path::toString)
              .collect(Collectors.joining("."));
          String clzName = name.substring(0, name.length() - 5);
          Class<?> clz = Class.forName(clzName);
          return clz.getAnnotation(CodecovIgnore.class) != null;
        })
        .doOnNext(p -> log.debug("Find file to ignore: " + p))
        .toList()
        .subscribe(ignores -> writeIgnore(codecov, ignores));
  }

  @SneakyThrows
  private static void writeIgnore(Path codecov, List<Path> ignores) {
    List<String> lines = Files.readAllLines(codecov, Charsets.UTF_8);
    List<String> ignoreLines = ignores.stream()
        .map(p -> p.toString().replace('\\', '/'))
        .map(s -> "  - \"" + s + "\"")
        .collect(Collectors.toList());
    ignoreLines.add(0, GENERATED);
    ignoreLines.add(END);
    int ignoreLine = lines.indexOf(IGNORE);
    int generateLine = lines.indexOf(GENERATED);
    int endLine = lines.indexOf(END);
    if (ignoreLine == -1) {
{'fraction_non_alphanumeric': 0.14393268379096547, 'fraction_numerical': 0.003985828166519044, 'mean_word_length': 3.0657065706570656, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1218884', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1446, 'n_words': 298}