Skip to content

Commit

Permalink
Move install.install_binary_rock[_deps] into build module
Browse files Browse the repository at this point in the history
Some installing logic was in luarocks.install, some was
in luarocks.build. Moved all of it into luarocks.build.
If some duplicated code is eliminated it should be possible
to actually have building in luarocks.build and installing
in luarocks.install.
  • Loading branch information
mpeterv committed Jan 4, 2016
1 parent 5b15cf5 commit 5fe80e2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 129 deletions.
120 changes: 118 additions & 2 deletions src/luarocks/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,131 @@ function build.build_rock(rock_file, need_to_fetch, deps_mode, build_only_deps)
fs.pop_dir()
return ok, err, errcode
end

--- Install a binary rock.
-- @param rock_file string: local or remote filename of a rock.
-- @param deps_mode: string: Which trees to check dependencies for:
-- "one" for the current default tree, "all" for all trees,
-- "order" for all trees with priority >= the current default, "none" for no trees.
-- @return (string, string) or (nil, string, [string]): Name and version of
-- installed rock if succeeded or nil and an error message followed by an error code.
function build.install_binary_rock(rock_file, deps_mode)
assert(type(rock_file) == "string")

local name, version, arch = path.parse_name(rock_file)
if not name then
return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'."
end

if arch ~= "all" and arch ~= cfg.arch then
return nil, "Incompatible architecture "..arch, "arch"
end
if repos.is_installed(name, version) then
repos.delete_version(name, version)
end

local rollback = util.schedule_function(function()
fs.delete(path.install_dir(name, version))
fs.remove_dir_if_empty(path.versions_dir(name))
end)

local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version))
if not ok then return nil, err, errcode end

local rockspec, err, errcode = fetch.load_rockspec(path.rockspec_file(name, version))
if err then
return nil, "Failed loading rockspec for installed package: "..err, errcode
end

if deps_mode == "none" then
util.printerr("Warning: skipping dependency checks.")
else
ok, err, errcode = deps.check_external_deps(rockspec, "install")
if err then return nil, err, errcode end
end

-- For compatibility with .rock files built with LuaRocks 1
if not fs.exists(path.rock_manifest_file(name, version)) then
ok, err = manif.make_rock_manifest(name, version)
if err then return nil, err end
end

if deps_mode ~= "none" then
ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
if err then return nil, err, errcode end
end

ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec))
if err then return nil, err end

util.remove_scheduled_function(rollback)
rollback = util.schedule_function(function()
repos.delete_version(name, version)
end)

ok, err = repos.run_hook(rockspec, "post_install")
if err then return nil, err end

ok, err = manif.update_manifest(name, version, nil, deps_mode)
if err then return nil, err end

local license = ""
if rockspec.description.license then
license = ("(license: "..rockspec.description.license..")")
end

local root_dir = path.root_dir(cfg.rocks_dir)
util.printout()
util.printout(name.." "..version.." is now installed in "..root_dir.." "..license)

util.remove_scheduled_function(rollback)
return name, version
end

--- Installs the dependencies of a binary rock.
-- @param rock_file string: local or remote filename of a rock.
-- @param deps_mode: string: Which trees to check dependencies for:
-- "one" for the current default tree, "all" for all trees,
-- "order" for all trees with priority >= the current default, "none" for no trees.
-- @return (string, string) or (nil, string, [string]): Name and version of
-- the rock whose dependencies were installed if succeeded or nil and an error message
-- followed by an error code.
function build.install_binary_rock_deps(rock_file, deps_mode)
assert(type(rock_file) == "string")

local name, version, arch = path.parse_name(rock_file)
if not name then
return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'."
end

if arch ~= "all" and arch ~= cfg.arch then
return nil, "Incompatible architecture "..arch, "arch"
end

local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version))
if not ok then return nil, err, errcode end

local rockspec, err, errcode = fetch.load_rockspec(path.rockspec_file(name, version))
if err then
return nil, "Failed loading rockspec for installed package: "..err, errcode
end

ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
if err then return nil, err, errcode end

util.printout()
util.printout("Succesfully installed dependencies for " ..name.." "..version)

return name, version
end

local function do_build(name, version, deps_mode, build_only_deps)
if name:match("%.rockspec$") then
return build.build_rockspec(name, true, false, deps_mode, build_only_deps)
elseif name:match("%.src%.rock$") then
return build.build_rock(name, false, deps_mode, build_only_deps)
elseif name:match("%.all%.rock$") then
local install = require("luarocks.install")
local install_fun = build_only_deps and install.install_binary_rock_deps or install.install_binary_rock
local install_fun = build_only_deps and build.install_binary_rock_deps or build.install_binary_rock
return install_fun(name, deps_mode)
elseif name:match("%.rock$") then
return build.build_rock(name, true, deps_mode, build_only_deps)
Expand Down
128 changes: 3 additions & 125 deletions src/luarocks/install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
local install = {}
package.loaded["luarocks.install"] = install

local path = require("luarocks.path")
local repos = require("luarocks.repos")
local fetch = require("luarocks.fetch")
local build = require("luarocks.build")
local util = require("luarocks.util")
local fs = require("luarocks.fs")
local deps = require("luarocks.deps")
local manif = require("luarocks.manif")
local remove = require("luarocks.remove")
local cfg = require("luarocks.cfg")

Expand All @@ -30,124 +27,6 @@ or a filename of a locally available rock.
--only-deps Installs only the dependencies of the rock.
]]..util.deps_mode_help()


--- Install a binary rock.
-- @param rock_file string: local or remote filename of a rock.
-- @param deps_mode: string: Which trees to check dependencies for:
-- "one" for the current default tree, "all" for all trees,
-- "order" for all trees with priority >= the current default, "none" for no trees.
-- @return (string, string) or (nil, string, [string]): Name and version of
-- installed rock if succeeded or nil and an error message followed by an error code.
function install.install_binary_rock(rock_file, deps_mode)
assert(type(rock_file) == "string")

local name, version, arch = path.parse_name(rock_file)
if not name then
return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'."
end

if arch ~= "all" and arch ~= cfg.arch then
return nil, "Incompatible architecture "..arch, "arch"
end
if repos.is_installed(name, version) then
repos.delete_version(name, version)
end

local rollback = util.schedule_function(function()
fs.delete(path.install_dir(name, version))
fs.remove_dir_if_empty(path.versions_dir(name))
end)

local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version))
if not ok then return nil, err, errcode end

local rockspec, err, errcode = fetch.load_rockspec(path.rockspec_file(name, version))
if err then
return nil, "Failed loading rockspec for installed package: "..err, errcode
end

if deps_mode == "none" then
util.printerr("Warning: skipping dependency checks.")
else
ok, err, errcode = deps.check_external_deps(rockspec, "install")
if err then return nil, err, errcode end
end

-- For compatibility with .rock files built with LuaRocks 1
if not fs.exists(path.rock_manifest_file(name, version)) then
ok, err = manif.make_rock_manifest(name, version)
if err then return nil, err end
end

if deps_mode ~= "none" then
ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
if err then return nil, err, errcode end
end

ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec))
if err then return nil, err end

util.remove_scheduled_function(rollback)
rollback = util.schedule_function(function()
repos.delete_version(name, version)
end)

ok, err = repos.run_hook(rockspec, "post_install")
if err then return nil, err end

ok, err = manif.update_manifest(name, version, nil, deps_mode)
if err then return nil, err end

local license = ""
if rockspec.description.license then
license = ("(license: "..rockspec.description.license..")")
end

local root_dir = path.root_dir(cfg.rocks_dir)
util.printout()
util.printout(name.." "..version.." is now installed in "..root_dir.." "..license)

util.remove_scheduled_function(rollback)
return name, version
end

--- Installs the dependencies of a binary rock.
-- @param rock_file string: local or remote filename of a rock.
-- @param deps_mode: string: Which trees to check dependencies for:
-- "one" for the current default tree, "all" for all trees,
-- "order" for all trees with priority >= the current default, "none" for no trees.
-- @return (string, string) or (nil, string, [string]): Name and version of
-- the rock whose dependencies were installed if succeeded or nil and an error message
-- followed by an error code.
function install.install_binary_rock_deps(rock_file, deps_mode)
assert(type(rock_file) == "string")

local name, version, arch = path.parse_name(rock_file)
if not name then
return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'."
end

if arch ~= "all" and arch ~= cfg.arch then
return nil, "Incompatible architecture "..arch, "arch"
end

local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version))
if not ok then return nil, err, errcode end

local rockspec, err, errcode = fetch.load_rockspec(path.rockspec_file(name, version))
if err then
return nil, "Failed loading rockspec for installed package: "..err, errcode
end

ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
if err then return nil, err, errcode end

util.printout()
util.printout("Succesfully installed dependencies for " ..name.." "..version)

return name, version
end

--- Driver function for the "install" command.
-- @param name string: name of a binary rock. If an URL or pathname
-- to a binary rock is given, fetches and installs it. If a rockspec or a
Expand All @@ -169,13 +48,12 @@ function install.run(...)

if name:match("%.rockspec$") or name:match("%.src%.rock$") then
util.printout("Using "..name.."... switching to 'build' mode")
local build = require("luarocks.build")
return build.run(name, util.forward_flags(flags, "local", "keep", "deps-mode", "only-deps"))
elseif name:match("%.rock$") then
if flags["only-deps"] then
ok, err = install.install_binary_rock_deps(name, deps.get_deps_mode(flags))
ok, err = build.install_binary_rock_deps(name, deps.get_deps_mode(flags))
else
ok, err = install.install_binary_rock(name, deps.get_deps_mode(flags))
ok, err = build.install_binary_rock(name, deps.get_deps_mode(flags))
end
if not ok then return nil, err end
local name, version = ok, err
Expand Down
3 changes: 1 addition & 2 deletions src/luarocks/validate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local dir = require("luarocks.dir")
local path = require("luarocks.path")
local cfg = require("luarocks.cfg")
local build = require("luarocks.build")
local install = require("luarocks.install")
local util = require("luarocks.util")

validate.help_summary = "Sandboxed test of build/install of all packages in a repository."
Expand Down Expand Up @@ -68,7 +67,7 @@ local function validate_src_rock(file)
end

local function validate_rock(file)
local ok, err, errcode = install.install_binary_rock(file, "one")
local ok, err, errcode = build.install_binary_rock(file, "one")
if not ok then
util.printerr(err)
end
Expand Down

0 comments on commit 5fe80e2

Please sign in to comment.