|
|
|
|
|
|
|
|
|
|
|
local unpack = {}
|
|
|
|
|
|
local fetch = require("luarocks.fetch")
|
|
|
local fs = require("luarocks.fs")
|
|
|
local util = require("luarocks.util")
|
|
|
local build = require("luarocks.build")
|
|
|
local dir = require("luarocks.dir")
|
|
|
local search = require("luarocks.search")
|
|
|
|
|
|
unpack.help_summary = "Unpack the contents of a rock."
|
|
|
unpack.help_arguments = "[--force] {<rock>|<name> [<version>]}"
|
|
|
unpack.help = [[
|
|
|
Unpacks the contents of a rock in a newly created directory.
|
|
|
Argument may be a rock file, or the name of a rock in a rocks server.
|
|
|
In the latter case, the app version may be given as a second argument.
|
|
|
|
|
|
--force Unpack files even if the output directory already exists.
|
|
|
]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function unpack_rockspec(rockspec_file, dir_name)
|
|
|
assert(type(rockspec_file) == "string")
|
|
|
assert(type(dir_name) == "string")
|
|
|
|
|
|
local rockspec, err = fetch.load_rockspec(rockspec_file)
|
|
|
if not rockspec then
|
|
|
return nil, "Failed loading rockspec "..rockspec_file..": "..err
|
|
|
end
|
|
|
local ok, err = fs.change_dir(dir_name)
|
|
|
if not ok then return nil, err end
|
|
|
local ok, sources_dir = fetch.fetch_sources(rockspec, true, ".")
|
|
|
if not ok then
|
|
|
return nil, sources_dir
|
|
|
end
|
|
|
ok, err = fs.change_dir(sources_dir)
|
|
|
if not ok then return nil, err end
|
|
|
ok, err = build.apply_patches(rockspec)
|
|
|
fs.pop_dir()
|
|
|
if not ok then return nil, err end
|
|
|
return rockspec
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function unpack_rock(rock_file, dir_name, kind)
|
|
|
assert(type(rock_file) == "string")
|
|
|
assert(type(dir_name) == "string")
|
|
|
|
|
|
local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, dir_name)
|
|
|
if not ok then
|
|
|
return nil, err, errcode
|
|
|
end
|
|
|
ok, err = fs.change_dir(dir_name)
|
|
|
if not ok then return nil, err end
|
|
|
local rockspec_file = dir_name..".rockspec"
|
|
|
local rockspec, err = fetch.load_rockspec(rockspec_file)
|
|
|
if not rockspec then
|
|
|
return nil, "Failed loading rockspec "..rockspec_file..": "..err
|
|
|
end
|
|
|
if kind == "src" then
|
|
|
if rockspec.source.file then
|
|
|
local ok, err = fs.unpack_archive(rockspec.source.file)
|
|
|
if not ok then
|
|
|
return nil, err
|
|
|
end
|
|
|
ok, err = fs.change_dir(rockspec.source.dir)
|
|
|
if not ok then return nil, err end
|
|
|
ok, err = build.apply_patches(rockspec)
|
|
|
fs.pop_dir()
|
|
|
if not ok then return nil, err end
|
|
|
end
|
|
|
end
|
|
|
return rockspec
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function run_unpacker(file, force)
|
|
|
assert(type(file) == "string")
|
|
|
|
|
|
local base_name = dir.base_name(file)
|
|
|
local dir_name, kind, extension = base_name:match("(.*)%.([^.]+)%.(rock)$")
|
|
|
if not extension then
|
|
|
dir_name, extension = base_name:match("(.*)%.(rockspec)$")
|
|
|
kind = "rockspec"
|
|
|
end
|
|
|
if not extension then
|
|
|
return nil, file.." does not seem to be a valid filename."
|
|
|
end
|
|
|
|
|
|
local exists = fs.exists(dir_name)
|
|
|
if exists and not force then
|
|
|
return nil, "Directory "..dir_name.." already exists."
|
|
|
end
|
|
|
if not exists then
|
|
|
local ok, err = fs.make_dir(dir_name)
|
|
|
if not ok then return nil, err end
|
|
|
end
|
|
|
local rollback = util.schedule_function(fs.delete, fs.absolute_name(dir_name))
|
|
|
|
|
|
local rockspec, err
|
|
|
if extension == "rock" then
|
|
|
rockspec, err = unpack_rock(file, dir_name, kind)
|
|
|
elseif extension == "rockspec" then
|
|
|
rockspec, err = unpack_rockspec(file, dir_name)
|
|
|
end
|
|
|
if not rockspec then
|
|
|
return nil, err
|
|
|
end
|
|
|
if kind == "src" or kind == "rockspec" then
|
|
|
if rockspec.source.dir ~= "." then
|
|
|
local ok = fs.copy(rockspec.local_abs_filename, rockspec.source.dir, "read")
|
|
|
if not ok then
|
|
|
return nil, "Failed copying unpacked rockspec into unpacked source directory."
|
|
|
end
|
|
|
end
|
|
|
util.printout()
|
|
|
util.printout("Done. You may now enter directory ")
|
|
|
util.printout(dir.path(dir_name, rockspec.source.dir))
|
|
|
util.printout("and type 'luarocks make' to build.")
|
|
|
end
|
|
|
util.remove_scheduled_function(rollback)
|
|
|
return true
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function unpack.command(flags, ns_name, version)
|
|
|
assert(type(version) == "string" or not version)
|
|
|
if type(ns_name) ~= "string" then
|
|
|
return nil, "Argument missing. "..util.see_help("unpack")
|
|
|
end
|
|
|
|
|
|
ns_name = util.adjust_name_and_namespace(ns_name, flags)
|
|
|
|
|
|
local url, err
|
|
|
if ns_name:match(".*%.rock") or ns_name:match(".*%.rockspec") then
|
|
|
url = ns_name
|
|
|
else
|
|
|
url, err = search.find_src_or_rockspec(ns_name, version, true)
|
|
|
if not url then
|
|
|
return nil, err
|
|
|
end
|
|
|
end
|
|
|
|
|
|
return run_unpacker(url, flags["force"])
|
|
|
end
|
|
|
|
|
|
return unpack
|
|
|
|