package com.muyu.rule.server.complie;

import com.muyu.rule.common.scan.JavaCodeScan;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;

/**
 * @Author：张承志
 * @Package：com.muyu.rule.server.complie
 * @Project：cloud-etl-rule
 * @name：SourceCode
 * @Date：2024/8/22 19:10
 */
public class SourceCodeComplier extends ClassLoader {

    private static JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    private static StandardJavaFileManager fileManager = compiler.getStandardFileManager(null,null,null);

    /**
     * 输入编译的文件夹路径
     * @param path 文件夹路径
     */

public static void javaCompilerPath(String path){
  File[] files = JavaCodeScan.javaSourceScanByPath(path);

    javaCampiler(files);
}
    /**
     * 输入编译的文件路径
     * @param filePath
     */
public static void javaCompilerFile(String... filePath){
    int filePathLength = filePath.length;
    File[] files = new File[filePathLength];

    for (int i = 0; i < filePathLength; i++) {
         files[i] = new File(filePath[i]);
    }
}

    /**
     * 可同时编译多个java源文件
     * @param file
     */
public static void javaCampiler(File... file){

   Iterable<? extends JavaFileObject> comilationUnits = fileManager.getJavaFileObjects(file);

   JavaCompiler.CompilationTask task= compiler.getTask(null,fileManager,null,null,null,comilationUnits);

   task.call();

}






}
