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