|
| 1 | +package com.devonfw.tools.ide.commandlet; |
| 2 | + |
| 3 | +import java.nio.file.Path; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Locale; |
| 7 | + |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import com.devonfw.tools.ide.cli.CliException; |
| 12 | +import com.devonfw.tools.ide.context.IdeContext; |
| 13 | +import com.devonfw.tools.ide.git.GitContext; |
| 14 | +import com.devonfw.tools.ide.property.StringProperty; |
| 15 | +import com.devonfw.tools.ide.tool.LocalToolCommandlet; |
| 16 | +import com.devonfw.tools.ide.tool.gradle.Gradle; |
| 17 | +import com.devonfw.tools.ide.tool.mvn.Mvn; |
| 18 | +import com.devonfw.tools.ide.tool.npm.Npm; |
| 19 | +import com.devonfw.tools.ide.tool.yarn.Yarn; |
| 20 | + |
| 21 | +/** |
| 22 | + * {@link Commandlet} to perform a release of the current project. See <a href="https://github.com/devonfw/IDEasy/issues/1594">#1594</a>. |
| 23 | + */ |
| 24 | +public class ReleaseCommandlet extends Commandlet { |
| 25 | + |
| 26 | + private static final Logger LOG = LoggerFactory.getLogger(ReleaseCommandlet.class); |
| 27 | + |
| 28 | + private static final List<Class<? extends LocalToolCommandlet>> BUILD_TOOLS = List.of(Mvn.class, Gradle.class, Yarn.class, Npm.class); |
| 29 | + |
| 30 | + public final StringProperty arguments; |
| 31 | + |
| 32 | + public ReleaseCommandlet(IdeContext context) { |
| 33 | + |
| 34 | + super(context); |
| 35 | + addKeyword(getName()); |
| 36 | + this.arguments = add(new StringProperty("", false, true, "args")); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String getName() { |
| 41 | + |
| 42 | + return "release"; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void doRun() { |
| 47 | + |
| 48 | + Path projectPath = this.context.getCwd(); |
| 49 | + GitContext git = this.context.getGitContext(); |
| 50 | + |
| 51 | + if (git.hasUntrackedFiles(projectPath)) { |
| 52 | + throw new CliException("Your project has uncommitted or untracked changes. Please commit or revert them before releasing."); |
| 53 | + } |
| 54 | + |
| 55 | + String releaseVersion = this.context.askForInput("Enter the release version to publish (e.g. 1.5.0):"); |
| 56 | + String nextVersion = getNextSnapshotVersion(releaseVersion); |
| 57 | + this.context.askToContinue("About to release version {} and then continue development on {}. Proceed?", releaseVersion, nextVersion); |
| 58 | + |
| 59 | + setProjectVersion(projectPath, releaseVersion); |
| 60 | + git.commit(projectPath, "release/" + releaseVersion); |
| 61 | + git.tag(projectPath, "release/" + releaseVersion); |
| 62 | + |
| 63 | + buildAndDeploy(projectPath); |
| 64 | + |
| 65 | + setProjectVersion(projectPath, nextVersion); |
| 66 | + git.commit(projectPath, "set next development version " + nextVersion); |
| 67 | + git.push(projectPath); |
| 68 | + |
| 69 | + LOG.info("Successfully released version {}.", releaseVersion); |
| 70 | + } |
| 71 | + |
| 72 | + private String getNextSnapshotVersion(String releaseVersion) { |
| 73 | + |
| 74 | + int lastDot = releaseVersion.lastIndexOf('.'); |
| 75 | + String prefix = releaseVersion.substring(0, lastDot + 1); |
| 76 | + int lastNumber = Integer.parseInt(releaseVersion.substring(lastDot + 1)); |
| 77 | + return prefix + (lastNumber + 1) + "-SNAPSHOT"; |
| 78 | + } |
| 79 | + |
| 80 | + private void setProjectVersion(Path projectPath, String version) { |
| 81 | + |
| 82 | + Mvn mvn = getCommandlet(Mvn.class); |
| 83 | + mvn.runTool(List.of("versions:set", "-DnewVersion=" + version, "-DgenerateBackupPoms=false")); |
| 84 | + } |
| 85 | + |
| 86 | + private void buildAndDeploy(Path projectPath) { |
| 87 | + |
| 88 | + for (Class<? extends LocalToolCommandlet> toolClass : BUILD_TOOLS) { |
| 89 | + LocalToolCommandlet tool = getCommandlet(toolClass); |
| 90 | + if (tool.findBuildDescriptor(projectPath) != null) { |
| 91 | + String releaseOptsVariable = tool.getName().toUpperCase(Locale.ROOT) + "_RELEASE_OPTS"; |
| 92 | + List<String> args = getReleaseOptions(releaseOptsVariable); |
| 93 | + args.addAll(this.arguments.asList()); |
| 94 | + tool.runTool(args); |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + throw new CliException("Could not find a build descriptor - no pom.xml, build.gradle, or package.json found!"); |
| 99 | + } |
| 100 | + |
| 101 | + private List<String> getReleaseOptions(String releaseOptsVariable) { |
| 102 | + |
| 103 | + String value = this.context.getVariables().get(releaseOptsVariable); |
| 104 | + if ((value == null) || value.isBlank()) { |
| 105 | + return new ArrayList<>(); |
| 106 | + } |
| 107 | + return new ArrayList<>(List.of(value.split(" "))); |
| 108 | + } |
| 109 | +} |
0 commit comments