1 package com.stateofflow.eclipse.metrics.ant;
2
3 import static com.stateofflow.eclipse.metrics.MetricsPlugin.*;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.HashSet;
8 import java.util.Set;
9
10 import org.apache.tools.ant.BuildException;
11 import org.apache.tools.ant.Project;
12 import org.apache.tools.ant.Task;
13 import org.apache.tools.ant.types.FileSet;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.jdt.core.JavaCore;
18 import org.eclipse.jdt.core.JavaModelException;
19
20 import com.stateofflow.eclipse.metrics.MetricsBuilder;
21 import com.stateofflow.eclipse.metrics.MetricsPlugin;
22 import com.stateofflow.eclipse.metrics.builder.list.CompilationUnitList;
23 import com.stateofflow.eclipse.metrics.export.Exporter;
24 import com.stateofflow.eclipse.metrics.export.html.HtmlExportConfiguration;
25
26 public class ExportMetricsTask extends Task {
27 private String eclipseProject;
28 private File dir;
29 private int htmlRowsPerPage = Integer.MAX_VALUE;
30 private boolean htmlDashboard = true;
31 private boolean htmlJava = false;
32 private boolean html = true;
33 private boolean csv = true;
34 private final Set<FileSet> compilationUnitSets = new HashSet<FileSet>();
35
36 private Exporter createCsvExporter() {
37 return !csv ? Exporter.NULL : getFactory().createCsvExporter(dir);
38 }
39
40 private Exporter[] createExporters() {
41 return new Exporter[]{createHtmlExporter(), createCsvExporter()};
42 }
43
44 private HtmlExportConfiguration createHtmlExportConfiguration() {
45 return new HtmlExportConfiguration(dir, eclipseProject, htmlDashboard, htmlRowsPerPage, htmlJava);
46 }
47
48 private Exporter createHtmlExporter() {
49 return !html ? Exporter.NULL : getFactory().createHtmlExporter(createHtmlExportConfiguration());
50 }
51
52 @Override
53 public void execute() {
54 verifyEclipseProject();
55 verifyDir();
56 verifyFormats();
57 verifyHtmlOptions();
58 export();
59 }
60
61 private void export() {
62 try {
63 if (compilationUnitSets.isEmpty()) {
64 new MetricsBuilder().export(getEclipseProject(), createExporters(), getProgressMonitor());
65 } else {
66 new MetricsBuilder().export(createCompilationUnitList(), createExporters(), getProgressMonitor());
67 }
68 } catch (final Exception e) {
69 e.printStackTrace(System.err);
70 log(e, Project.MSG_ERR);
71 throw new BuildException(e);
72 }
73 }
74
75 private CompilationUnitList createCompilationUnitList() throws IOException, JavaModelException {
76 return new CompilationUnitListBuilder().build(JavaCore.create(getEclipseProject()), compilationUnitSets);
77 }
78
79 private IProject getEclipseProject() {
80 return MetricsPlugin.getWorkspaceRoot().getProject(eclipseProject);
81 }
82
83 private IProgressMonitor getProgressMonitor() {
84 return new NullProgressMonitor() {
85 @Override
86 public void beginTask(final String name, final int totalWork) {
87 logTask(name);
88 }
89
90 @Override
91 public void setTaskName(final String name) {
92 logTask(name);
93 }
94
95 @Override
96 public void subTask(final String name) {
97 logTask(name);
98 }
99 };
100 }
101
102 private void logTask(final String name) {
103 log(name, Project.MSG_VERBOSE);
104 }
105
106 private String quote(final String s) {
107 return "\"" + s + "\"";
108 }
109
110 public void setCsv(final boolean csv) {
111 this.csv = csv;
112 }
113
114 public void setDir(final File dir) {
115 this.dir = dir;
116 }
117
118 public void setEclipseProject(final String eclipseProject) {
119 this.eclipseProject = eclipseProject;
120 }
121
122 public void setHtml(final boolean html) {
123 this.html = html;
124 }
125
126 public void setHtmlDashboard(final boolean htmlDashboard) {
127 this.htmlDashboard = htmlDashboard;
128 }
129
130 public void setHtmlJava(final boolean htmlJava) {
131 this.htmlJava = htmlJava;
132 }
133
134 public void setHtmlRowsPerPage(final int htmlRowsPerPage) {
135 this.htmlRowsPerPage = htmlRowsPerPage;
136 }
137
138 public void addCompilationUnits(final FileSet compilationUnits) {
139 compilationUnitSets.add(compilationUnits);
140 }
141
142 private void verify(final boolean value, final String message) {
143 if (!value) {
144 throw new BuildException(message);
145 }
146 }
147
148 private void verifyDir() {
149 verify(dir != null, "dir must be set");
150 verify(dir.exists(), "dir " + quote(dir.getAbsolutePath()) + " must exist");
151 verify(dir.isDirectory(), "dir " + quote(dir.getAbsolutePath()) + " must be a directory");
152 verify(dir.canWrite(), "dir " + quote(dir.getAbsolutePath()) + " must be writable");
153 }
154
155 private void verifyEclipseProject() {
156 verify(eclipseProject != null, "eclipseProject must be set");
157 verify(getEclipseProject().exists(), "eclipseProject " + quote(eclipseProject) + " does not exist");
158 verify(getEclipseProject().isOpen(), "eclipseProject " + quote(eclipseProject) + " must be open");
159 }
160
161 private void verifyFormats() {
162 verify(csv || html, "At least one of " + quote("html") + " or " + quote("csv") + " must be true");
163 }
164
165 private void verifyHtmlOptions() {
166 if (!html) {
167 return;
168 }
169
170 verify(htmlRowsPerPage > 0, "htmlRowsPerPage must be greater than 0");
171 }
172 }
173