2022-05-14 10:44:24 +00:00
|
|
|
package pw.yumc.MiaoScript.api;
|
2022-04-01 10:47:28 +00:00
|
|
|
|
2017-10-09 13:17:24 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-09-21 08:06:37 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2017-10-09 13:17:24 +00:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2022-04-19 09:20:43 +00:00
|
|
|
import java.nio.file.Paths;
|
2017-10-09 13:17:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created with IntelliJ IDEA
|
|
|
|
*
|
|
|
|
* @author 喵♂呜
|
|
|
|
* Created on 2017/10/9 12:40.
|
|
|
|
*/
|
|
|
|
public class Base {
|
2022-05-14 10:44:24 +00:00
|
|
|
private final Object instance;
|
2020-01-17 01:17:13 +00:00
|
|
|
|
2022-04-01 10:47:28 +00:00
|
|
|
Base(Object instance) {
|
2020-01-17 01:17:13 +00:00
|
|
|
this.instance = instance;
|
|
|
|
}
|
|
|
|
|
2022-02-16 06:36:22 +00:00
|
|
|
public String getVersion() {
|
2022-04-01 10:47:28 +00:00
|
|
|
return MiaoScriptAPI.VERSION;
|
2022-02-16 06:36:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 09:05:47 +00:00
|
|
|
public Class<?> getClass(String name) throws ClassNotFoundException {
|
2022-06-01 09:52:55 +00:00
|
|
|
try {
|
|
|
|
return Class.forName(name);
|
|
|
|
} catch (Throwable ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return Class.forName(name, true, instance.getClass().getClassLoader());
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
return Class.forName(name, true, instance.getClass().getClassLoader().getParent());
|
|
|
|
}
|
2017-10-09 13:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 01:17:13 +00:00
|
|
|
public Object getInstance() {
|
|
|
|
return this.instance;
|
|
|
|
}
|
|
|
|
|
2020-05-28 09:05:47 +00:00
|
|
|
public Class<?> getProxyClass() {
|
2020-01-17 01:17:13 +00:00
|
|
|
return ProxyClass.class;
|
|
|
|
}
|
|
|
|
|
2020-11-20 02:30:05 +00:00
|
|
|
public Class<?> getJavaScriptTaskClass() {
|
|
|
|
return JavaScriptTask.class;
|
|
|
|
}
|
|
|
|
|
2022-05-14 10:44:24 +00:00
|
|
|
public File[] loadMavenDepend(String groupId, String artifactId, String version) {
|
|
|
|
return MiaoScriptAPI.loadMavenDepend(groupId, artifactId, version);
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:52:55 +00:00
|
|
|
public File[] loadMavenDepend(String groupId, String artifactId, String version, ClassLoader classLoader) {
|
|
|
|
return MiaoScriptAPI.loadMavenDepend(groupId, artifactId, version, classLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
public File[] parentLoadMavenDepend(String groupId, String artifactId, String version) {
|
|
|
|
return MiaoScriptAPI.parentLoadMavenDepend(groupId, artifactId, version);
|
|
|
|
}
|
|
|
|
|
2017-10-09 13:17:24 +00:00
|
|
|
public String read(String path) throws IOException {
|
2022-04-19 09:20:43 +00:00
|
|
|
return read(Paths.get(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
public String read(File file) throws IOException {
|
|
|
|
return read(file.toPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public String read(Path path) throws IOException {
|
|
|
|
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
2017-10-09 13:17:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 06:36:22 +00:00
|
|
|
public Path save(String path, String content) throws IOException {
|
2022-04-19 09:20:43 +00:00
|
|
|
return save(Paths.get(path), content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Path save(File file, String content) throws IOException {
|
|
|
|
return save(file.toPath(), content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Path save(Path path, String content) throws IOException {
|
|
|
|
path.getParent().toFile().mkdirs();
|
|
|
|
return Files.write(path, content.getBytes(StandardCharsets.UTF_8));
|
2017-10-09 13:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 08:06:37 +00:00
|
|
|
public boolean move(String source, String target) {
|
2022-04-19 09:20:43 +00:00
|
|
|
return move(new File(source), new File(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean move(File source, File target) {
|
|
|
|
return source.renameTo(target);
|
2020-09-21 08:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean delete(String path) throws IOException {
|
|
|
|
return delete(new File(path));
|
2017-10-09 13:17:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 06:36:22 +00:00
|
|
|
public boolean delete(Path path) throws IOException {
|
|
|
|
return delete(path.toFile());
|
2020-09-21 08:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean delete(File file) throws IOException {
|
|
|
|
if (!file.exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (file.isFile()) {
|
|
|
|
return file.delete();
|
|
|
|
}
|
|
|
|
File[] files = file.listFiles();
|
|
|
|
if (files != null) {
|
|
|
|
for (File f : files) {
|
|
|
|
if (f.isFile()) {
|
|
|
|
if (!f.delete()) {
|
|
|
|
f.deleteOnExit();
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-19 09:20:43 +00:00
|
|
|
this.delete(f);
|
2020-09-21 08:06:37 +00:00
|
|
|
}
|
2017-10-09 13:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 08:06:37 +00:00
|
|
|
boolean result = file.delete();
|
|
|
|
if (!result) {
|
|
|
|
file.deleteOnExit();
|
|
|
|
}
|
|
|
|
return result;
|
2017-10-09 13:17:24 +00:00
|
|
|
}
|
|
|
|
}
|