View Javadoc
1   /*
2    * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu, jcarsique, slacoin
16   */
17  package org.nuxeo.build.ant.artifact;
18  
19  import java.io.FileOutputStream;
20  import java.io.IOException;
21  import java.io.OutputStream;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.StringTokenizer;
26  
27  import org.apache.commons.io.IOUtils;
28  import org.apache.tools.ant.BuildException;
29  import org.apache.tools.ant.Project;
30  import org.apache.tools.ant.Task;
31  import org.eclipse.aether.util.artifact.JavaScopes;
32  import org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor;
33  import org.nuxeo.build.maven.AntBuildMojo;
34  import org.nuxeo.build.maven.filter.TrueFilter;
35  import org.nuxeo.build.maven.graph.FlatPrinterDependencyVisitor;
36  import org.nuxeo.build.maven.graph.Graph;
37  import org.nuxeo.build.maven.graph.Node;
38  import org.nuxeo.build.maven.graph.TreePrinterDependencyVisitor;
39  
40  /**
41   * TODO NXBT-258
42   */
43  public class PrintGraphTask extends Task {
44  
45      private String output;
46  
47      private String mode = PrintGraphTask.MODE_TREE;
48  
49      /**
50       * Default format with GAV: group:artifact:version:type:classifier
51       */
52      public static final int FORMAT_GAV = 0;
53  
54      /**
55       * Key-value format: FILENAME=GAV
56       */
57      public static final int FORMAT_KV_F_GAV = 1;
58  
59      private int format = FORMAT_GAV;
60  
61      private boolean append = false;
62  
63      private String source;
64  
65      private List<String> scopes = null;
66  
67      /**
68       * In sdk mode, root nodes are not printed
69       *
70       * @since 1.10.2
71       */
72      public static final String MODE_SDK = "sdk";
73  
74      /**
75       * In flat mode, root nodes are not printed
76       *
77       * @since 1.10.2
78       */
79      public static final String MODE_FLAT = "flat";
80  
81      /**
82       * @since 1.10.2
83       */
84      public static final String MODE_TREE = "tree";
85  
86      @Override
87      public void execute() throws BuildException {
88          // If scopes is undefined, set default depending on the mode
89          if (scopes == null) {
90              // In tree mode, we usually want all scopes
91              if (MODE_TREE.equals(mode)) {
92                  scopes = Arrays.asList(new String[] { JavaScopes.COMPILE,
93                          JavaScopes.PROVIDED, JavaScopes.RUNTIME,
94                          JavaScopes.SYSTEM, JavaScopes.TEST });
95              } else {
96                  scopes = Arrays.asList(new String[] { JavaScopes.COMPILE,
97                          JavaScopes.RUNTIME, JavaScopes.SYSTEM });
98              }
99          }
100         List<Node> roots;
101         if (source != null) {
102             // TODO NXBT-258
103             // ExpandTask expandTask = new NuxeoExpandTask();
104             // ExpandTask expandTask = new ExpandTask();
105             // expandTask.setDepth("all");
106             // expandTask.execute(AntBuildMojo.getInstance().newGraph(source));
107             roots = new ArrayList<>();
108             Graph graph = new Graph();
109             roots.add(graph.addRootNode(source));
110             graph.resolveDependencies(new TrueFilter(), 0);
111         } else {
112             roots = AntBuildMojo.getInstance().getGraph().getRoots();
113         }
114         OutputStream out = System.out;
115         try {
116             if (output != null) {
117                 out = new FileOutputStream(output, append);
118             }
119             // TODO NXBT-258 see DependencyFilterUtils.classpathFilter(
120             // JavaScopes.COMPILE );
121             // system.resolveDependencies( session, dependencyRequest
122             // ).getArtifactResults();
123             if (MODE_TREE.equals(mode)) {
124                 TreePrinterDependencyVisitor pdv = new TreePrinterDependencyVisitor(
125                         out, format, scopes, roots);
126                 for (Node node : roots) {
127                     log("Visiting " + node, Project.MSG_DEBUG);
128                     // Work on a clone to avoid graph being altered
129                     CloningDependencyVisitor cdv = new CloningDependencyVisitor();
130                     node.accept(cdv);
131                     cdv.getRootNode().accept(pdv);
132                 }
133             } else {
134                 FlatPrinterDependencyVisitor pdv = new FlatPrinterDependencyVisitor(
135                         out, format, scopes);
136                 // Ignore roots in flat mode
137                 pdv.addIgnores(roots);
138                 for (Node node : roots) {
139                     log("Visiting " + node, Project.MSG_DEBUG);
140                     node.accept(pdv);
141                 }
142                 pdv.print();
143             }
144         } catch (IOException e) {
145             throw new BuildException(e);
146         } finally {
147             IOUtils.closeQuietly(out);
148         }
149     }
150 
151     public void setOutput(String output) {
152         this.output = output;
153     }
154 
155     /**
156      * @since 1.10.2
157      */
158     public void setMode(String mode) {
159         if (MODE_SDK.equals(mode)) {
160             this.format = FORMAT_KV_F_GAV;
161         }
162         this.mode = mode;
163     }
164 
165     /**
166      * Defines output format
167      *
168      * @param format
169      * @since 1.10.2
170      */
171     public void setFormat(int format) {
172         this.format = format;
173     }
174 
175     /**
176      * Output append mode
177      *
178      * @param append
179      * @since 1.10.2
180      */
181     public void setAppend(boolean append) {
182         this.append = append;
183     }
184 
185     /**
186      * If set, print another graph than the current one
187      *
188      * @param source GAV key of root node to expand as a graph
189      * @since 1.10.2
190      */
191     public void setSource(String source) {
192         this.source = source;
193     }
194 
195     /**
196      * @since 1.10.2
197      * @param scopes Comma separated list of scopes to include. Defaults to
198      *            "compile,runtime,system".
199      */
200     public void setScopes(String scopes) {
201         StringTokenizer st = new StringTokenizer(scopes, ",");
202         this.scopes = new ArrayList<>();
203         while (st.hasMoreTokens()) {
204             this.scopes.add(st.nextToken());
205         }
206     }
207 
208 }