Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import io.jenkins.plugins.conventionalcommits.utils.VersionFileProjectType;

/** Factory class to support multiple project types. */
public class ProjectTypeFactory {
Expand All @@ -18,6 +19,7 @@ public class ProjectTypeFactory {
projectTypeMap.put("helm", new HelmProjectType());
projectTypeMap.put("go", new GoProjectType());
projectTypeMap.put("php", new PhpProjectType());
projectTypeMap.put("version", new VersionFileProjectType());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.jenkins.plugins.conventionalcommits.utils;

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* ProjectType implementation that uses a simple VERSION file.
*
* This class detects a plain text file named VERSION (or a custom file
* name passed to the constructor) in the project root directory. The file
* must contain a single semantic version (e.g. "1.0.0"). When present,
* this ProjectType returns the current version from the file and writes
* the next version back to the same file after a bump.
*/
public class VersionFileProjectType extends ProjectType {

private final String versionFileName;

/** Creates a VersionFileProjectType that looks for a file named "VERSION". */
public VersionFileProjectType() {
this("VERSION");
}

/**
* Creates a VersionFileProjectType with a custom version file name.
*
* @param versionFileName name of the file containing the version string
*/
public VersionFileProjectType(String versionFileName) {
this.versionFileName = versionFileName;
}

@Override
public boolean check(File directory) {
return new File(directory, versionFileName).exists();
}

@Override
public Version getCurrentVersion(File directory, ProcessHelper processHelper)
throws IOException, InterruptedException {
// Ignore the processHelper parameter as this project type does not need it
Path path = new File(directory, versionFileName).toPath();
String content = Files.readString(path).trim();
return Version.valueOf(content);
}

@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException {
// Ignore the processHelper parameter; simply write the version string
Path path = new File(directory, versionFileName).toPath();
Files.writeString(path, nextVersion.toString() + System.lineSeparator());
}
}