Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 11, 2022
2 parents dc7e1ba + cd1b497 commit 1d30f50
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 10 deletions.
36 changes: 35 additions & 1 deletion eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.scalar.Unchecked;
import org.eolang.maven.optimization.OptCached;
import org.eolang.maven.optimization.OptLambda;
import org.eolang.maven.optimization.Optimization;
import org.eolang.parser.ParsingTrain;

/**
Expand Down Expand Up @@ -146,6 +149,14 @@ public final class OptimizeMojo extends SafeMojo {
)
private boolean failOnWarning;

/**
* EO cache directory.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(property = "eo.cache")
@SuppressWarnings("PMD.ImmutableField")
private Path cache = Paths.get(System.getProperty("user.home")).resolve(".eo");

@Override
public void exec() throws IOException {
final Collection<Tojo> sources = this.scopedTojos().select(
Expand Down Expand Up @@ -176,7 +187,7 @@ public void exec() throws IOException {
Executors.callable(
() -> {
try {
final XML optimized = this.optimize(src);
final XML optimized = this.optimization(tojo).apply(src);
done.incrementAndGet();
if (this.shouldPass(optimized)) {
tojo.set(
Expand Down Expand Up @@ -236,6 +247,26 @@ public void exec() throws IOException {
}
}

/**
* Optimization for specific tojo.
*
* @param tojo Tojp
* @return Optimization for specific Tojo
*/
private Optimization optimization(final SynchronizedTojo tojo) {
final Optimization optimization;
if (tojo.exists(AssembleMojo.ATTR_HASH)) {
optimization = new OptCached(
new OptLambda(this::optimize),
this.cache.resolve(OptimizeMojo.OPTIMIZED)
.resolve(tojo.get(AssembleMojo.ATTR_HASH))
);
} else {
optimization = new OptLambda(this::optimize);
}
return optimization;
}

/**
* Optimize XML file after parsing.
*
Expand All @@ -244,6 +275,9 @@ public void exec() throws IOException {
* @throws FileNotFoundException If fails
* @throws IllegalArgumentException If error is detected within XMIR and
* fail on error is enabled.
* @todo #1431:90min move that method implementation to a separate class under
* {@link org.eolang.maven.optimization} package. Probably, after implementation we will able
* to remove {@link org.eolang.maven.optimization.OptLambda}.
*/
private XML optimize(final Path file) throws FileNotFoundException {
final String name = new XMLDocument(file).xpath("/program/@name").get(0);
Expand Down
4 changes: 2 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @since 0.1
*/
final class Place {
public final class Place {

/**
* The name of the object, e.g. "org.eolang.io.stdout"
Expand All @@ -42,7 +42,7 @@ final class Place {
* Ctor.
* @param obj The name of the object
*/
Place(final String obj) {
public Place(final String obj) {
this.name = obj;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.optimization;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eolang.maven.AssembleMojo;
import org.eolang.maven.Place;

/**
* The cached optimization.
* Returns already optimized XML if it's found in the cache.
*
* @since 0.28.11
* @todo #1431:90min Unit test are required. We have to test different cases like:
* - if XML file is already in cache
* - if XML file is absent
* - if some {@link java.io.IOException} happens
*/
public final class OptCached implements Optimization {

/**
* Real optimization.
*/
private final Optimization delegate;

/**
* Cache folder.
*/
private final Path folder;

/**
* The main constructor.
*
* @param delegate Real optimization.
* @param folder Cache folder.
*/
public OptCached(
final Optimization delegate,
final Path folder
) {
this.delegate = delegate;
this.folder = folder;
}

@Override
public XML apply(final Path xml) {
try {
final Path path = new Place(
new XMLDocument(xml).xpath("/program/@name").get(0)
).make(this.folder, AssembleMojo.ATTR_XMIR);
final XML optimized;
if (Files.exists(path)) {
optimized = new XMLDocument(path);
} else {
optimized = this.delegate.apply(xml);
Files.createDirectories(path.getParent());
Files.createFile(path);
Files.write(path, optimized.toString().getBytes(StandardCharsets.UTF_8));
}
return optimized;
} catch (final IOException ex) {
throw new IllegalStateException(String.format("Can't optimize '%s'", xml), ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.optimization;

import com.jcabi.xml.XML;
import java.nio.file.Path;
import org.cactoos.Func;
import org.cactoos.func.UncheckedFunc;

/**
* Lambda optimization is adapter for custom optimization.
* Optimization provided by external function.
*
* @since 0.28.11
*/
public final class OptLambda implements Optimization {

/**
* Custom foreign optimisation.
*/
private final UncheckedFunc<Path, XML> delegate;

/**
* The main constructor.
*
* @param delegate Custom optimization.
*/
public OptLambda(final Func<Path, XML> delegate) {
this.delegate = new UncheckedFunc<>(delegate);
}

@Override
public XML apply(final Path xml) {
return this.delegate.apply(xml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.optimization;

import com.jcabi.xml.XML;
import java.nio.file.Path;
import java.util.function.Function;

/**
* Abstraction for XML optimizations.
*
* @since 0.28.11
*/
@FunctionalInterface
public interface Optimization extends Function<Path, XML> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.
*/
/**
* Optimizations package.
* The main purpose of classes under the package is to provide different implementations
* of xml optimizations:
* - {@link org.eolang.maven.optimization.OptLambda} - makes an optimization using foreign function
* - {@link org.eolang.maven.optimization.OptCached} - looks for optimization in cache first
*/
package org.eolang.maven.optimization;
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.hamcrest.Matchers;
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand Down Expand Up @@ -137,13 +136,8 @@ void optimizesIfExpired(@TempDir final Path temp) throws Exception {
*
* @param temp Temporary test directory.
* @throws Exception if unexpected error happened.
* @todo #1223:90min Implement caching for optimization step. After implementation don't forget
* to remove '@Disabled' annotation from the next tests:
* - 'getsAlreadyOptimizedResultsFromCache'
* - 'savesOptimizedResultsToCache'
*/
@Test
@Disabled
void getsAlreadyOptimizedResultsFromCache(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
Expand Down Expand Up @@ -194,7 +188,6 @@ void getsAlreadyOptimizedResultsFromCache(@TempDir final Path temp) throws Excep
}

@Test
@Disabled
void savesOptimizedResultsToCache(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("foo/main.eo");
new Home(temp).save(
Expand Down

3 comments on commit 1d30f50

@0pdd
Copy link

@0pdd 0pdd commented on 1d30f50 Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1223-c08eee6b disappeared from eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java), that's why I closed #1431. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 1d30f50 Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1431-4de3d363 discovered in eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java) and submitted as #1442. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 1d30f50 Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1431-d4218ffb discovered in eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java) and submitted as #1443. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.