forked from objectionary/eo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptimizationTask.java
166 lines (155 loc) 路 5.21 KB
/
OptimizationTask.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.IOException;
import java.nio.file.Path;
import java.util.Map;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.eolang.maven.optimization.OptCached;
import org.eolang.maven.optimization.Optimization;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Rel;
/**
* Optimized Tojos.
* @since 0.34.0
*/
final class OptimizationTask {
/**
* The map with optimization folder paths of Mojo.
*/
private final Map<String, Path> paths;
/**
* The map with target directory and EO cache directory of Mojo.
*/
private final Map<String, String> dirs;
/**
* The function that get ForeignTojo of Mojo.
*/
private final BiFunc<ForeignTojo, Path, ForeignTojo> update;
/**
* The function that get Tojo path of Mojo.
*/
private final Func<ForeignTojo, Path> source;
/**
* Ctor.
* @param paths Map with optimization folder paths of Mojo.
* @param dirs Map with target directory and EO cache directory of Mojo.
* @param update Function that updates ForeignTojo.
* @param source Function that gets Tojo path.
* @checkstyle ParameterNumberCheck (15 lines)
*/
OptimizationTask(
final Map<String, Path> paths,
final Map<String, String> dirs,
final BiFunc<ForeignTojo, Path, ForeignTojo> update,
final Func<ForeignTojo, Path> source
) {
this.paths = paths;
this.dirs = dirs;
this.update = update;
this.source = source;
}
/**
* Converts tojo to optimization task.
*
* @param tojo Tojo that should be optimized.
* @param common Optimization.
* @return Optimization task.
* @throws Exception If fail during optimization.
*/
public Scalar<Integer> value(
final ForeignTojo tojo,
final Optimization common
) throws Exception {
final Path src = this.source.apply(tojo);
Logger.debug(
this, "Adding optimization task for %s",
src
);
return () -> {
this.update.apply(
tojo,
this.make(
this.optimization(tojo, common).apply(new XMLDocument(src)), src
).toAbsolutePath()
);
return 1;
};
}
/**
* Make a path with optimized XML file after parsing.
*
* @param xml Optimized xml
* @param file EO file
* @return The file with optimized XMIR
* @throws IOException If fails
*/
private Path make(final XML xml, final Path file) throws IOException {
final String name = new XMLDocument(file).xpath("/program/@name").get(0);
final Place place = new Place(name);
final Path dir = this.paths.get(OptimizationFolder.TARGET.getKey());
final Path target = place.make(
dir.resolve(this.dirs.get(OptimizationFolder.TARGET.getKey())), TranspileMojo.EXT
);
new HmBase(dir).save(
xml.toString(),
dir.relativize(target)
);
Logger.debug(
this, "Optimized %s (program:%s) to %s",
new Rel(file), name, new Rel(target)
);
return target;
}
/**
* Optimization for specific tojo.
*
* @param tojo Tojo
* @param common Optimization
* @return Optimization for specific Tojo
* @throws Exception If fails
*/
private Optimization optimization(final ForeignTojo tojo, final Optimization common)
throws Exception {
final Optimization res;
if (tojo.hasHash()) {
res = new OptCached(
common,
this.paths.get(
OptimizationFolder.CACHE.getKey()
).resolve(this.dirs.get(OptimizationFolder.CACHE.getKey())).resolve(tojo.hash()),
this.source.apply(tojo)
);
} else {
res = common;
}
return res;
}
}