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.api;
020
021import java.io.IOException;
022import java.io.PrintStream;
023import java.nio.file.Path;
024import java.util.stream.Stream;
025import org.apache.commons.cli.CommandLine;
026
027/// The context of a build.
028///
029public interface BuildContext extends AutoCloseable {
030
031    /// The key for specifying the builder directory in the properties file.
032    String JDBLD_DIRECTORY = "jdbldDirectory";
033
034    /// The key for specifying the builder version in the properties file.
035    String JDBLD_VERSION = "jdbldVersion";
036
037    /// The key for specifying the build extensions in the properties file.
038    String BUILD_EXTENSIONS = "buildExtensions";
039
040    /// The key for specifying the extensions snapshot repository in the
041    /// properties file.
042    @SuppressWarnings("PMD.LongVariable")
043    String EXTENSIONS_SNAPSHOT_REPOSITORY = "extensionsSnapshotRepository";
044
045    /// The key for specifying the common configuration and cache directory.
046    String JDBLD_COMMON_DIRECTORY = "jdbldCommonDirectory";
047
048    /// Returns the relative path from a project directory to
049    /// the JDrupes Builder directory.
050    ///
051    /// @return the path
052    ///
053    default Path jdbldDirectory() {
054        return Path.of(property(JDBLD_DIRECTORY, "_jdbld"));
055    }
056
057    /// The path to the common cache directory. This may be used by
058    /// providers to cache information that is shared between projects.
059    /// Providers must create a sub-directory of this directory, preferably
060    /// with the same name FQN as the provider.
061    ///
062    /// @return the path
063    ///
064    default Path commonCacheDirectory() {
065        return Path.of(property(JDBLD_COMMON_DIRECTORY,
066            System.getProperty("user.home") + "/.jdbld")).resolve("cache");
067    }
068
069    /// The command line as processed by Apache Commons CLI.
070    ///
071    /// @return the parsed command line
072    ///
073    CommandLine commandLine();
074
075    /// Obtains the stream of resources of the given type from the
076    /// given provider. The result from invoking the provider is
077    /// evaluated asynchronously and cached. Only when the returned
078    /// stream is terminated will the invocation block until the
079    /// result from the provider becomes available.
080    /// 
081    /// To avoid duplicate invocations of a non-project provider,
082    /// any intends are removed from the request before such a
083    /// provider is invoked.
084    ///
085    /// @param <T> the resource type
086    /// @param provider the provider
087    /// @param request the request
088    /// @return the results
089    ///
090    <T extends Resource> Stream<T> resources(ResourceProvider provider,
091            ResourceRequest<T> request);
092
093    /// Returns the value of the given property. Properties are defined by
094    /// (in order of precedence):
095    ///   1. command line options
096    ///   2. the file `.jdbld.properties` in the directory of the
097    ///      root project
098    ///   3. the file `.jdbld/jdbld.properties` in the user's home directory
099    ///
100    /// @param name the name
101    /// @param defaultValue the default value
102    /// @return the string
103    ///
104    String property(String name, String defaultValue);
105
106    /// Returns the status line for the current thread. The status line
107    /// may be used by [ResourceProvider]s to indicate progress during
108    /// the execution of [ResourceProviderSpi#provide(ResourceRequest)].
109    /// A [StatusLine] is automatically allocated by the context when
110    /// [#resources] is invoked.
111    /// 
112    /// When called while not executing [#resources], this method
113    /// returns a dummy status line that discards all updates.
114    ///
115    /// @return the status line
116    ///
117    StatusLine statusLine();
118
119    /// Returns the [PrintStream] for the standard output.
120    ///
121    /// @return the prints the stream
122    ///
123    PrintStream out();
124
125    /// Returns a [PrintStream] for errors. The data is sent to the
126    /// standard output stream as with [#out], but it is marked,
127    /// typically in red, to indicate an error.
128    ///
129    /// @return the prints the stream
130    ///
131    PrintStream error();
132
133    /// Close the context. The re-declaration of this method removes
134    /// the [IOException], which is never thrown.
135    ///
136    @Override
137    void close();
138}