1 package org.antmod.tasks; 2 3 import java.io.File ; 4 5 import org.antmod.descriptor.DescriptorStoreFactory; 6 import org.antmod.descriptor.ReleaseDescriptor; 7 import org.antmod.scm.ScmDifference; 8 import org.antmod.scm.ScmSystem; 9 import org.antmod.scm.ScmSystemFactory; 10 import org.antmod.scm.ScmVersion; 11 import org.antmod.util.AntUtil; 12 import org.apache.commons.lang.StringUtils; 13 import org.apache.tools.ant.BuildException; 14 import org.apache.tools.ant.Project; 15 import org.apache.tools.ant.Task; 16 17 23 public class PromoteVersion extends Task { 24 private String descriptorName; 25 private String moduleName; 26 private boolean checkForChanges = true; 27 private boolean confirmations = true; 28 private boolean promoteTag; 29 private boolean promoteInBranch; 30 private boolean promoteInTrunk; 31 32 33 ScmSystem scm; 34 35 38 public PromoteVersion() { 39 } 40 41 public void setDescriptor(String descriptorName) { 42 this.descriptorName = descriptorName; 43 } 44 45 public void setModuleName(String moduleName) { 46 this.moduleName = moduleName; 47 } 48 49 public void setCheckForChanges(boolean checkForChanges) { 50 this.checkForChanges = checkForChanges; 51 } 52 53 public void setConfirmations(boolean confirmations) { 54 this.confirmations = confirmations; 55 } 56 57 public void setPromoteTag(boolean promoteTag) { 58 this.promoteTag = promoteTag; 59 } 60 61 public void setPromoteInBranch(boolean promoteInBranch) { 62 this.promoteInBranch = promoteInBranch; 63 } 64 65 public void setPromoteInTrunk(boolean promoteInTrunk) { 66 this.promoteInTrunk = promoteInTrunk; 67 } 68 69 73 public void execute() throws BuildException { 74 if (this.moduleName == null) { 75 throw new BuildException("FAIL: 'modulename' attribute of promoteversion task has to be set."); 76 } 77 if (this.descriptorName == null) { 78 throw new BuildException("FAIL: 'descriptor' attribute of promoteversion task has to be set."); 79 } 80 81 ReleaseDescriptor descriptor = DescriptorStoreFactory.getConfiguredDescriptorStore().getReleaseDescriptor(this.descriptorName); 83 ReleaseDescriptor.Module module = descriptor.getModuleByName(this.moduleName); 84 this.scm = ScmSystemFactory.getScmSystemByName(module.getRepos()); 85 File moduleDir = getProject().getBaseDir(); 86 87 ScmVersion localVersion = scm.getLocalVersion(moduleDir); 89 if (localVersion.isTag()) { 90 if (promoteTag) { 91 promoteInParentBranch(localVersion); 92 } else { 93 logWarning("No new version: local version \"" + localVersion + "\" is a tag, and 'promoteTag' attribute is false."); 94 } 95 } else if (localVersion.isBranch()) { 96 if (promoteInBranch) { 97 promoteInBranch(localVersion); 98 } else { 99 logWarning("No new version: local version \"" + localVersion + "\" is a branch, and 'promoteInBranch' attribute is false."); 100 } 101 } else if (localVersion.isTrunk()) { 102 if (promoteInTrunk) { 103 promoteInBranch(localVersion); 104 } else { 105 logWarning("No new version: local version \"" + localVersion + "\" is trunk, and 'promoteInTrunk' attribute is false."); 106 } 107 } 108 } 109 110 113 private void promoteInParentBranch(ScmVersion localVersion) throws BuildException { 114 throw new BuildException("Promoting tags is deliberately not supported!"); 115 } 116 117 120 private void promoteInBranch(ScmVersion localVersion) throws BuildException { 121 File moduleDir = getProject().getBaseDir(); 122 ScmVersion latestVersion = scm.getLatestVersion(moduleDir); 123 logWarning("(latest version = " + latestVersion + ")"); 124 125 if (checkForChanges && latestVersion != null) { 127 boolean hasChanges = false; 128 if (localVersion.isTrunk()) { 129 ScmVersion latestVersionTag = new ScmVersion(latestVersion.getModuleName(), latestVersion.getMajor(), latestVersion.getMinor(), 0); 131 logWarning("Comparing trunk against version " + latestVersionTag + "..."); 132 hasChanges = hasScmChanges(localVersion, latestVersionTag); 133 } else { 134 hasChanges = hasScmChanges(localVersion, latestVersion); 135 } 136 137 if (!hasChanges) { 138 logWarning("No new version: no changes between local version \"" + localVersion + "\" and latest version \"" + latestVersion + "\""); 139 logWarning("Use latest version \"" + latestVersion + "\" instead."); 140 return; 141 } 142 } 143 144 ScmVersion toBeTaggedBranch = localVersion; 145 ScmVersion newTag; 146 147 if (localVersion.isTrunk()) { 148 ScmVersion newBranch = (latestVersion == null) ? 150 new ScmVersion(localVersion.getModuleName(), null).getNextIncrement() : 151 latestVersion.getNextIncrement(); 152 153 if (this.confirmations) { 154 String isOkay = AntUtil.ask(getProject(), "Is it okay to create branch " + newBranch.getModuleName() + " " + newBranch.toString() + " with initial '-0' tag?", "yes,no"); 155 if ("no".equalsIgnoreCase(isOkay)) { 156 return; 157 } 158 } 159 160 scm.createBranchInTrunk(newBranch); 161 if (!StringUtils.isBlank(scm.getStandardOutput())) { 162 throw new BuildException(scm.getStandardOutput()); 163 } 164 165 toBeTaggedBranch = newBranch; 167 newTag = new ScmVersion(toBeTaggedBranch.getModuleName(), toBeTaggedBranch.getMajor(), toBeTaggedBranch.getMinor(), 0); 168 } else { 169 if (latestVersion == null) { 170 newTag = new ScmVersion(localVersion.getModuleName(), localVersion.getMajor(), localVersion.getMinor(), 0); 171 } else { 172 newTag = latestVersion.getNextIncrement(); 173 } 174 175 if (this.confirmations) { 177 String isOkay = AntUtil.ask(getProject(), "Is it okay to create tag " + newTag.toFullString() + " in branch " + toBeTaggedBranch.toFullString() + "?", "yes,no"); 178 if ("no".equalsIgnoreCase(isOkay)) { 179 return; 180 } 181 } 182 } 183 184 scm.createTagInBranch(toBeTaggedBranch, newTag); 186 } 187 188 189 192 private boolean hasScmChanges(ScmVersion version1, ScmVersion version2) { 193 ScmDifference[] diffs = scm.getDifferences(version1, version2); 194 195 return diffs.length > 0; 197 } 198 199 202 private void logWarning(String msg) { 203 log(msg, Project.MSG_WARN); 204 } 205 } 206 | Popular Tags |