-
Notifications
You must be signed in to change notification settings - Fork 127
/
DiscoverMojo.java
182 lines (176 loc) · 6.2 KB
/
DiscoverMojo.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* 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.eolang.maven;
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.cactoos.iterable.Filtered;
import org.cactoos.list.ListOf;
import org.cactoos.set.SetOf;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.Rel;
/**
* Read all XMIR files and find foreign objects in them, then
* add them to the catalog.
*
* @since 0.1
*/
@Mojo(
name = "discover-foreign",
defaultPhase = LifecyclePhase.PROCESS_SOURCES,
threadSafe = true
)
public final class DiscoverMojo extends SafeMojo {
@Override
public void exec() throws IOException {
final Collection<ForeignTojo> tojos = this.scopedTojos().notDiscovered();
final Collection<String> discovered = new HashSet<>(1);
for (final ForeignTojo tojo : tojos) {
final Path src = tojo.optimized();
final Collection<String> names = this.discover(src);
for (final String name : names) {
this.scopedTojos().add(name).withDiscoveredAt(src);
discovered.add(name);
}
tojo.withDiscovered(names.size());
}
if (tojos.isEmpty()) {
if (this.scopedTojos().size() == 0) {
Logger.warn(this, "Nothing to discover, since there are no programs");
} else {
Logger.info(this, "Nothing to discover, all programs checked already");
}
} else if (discovered.isEmpty()) {
Logger.info(
this, "No foreign objects discovered in %d programs",
tojos.size()
);
} else {
Logger.info(
this, "Discovered %d foreign objects in %d programs: %s",
discovered.size(), tojos.size(), discovered
);
}
}
/**
* Pull all deps found in the provided XML file.
*
* @param file The .xmir file
* @return List of foreign objects found
* @throws FileNotFoundException If not found
*/
private Collection<String> discover(final Path file)
throws FileNotFoundException {
final XML xml = new XMLDocument(file);
final Collection<String> names = DiscoverMojo.names(
xml, this.xpath(false)
);
if (this.withVersions) {
names.addAll(
DiscoverMojo.names(xml, this.xpath(true))
);
}
if (!xml.nodes("//o[@vararg]").isEmpty()) {
names.add("org.eolang.tuple");
}
if (names.isEmpty()) {
Logger.debug(
this, "Didn't find any foreign objects in %s",
new Rel(file)
);
} else {
Logger.debug(
this, "Found %d foreign objects in %s: %s",
names.size(), new Rel(file), names
);
}
return names;
}
/**
* Xpath for selecting objects from given xml.
* @param versioned Select with versions or not.
* @return Xpath as list of strings
* @todo #1602:30min Simplify xpath. Current implementation for building
* xpath with and without versions is quite ugly. For some reason
* if we try to take `/concat(@base,'|',@ver)` and there are object without
* attribute `ver` - xpath returns nothing. So we need to take `/@base`
* from objects where attribute `ver` is not present in both cases and
* then if flag `withVersions` is `true` - take `concat(@base,'|',@ver)`
* from objects attribute `ver` is present.
*/
private List<String> xpath(final boolean versioned) {
final List<String> xpath = new ListOf<>(
"//o[",
"not(starts-with(@base,'.'))",
"and @base != 'Q'",
"and @base != '^'",
"and @base != '$'",
"and @base != '&'",
"and not(@ref)"
);
final List<String> tail;
if (versioned) {
tail = new ListOf<>(
"and @ver",
"]/concat(@base,'|',@ver)"
);
} else {
tail = new ListOf<>();
if (this.withVersions) {
tail.add("and not(@ver)");
}
tail.add("]/@base");
}
xpath.addAll(tail);
return xpath;
}
/**
* Get unique list of object names from given XML by provided xpath.
* @param xml XML.
* @param xpath Xpath.
* @return List of object names.
*/
private static Set<String> names(final XML xml, final List<String> xpath) {
return new SetOf<>(
new Filtered<>(
obj -> !obj.isEmpty(),
xml.xpath(
String.join(
" ",
xpath
)
)
)
);
}
}