1 package org.antmod.tasks; 2 3 import java.io.File ; 4 5 import org.antmod.conf.AntmodProperties; 6 import org.antmod.descriptor.DescriptorStoreFactory; 7 import org.antmod.descriptor.ReleaseDescriptor; 8 import org.antmod.scm.ScmSystem; 9 import org.antmod.scm.ScmSystemFactory; 10 import org.antmod.scm.ScmVersion; 11 import org.apache.tools.ant.BuildException; 12 import org.apache.tools.ant.Task; 13 import org.apache.tools.ant.types.EnumeratedAttribute; 14 15 21 public final class ModuleInfo extends Task { 22 private String addProperty; 23 private String descriptorName; 24 private String moduleName; 25 private String moduleVersion; 26 private Info info; 27 28 31 public ModuleInfo() { 32 } 33 34 public void setAddProperty(String addProperty) { 35 this.addProperty = addProperty; 36 } 37 38 public void setDescriptor(String descriptorName) { 39 this.descriptorName = descriptorName; 40 } 41 42 public void setModuleName(String moduleName) { 43 if (moduleName != null) { 44 moduleName = moduleName.trim(); 45 } 46 this.moduleName = moduleName; 47 } 48 49 public void setModuleVersion(String moduleVersion) { 50 if (moduleVersion != null) { 51 moduleVersion = moduleVersion.trim(); 52 } 53 this.moduleVersion = moduleVersion; 54 } 55 56 public void setInfo(Info info) { 57 this.info = info; 58 } 59 60 public void execute() throws BuildException { 61 if (this.addProperty == null) { 62 throw new BuildException("FAIL: 'addProperty' attribute of moduleinfo task has to be set."); 63 } 64 if (this.moduleName == null) { 65 throw new BuildException("FAIL: 'modulename' attribute of moduleinfo task has to be set."); 66 } 67 if (this.descriptorName == null) { 68 throw new BuildException("FAIL: 'descriptor' attribute of moduleinfo task has to be set."); 69 } 70 if (this.info == null) { 71 throw new BuildException("FAIL: 'info' attribute of moduleinfo task has to be set."); 72 } 73 74 ReleaseDescriptor descriptor = DescriptorStoreFactory.getConfiguredDescriptorStore().getReleaseDescriptor(this.descriptorName); 76 ReleaseDescriptor.Module module = descriptor.getModuleByName(this.moduleName); 77 ScmSystem scm = ScmSystemFactory.getScmSystemByName(module.getRepos()); 78 79 String str = this.info.getValue(); 81 if (str.equals(Info.LOCALVERSION)) { 82 getProject().setProperty(this.addProperty, scm.getLocalVersion(getModuleDir()).toString()); 83 84 } else if (str.equals(Info.REVISIONAVAILABLE)) { 85 if (this.moduleVersion == null) { 86 throw new BuildException("FAIL: 'moduleversion' attribute of moduleinfo task has to be set for 'revisionavailable' info."); 87 } 88 89 ScmVersion ver = new ScmVersion(this.moduleName, this.moduleVersion); 91 boolean hasVersion = ver.isTrunk(); 92 if (!hasVersion) { 93 ScmVersion parentVer = ver.getParentBranch(); 94 95 106 ScmVersion[] revs = scm.getVersionsInBranch(new File (getModuleDir(), "module.xml"), parentVer); 107 for (int i = 0; i < revs.length; i++) { 108 ScmVersion verInBranch = revs[i]; 109 if (verInBranch.equals(ver)) { 110 hasVersion = true; 111 break; 112 } 113 } 114 } 115 116 if (hasVersion) { 117 getProject().setProperty(this.addProperty, "true"); 118 } 119 120 } else if (str.equals(Info.LISTREVISIONS)) { 121 ScmVersion localVersion = scm.getLocalVersion(getModuleDir()); 123 ScmVersion branchToList = localVersion; 125 if (branchToList.isTag()) { 126 branchToList = branchToList.getParentBranch(); 127 } 128 129 StringBuffer result = new StringBuffer (); 131 ScmVersion[] revs = scm.getVersionsInBranch(new File (getModuleDir(), "module.xml"), branchToList); 132 for (int i = 0; i < revs.length; i++) { 134 if (i > 0) { 135 result.append(", "); 136 } 137 result.append(revs[i].toString()); 138 } 139 getProject().setProperty(this.addProperty, result.toString()); 140 141 } else if (str.equals(Info.SCMREPOSITORY)) { 142 if (module.getRepos() != null) { 143 getProject().setProperty(this.addProperty, module.getRepos()); 144 } else { 145 getProject().setProperty(this.addProperty, ""); 146 } 147 148 } else if (str.equals(Info.ISUPTODATE)) { 149 getProject().setProperty(this.addProperty, String.valueOf(scm.isUpToDate(getModuleDir()))); 150 151 } else if (str.equals(Info.LATESTVERSION)) { 152 getProject().setProperty(this.addProperty, scm.getLatestVersion(getModuleDir()).toString()); 153 154 } 155 } 156 157 160 private File getModuleDir() { 161 if (this.moduleName == null) { 162 throw new RuntimeException ("Cannot return valid moduledir because 'moduleName' attribute is not set."); 163 } 164 if (new File (getProject().getBaseDir(), AntmodProperties.getProperty("antmod.release.metadata.file")).exists()) { 165 return new File (getProject().getBaseDir(), this.moduleName); 166 } else { 167 return new File (getProject().getBaseDir().getParentFile(), this.moduleName); 168 } 169 } 170 171 177 public static class Info extends EnumeratedAttribute { 178 179 public static String LOCALVERSION = "localversion"; 180 public static String REVISIONAVAILABLE = "revisionavailable"; 181 public static String LISTREVISIONS = "listrevisions"; 182 public static String SCMREPOSITORY = "scmrepository"; 183 public static String ISUPTODATE = "isuptodate"; 184 public static String LATESTVERSION = "latestversion"; 185 186 private static String [] values = 187 new String []{ 188 LOCALVERSION, 189 REVISIONAVAILABLE, 190 LISTREVISIONS, 191 SCMREPOSITORY, 192 ISUPTODATE, 193 LATESTVERSION 194 }; 195 196 public String [] getValues() { 197 return Info.values; 198 } 199 200 } 201 } 202 | Popular Tags |