org.eolang.hone.OptimizeMojo Maven / Gradle / Ivy
Show all versions of hone-maven-plugin Show documentation
/*
* The MIT License (MIT)
*
* Copyright (c) 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.hone;
import com.jcabi.log.Logger;
import com.sun.security.auth.module.UnixSystem;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* Converts Bytecode to Bytecode in order to make it faster.
*
* This goal takes every .class file from the
* target/classes/ directory, converts it to .xmir
* format (which is XML representation of EO),
* then converts .xmir to .phi (which is
* 𝜑-calculus),
* then optimizes it via
* eo-phi-normalizer,
* and then back to .xmir and to .class. As a result,
* you obtain optimized Bytecode in the target/classes/ directory,
* which supposedly works faster than before.
*
* The entire optimization pipeline happens inside Docker container,
* which is run from the image specified in the image parameter.
* The image may either be pulled or built locally. We recommend pulling
* it from the Docker Hub with the help of the pull goal. Also,
* we recommend deleting the image after optimization is done, with the help
* of the rmi goal.
*
* @since 0.1.0
*/
@Mojo(name = "optimize", defaultPhase = LifecyclePhase.PROCESS_CLASSES)
public final class OptimizeMojo extends AbstractMojo {
/**
* List of rules to use for optimizations.
*
* For example, "simple,b*,!abc" would include
* the simple rule, all rules that start
* with the b character, and exclude the abc
* rule.
*
* @since 0.1.0
* @checkstyle MemberNameCheck (6 lines)
*/
@Parameter(property = "hone.rules", defaultValue = "*")
private String rules;
/**
* EO version to use.
*
* @since 0.1.0
* @checkstyle MemberNameCheck (6 lines)
*/
@Parameter(property = "hone.eo-version")
private String eoVersion;
/**
* JEO version to use.
*
* @since 0.1.0
* @checkstyle MemberNameCheck (6 lines)
*/
@Parameter(property = "hone.jeo-version")
private String jeoVersion;
/**
* EO cache directory.
* @checkstyle MemberNameCheck (7 lines)
* @checkstyle VisibilityModifierCheck (10 lines)
*/
@Parameter(property = "hone.cache")
@SuppressWarnings("PMD.ImmutableField")
private File cache = Paths.get(System.getProperty("user.home")).resolve(".eo").toFile();
@Override
public void exec() throws IOException {
final Collection command = new LinkedList<>(
Arrays.asList(
"run",
"--rm",
"--volume", String.format("%s:/target", this.target),
"--volume", String.format("%s:/eo-cache", this.cache),
"--env", "TARGET=/target"
)
);
if (this.eoVersion == null) {
Logger.debug(this, "EO version is not set, we use the default one");
} else {
command.addAll(
Arrays.asList(
"--env", String.format("EO_VERSION=%s", this.eoVersion)
)
);
}
if (this.jeoVersion == null) {
Logger.debug(this, "JEO version is not set, we use the default one");
} else {
command.addAll(
Arrays.asList(
"--env", String.format("JEO_VERSION=%s", this.jeoVersion)
)
);
}
command.add("--user");
command.add(
String.format(
"%d:%d",
new UnixSystem().getUid(),
new UnixSystem().getGid()
)
);
command.addAll(
Arrays.asList(
"--env",
String.format(
"RULES=%s", String.join(" ", new Rules(this.rules).yamls())
)
)
);
command.add(this.image);
this.timings.through(
"optimize",
() -> new Docker(this.sudo).exec(command)
);
Logger.info(this, "Bytecode was optimized in '%s'", this.target);
}
}