com.telenav.cactus.maven.commit.CommitMessage Maven / Gradle / Ivy
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// © 2011-2022 Telenav, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package com.telenav.cactus.maven.commit;
import com.telenav.cactus.maven.commit.CommitMessage.Section;
import com.telenav.cactus.metadata.BuildMetadata;
import com.telenav.cactus.util.SectionedMessage;
import java.time.Instant;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import static com.telenav.cactus.maven.common.CactusCommonPropertyNames.PLUGIN_FAMILY_NAME;
import static java.lang.Math.min;
/**
* Consistently formatted commit messages with provenance information.
*
* @author Tim Boudreau
*/
public class CommitMessage implements SectionedMessage>
{
private final String provenanceIdentifier;
private final String generatorClass;
private final StringBuilder summary = new StringBuilder();
private final StringBuilder detail = new StringBuilder();
private final List> sections = new ArrayList<>();
public CommitMessage(Class> generator, String provenance,
String initialSummary)
{
this.provenanceIdentifier = provenance;
this.summary.append(initialSummary);
this.generatorClass = generator.getSimpleName();
}
public CommitMessage(Class> generator, String summary)
{
this(generator, PLUGIN_FAMILY_NAME, summary);
}
public CommitMessage appendToSummary(CharSequence what)
{
summary.append(what);
return this;
}
public CommitMessage append(CharSequence what)
{
detail.append(what).append('\n');
return this;
}
@Override
public CommitMessage paragraph(CharSequence text)
{
return append(text);
}
@Override
public Section section(String title)
{
return new Section<>(title, 0, section ->
{
sections.add(section);
return this;
});
}
/**
* Provides a generic function that can be passed to libraries that need to
* add sections without requiring they depend on this api.
*
* @param sections A set that created sections will be placed into, so they
* can be closed
* @return a function
*/
public Function super String, Consumer