001/*
002 * JDrupes Builder
003 * Copyright (C) 2025 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.core;
020
021import java.lang.reflect.InvocationTargetException;
022import java.util.List;
023import java.util.Properties;
024import org.apache.commons.cli.CommandLine;
025import org.jdrupes.builder.api.Project;
026import org.jdrupes.builder.api.ResourceRequest;
027import org.jdrupes.builder.api.RootProject;
028
029/// Provides support for creating projects based on [AbstractProject].
030///
031public final class LauncherSupport {
032
033    private static Properties jdbldProps;
034    private static CommandLine commandLine;
035
036    private LauncherSupport() {
037    }
038
039    /// Creates and initializes the root project and the sub projects.
040    /// Adds the sub projects to the root project automatically. This
041    /// method should be used if the launcher detects the sub projects
042    /// e.g. by reflection and the root project does not add its sub
043    /// projects itself.
044    ///
045    /// @param rootProject the root project
046    /// @param subprojects the sub projects
047    /// @param jdbldProps the builder properties
048    /// @param commandLine the command line
049    /// @return the root project
050    ///
051    public static RootProject createProjects(
052            Class<? extends RootProject> rootProject,
053            List<Class<? extends Project>> subprojects,
054            Properties jdbldProps, CommandLine commandLine) {
055        try {
056            LauncherSupport.jdbldProps = jdbldProps;
057            LauncherSupport.commandLine = commandLine;
058            var result = rootProject.getConstructor().newInstance();
059            subprojects.forEach(result::project);
060            return result;
061        } catch (SecurityException | NegativeArraySizeException
062                | IllegalArgumentException | InstantiationException
063                | IllegalAccessException | InvocationTargetException
064                | NoSuchMethodException e) {
065            throw new IllegalArgumentException(e);
066        }
067    }
068
069    /* default */ static Properties jdbldProperties() {
070        return jdbldProps;
071    }
072
073    /* default */
074    static CommandLine commandLine() {
075        return commandLine;
076    }
077
078    /// Lookup the command in the given root project.
079    ///
080    /// @param project the project
081    /// @param name the name
082    /// @return the optional
083    ///
084    public static ResourceRequest<?>[]
085            lookupCommand(RootProject project, String name) {
086        return ((AbstractProject) project).lookupCommand(name);
087    }
088}