1 package fr.jayasoft.ivy.ant; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import org.apache.tools.ant.BuildException; 10 import org.apache.tools.ant.taskdefs.XSLTProcess; 11 12 import fr.jayasoft.ivy.DefaultModuleDescriptor; 13 import fr.jayasoft.ivy.Ivy; 14 import fr.jayasoft.ivy.ModuleId; 15 import fr.jayasoft.ivy.ModuleRevisionId; 16 import fr.jayasoft.ivy.filter.FilterHelper; 17 import fr.jayasoft.ivy.matcher.PatternMatcher; 18 import fr.jayasoft.ivy.report.ResolveReport; 19 import fr.jayasoft.ivy.report.XmlReportOutputter; 20 import fr.jayasoft.ivy.util.FileUtil; 21 22 30 public class IvyRepositoryReport extends IvyTask { 31 private String _organisation = "*"; 32 private String _module; 33 private String _branch; 34 private String _revision = "latest.integration"; 35 private File _cache; 36 private String _matcher = PatternMatcher.EXACT_OR_REGEXP; 37 38 private File _todir = new File ("."); 39 private boolean _graph = false; 40 private boolean _dot = false; 41 private boolean _xml = true; 42 private boolean _xsl = false; 43 private String _xslFile; 44 private String _outputname = "ivy-repository-report"; 45 private String _xslext = "html"; 46 private List _params = new ArrayList (); 47 48 public void execute() throws BuildException { 49 Ivy ivy = getIvyInstance(); 50 if (_cache == null) { 51 _cache = ivy.getDefaultCache(); 52 } 53 if (_xsl && _xslFile == null) { 54 throw new BuildException("xsl file is mandatory when using xsl generation"); 55 } 56 if (_module == null && PatternMatcher.EXACT.equals(_matcher)) { 57 throw new BuildException("no module name provided for ivy repository graph task: It can either be set explicitely via the attribute 'module' or via 'ivy.module' property or a prior call to <resolve/>"); 58 } else if (_module == null && !PatternMatcher.EXACT.equals(_matcher)) { 59 _module = PatternMatcher.ANY_EXPRESSION; 60 } 61 ModuleRevisionId mrid = ModuleRevisionId.newInstance(_organisation, _module, _revision); 62 try { 63 ModuleId[] mids = ivy.listModules(new ModuleId(_organisation, _module), ivy.getMatcher(_matcher)); 64 ModuleRevisionId[] mrids = new ModuleRevisionId[mids.length]; 65 for (int i = 0; i < mrids.length; i++) { 66 if (_branch != null) { 67 mrids[i] = new ModuleRevisionId(mids[i], _branch, _revision); 68 } else { 69 mrids[i] = new ModuleRevisionId(mids[i], _revision); 70 } 71 } 72 DefaultModuleDescriptor md = DefaultModuleDescriptor.newCallerInstance(mrids, true, false); 73 ResolveReport report = ivy.resolve(md, new String [] {"*"}, _cache, null, doValidate(ivy), false, true, false, false, FilterHelper.NO_FILTER); 74 new XmlReportOutputter().output(report, _cache); 75 if (_graph) { 76 gengraph(_cache, md.getModuleRevisionId().getOrganisation(), md.getModuleRevisionId().getName()); 77 } 78 if (_dot) { 79 gendot(_cache, md.getModuleRevisionId().getOrganisation(), md.getModuleRevisionId().getName()); 80 } 81 if (_xml) { 82 FileUtil.copy(new File (_cache, XmlReportOutputter.getReportFileName(md.getModuleRevisionId().getModuleId(), "default")), new File (_todir, _outputname+".xml"), null); 83 } 84 if (_xsl) { 85 genreport(_cache, md.getModuleRevisionId().getOrganisation(), md.getModuleRevisionId().getName()); 86 } 87 } catch (Exception e) { 88 throw new BuildException("impossible to generate graph for "+ mrid +": "+e, e); 89 } 90 } 91 92 private void genreport(File cache, String organisation, String module) throws IOException { 93 XSLTProcess xslt = new XSLTProcess(); 95 xslt.setTaskName(getTaskName()); 96 xslt.setProject(getProject()); 97 xslt.init(); 98 99 xslt.setIn(new File (cache, XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), "default"))); 100 xslt.setOut(new File (_todir, _outputname+"."+_xslext)); 101 102 xslt.setStyle(_xslFile); 103 104 XSLTProcess.Param param = xslt.createParam(); 105 param.setName("extension"); 106 param.setExpression(_xslext); 107 108 for (Iterator it = _params.iterator(); it.hasNext(); ) { 110 param = (XSLTProcess.Param) it.next(); 111 XSLTProcess.Param realParam = xslt.createParam(); 112 realParam.setName(param.getName()); 113 realParam.setExpression(param.getExpression()); 114 } 115 116 xslt.execute(); 117 } 118 119 private void gengraph(File cache, String organisation, String module) throws IOException { 120 gen(cache, organisation, module, getGraphStylePath(cache), "graphml"); 121 } 122 123 private String getGraphStylePath(File cache) throws IOException { 124 File style = new File (cache, "ivy-report-graph-all.xsl"); 127 FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report-graph-all.xsl"), style, null); 128 return style.getAbsolutePath(); 129 } 130 131 private void gendot(File cache, String organisation, String module) throws IOException { 132 gen(cache, organisation, module, getDotStylePath(cache), "dot"); 133 } 134 135 private String getDotStylePath(File cache) throws IOException { 136 File style = new File (cache, "ivy-report-dot-all.xsl"); 139 FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report-dot-all.xsl"), style, null); 140 return style.getAbsolutePath(); 141 } 142 143 private void gen(File cache, String organisation, String module, String style, String ext) throws IOException { 144 XSLTProcess xslt = new XSLTProcess(); 145 xslt.setTaskName(getTaskName()); 146 xslt.setProject(getProject()); 147 xslt.init(); 148 149 xslt.setIn(new File (cache, XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), "default"))); 150 xslt.setOut(new File (_todir, _outputname+"."+ext)); 151 xslt.setBasedir(cache); 152 xslt.setStyle(style); 153 xslt.execute(); 154 } 155 156 public File getTodir() { 157 return _todir; 158 } 159 public void setTodir(File todir) { 160 _todir = todir; 161 } 162 public boolean isGraph() { 163 return _graph; 164 } 165 166 public void setGraph(boolean graph) { 167 _graph = graph; 168 } 169 public String getXslfile() { 170 return _xslFile; 171 } 172 173 public void setXslfile(String xslFile) { 174 _xslFile = xslFile; 175 } 176 public boolean isXml() { 177 return _xml; 178 } 179 public void setXml(boolean xml) { 180 _xml = xml; 181 } 182 public boolean isXsl() { 183 return _xsl; 184 } 185 public void setXsl(boolean xsl) { 186 _xsl = xsl; 187 } 188 public String getXslext() { 189 return _xslext; 190 } 191 public void setXslext(String xslext) { 192 _xslext = xslext; 193 } 194 195 public XSLTProcess.Param createParam() { 196 XSLTProcess.Param result = new XSLTProcess.Param(); 197 _params.add(result); 198 return result; 199 } 200 201 public String getOutputname() { 202 return _outputname; 203 } 204 205 public void setOutputname(String outputpattern) { 206 _outputname = outputpattern; 207 } 208 209 public File getCache() { 210 return _cache; 211 } 212 public void setCache(File cache) { 213 _cache = cache; 214 } 215 public String getMatcher() { 216 return _matcher; 217 } 218 public void setMatcher(String matcher) { 219 _matcher = matcher; 220 } 221 public String getModule() { 222 return _module; 223 } 224 public void setModule(String module) { 225 _module = module; 226 } 227 public String getOrganisation() { 228 return _organisation; 229 } 230 public void setOrganisation(String organisation) { 231 _organisation = organisation; 232 } 233 public String getRevision() { 234 return _revision; 235 } 236 public void setRevision(String revision) { 237 _revision = revision; 238 } 239 240 241 public String getBranch() { 242 return _branch; 243 } 244 245 246 public void setBranch(String branch) { 247 _branch = branch; 248 } 249 250 public boolean isDot() { 251 return _dot; 252 } 253 254 public void setDot(boolean dot) { 255 _dot = dot; 256 } 257 258 259 } 260 | Popular Tags |