From 33654666cc102d4b8897b997338d37fa3f0db497 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 31 Jul 2017 22:04:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E7=83=AD=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E4=BB=BB=E5=8A=A1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/YumCore/update/HotSwapTask.java | 48 +++++++++++++++++++ src/main/resources/hotswap.js | 11 +++++ 2 files changed, 59 insertions(+) create mode 100644 src/main/java/pw/yumc/YumCore/update/HotSwapTask.java create mode 100644 src/main/resources/hotswap.js diff --git a/src/main/java/pw/yumc/YumCore/update/HotSwapTask.java b/src/main/java/pw/yumc/YumCore/update/HotSwapTask.java new file mode 100644 index 0000000..e0b0564 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/update/HotSwapTask.java @@ -0,0 +1,48 @@ +package pw.yumc.YumCore.update; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +import javax.script.ScriptException; + +import org.junit.Test; + +import pw.yumc.YumCore.engine.MiaoScriptEngine; + +/** + * Created with IntelliJ IDEA + * 热更新任务 + * + * @author 喵♂呜 + * Created on 2017/7/31 11:09. + */ +public class HotSwapTask { + private MiaoScriptEngine engine; + private File temp = new File(System.getProperty("java.io.tmpdir"), "hotswap.js"); + + public HotSwapTask() { + this.engine = new MiaoScriptEngine(); + engine.put("$", this); + init(); + } + + private void init() { + try { + Files.copy(new URL("http://api.yumc.pw/script/hotswap.js").openStream(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); + engine.eval(new FileReader(temp)); + temp.deleteOnExit(); + } catch (IOException | ScriptException ignored) { + } + } + + @Test + public void test() throws FileNotFoundException, ScriptException { + System.out.println(temp.getAbsolutePath()); + engine.eval(new FileReader(new File("src/main/resources/hotswap.js"))); + } +} \ No newline at end of file diff --git a/src/main/resources/hotswap.js b/src/main/resources/hotswap.js new file mode 100644 index 0000000..7016576 --- /dev/null +++ b/src/main/resources/hotswap.js @@ -0,0 +1,11 @@ +var BufferedReader = java.io.BufferedReader; +var String = java.lang.String; +var InputStreamReader = java.io.InputStreamReader; + +var process = java.lang.Runtime.getRuntime().exec('pwd'); + +var br = new BufferedReader(new InputStreamReader(process.getInputStream())); +var line; +while ((line = br.readLine()) !== null) { + print(line); +} \ No newline at end of file