1 package org.antmod.scm; 2 3 import java.io.File ; 4 import java.text.ParseException ; 5 6 import org.apache.tools.ant.BuildException; 7 import org.apache.tools.ant.Project; 8 import org.apache.tools.ant.Task; 9 import org.apache.tools.ant.types.EnumeratedAttribute; 10 11 16 public class ScmTask extends Task { 17 private String dest; 18 19 private String url; 21 private String urlProperty; 22 private String repos; 23 24 private Command command; 25 private String revision; 26 private String module; 27 private boolean quiet; 28 private String message; 29 private String property; 30 31 32 public void execute() throws BuildException { 33 if (url == null && urlProperty == null && repos == null) { 34 throw new BuildException("Either 'url', 'urlProperty' or 'repos' attribute has to be set for the ScmTask."); 35 } 36 37 try { 38 ScmSystem scm; 40 if (this.urlProperty != null) { 41 this.url = getProject().getProperty(this.urlProperty); 42 try { 43 scm = ScmSystemFactory.getScmSystemByUrl(this.url); 44 } catch (IllegalArgumentException iae) { 45 throw new BuildException("Antmod property '" + this.urlProperty + "' not configured correctly -> " + iae.getMessage()); 46 } 47 } else if (this.url != null) { 48 scm = ScmSystemFactory.getScmSystemByUrl(this.url); 49 } else { 50 scm = ScmSystemFactory.getScmSystemByName(this.repos); 51 } 52 53 if (this.module == null) { 55 this.module = scm.getUrl().getModule(); 56 } 57 58 String cmd = this.command.getValue(); 60 if (cmd.equals(Command.UPDATE)) { 61 scm.doUpdate(new File (this.dest), new ScmVersion(module, revision)); 62 63 } else if (cmd.equals(Command.CHECKOUT_OR_UPDATE)) { 64 if (this.module == null) { 65 throw new BuildException("No modulename specified in SCM url property '" + this.urlProperty + "' (currently set to '" + this.url + "')"); 66 } 67 scm.doCheckoutOrUpdate(this.module, new File (this.dest), new ScmVersion(module, revision), this.quiet); 68 69 } else if (cmd.equals(Command.ADD)) { 70 boolean recursiveAdd = true; 71 scm.doAdd(new File (this.dest, this.module), recursiveAdd); 72 73 } else if (cmd.equals(Command.COMMIT)) { 74 scm.doCommit(new File (this.dest), this.message); 75 76 } else if (cmd.equals(Command.GETURLMODULE)) { 77 if (this.property == null) { 78 throw new BuildException("Cannot get module of URL ('geturlmodule' command) without 'property' attribute set."); 79 } 80 if (scm.getUrl().getModule() != null) { 81 getProject().setProperty(this.property, scm.getUrl().getModule()); 82 } 83 84 } else if (cmd.equals(Command.GETURLBASE)) { 85 if (this.property == null) { 86 throw new BuildException("Cannot get base of URL ('geturlbase' command) without 'property' attribute set."); 87 } 88 getProject().setProperty(this.property, scm.getUrl().getUrlStringNoModule()); 89 90 } else if (cmd.equals(Command.GETREPOSURL)) { 91 if (this.property == null) { 92 throw new BuildException("Cannot get URL of a repository ('getreposurl' command) without 'property' attribute set."); 93 } 94 getProject().setProperty(this.property, scm.getUrl().getUrlString()); 95 96 } else if (cmd.equals(Command.GETREVISION)) { 97 if (this.property == null) { 98 throw new BuildException("Cannot get revision of dest attribute ('getrevision' command) without 'property' attribute set."); 99 } 100 getProject().setProperty(this.property, scm.getRevisionNumber(new File (this.dest))); 101 102 } else if (cmd.equals(Command.ISUPTODATE)) { 103 if (this.property == null) { 104 throw new BuildException("Cannot check up-to-date-ness without 'property' attribute set."); 105 } 106 getProject().setProperty(this.property, String.valueOf(scm.isUpToDate(new File (this.dest)))); 107 } 108 109 if (!quiet) { 110 if (scm.getStandardOutput() != null) { 111 log(scm.getStandardOutput(), Project.MSG_WARN); 112 } 113 } 114 115 } catch (IllegalArgumentException e) { 116 throw new BuildException(e); 117 } catch (ParseException e) { 118 throw new BuildException(e); 119 } 120 } 121 122 125 public Command getCommand() { 126 return command; 127 } 128 129 132 public String getDest() { 133 return dest; 134 } 135 136 139 public String getUrl() { 140 return url; 141 } 142 143 146 public void setCommand(Command cmd) { 147 this.command = cmd; 148 } 149 150 153 public void setDest(String string) { 154 dest = string; 155 } 156 157 public void setUrl(String string) { 158 url = string; 159 } 160 161 public void setUrlProperty(String string) { 162 this.urlProperty = string; 163 } 164 165 public void setRepos(String repos) { 166 this.repos = repos; 167 } 168 169 public void setRevision(String rev) { 170 if (rev != null) { 171 rev = rev.trim(); 172 } 173 this.revision = rev; 174 } 175 176 public void setModule(String module) { 177 if (module != null) { 178 module = module.trim(); 179 } 180 this.module = module; 181 } 182 183 public void setQuiet(boolean quiet) { 184 this.quiet = quiet; 185 } 186 187 public void setMessage(String message) { 188 this.message = message; 189 } 190 191 public void setProperty(String property) { 192 this.property = property; 193 } 194 195 196 public static class Command extends EnumeratedAttribute { 197 198 public static String UPDATE = "update"; 199 public static String CHECKOUT_OR_UPDATE = "checkoutOrUpdate"; 200 public static String ADD = "add"; 201 public static String COMMIT = "commit"; 202 public static String GETURLMODULE = "geturlmodule"; 203 public static String GETURLBASE = "geturlbase"; 204 public static String GETREPOSURL = "getreposurl"; 205 public static String GETREVISION = "getrevision"; 206 public static String ISUPTODATE = "ISUPTODATE"; 207 208 private static String [] values = 209 new String []{ 210 UPDATE, 211 CHECKOUT_OR_UPDATE, 212 ADD, 213 COMMIT, 214 GETURLMODULE, 215 GETURLBASE, 216 GETREPOSURL, 217 GETREVISION, 218 ISUPTODATE 219 }; 220 221 public String [] getValues() { 222 return Command.values; 223 } 224 225 } 226 } 227 | Popular Tags |