Skip to content

Commit

Permalink
Add script export/deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Jan 1, 2025
1 parent aaa5ef2 commit d141154
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions extensions/web-scripts/web-scripts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,37 @@ local FileHttpHandler = require('jls.net.http.handler.FileHttpHandler')
local RestHttpHandler = require('jls.net.http.handler.RestHttpHandler')
local HttpExchange = require('jls.net.http.HttpExchange')
local json = require('jls.util.json')
local Date = require('jls.util.Date')
local ZipFile = require('jls.util.zip.ZipFile')

local webBaseAddons = extension:require('web-base.addons', true)

webBaseAddons.registerAddonExtension(extension)

local function deployScript(exchange, engine, extId)
local zipName = exchange:getRequest():getBody() or 'lha-ext.zip'
local backup = File:new(engine:getTemporaryDirectory(), zipName)
if not backup:isFile() then
HttpExchange.notFound(exchange)
return false
end
local extDir = File:new(engine:getScriptsDirectory(), extId)
if extDir:exists() or not extDir:mkdir() then
HttpExchange.internalServerError(exchange)
return false
end
if not ZipFile.unzipTo(backup, extDir) then
extDir:deleteRecursive()
HttpExchange.badRequest(exchange)
return false
end
logger:fine('Deployed script "%s"', extId)
engine:loadExtensionFromDirectory(extDir, 'script')
return extId
end

local REST_SCRIPTS = {
['(engine)?method=GET'] = function(exchange, engine)
['(engine)?method=GET'] = function(_, engine)
local list = {}
for _, ext in ipairs(engine.extensions) do
if ext:getType() == 'script' then
Expand All @@ -29,10 +53,9 @@ local REST_SCRIPTS = {
if not name or name == '' then
name = 'New script'
end
local dir = engine:getScriptsDirectory()
local scriptName = 'script.lua'
local extId = engine:generateId()
local extDir = File:new(dir, extId)
local extDir = File:new(engine:getScriptsDirectory(), extId)
extDir:mkdir()
local scriptFile = File:new(extDir, scriptName)
scriptFile:write('local script = ...\n\n')
Expand All @@ -47,6 +70,9 @@ local REST_SCRIPTS = {
engine:loadExtensionFromDirectory(extDir, 'script')
return extId
end,
['(engine)?method=POST'] = function(exchange, engine)
return deployScript(exchange, engine, engine:generateId())
end,
['{+}(engine)'] = function(exchange, name, engine)
local ext = engine:getExtensionById(name)
if ext:getType() ~= 'script' then
Expand All @@ -56,7 +82,7 @@ local REST_SCRIPTS = {
exchange:setAttribute('extension', ext)
end,
['{extensionId}'] = {
['(engine, extension)?method=DELETE'] = function(exchange, engine, ext)
['(engine, extension)?method=DELETE'] = function(_, engine, ext)
local extensionDir = ext:getDir()
if ext:isActive() then
ext:publishEvent('shutdown')
Expand All @@ -65,7 +91,16 @@ local REST_SCRIPTS = {
engine:removeExtension(ext)
extensionDir:deleteRecursive()
end,
['reload(extension)'] = function(exchange, ext)
['deploy(engine, extensionId)?method=PUT'] = function(exchange, engine, extId)
return deployScript(exchange, engine, extId)
end,
['export(engine, extension)'] = function(_, engine, ext)
local backup = File:new(engine:getTemporaryDirectory(), 'lha-ext-'..ext:getId()..'.'..Date.timestamp()..'.zip')
return ZipFile.zipToAsync(backup, ext:getDir():listFiles()):next(function()
return backup:getName()
end)
end,
['reload(extension)'] = function(_, ext)
ext:restartExtension()
end,
['name(extension)?method=PUT'] = function(exchange, ext)
Expand Down

0 comments on commit d141154

Please sign in to comment.