-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJpeekMojo.java
107 lines (100 loc) · 4.01 KB
/
JpeekMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek.plugin;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.File;
import java.io.IOException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.jpeek.App;
/**
* {@link org.apache.maven.plugin.AbstractMojo} implementation for jPeek.
*
* @since 0.1
* @todo #3:30min Add some tests for this Mojo using AbstractMojoTestCase
* from maven-plugin-testing-harness. A good resource for examples is
* maven-checkstyle-plugin. It has been verified that it works from the
* command line (see README).
* @todo #3:30min Add support for analyzing classes in the test directory.
* This should output an analysis most certainly in a different directory
* from the main classes.
*/
@Mojo(name = "analyze", defaultPhase = LifecyclePhase.VERIFY)
public final class JpeekMojo extends AbstractMojo {
/**
* Specifies the path where to find classes analyzed by jPeek.
*
* @checkstyle MemberNameCheck (3 lines)
*/
@Parameter(property = "jpeek.input", defaultValue = "${project.build.outputDirectory}")
private File inputDirectory;
/**
* Specifies the path to save the jPeek output.
*
* @checkstyle MemberNameCheck (3 lines)
*/
@Parameter(property = "jpeek.output", defaultValue = "${project.build.directory}/jpeek/")
private File outputDirectory;
/**
* Specifies expected cohesion ration of a project.
*
* @checkstyle MemberNameCheck (3 lines)
*/
@Parameter(property = "jpeek.cohesionrate", defaultValue = "8.0")
private double cohesionRate;
/**
* Skip analyze.
*/
@Parameter(property = "jpeek.skip", defaultValue = "false")
private boolean skip;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!this.skip) {
try {
new App(
this.inputDirectory.toPath(),
this.outputDirectory.toPath()
).analyze();
final XML index = new XMLDocument(
new File(String.format("%s\\%s", this.outputDirectory, "index.xml"))
);
final double score = Double.parseDouble(
index.xpath("/index/@score").get(0)
);
if (score < this.cohesionRate) {
throw new MojoFailureException(
String.format("Project cohesion rate is less than %.2f", this.cohesionRate)
);
}
} catch (final IOException ex) {
throw new MojoExecutionException("Couldn't analyze", ex);
}
}
}
}