Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Last active October 8, 2022 15:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daurnimator/11148106 to your computer and use it in GitHub Desktop.
Save daurnimator/11148106 to your computer and use it in GitHub Desktop.
Script to convert LuaRocks to Arch Linux PKGBUILDs
#!/usr/bin/env lua
local rockspec_name = assert(arg[1])
local function log(level, fmt, ...)
io.stderr:write(level .. "\t" .. string.format(fmt, ...) .. "\n")
end
local function append(tbl, ...)
for _, v in ipairs {...} do
tbl[#tbl+1] = v
end
return tbl
end
local function shell_single_quote(str)
str = "'" .. str:gsub("'", "'\\''") .. "'"
return str
end
local function shell_quote(str)
if str:match("[^%w%_%:%/%@%^%.%-]") then
str = shell_single_quote(str)
end
return str
end
local q = shell_quote
local function load_rockspec(path)
local env = {}
local rockspec = assert(loadfile(path, "t", env))
rockspec()
return env
end
local config = load_rockspec(rockspec_name)
local PKGBUILD = {}
local function add_var(name, value)
if type(value) == "string" then
append(PKGBUILD, name .. "=" .. shell_quote(value))
elseif type(value) == "table" then
if next(value) == nil then
-- Do nothing
elseif value[1] then -- Array
local t = {}
for i,v in ipairs(value) do
t[i] = shell_quote(v)
end
append(PKGBUILD, name .. "=(" .. table.concat(t, " ") .. ")")
else
error("Unable to serialise table" .. name)
end
else
log("warn", name .. " is not a string: ", value)
end
end
add_var("pkgname", config.package)
add_var("pkgver", config.version:gsub("%-","_"))
add_var("pkgrel", "0")
add_var("pkgdesc", config.description.summary)
add_var("arch", {"any"})
add_var("url", config.description.homepage)
add_var("license", {(config.description.license:gsub("^MIT/X11$","MIT"))})
do
local depends = { "luarocks" }
for _, d in ipairs(config.dependencies or {}) do
-- PKGBUILD dependencies can't have whitespace
d = d:gsub("%s+","")
append(depends, d)
end
for d in pairs(config.external_dependencies or {}) do
-- take a wild guess at the package name by lower casing and swapping _ for -
d = d:gsub("%s+","")
d = d:lower()
d = d:gsub("_","-")
append(depends, d)
end
add_var("depends", depends)
end
local url = config.source.url
add_var("source", {rockspec_name, url})
local md5 = config.source.md5
--[[if md5 == nil then
log("info", "No md5 sum found in rockspec downloading source to calculate it")
--md5 = io.popen("curl -Ls " .. shell_quote(url) .." | md5sum -b"):read"*l":match"%x+"
md5 = "SKIP"
end]]
log("debug", "Calculating md5 for rockspec")
local rockspec_md5 = io.popen("md5sum " .. shell_quote(rockspec_name)):read"*l":match"%x+"
add_var("md5sums", {rockspec_md5, md5 or "SKIP"})
add_var("install", "rock.install")
local source_dir = config.source.dir or config.source.url:match("([^/.]+)%.?[^/]*$")
table.insert(PKGBUILD, [[
build() {
cd ]] .. q(source_dir) .. [[;
luarocks make --pack-binary-rock --deps-mode=none "${srcdir}"/]] .. q(rockspec_name) .. [[;
}
package() {
cd ]] .. q(source_dir) .. [[;
luarocks --tree="${pkgdir}/usr" install --deps-mode=none --no-manifest ]] .. q(config.package.."-"..config.version) .. [[.*.rock
}]])
print(table.concat(PKGBUILD, "\n"))
@alerque
Copy link

alerque commented Dec 21, 2020

Hey @daurnimator is this post_install() hook system still how rocks should be packaged? I know you've messed with Lua packaging recently and I'm having trouble because my systems are ending up without manifest files so luarocks list for example reports bad info.

@daurnimator
Copy link
Author

Hey @daurnimator is this post_install() hook system still how rocks should be packaged? I know you've messed with Lua packaging recently and I'm having trouble because my systems are ending up without manifest files so luarocks list for example reports bad info.

@alerque It was never really 100% finished. I was adding more capabilities to luarocks at the time to fix some deficiencies.

@alerque
Copy link

alerque commented Dec 21, 2020

Nevermind if this whole bag of tricks is finished, I'm mostly just asking about the current total lack of manifests for any Lua rocks.

  • Should I look into adding a custom package hook on my system to generate them on package updates
  • Should something like that be contributed upstream?
  • Or should all individual packages include something like this?
  • Or am I completely barking up up the wrong tree expecting luarocks list to work at all?

@daurnimator
Copy link
Author

I've added a post-hook to the luarocks package now; should work going forward for packages that include manifests.

@alerque
Copy link

alerque commented Jan 27, 2021

I see that, looks great, thanks for the news!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment