All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.knowledgelinks.cicd.semantic.versioning.formatters.StandardVersionFormatter Maven / Gradle / Ivy

package io.knowledgelinks.cicd.semantic.versioning.formatters;

/*-
 * #%L
 * Semantic Versioning
 * %%
 * Copyright (C) 2022 - 2023 Knowledgelinks
 * %%
 * 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
 * 
 *      http://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.
 * #L%
 */

import io.knowledgelinks.cicd.semantic.versioning.config.Configuration;
import io.knowledgelinks.cicd.semantic.versioning.config.Globals;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.SemanticVersioningException;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.VersionTypeException;
import io.knowledgelinks.cicd.semantic.versioning.versions.VersionInfo;

public class StandardVersionFormatter implements VersionFormatter {
  private Configuration config;

  public StandardVersionFormatter() throws SemanticVersioningException {
    this(null);
  }

  public StandardVersionFormatter(Configuration config) throws SemanticVersioningException {
    this.config = config == null ? Configuration.getInstance() : config;
  }


  @Override
  public String format(VersionInfo versionInfo) throws VersionTypeException {
    switch (versionInfo.getVersionType()) {
      case RELEASE:
        return formatRelease(versionInfo, config);
      case SNAPSHOT:
        switch (versionInfo.getBranchType()) {
          case MASTER:
            return formatMaster(versionInfo, config);
          default:
            return formatSnapshot(versionInfo, config);
        }
      default:
        throw new VersionTypeException(
            String.format("VersionType (%s) is not supported.", versionInfo.getVersionType()));
    }
  }



  private String replaceValues(String formatString, VersionInfo versionInfo, Configuration config) {
    String versionString = formatString;
    versionString = versionString.replace(Globals.MAJOR_MARKER,
        Functions.getStringValue(versionInfo.getMajor(), 1));
    versionString = versionString.replace(Globals.MINOR_MARKER,
        Functions.getStringValue(versionInfo.getMinor(), 1));
    versionString = versionString.replace(Globals.PATCH_MARKER,
        Functions.getStringValue(versionInfo.getPatch(), 1));
    versionString = versionString.replace(Globals.BUILD_NUMBER_MARKER,
        Functions.getStringValue(versionInfo.getBuildNumber(), config.getBuildNumberPadLength()));
    versionString = versionString.replace(Globals.BRANCH_MARKER,
        Functions.cleanBranchName(versionInfo.getBranchName(), config.getFeatureBranchIdentifier(),
            config.getDetailsSeparator(), config.getBranchTrimLength()));
    if (formatString.contains(Globals.RELEASE_MARKER)) {
      versionString =
          versionString.replace(Globals.RELEASE_MARKER, formatRelease(versionInfo, config));
    }

    return versionString;
  }


  private String formatRelease(VersionInfo versionInfo, Configuration config) {
    return replaceValues(config.getReleaseFormat(), versionInfo, config);
  }

  private String formatMaster(VersionInfo versionInfo, Configuration config) {
    return replaceValues(config.getMasterFormat(), versionInfo, config);
  }

  private String formatSnapshot(VersionInfo versionInfo, Configuration config) {
    return replaceValues(config.getSnapshotFormat(), versionInfo, config);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy