fix plugin loading after install, move updater reloads to loader

This commit is contained in:
AAGaming
2022-09-18 14:49:32 -04:00
parent 8829adc5b6
commit 7716c73014
4 changed files with 23 additions and 10 deletions
+13 -2
View File
@@ -28,9 +28,10 @@ class PluginInstallContext:
self.hash = hash self.hash = hash
class PluginBrowser: class PluginBrowser:
def __init__(self, plugin_path, plugins) -> None: def __init__(self, plugin_path, plugins, loader) -> None:
self.plugin_path = plugin_path self.plugin_path = plugin_path
self.plugins = plugins self.plugins = plugins
self.loader = loader
self.install_requests = {} self.install_requests = {}
def _unzip_to_plugin_dir(self, zip, name, hash): def _unzip_to_plugin_dir(self, zip, name, hash):
@@ -58,6 +59,8 @@ class PluginBrowser:
logger.debug(f"skipping {folder}") logger.debug(f"skipping {folder}")
async def uninstall_plugin(self, name): async def uninstall_plugin(self, name):
if self.loader.watcher:
self.loader.watcher.disabled = True
tab = await get_tab("SP") tab = await get_tab("SP")
try: try:
logger.info("uninstalling " + name) logger.info("uninstalling " + name)
@@ -74,8 +77,12 @@ class PluginBrowser:
except Exception as e: except Exception as e:
logger.error(f"Plugin {name} in {self.find_plugin_folder(name)} was not uninstalled") logger.error(f"Plugin {name} in {self.find_plugin_folder(name)} was not uninstalled")
logger.error(f"Error at %s", exc_info=e) logger.error(f"Error at %s", exc_info=e)
if self.loader.watcher:
self.loader.watcher.disabled = False
async def _install(self, artifact, name, version, hash): async def _install(self, artifact, name, version, hash):
if self.loader.watcher:
self.loader.watcher.disabled = True
try: try:
await self.uninstall_plugin(name) await self.uninstall_plugin(name)
except: except:
@@ -93,11 +100,15 @@ class PluginBrowser:
ret = self._unzip_to_plugin_dir(res_zip, name, hash) ret = self._unzip_to_plugin_dir(res_zip, name, hash)
if ret: if ret:
logger.info(f"Installed {name} (Version: {version})") logger.info(f"Installed {name} (Version: {version})")
await inject_to_tab("SP", "window.syncDeckyPlugins()") plugin_dir = self.find_plugin_folder(name)
self.loader.import_plugin(path.join(plugin_dir, "main.py"), plugin_dir)
# await inject_to_tab("SP", "window.syncDeckyPlugins()")
else: else:
self.log.fatal(f"SHA-256 Mismatch!!!! {name} (Version: {version})") self.log.fatal(f"SHA-256 Mismatch!!!! {name} (Version: {version})")
else: else:
logger.fatal(f"Could not fetch from URL. {await res.text()}") logger.fatal(f"Could not fetch from URL. {await res.text()}")
if self.loader.watcher:
self.loader.watcher.disabled = False
async def request_plugin_install(self, artifact, name, version, hash): async def request_plugin_install(self, artifact, name, version, hash):
request_id = str(time()) request_id = str(time())
+6 -1
View File
@@ -25,8 +25,11 @@ class FileChangeHandler(RegexMatchingEventHandler):
self.logger = getLogger("file-watcher") self.logger = getLogger("file-watcher")
self.plugin_path = plugin_path self.plugin_path = plugin_path
self.queue = queue self.queue = queue
self.disabled = False
def maybe_reload(self, src_path): def maybe_reload(self, src_path):
if self.disabled:
return
plugin_dir = Path(path.relpath(src_path, self.plugin_path)).parts[0] plugin_dir = Path(path.relpath(src_path, self.plugin_path)).parts[0]
if exists(path.join(self.plugin_path, plugin_dir, "plugin.json")): if exists(path.join(self.plugin_path, plugin_dir, "plugin.json")):
self.queue.put_nowait((path.join(self.plugin_path, plugin_dir, "main.py"), plugin_dir, True)) self.queue.put_nowait((path.join(self.plugin_path, plugin_dir, "main.py"), plugin_dir, True))
@@ -66,11 +69,13 @@ class Loader:
self.plugin_path = plugin_path self.plugin_path = plugin_path
self.logger.info(f"plugin_path: {self.plugin_path}") self.logger.info(f"plugin_path: {self.plugin_path}")
self.plugins = {} self.plugins = {}
self.watcher = None
if live_reload: if live_reload:
self.reload_queue = Queue() self.reload_queue = Queue()
self.observer = Observer() self.observer = Observer()
self.observer.schedule(FileChangeHandler(self.reload_queue, plugin_path), self.plugin_path, recursive=True) self.watcher = FileChangeHandler(self.reload_queue, plugin_path)
self.observer.schedule(self.watcher, self.plugin_path, recursive=True)
self.observer.start() self.observer.start()
self.loop.create_task(self.handle_reloads()) self.loop.create_task(self.handle_reloads())
+4 -3
View File
@@ -4,6 +4,7 @@ from json import dumps, loads
from logging import DEBUG, INFO, basicConfig, getLogger from logging import DEBUG, INFO, basicConfig, getLogger
from os import getenv, path from os import getenv, path
from subprocess import call from subprocess import call
from traceback import format_exc
import aiohttp_cors import aiohttp_cors
# Partial imports # Partial imports
@@ -70,7 +71,7 @@ class PluginManager:
) )
}) })
self.plugin_loader = Loader(self.web_app, CONFIG["plugin_path"], self.loop, CONFIG["live_reload"]) self.plugin_loader = Loader(self.web_app, CONFIG["plugin_path"], self.loop, CONFIG["live_reload"])
self.plugin_browser = PluginBrowser(CONFIG["plugin_path"], self.plugin_loader.plugins) self.plugin_browser = PluginBrowser(CONFIG["plugin_path"], self.plugin_loader.plugins, self.plugin_loader)
self.settings = SettingsManager("loader", path.join(HOMEBREW_PATH, "settings")) self.settings = SettingsManager("loader", path.join(HOMEBREW_PATH, "settings"))
self.utilities = Utilities(self) self.utilities = Utilities(self)
self.updater = Updater(self) self.updater = Updater(self)
@@ -123,9 +124,9 @@ class PluginManager:
async def inject_javascript(self, request=None): async def inject_javascript(self, request=None):
try: try:
await inject_to_tab("SP", "try{window.deckyHasLoaded = true;(async()=>{while(!window.SP_REACT){await new Promise(r => setTimeout(r, 10))};await import('http://localhost:1337/frontend/index.js')})();}catch(e){console.error(e)}", True) await inject_to_tab("SP", "try{if (window.deckyHasLoaded) location.reload();window.deckyHasLoaded = true;(async()=>{while(!window.SP_REACT){await new Promise(r => setTimeout(r, 10))};await import('http://localhost:1337/frontend/index.js')})();}catch(e){console.error(e)}", True)
except: except:
logger.info("Failed to inject JavaScript into tab") logger.info("Failed to inject JavaScript into tab\n" + format_exc())
pass pass
def run(self): def run(self):
-4
View File
@@ -1,5 +1,3 @@
import { sleep } from 'decky-frontend-lib';
export enum Branches { export enum Branches {
Release, Release,
Prerelease, Prerelease,
@@ -46,6 +44,4 @@ export async function callUpdaterMethod(methodName: string, args = {}) {
export async function finishUpdate() { export async function finishUpdate() {
callUpdaterMethod('do_restart'); callUpdaterMethod('do_restart');
await sleep(3000);
location.reload();
} }