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.util.Collection;
022import java.util.List;
023import org.jdrupes.builder.api.CoreProperties;
024import org.jdrupes.builder.api.Project;
025import org.jdrupes.builder.api.ProjectVersion;
026import org.jdrupes.builder.api.Resource;
027import org.jdrupes.builder.api.ResourceRequest;
028import static org.jdrupes.builder.api.ResourceType.ProjectVersionType;
029
030/// A provider that reports the version of a project as a
031/// [ProjectVersion] resource. The version is obtained from
032/// the project's [Version property][CoreProperties#Version].
033/// 
034/// Add the generator to a project like this:
035/// ```java
036/// generator(VersionReporter::new);
037///  ```
038/// 
039/// Also add a command alias that requests the [ProjectVersion] resource:
040/// ```java
041/// commandAlias("version")
042///     .resources(of(ProjectVersionType).using(Supply));
043/// ```
044/// 
045/// This makes it possible to query the project version from the command line
046/// with:
047/// ```bash
048/// ./jdbld version
049/// ```
050///
051public class VersionReporter extends AbstractGenerator {
052
053    /// Instantiates a new version reporter.
054    ///
055    /// @param project the project
056    ///
057    public VersionReporter(Project project) {
058        super(project);
059    }
060
061    @Override
062    @SuppressWarnings("unchecked")
063    protected <R extends Resource> Collection<R>
064            doProvide(ResourceRequest<R> requested) {
065        if (!requested.accepts(ProjectVersionType)) {
066            return List.of();
067        }
068        var version = ProjectVersion.of(project(),
069            project().get(CoreProperties.Version));
070        return List.of((R) version);
071    }
072}