-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreindex.lua
151 lines (126 loc) · 4.2 KB
/
reindex.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
-- Requires:
-- sudo apt install libzzip-dev
-- sudo luarocks install json-lua
-- sudo luarocks install luazip
-- sudo luarocks install luafilesystem
local json = require "JSON"
local zip = require "zip"
local lfs = require "lfs"
local yajl = require "yajl"
-- Create icons directory if it doesn't exist
lfs.mkdir("packages/icons")
local function urlEncode(str)
local encodeChars = {
-- [" "] = "%20", -- spaces are commented out for better URL aesthetic
["!"] = "%21",
["#"] = "%23",
["$"] = "%24",
["%"] = "%25",
["&"] = "%26",
["'"] = "%27",
["("] = "%28",
[")"] = "%29",
["*"] = "%2A",
["+"] = "%2B",
[","] = "%2C",
["/"] = "%2F",
[":"] = "%3A",
[";"] = "%3B",
["="] = "%3D",
["?"] = "%3F",
["@"] = "%40",
["["] = "%5B",
["]"] = "%5D"
}
return str:gsub("[^%w]", encodeChars)
end
local function getFileModTime(filepath)
local attr = lfs.attributes(filepath)
if attr and attr.mode == "file" then
return attr.modification
end
return os.time() -- fallback to current time if we can't get the file time
end
local function clearPackageVariables()
mpackage = nil
author = nil
title = nil
description = nil
created = nil
version = nil
icon = nil
end
local function extractIcon(zfile, packageName, iconName)
if not iconName then return nil end
local iconPath = ".mudlet/Icon/" .. iconName
local iconFile, err = zfile:open(iconPath)
if not iconFile then return nil end
local iconData = iconFile:read("*a")
iconFile:close()
-- Get the file extension from iconName
local extension = iconName:match("^.+(%..+)$") or ".png"
-- Save icon with URL-encoded package name and original extension
local iconFilename = "packages/icons/" .. urlEncode(packageName) .. extension
local f = io.open(iconFilename, "wb")
if f then
f:write(iconData)
f:close()
return iconFilename
end
return nil
end
local pkg = {}
print("Running creation loop...")
-- loop through all .mpackage files in the directory
local packagecount = 0
for file in io.popen("ls -pa packages/*"):lines() do
clearPackageVariables()
print("Found "..file)
-- read config.lua from the zip file
local zfile, err = zip.open(file)
if not err then
local f1, err = zfile:open('config.lua')
local s1 = f1:read("*a")
-- output config.lua and run it to gather the variables inside
infoFile = io.open("config.lua", "w+")
io.output(infoFile)
io.write(s1)
io.close(infoFile)
dofile("config.lua")
packagecount = packagecount + 1
-- Extract icon if present
local iconUrl = nil
if icon then
iconUrl = extractIcon(zfile, mpackage, icon)
end
-- insert package details in table
table.insert(pkg, {
["mpackage"] = mpackage,
["author"] = author,
["title"] = title,
["description"] = description,
["created"] = created,
["version"] = version,
["uploaded"] = getFileModTime(file),
["filename"] = file:gsub("packages/", ""),
["icon"] = iconUrl
})
f1:close()
zfile:close()
-- Write JSON file after each package
local index = { ["name"] = "mudlet package repository listing", ["updated"] = os.date("%c"), ["packages"] = pkg }
local indexFile = io.open("packages/mpkg.packages.json", "w+")
io.output(indexFile)
io.write(json:encode_pretty(index, nil, { pretty = true, align_keys = true, array_newline = true, indent = " " }))
io.close(indexFile)
-- Validate JSON after each package
local f = io.open("packages/mpkg.packages.json", "r")
local content = f:read("*a")
f:close()
local success, result = pcall(yajl.to_value, content)
if not success then
error("Generated JSON file failed YAJL validation after adding package " .. mpackage .. ": " .. tostring(result))
end
end
end
print("Finished processing " .. packagecount .. " packages in the repository.")