001/*
002 * JDrupes Builder
003 * Copyright (C) 2026 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.startup;
020
021import com.google.common.flogger.FluentLogger;
022import java.nio.file.Path;
023import java.util.Properties;
024import java.util.stream.Stream;
025import org.jdrupes.builder.api.Launcher;
026import org.jdrupes.builder.api.Project;
027import org.jdrupes.builder.api.Resource;
028import org.jdrupes.builder.api.ResourceRequest;
029import static org.jdrupes.builder.api.ResourceType.CleanlinessType;
030import org.jdrupes.builder.core.ScopedValueContext;
031
032/// An implementation of a [Launcher] that launches a build configuration.
033/// It expects that the JDrupes Builder project has already been compiled
034/// and its classes are available on the classpath. It can be used to
035/// launch a build configuration from a unit test where the context is
036/// already is bound. This is only needed when jdbld runs unit tests
037/// for jdbld.
038///
039@SuppressWarnings("PMD.TestClassWithoutTestCases")
040public class TestLauncher extends BuildProjectLauncher {
041
042    private static final FluentLogger logger = FluentLogger.forEnclosingClass();
043
044    /// Initializes a new test launcher.
045    ///
046    /// @param classloader the classloader
047    /// @param buildRoot the build root
048    /// @param args the args
049    ///
050    @SuppressWarnings("PMD.UseVarargs")
051    public TestLauncher(ClassLoader classloader, Path buildRoot,
052            String[] args) {
053        super(classloader, buildRoot, args);
054    }
055
056    @Override
057    protected void doConfigureLogging(Path buildRoot, Properties jdbldProps) {
058        logger.atConfig()
059            .log("Re-configuration of logging is skipped for jdbld tests.");
060    }
061
062    @Override
063    public <T extends Resource> Stream<T> resources(Stream<Project> projects,
064            ResourceRequest<T> request) {
065        var snapshot = ScopedValueContext.snapshot();
066        @SuppressWarnings("PMD.CloseResource")
067        var context = rootProject().context();
068        var result = reportBuildException(() -> projects.parallel()
069            .map(p -> snapshot.where(scopedBuildContext, context)
070                .call(() -> context.resources(p, request)))
071            .flatMap(r -> r).toList().stream());
072        if (request.isFor(CleanlinessType)) {
073            regenerateRootProject();
074        }
075        return result;
076    }
077}