Skip to content

Commit 366715e

Browse files
authored
Merge pull request #98 from h1alexbel/9
bug(#9): refactored, simplified `MjSodg` and related classes
2 parents e9b77aa + f081969 commit 366715e

File tree

10 files changed

+439
-404
lines changed

10 files changed

+439
-404
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
package org.eolang.sodg;
6+
7+
import com.jcabi.log.Logger;
8+
import com.yegor256.xsline.Shift;
9+
import com.yegor256.xsline.TrLambda;
10+
import com.yegor256.xsline.Train;
11+
import java.io.File;
12+
import java.util.Map;
13+
import java.util.logging.Level;
14+
import org.cactoos.map.MapEntry;
15+
import org.cactoos.map.MapOf;
16+
17+
/**
18+
* The depot that produces trains.
19+
* @since 0.0.3
20+
* @todo #9:35min Add unit tests for `Depot`.
21+
* We should test the trains inside of it, and method `train()`, that supposed
22+
* to return proper train by it's name. Don't forget to remove this puzzle.
23+
*/
24+
final class Depot {
25+
26+
/**
27+
* The trains.
28+
*/
29+
private final Map<String, Train<Shift>> trains;
30+
31+
/**
32+
* Ctor.
33+
* @param measures Measures
34+
*/
35+
Depot(final File measures) {
36+
this(
37+
new MapOf<>(
38+
new MapEntry<>(
39+
"sodg", Depot.measured(new TrSodg(Depot.loggingLevel()), measures)
40+
),
41+
new MapEntry<>(
42+
"dot", Depot.measured(new TrDot(Depot.loggingLevel()), measures)
43+
),
44+
new MapEntry<>("xembly", Depot.measured(new TrXembly(), measures)),
45+
new MapEntry<>("text", Depot.measured(new TrText(), measures)),
46+
new MapEntry<>(
47+
"finish", Depot.measured(new TrFinish(Depot.loggingLevel()), measures)
48+
)
49+
)
50+
);
51+
}
52+
53+
/**
54+
* Ctor.
55+
* @param trns The trains
56+
*/
57+
Depot(final Map<String, Train<Shift>> trns) {
58+
this.trains = trns;
59+
}
60+
61+
/**
62+
* Train.
63+
* @param name Name
64+
* @return Train
65+
*/
66+
Train<Shift> train(final String name) {
67+
return this.trains.get(name);
68+
}
69+
70+
/**
71+
* We are in IntelliJ IDEA at the moment?
72+
* <p>
73+
* This is for testing purposes, to enable higher visibility of logs inside
74+
* tests being executed interactively in the IDE.
75+
*
76+
* @return TRUE if inside IDE
77+
*/
78+
private static Level loggingLevel() {
79+
Level lvl = Level.FINEST;
80+
if (System.getProperty("java.class.path").contains("idea_rt.jar")) {
81+
lvl = Level.INFO;
82+
}
83+
return lvl;
84+
}
85+
86+
/**
87+
* Measured train.
88+
* @param train Train to measure
89+
* @param measures Measures
90+
* @return Measured train.
91+
*/
92+
private static Train<Shift> measured(final Train<Shift> train, final File measures) {
93+
if (measures.getParentFile().mkdirs()) {
94+
Logger.debug(Depot.class, "Directory created for %[file]s", measures);
95+
}
96+
if (!measures.getParentFile().exists()) {
97+
throw new IllegalArgumentException(
98+
String.format(
99+
"For some reason, the directory %s is absent, can't write measures to %s",
100+
measures.getParentFile(),
101+
measures
102+
)
103+
);
104+
}
105+
if (measures.isDirectory()) {
106+
throw new IllegalArgumentException(
107+
String.format(
108+
"This is not a file but a directory, can't write to it: %s",
109+
measures
110+
)
111+
);
112+
}
113+
return new TrLambda(
114+
train,
115+
shift -> new StMeasured(
116+
shift,
117+
measures.toPath()
118+
)
119+
);
120+
}
121+
}

src/main/java/org/eolang/sodg/MjSodg.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.apache.maven.plugins.annotations.Mojo;
1616
import org.apache.maven.plugins.annotations.Parameter;
1717
import org.apache.maven.plugins.annotations.ResolutionScope;
18+
import org.cactoos.map.MapEntry;
19+
import org.cactoos.map.MapOf;
1820
import org.cactoos.set.SetOf;
1921

2022
/**
@@ -32,8 +34,7 @@
3234
* <p>
3335
* This class was copy-pasted from objectionary/eo/eo-maven-plugin.
3436
*
35-
* @since 0.27
36-
* @checkstyle ClassFanOutComplexityCheck (500 lines)
37+
* @since 0.0.1
3738
*/
3839
@Mojo(
3940
name = "sodg",
@@ -44,6 +45,10 @@
4445
)
4546
@SuppressWarnings({"PMD.ImmutableField", "PMD.AvoidProtectedFieldInFinalClass"})
4647
public final class MjSodg extends AbstractMojo {
48+
/**
49+
* The directory where to save SODG to.
50+
*/
51+
private static final String DIR = "sodg";
4752

4853
/**
4954
* Whether we should skip goal execution.
@@ -203,18 +208,30 @@ public void execute() throws MojoFailureException {
203208
}
204209
} else {
205210
try {
211+
if (this.generateGraphFiles && !this.generateXemblyFiles) {
212+
throw new IllegalStateException(
213+
"Setting generateGraphFiles and not setting generateXemblyFiles has no effect because .graph files require .xe files"
214+
);
215+
}
216+
if (this.generateDotFiles && !this.generateGraphFiles) {
217+
throw new IllegalStateException(
218+
"Setting generateDotFiles and not setting generateGraphFiles has no effect because .dot files require .graph files"
219+
);
220+
}
206221
new SodgFiles(
207-
this.generateGraphFiles,
208-
this.generateXemblyFiles,
209-
this.generateSodgXmlFiles,
210-
this.xslMeasures,
211-
this.generateDotFiles,
212-
this.targetDir,
213-
this.tojos,
222+
new SodgInstructions(
223+
new Depot(this.xslMeasures),
224+
new MapOf<>(
225+
new MapEntry<>("generateSodgXmlFiles", this.generateSodgXmlFiles),
226+
new MapEntry<>("generateXemblyFiles", this.generateXemblyFiles),
227+
new MapEntry<>("generateXemblyFiles", this.generateXemblyFiles),
228+
new MapEntry<>("generateGraphFiles", this.generateGraphFiles),
229+
new MapEntry<>("generateDotFiles", this.generateDotFiles)
230+
)
231+
),
214232
this.sodgIncludes,
215-
this.sodgExcludes,
216-
this.descriptor.getVersion()
217-
).generate();
233+
this.sodgExcludes
234+
).generate(this.tojos.withXmir(), this.targetDir.toPath().resolve(MjSodg.DIR));
218235
} catch (final IOException exception) {
219236
throw new MojoFailureException("Can't convert XMIR to SODG", exception);
220237
}

0 commit comments

Comments
 (0)