<commit_before>package com.github.sormuras.bach.internal;

import com.github.sormuras.bach.Bach;
import com.github.sormuras.bach.Command;
import java.lang.ModuleLayer.Controller;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.spi.ToolProvider;

/** A builder for building module layers. */
public class ModuleLayerBuilder {

  public static ModuleLayer build(Path... entries) {
    return new ModuleLayerBuilder().before(ModuleFinder.of(entries)).build();
  }

  public static ModuleLayer build(String module) {
    return build(Path.of(".bach"), module);
  }

  public static ModuleLayer build(Path moduleSourceDirectory, String module) {
    var buildModulePath = moduleSourceDirectory.resolve(module);
    var buildModuleInfo = buildModulePath.resolve("module-info.java");
    if (Files.notExists(buildModuleInfo)) return ModuleLayer.empty();

    var destination = Bach.WORKSPACE.resolve(moduleSourceDirectory);
    var args =
        Command.of("javac")
            .add("--module", module)
            .add("--module-source-path", moduleSourceDirectory)
            .add("--module-path", Bach.CACHE)
            .add("-encoding", "UTF-8")
            .add("-d", destination)
            .toStrings()
            .toArray(String[]::new);
    var result = ToolProvider.findFirst("javac").orElseThrow().run(System.out, System.err, args);
    if (result != 0) throw new RuntimeException("Non-zero exit code: " + result);

    var boot = ModuleLayer.boot();
    return new ModuleLayerBuilder()
        .bindServices(true)
        .oneLoader(true)
        .parentConfigurations(List.of(boot.configuration()))
        .before(ModuleFinder.of())
        .after(ModuleFinder.of(destination, Bach.CACHE))
        .roots(Set.of(module))
        .parentLayers(List.of(boot))
        .parentLoader(ClassLoader.getPlatformClassLoader())
        .controllerConsumer(controller -> {})<commit_msg>Restrict build module layer to one single module<commit_after>package com.github.sormuras.bach.internal;

import com.github.sormuras.bach.Bach;
import com.github.sormuras.bach.Command;
import java.lang.ModuleLayer.Controller;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.spi.ToolProvider;

/** A builder for building module layers. */
public class ModuleLayerBuilder {

  public static ModuleLayer build(Path... entries) {
    return new ModuleLayerBuilder().before(ModuleFinder.of(entries)).build();
  }

  public static ModuleLayer build(String module) {
    return build(Path.of(".bach"), module);
  }

  public static ModuleLayer build(Path moduleSourceDirectory, String module) {
    var buildModulePath = moduleSourceDirectory.resolve(module);
    var buildModuleInfo = buildModulePath.resolve("module-info.java");
    if (Files.notExists(buildModuleInfo)) return ModuleLayer.empty();

    var destination = Bach.WORKSPACE.resolve(moduleSourceDirectory);
    var args =
        Command.of("javac")
            .add("--module", module)
            .add("--module-source-path", moduleSourceDirectory)
            .add("--module-path", Bach.CACHE)
            .add("-encoding", "UTF-8")
            .add("-d", destination)
            .toStrings()
            .toArray(String[]::new);
    var result = ToolProvider.findFirst("javac").orElseThrow().run(System.out, System.err, args);
    if (result != 0) throw new RuntimeException("Non-zero exit code: " + result);

    var boot = ModuleLayer.boot();
    return new ModuleLayerBuilder()
        .bindServices(true)
        .oneLoader(true)
        .parentConfigurations(List.of(boot.configuration()))
        .before(ModuleFinder.of())
        .after(ModuleFinder.of(destination.resolve(module), Bach.CACHE))
        .roots(Set.of(module))
        .parentLayers(List.of(boot))
        .parentLoader(ClassLoader.getPlatformClassLoader())
        .controllerConsumer(controller -> {})
{'fraction_non_alphanumeric': 0.13256416293854487, 'fraction_numerical': 0.0009418412997409936, 'mean_word_length': 3.5433155080213905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4560270', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1192, 'n_words': 239}