KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > tasks > PromoteVersion


1 package org.antmod.tasks;
2
3 import java.io.File JavaDoc;
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 /**
18  * Promotes the version of a module.
19  *
20  * @author Klaas Waslander
21  * @version Nov 10, 2003 5:52:12 PM
22  */

23 public class PromoteVersion extends Task {
24     private String JavaDoc descriptorName;
25     private String JavaDoc 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     /** the scm system for the module to-be-promoted, initialized during 'execute()' */
33     ScmSystem scm;
34
35     /**
36      * Constructor.
37      */

38     public PromoteVersion() {
39     }
40
41     public void setDescriptor(String JavaDoc descriptorName) {
42         this.descriptorName = descriptorName;
43     }
44
45     public void setModuleName(String JavaDoc 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     /**
70      * Does the version promotion if possible.
71      * @throws BuildException
72      */

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         // get release descriptor, module and scm system
82
ReleaseDescriptor descriptor = DescriptorStoreFactory.getConfiguredDescriptorStore().getReleaseDescriptor(this.descriptorName);
83         ReleaseDescriptor.Module module = descriptor.getModuleByName(this.moduleName);
84         this.scm = ScmSystemFactory.getScmSystemByName(module.getRepos());
85         File JavaDoc moduleDir = getProject().getBaseDir();
86
87         // do the work
88
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     /**
111      * Promotes the version of the module in the parent branch.
112      */

113     private void promoteInParentBranch(ScmVersion localVersion) throws BuildException {
114         throw new BuildException("Promoting tags is deliberately not supported!");
115     }
116
117     /**
118      * Promotes the version of the module in given branch (= either branch or trunk).
119      */

120     private void promoteInBranch(ScmVersion localVersion) throws BuildException {
121         File JavaDoc moduleDir = getProject().getBaseDir();
122         ScmVersion latestVersion = scm.getLatestVersion(moduleDir);
123         logWarning("(latest version = " + latestVersion + ")");
124
125         // determine if anything changed
126
if (checkForChanges && latestVersion != null) {
127             boolean hasChanges = false;
128             if (localVersion.isTrunk()) {
129                 // compare trunk to the start of the latest branch (being major.minor.0 = patchlevel zero)
130
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             // let's first create a new branch
149
ScmVersion newBranch = (latestVersion == null) ?
150                     new ScmVersion(localVersion.getModuleName(), null).getNextIncrement() :
151                     latestVersion.getNextIncrement();
152
153             if (this.confirmations) {
154                 String JavaDoc 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             // make sure we create initial tag in new branch afterwards
166
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             // let's make sure the user wants to create a new tag
176
if (this.confirmations) {
177                 String JavaDoc 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         // we need to create a new tag in the branch
185
scm.createTagInBranch(toBeTaggedBranch, newTag);
186     }
187
188
189     /**
190      * Find whether there were any changes in CVS between the two given versions.
191      */

192     private boolean hasScmChanges(ScmVersion version1, ScmVersion version2) {
193         ScmDifference[] diffs = scm.getDifferences(version1, version2);
194
195         // check whether any file changed
196
return diffs.length > 0;
197     }
198
199     /**
200      * Helper method within this class for quickly logging a warning.
201      */

202     private void logWarning(String JavaDoc msg) {
203         log(msg, Project.MSG_WARN);
204     }
205 }
206
Popular Tags