Skip to content

Commit

Permalink
objectionary#2648: Transferred functionality from here from BinarizeM…
Browse files Browse the repository at this point in the history
…ojo.java
  • Loading branch information
levBagryansky committed Jan 10, 2024
1 parent 4566cc8 commit dd6e9cc
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 192 deletions.
191 changes: 10 additions & 181 deletions eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,23 @@
package org.eolang.maven;

import com.jcabi.log.Logger;
import com.yegor256.Jaxec;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import java.util.Collection;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.eolang.maven.rust.BuildFailureException;
import org.eolang.maven.rust.Buildable;
import org.eolang.maven.rust.Names;

/**
* Compile binaries.
*
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @since 0.1
* @todo #2118:90min Make it more generic. Perhaps by compilation of
* RustNode instead of going and building throw directories. Maybe it
* is better to get from BinarizeParseMojo.
*/
@Mojo(
name = "binarize",
Expand All @@ -62,11 +51,6 @@
@SuppressWarnings("PMD.LongVariable")
public final class BinarizeMojo extends SafeMojo {

/**
* Name of executable file which is result of cargo building.
*/
public static final String LIB = BinarizeMojo.common();

/**
* The directory where to binarize to.
*/
Expand Down Expand Up @@ -109,169 +93,14 @@ public final class BinarizeMojo extends SafeMojo {

@Override
public void exec() throws IOException {
new Moja<>(BinarizeParseMojo.class).with(
"names", new Names(this.namesDir.toPath())
).copy(this).execute();
final int total = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
new Mapped<>(
project -> () -> {
this.build(project);
return 1;
},
new Filtered<>(
BinarizeMojo::valid,
targetDir.toPath().resolve("Lib").toFile().listFiles()
)
)
)
).intValue();
Logger.info(this, "Built in total %d cargo projects", total);
}

/**
* Calculates name for Rust shared library depending on OS.
* @return Name.
*/
private static String common() {
final String result;
if (SystemUtils.IS_OS_WINDOWS) {
result = "common.dll";
} else if (SystemUtils.IS_OS_LINUX) {
result = "libcommon.so";
} else if (SystemUtils.IS_OS_MAC) {
result = "libcommon.dylib";
} else {
throw new IllegalArgumentException(
String.format(
"Rust inserts are not supported in %s os. Only windows, linux and macos are allowed.",
System.getProperty("os.name")
)
);
}
return result;
}

/**
* Is the project valid?
* @param project File to check.
* @return True if valid. Otherwise, false.
*/
private static boolean valid(final File project) {
return project.isDirectory()
&& project.toPath().resolve("Cargo.toml").toFile().exists();
}

/**
* Builds cargo project.
* @param project Path to the project.
* @throws IOException If any issues with IO.
*/
private void build(final File project) throws IOException {
final File cached = this.cache
.resolve("Lib")
.resolve(project.getName())
.resolve("target").toFile();
if (BinarizeMojo.sameProject(
project.toPath(),
this.cache
.resolve("Lib")
.resolve(project.getName())
)) {
Logger.info(
this,
"content of %s was not changed since the last launch",
project.getName()
);
final File executable = cached.toPath()
.resolve("debug")
.resolve(BinarizeMojo.LIB)
.toFile();
if (executable.exists()) {
FileUtils.copyFile(
executable,
project.toPath()
.resolve("target")
.resolve("debug")
.resolve(BinarizeMojo.LIB)
.toFile()
);
}
} else {
final File target = project.toPath().resolve("target").toFile();
if (cached.exists()) {
Logger.info(this, "Copying %s to %s", cached, target);
FileUtils.copyDirectory(cached, target);
}
Logger.info(this, "Building %s rust project..", project.getName());
try {
new Jaxec("cargo", "build").withHome(project).execUnsafe();
} catch (final IOException | IllegalArgumentException ex) {
throw new BuildFailureException(
String.format(
"Failed to build cargo project with dest = %s",
project
),
ex
);
}
Logger.info(
this,
"Cargo building succeeded, update cached %s with %s",
cached,
target
);
FileUtils.copyDirectory(target.getParentFile(), cached.getParentFile());
}
final Collection<Buildable> ffis = new BinarizeParse(
this.generatedDir,
this.eoPortalDir,
new Names(this.namesDir.toPath()),
this.targetDir
).exec(this.scopedTojos().withOptimized());
ffis.stream().parallel().forEach(ffi -> ffi.build(this.cache));
Logger.info(this, "Built in total %d cargo projects", ffis.size());
}

/**
* Check if the project was not changed.
* @param src Directory in current target.
* @param cached Directory in cache.
* @return True if the project is the same.
*/
private static boolean sameProject(final Path src, final Path cached) {
return BinarizeMojo.sameFile(
src.resolve("src/foo.rs"), cached.resolve("src/foo.rs")
) && BinarizeMojo.sameFile(
src.resolve("src/lib.rs"), cached.resolve("src/lib.rs")
) && BinarizeMojo.sameFile(
src.resolve("Cargo.toml"), cached.resolve("Cargo.toml")
);
}

/**
* Check if the source file is the same as in cache.
* @param src Source file.
* @param cached Cache file.
* @return True if the same.
*/
private static boolean sameFile(final Path src, final Path cached) {
return cached.toFile().exists() && BinarizeMojo.uncomment(
new UncheckedText(
new TextOf(src)
).asString()
).equals(
BinarizeMojo.uncomment(
new UncheckedText(
new TextOf(cached)
).asString()
)
);
}

/**
* Removed the first line from the string.
* We need it because generated files are disclaimed.
* @param content Content.
* @return String without the first line.
* @checkstyle StringLiteralsConcatenationCheck (8 lines)
*/
private static String uncomment(final String content) {
return content.substring(
1 + content.indexOf(System.getProperty("line.separator"))
);
}
}
Loading

0 comments on commit dd6e9cc

Please sign in to comment.