All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.neow3j.devpack.gradle.Neow3jCompileAction Maven / Gradle / Ivy

package io.neow3j.devpack.gradle;

import static io.neow3j.contract.ContractUtils.generateContractManifestFile;
import static io.neow3j.devpack.gradle.Neow3jCompileTask.NEOW3J_COMPILER_OPTIONS_NAME;
import static io.neow3j.devpack.gradle.Neow3jPluginOptions.CLASSNAME_NAME;
import static io.neow3j.devpack.gradle.Neow3jPluginUtils.generateDebugInfoZip;
import static io.neow3j.devpack.gradle.Neow3jPluginUtils.getBuildDirURL;
import static io.neow3j.devpack.gradle.Neow3jPluginUtils.getCompileOutputFileName;
import static io.neow3j.devpack.gradle.Neow3jPluginUtils.getSourceSetsDirsURL;
import static io.neow3j.devpack.gradle.Neow3jPluginUtils.getSourceSetsFilesURL;
import static io.neow3j.devpack.gradle.Neow3jPluginUtils.writeToFile;
import static java.nio.file.Files.createDirectories;
import static java.util.Optional.ofNullable;

import io.neow3j.compiler.CompilationUnit;
import io.neow3j.compiler.Compiler;
import io.neow3j.utils.ClassUtils;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.gradle.api.Action;

public class Neow3jCompileAction implements Action {

    @Override
    public void execute(Neow3jCompileTask neow3jPluginCompile) {
        String canonicalClassName = neow3jPluginCompile.getOptions().getClassName();
        Boolean debugSymbols = neow3jPluginCompile.getOptions().getDebug();

        ofNullable(canonicalClassName).orElseThrow(() ->
                new IllegalArgumentException("The parameter "
                        + "'" + CLASSNAME_NAME + "' needs to be set in the "
                        + "'" + NEOW3J_COMPILER_OPTIONS_NAME + "' "
                        + "declaration in your build.gradle file."));

        List clDirs = new ArrayList<>();

        // adding the source sets dir of the project
        List sourceSetDirsURL = getSourceSetsDirsURL(neow3jPluginCompile.getProject());
        clDirs.addAll(sourceSetDirsURL);

        // adding the build dir of the project
        URL buildDirsURL = getBuildDirURL(neow3jPluginCompile.getProjectBuildDir());
        clDirs.add(buildDirsURL);

        URL[] clDirsArray = clDirs.stream().toArray(URL[]::new);
        URLClassLoader compilerClassLoader = new URLClassLoader(clDirsArray,
                this.getClass().getClassLoader());

        // Get the absolute path of the source file
        String setOfSourceSetFilesPath = getSourceSetsFilesURL(
                neow3jPluginCompile.getProject()).stream()
                .map(URL::getPath)
                .filter(s -> s.contains(canonicalClassName.replace(".", "/")))
                .findFirst()
                .orElseThrow(() -> new IllegalStateException("Couldn't find the source file "
                        + "belonging to " + canonicalClassName));

        Compiler n = new Compiler(compilerClassLoader);

        try {
            // compile
            CompilationUnit compilationUnit;
            if (debugSymbols) {
                compilationUnit = n.compileClass(canonicalClassName, setOfSourceSetFilesPath);
            } else {
                compilationUnit = n.compileClass(canonicalClassName);
            }
            byte[] nefBytes = compilationUnit.getNefFile().toArray();

            // get the output directory
            String outDirString = createDirectories(neow3jPluginCompile.getCompilerOutputDir())
                    .toString();
            Path outDirPath = Paths.get(outDirString);

            // output the result to the output file
            String nefOutFileName = getCompileOutputFileName(canonicalClassName);
            Path outputFile = Paths.get(outDirString, nefOutFileName);
            writeToFile(outputFile.toFile(), nefBytes);

            // generate the manifest to the output dir
            String manifestOutFileName = generateContractManifestFile(
                    compilationUnit.getManifest(),
                    outDirPath.toFile());

            // if everything goes fine, print info
            System.out.println("Compilation succeeded!");
            System.out.println("NEF file: " + outputFile.toAbsolutePath());
            System.out.println("Manifest file: " + manifestOutFileName);

            if (debugSymbols) {
                // Pack the debug info into a ZIP archive.
                String debugInfoZipFileName = generateDebugInfoZip(compilationUnit.getDebugInfo(),
                        outDirString, ClassUtils.getClassName(canonicalClassName));
                System.out.println("Debug info zip file: " + debugInfoZipFileName);
            }

        } catch (Exception e) {
            System.out.println("Compilation failed. Reason: " + e.getMessage());
            e.printStackTrace();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy